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
|
<!doctype html>
<meta charset=utf-8>
<title>Service Worker: claim() should affect other registration</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 frame;
var init_scope = 'resources/blank.html?affect-other';
var init_worker_url = 'resources/empty.js';
var init_registration;
var init_workers = [undefined, undefined];
var claim_scope = 'resources/blank.html?affect-other-registration';
var claim_worker_url = 'resources/claim-worker.js';
var claim_registration;
var claim_worker;
return Promise.resolve()
// Register the first service worker to init_scope.
.then(() => navigator.serviceWorker.register(init_worker_url + '?v1',
{scope: init_scope}))
.then(r => {
init_registration = r;
init_workers[0] = r.installing;
return Promise.resolve()
.then(() => wait_for_state(t, init_workers[0], 'activated'))
.then(() => assert_array_equals([init_registration.active,
init_registration.waiting,
init_registration.installing],
[init_workers[0],
null,
null],
'Wrong workers.'));
})
// Create an iframe as the client of the first service worker of init_scope.
.then(() => with_iframe(claim_scope))
.then(f => frame = f)
// Check the controller.
.then(() => frame.contentWindow.navigator.serviceWorker.getRegistration(
normalizeURL(init_scope)))
.then(r => assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
r.active,
'.controller should belong to init_scope.'))
// Register the second service worker to init_scope.
.then(() => navigator.serviceWorker.register(init_worker_url + '?v2',
{scope: init_scope}))
.then(r => {
assert_equals(r, init_registration, 'Should be the same registration');
init_workers[1] = r.installing;
return Promise.resolve()
.then(() => wait_for_state(t, init_workers[1], 'installed'))
.then(() => assert_array_equals([init_registration.active,
init_registration.waiting,
init_registration.installing],
[init_workers[0],
init_workers[1],
null],
'Wrong workers.'));
})
// Register a service worker to claim_scope.
.then(() => navigator.serviceWorker.register(claim_worker_url,
{scope: claim_scope}))
.then(r => {
claim_registration = r;
claim_worker = r.installing;
return wait_for_state(t, claim_worker, 'activated')
})
// Let claim_worker claim the created iframe.
.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, 'PASS',
'Worker call to claim() should fulfill.');
resolve();
});
});
claim_worker.postMessage({port: channel.port2}, [channel.port2]);
return saw_message;
})
// Check the controller.
.then(() => frame.contentWindow.navigator.serviceWorker.getRegistration(
normalizeURL(claim_scope)))
.then(r => assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
r.active,
'.controller should belong to claim_scope.'))
// Check the status of created registrations and service workers.
.then(() => wait_for_state(t, init_workers[1], 'activated'))
.then(() => {
assert_array_equals([claim_registration.active,
claim_registration.waiting,
claim_registration.installing],
[claim_worker,
null,
null],
'claim_worker should be the only worker.')
assert_array_equals([init_registration.active,
init_registration.waiting,
init_registration.installing],
[init_workers[1],
null,
null],
'The waiting sw should become the active worker.')
assert_array_equals([init_workers[0].state,
init_workers[1].state,
claim_worker.state],
['redundant',
'activated',
'activated'],
'Wrong worker states.');
})
// Cleanup and finish testing.
.then(() => frame.remove())
.then(() => Promise.all([
init_registration.unregister(),
claim_registration.unregister()
]))
.then(() => t.done());
}, 'claim() should affect the originally controlling registration.');
</script>
|