summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/claim-using-registration.https.html
blob: 640b0be3e50e5d65c340a14e3aa6e0ab3518fafe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<title>Service Worker: claim client using registration</title>
<script src="/resources/testharness.js"></script>
<script src="resources/testharness-helpers.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>

promise_test(function(t) {
    var scope = 'resources/';
    var frame_url = 'resources/blank.html?using-different-registration';
    var url1 = 'resources/empty.js';
    var url2 = 'resources/claim-worker.js';
    var worker, sw_registration, frame;
    return service_worker_unregister_and_register(t, url1, scope)
      .then(function(registration) {
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() {
          return with_iframe(frame_url);
        })
      .then(function(f) {
          frame = f;
          return navigator.serviceWorker.register(url2, {scope: frame_url});
        })
      .then(function(registration) {
          worker = registration.installing;
          sw_registration = registration;
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() {
          var saw_controllerchanged = new Promise(function(resolve) {
              frame.contentWindow.navigator.serviceWorker.oncontrollerchange =
                  function() { resolve(); }
            });
          var channel = new MessageChannel();
          var saw_message = new Promise(function(resolve) {
              channel.port1.onmessage = t.step_func(function(e) {
                  assert_equals(e.data, 'PASS',
                                'Worker call to claim() should fulfill.');
                  resolve();
                });
            });
          worker.postMessage({port: channel.port2}, [channel.port2]);
          return Promise.all([saw_controllerchanged, saw_message]);
        })
      .then(function() {
          assert_equals(
              frame.contentWindow.navigator.serviceWorker.controller.scriptURL,
              normalizeURL(url2),
              'Frame1 controller scriptURL should be changed to url2');
          frame.remove();
          return sw_registration.unregister();
        })
      .then(function() {
          return service_worker_unregister_and_done(t, scope);
        });
  }, 'Test worker claims client which is using another registration');

promise_test(function(t) {
    var scope = 'resources/blank.html?using-same-registration';
    var url1 = 'resources/empty.js';
    var url2 = 'resources/claim-worker.js';
    var frame, worker;
    return service_worker_unregister_and_register(t, url1, scope)
      .then(function(registration) {
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(f) {
          frame = f;
          return navigator.serviceWorker.register(url2, {scope: scope});
        })
      .then(function(registration) {
          worker = registration.installing;
          return wait_for_state(t, registration.installing, 'installed');
        })
      .then(function() {
          var channel = new MessageChannel();
          var saw_message = new Promise(function(resolve) {
              channel.port1.onmessage = t.step_func(function(e) {
                  assert_equals(e.data, 'FAIL: exception: InvalidStateError',
                                'Worker call to claim() should reject with ' +
                                'InvalidStateError');
                  resolve();
                });
            });
          worker.postMessage({port: channel.port2}, [channel.port2]);
          return saw_message;
        })
      .then(function() {
          frame.remove();
          return service_worker_unregister_and_done(t, scope);
        });
  }, 'Test for the waiting worker claims a client which is using the the ' +
     'same registration');

</script>