summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/ServiceWorkerGlobalScope/unregister.https.html
blob: 1a124d727687ecfc62bb1443bed2313312762997 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!DOCTYPE html>
<title>ServiceWorkerGlobalScope: unregister</title>
<script src='/resources/testharness.js'></script>
<script src='/resources/testharnessreport.js'></script>
<script src='../resources/test-helpers.sub.js'></script>
<script>

promise_test(function(t) {
    var script = 'resources/unregister-worker.js?evaluation';
    var scope = 'resources/scope/unregister-on-script-evaluation';

    return service_worker_unregister_and_register(t, script, scope)
      .then(function(registration) {
          t.add_cleanup(function() {
              return service_worker_unregister(t, scope);
            });

          return wait_for_state(t, registration.installing, 'redundant');
        })
      .then(function() {
          return navigator.serviceWorker.getRegistration(scope);
        })
      .then(function(result) {
          assert_equals(
            result,
            undefined,
            'After unregister(), the registration should not found');
        });
  }, 'Unregister on script evaluation');

promise_test(function(t) {
    var script = 'resources/unregister-worker.js?install';
    var scope = 'resources/scope/unregister-on-install-event';

    return service_worker_unregister_and_register(t, script, scope)
      .then(function(registration) {
          t.add_cleanup(function() {
              return service_worker_unregister(t, scope);
            });

          return wait_for_state(t, registration.installing, 'redundant');
        })
      .then(function() {
          return navigator.serviceWorker.getRegistration(scope);
        })
      .then(function(result) {
          assert_equals(
            result,
            undefined,
            'After unregister(), the registration should not found');
        });
  }, 'Unregister on installing event');

promise_test(function(t) {
    var script = 'resources/unregister-worker.js?activate';
    var scope = 'resources/scope/unregister-on-activate-event';

    return service_worker_unregister_and_register(t, script, scope)
      .then(function(registration) {
          t.add_cleanup(function() {
              return service_worker_unregister(t, scope);
            });

          return wait_for_state(t, registration.installing, 'redundant');
        })
      .then(function() {
          return navigator.serviceWorker.getRegistration(scope);
        })
      .then(function(result) {
          assert_equals(
            result,
            undefined,
            'After unregister(), the registration should not found');
        });
  }, 'Unregister on activate event');

promise_test(function(t) {
    var script = 'resources/unregister-worker.js';
    var scope = 'resources/unregister-controlling-worker.html';

    var controller;
    var frame;

    return service_worker_unregister_and_register(t, script, scope)
      .then(function(registration) {
          t.add_cleanup(function() {
              return service_worker_unregister(t, scope);
            });

          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() { return with_iframe(scope); })
      .then(function(f) {
          frame = f;
          controller = frame.contentWindow.navigator.serviceWorker.controller;

          assert_equals(
            controller.scriptURL,
            normalizeURL(script),
            'Service worker should control a new document')

          // Wait for the completion of unregister() on the worker.
          var channel = new MessageChannel();
          var promise = new Promise(function(resolve) {
              channel.port1.onmessage = t.step_func(function(e) {
                  assert_true(e.data.result,
                              'unregister() should successfully finish');
                  resolve();
                });
            });
          controller.postMessage({port: channel.port2}, [channel.port2]);
          return promise;
        })
      .then(function() {
          return navigator.serviceWorker.getRegistration(scope);
        })
      .then(function(result) {
          assert_equals(
            result,
            undefined,
            'After unregister(), the registration should not found');
          assert_equals(
            frame.contentWindow.navigator.serviceWorker.controller,
            controller,
            'After unregister(), the worker should still control the document');
          return with_iframe(scope);
        })
      .then(function(new_frame) {
          assert_equals(
            new_frame.contentWindow.navigator.serviceWorker.controller,
            null,
            'After unregister(), the worker should not control a new document');

          frame.remove();
          new_frame.remove();
        })
  }, 'Unregister controlling service worker');

</script>