summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/register-same-scope-different-script-url.https.html
blob: 445be740951b16aaec304e1679f58981f3e3d7ba (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
var script1 = normalizeURL('resources/empty-worker.js');
var script2 = normalizeURL('resources/empty-worker.js?new');

async_test(function(t) {
    var scope = 'resources/scope/register-new-script-concurrently';
    var register_promise1;
    var register_promise2;

    service_worker_unregister(t, scope)
      .then(function() {
          register_promise1 = navigator.serviceWorker.register(script1,
                                                               {scope: scope});
          register_promise2 = navigator.serviceWorker.register(script2,
                                                               {scope: scope});
          return register_promise1;
        })
      .then(function(registration) {
          assert_equals(registration.installing.scriptURL, script1,
                        'on first register, first script should be installing');
          assert_equals(registration.waiting, null,
                        'on first register, waiting should be null');
          assert_equals(registration.active, null,
                        'on first register, active should be null');
          return register_promise2;
        })
      .then(function(registration) {
          assert_equals(
              registration.installing.scriptURL, script2,
              'on second register, second script should be installing');
          // Spec allows racing: the first register may have finished
          // or the second one could have terminated the installing worker.
          assert_true(registration.waiting == null ||
                      registration.waiting.scriptURL == script1,
                      'on second register, .waiting should be null or the ' +
                      'first script');
          assert_true(registration.active == null ||
                      (registration.waiting == null &&
                       registration.active.scriptURL == script1),
                      'on second register, .active should be null or the ' +
                      'first script');
          return registration.unregister();
        })
      .then(function() {
          t.done();
        })
      .catch(unreached_rejection(t));
  }, 'Register different scripts concurrently');

async_test(function(t) {
    var scope = 'resources/scope/register-then-register-new-script';
    var registration;

    service_worker_unregister_and_register(t, script1, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() {
          assert_equals(registration.installing, null,
                        'on activated, installing should be null');
          assert_equals(registration.waiting, null,
                        'on activated, waiting should be null');
          assert_equals(registration.active.scriptURL, script1,
                        'on activated, the first script should be active');
          return navigator.serviceWorker.register(script2, {scope:scope});
        })
      .then(function(r) {
          registration = r;
          assert_equals(registration.installing.scriptURL, script2,
                        'on second register, the second script should be ' +
                        'installing');
          assert_equals(registration.waiting, null,
                        'on second register, waiting should be null');
          assert_equals(registration.active.scriptURL, script1,
                        'on second register, the first script should be ' +
                        'active');
          return wait_for_state(t, registration.installing, 'installed');
        })
      .then(function() {
          assert_equals(registration.installing, null,
                        'on installed, installing should be null');
          // Since the registration is not controlling any document, the new
          // worker can immediately transition to active.
          if (registration.waiting) {
            assert_equals(registration.waiting.scriptURL, script2,
                          'on installed, the second script may still be waiting');
            assert_equals(registration.active.scriptURL, script1,
                          'on installed, the first script may be active');
          } else {
            assert_equals(registration.active.scriptURL, script2,
                          'on installed, the second script may be active');
          }
          return registration.unregister();
        })
      .then(function() {
          t.done();
        })
      .catch(unreached_rejection(t));
  }, 'Register then register new script URL');

async_test(function(t) {
    var scope = 'resources/scope/register-then-register-new-script-404';
    var registration;

    service_worker_unregister_and_register(t, script1, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() {
          assert_equals(registration.installing, null,
                        'on activated, installing should be null');
          assert_equals(registration.waiting, null,
                        'on activated, waiting should be null');
          assert_equals(registration.active.scriptURL, script1,
                        'on activated, the first script should be active');
          return navigator.serviceWorker.register('this-will-404.js',
                                                  {scope:scope});
        })
      .then(
        function() { assert_unreached('register should reject'); },
        function(error) {
          assert_equals(registration.installing, null,
                        'on rejected, installing should be null');
          assert_equals(registration.waiting, null,
                        'on rejected, waiting should be null');
          assert_equals(registration.active.scriptURL, script1,
                        'on rejected, the first script should be active');
          return registration.unregister();
        })
      .then(function() {
          t.done();
        })
      .catch(unreached_rejection(t));
  }, 'Register then register new script URL that 404s');

async_test(function(t) {
    var scope = 'resources/scope/register-then-register-new-script-reject-install';
    var reject_script = normalizeURL('resources/reject-install-worker.js');
    var registration;

    service_worker_unregister_and_register(t, script1, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() {
          assert_equals(registration.installing, null,
                        'on activated, installing should be null');
          assert_equals(registration.waiting, null,
                        'on activated, waiting should be null');
          assert_equals(registration.active.scriptURL, script1,
                        'on activated, the first script should be active');
          return navigator.serviceWorker.register(reject_script, {scope:scope});
        })
      .then(function(r) {
          registration = r;
          assert_equals(registration.installing.scriptURL, reject_script,
                        'on update, the second script should be installing');
          assert_equals(registration.waiting, null,
                        'on update, waiting should be null');
          assert_equals(registration.active.scriptURL, script1,
                        'on update, the first script should be active');
          return wait_for_state(t, registration.installing, 'redundant');
        })
      .then(function() {
          assert_equals(registration.installing, null,
                        'on redundant, installing should be null');
          assert_equals(registration.waiting, null,
                        'on redundant, waiting should be null');
          assert_equals(registration.active.scriptURL, script1,
                        'on redundant, the first script should be active');
          return registration.unregister();
        })
      .then(function() {
          t.done();
        })
      .catch(unreached_rejection(t));
  }, 'Register then register new script that does not install');

async_test(function(t) {
    var scope = 'resources/scope/register-new-script-controller';
    var iframe;
    var registration;

    service_worker_unregister_and_register(t, script1, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          iframe = frame;
          return navigator.serviceWorker.register(script2, { scope: scope })
        })
      .then(function(r) {
          registration = r;
          return wait_for_state(t, registration.installing, 'installed');
        })
      .then(function() {
          var sw_container = iframe.contentWindow.navigator.serviceWorker;
          assert_equals(sw_container.controller.scriptURL, script1,
                        'the old version should control the old doc');
          return with_iframe(scope);
        })
      .then(function(frame) {
          var sw_container = frame.contentWindow.navigator.serviceWorker;
          assert_equals(sw_container.controller.scriptURL, script1,
                        'the old version should control a new doc');
          var onactivated_promise = wait_for_state(t,
                                                   registration.waiting,
                                                   'activated');
          frame.remove();
          iframe.remove();
          return onactivated_promise;
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          var sw_container = frame.contentWindow.navigator.serviceWorker;
          assert_equals(sw_container.controller.scriptURL, script2,
                        'the new version should control a new doc');
          frame.remove();
          return registration.unregister();
        })
      .then(function() {
          t.done();
        })
      .catch(unreached_rejection(t));
  }, 'Register same-scope new script url effect on controller');

</script>