-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathready.https.html
317 lines (282 loc) · 12.3 KB
/
ready.https.html
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!DOCTYPE html>
<title>Service Worker: navigator.serviceWorker.ready</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
test(function() {
var promise = navigator.serviceWorker.ready;
assert_equals(promise, navigator.serviceWorker.ready,
'repeated access to ready without intervening ' +
'registrations should return the same Promise object');
}, 'ready returns the same Promise object');
promise_test(function(t) {
return with_iframe('resources/blank.html?uncontrolled')
.then(t.step_func(function(frame) {
var promise = frame.contentWindow.navigator.serviceWorker.ready;
t.add_cleanup(function() {
frame.remove();
});
assert_equals(Object.getPrototypeOf(promise),
frame.contentWindow.Promise.prototype,
'the Promise should be in the context of the ' +
'related document');
}));
}, 'ready returns a Promise object in the context of the related document');
promise_test(function(t) {
var url = 'resources/empty-worker.js';
var scope = 'resources/blank.html?ready-controlled';
var expected_url = normalizeURL(url);
var frame;
return service_worker_unregister_and_register(t, url, scope)
.then(function(registration) {
add_completion_callback(function() {
registration.unregister();
});
return wait_for_state(t, registration.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(f) {
t.add_cleanup(function() {
f.remove();
});
frame = f;
return frame.contentWindow.navigator.serviceWorker.ready;
})
.then(function(registration) {
assert_equals(registration.installing, null,
'installing should be null');
assert_equals(registration.waiting, null,
'waiting should be null');
assert_equals(registration.active.scriptURL, expected_url,
'active after ready should not be null');
assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
registration.active,
'the controller should be the active worker');
assert_in_array(registration.active.state,
['activating', 'activated'],
'.ready should be resolved when the registration ' +
'has an active worker');
});
}, 'ready on a controlled document');
promise_test(function(t) {
var url = 'resources/empty-worker.js';
var scope = 'resources/blank.html?ready-potential-controlled';
var expected_url = normalizeURL(url);
var frame;
return with_iframe(scope)
.then(function(f) {
t.add_cleanup(function() {
f.remove();
});
frame = f;
return navigator.serviceWorker.register(url, {scope:scope});
})
.then(function(r) {
add_completion_callback(function() {
r.unregister();
});
return frame.contentWindow.navigator.serviceWorker.ready;
})
.then(function(registration) {
assert_equals(registration.installing, null,
'installing should be null');
assert_equals(registration.waiting, null,
'waiting should be null.')
assert_equals(registration.active.scriptURL, expected_url,
'active after ready should not be null');
assert_in_array(registration.active.state,
['activating', 'activated'],
'.ready should be resolved when the registration ' +
'has an active worker');
assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
null,
'uncontrolled document should not have a controller');
});
}, 'ready on a potential controlled document');
promise_test(function(t) {
var url = 'resources/empty-worker.js';
var scope = 'resources/blank.html?ready-installing';
return service_worker_unregister(t, scope)
.then(function() {
return with_iframe(scope);
})
.then(function(f) {
var promise = f.contentWindow.navigator.serviceWorker.ready;
t.add_cleanup(function() {
f.remove();
});
navigator.serviceWorker.register(url, {scope: scope});
return promise;
})
.then(function(registration) {
add_completion_callback(function() {
registration.unregister();
});
assert_equals(registration.installing, null,
'installing should be null');
assert_equals(registration.waiting, null, 'waiting should be null');
assert_not_equals(registration.active, null,
'active after ready should not be null');
assert_in_array(registration.active.state,
['activating', 'activated'],
'.ready should be resolved when the registration ' +
'has an active worker');
});
}, 'ready on an iframe whose parent registers a new service worker');
promise_test(function(t) {
var url = 'resources/empty-worker.js';
var scope = 'resources/register-iframe.html';
var expected_url = normalizeURL(url);
return with_iframe(scope)
.then(function(f) {
t.add_cleanup(function() {
f.remove();
});
return f.contentWindow.navigator.serviceWorker.ready;
})
.then(function(registration) {
add_completion_callback(function() {
registration.unregister();
});
assert_equals(registration.installing, null,
'installing should be null');
assert_equals(registration.waiting, null, 'waiting should be null');
assert_not_equals(registration.active, null,
'active after ready should not be null');
assert_in_array(registration.active.state,
['activating', 'activated'],
'.ready should be resolved with "active worker"');
});
}, 'ready on an iframe that installs a new service worker');
promise_test(function(t) {
var url = 'resources/empty-worker.js';
var matched_scope = 'resources/blank.html?ready-after-match';
var longer_matched_scope = 'resources/blank.html?ready-after-match-longer';
var frame, registration;
return Promise.all([service_worker_unregister(t, matched_scope),
service_worker_unregister(t, longer_matched_scope)])
.then(function() {
return with_iframe(longer_matched_scope);
})
.then(function(f) {
t.add_cleanup(function() {
f.remove();
});
frame = f;
return navigator.serviceWorker.register(url, {scope: matched_scope});
})
.then(function(r) {
add_completion_callback(function() {
r.unregister();
});
registration = r;
return wait_for_state(t, r.installing, 'activated');
})
.then(function() {
return navigator.serviceWorker.register(
url, {scope: longer_matched_scope});
})
.then(function(r) {
add_completion_callback(function() {
r.unregister();
});
return frame.contentWindow.navigator.serviceWorker.ready;
})
.then(function(r) {
assert_equals(r.scope, normalizeURL(longer_matched_scope),
'longer matched registration should be returned');
assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
null, 'controller should be null');
});
}, 'ready after a longer matched registration registered');
promise_test(function(t) {
var url = 'resources/empty-worker.js';
var matched_scope = 'resources/blank.html?ready-after-resolve';
var longer_matched_scope =
'resources/blank.html?ready-after-resolve-longer';
var frame, registration;
return service_worker_unregister_and_register(t, url, matched_scope)
.then(function(r) {
add_completion_callback(function() {
r.unregister();
});
registration = r;
return wait_for_state(t, r.installing, 'activated');
})
.then(function() {
return with_iframe(longer_matched_scope);
})
.then(function(f) {
t.add_cleanup(function() {
f.remove();
});
frame = f;
return f.contentWindow.navigator.serviceWorker.ready;
})
.then(function(r) {
assert_equals(r.scope, normalizeURL(matched_scope),
'matched registration should be returned');
return navigator.serviceWorker.register(
url, {scope: longer_matched_scope});
})
.then(function(r) {
add_completion_callback(function() {
r.unregister();
});
return frame.contentWindow.navigator.serviceWorker.ready;
})
.then(function(r) {
assert_equals(r.scope, normalizeURL(matched_scope),
'ready should only be resolved once');
});
}, 'access ready after it has been resolved');
promise_test(async function(t) {
const url1 = 'resources/empty-worker.js';
const url2 = url1 + '?2';
const matched_scope = 'resources/blank.html?ready-after-unregister';
const reg1 = await service_worker_unregister_and_register(t, url1, matched_scope);
t.add_cleanup(() => reg1.unregister());
await wait_for_state(t, reg1.installing, 'activating');
// This registration will resolve all ready promises in clients that match the scope.
// But there are no clients.
const frame = await with_iframe(matched_scope);
t.add_cleanup(() => frame.remove());
await reg1.unregister();
// Access the ready promise while the registration is unregistering.
const readyPromise = frame.contentWindow.navigator.serviceWorker.ready;
// Create a new registration.
const reg2 = await navigator.serviceWorker.register(url2, { scope: matched_scope });
t.add_cleanup(() => reg2.unregister());
// This registration will resolve all ready promises in clients that match the scope.
// That includes frame's client.
const readyReg = await readyPromise;
assert_equals(readyReg.active.scriptURL, reg2.active.scriptURL, 'Resolves with the second registration');
assert_not_equals(reg1, reg2, 'Registrations should be different');
}, 'resolve ready after unregistering and reregistering');
promise_test(async function(t) {
const url1 = 'resources/empty-worker.js';
const url2 = url1 + '?2';
const matched_scope = 'resources/blank.html?ready-after-unregister';
const frame = await with_iframe(matched_scope);
t.add_cleanup(() => frame.remove());
const reg1 = await service_worker_unregister_and_register(t, url1, matched_scope);
t.add_cleanup(() => reg1.unregister());
await wait_for_state(t, reg1.installing, 'activated');
// This registration will resolve all ready promises in clients that match the scope.
// That includes frame's client.
const reg1Active = reg1.active;
await reg1.unregister();
// Access the ready promise while the registration is unregistering.
const readyPromise = frame.contentWindow.navigator.serviceWorker.ready;
// Create a new registration.
const reg2 = await navigator.serviceWorker.register(url2, { scope: matched_scope });
t.add_cleanup(() => reg2.unregister());
// This registration will resolve all ready promises in clients that match the scope.
// That includes frame's client, but its ready promise has already resolved.
const readyReg = await readyPromise;
assert_equals(readyReg.active.scriptURL, reg1Active.scriptURL, 'Resolves with the first registration');
assert_not_equals(reg1, reg2, 'Registrations should be different');
}, 'resolve ready before unregistering and reregistering');
</script>