OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <meta charset="utf-8"> | |
3 <title>service worker: activation</title> | |
4 <script src="../resources/testharness.js"></script> | |
5 <script src="../resources/testharnessreport.js"></script> | |
6 <script src="resources/test-helpers.js"></script> | |
7 <script> | |
8 | |
9 // Registers, waits for activation, then unregisters on a dummy scope. | |
10 // | |
11 // This helper can be used in tests that assert that activation doesn't happen. | |
12 // It would not be sufficient to check the .waiting/.active properties once, | |
13 // since activation could be scheduled and just hasn't happened yet. Since this | |
14 // helper shows that activation of another registration completed, we can be | |
15 // sure that activation really will not happen. | |
16 function wait_for_activation_on_dummy_scope(t) { | |
17 var dummy_scope = '/there/is/no/there/there'; | |
18 navigator.serviceWorker.register('resources/empty-worker.js', { scope: dummy_s cope }) | |
nhiroki
2016/07/13 04:48:43
Can you wrap at 80 columns?
falken
2016/07/14 13:52:08
Done.
falken
2016/07/14 13:52:08
OMG more importantly this didn't return anything s
| |
19 .then(r => { | |
20 registration = r; | |
21 return wait_for_state(t, registration.installing, 'active'); | |
22 }) | |
23 .then(() => registration.unregister()); | |
24 } | |
25 | |
26 // Returns {registration, iframe}, where |registration| has an active and | |
27 // waiting worker. The active worker controls |iframe| and has an inflight | |
28 // message event that can be finished by calling | |
29 // |registration.active.postMessage('go')|. | |
30 function setup_activation_test(t, scope, worker_url) { | |
31 var registration; | |
32 var iframe; | |
33 | |
34 return navigator.serviceWorker.getRegistration(scope) | |
35 .then(r => { | |
36 if (r) | |
37 return r.unregister(); | |
38 }) | |
39 .then(() => { | |
40 // Create an in-scope iframe. Do this prior to registration to avoid | |
41 // racing between an update triggered by navigation and the update() | |
42 // call below. | |
43 return with_iframe(scope); | |
44 }) | |
45 .then(f => { | |
46 iframe = f; | |
47 | |
48 // Register. | |
nhiroki
2016/07/13 04:48:43
This comment seems useless.
falken
2016/07/14 13:52:08
Moved "Create an active worker" up here.
| |
49 return navigator.serviceWorker.register(worker_url, { scope: scope }); | |
50 }) | |
51 .then(r => { | |
52 registration = r; | |
53 add_result_callback(() => registration.unregister); | |
54 | |
55 // Create an active worker. | |
56 return wait_for_state(t, r.installing, 'activated'); | |
57 }) | |
58 .then(() => { | |
59 // Check that the frame was claimed. | |
60 assert_not_equals(iframe.contentWindow.navigator.serviceWorker.controlle r, null); | |
nhiroki
2016/07/13 04:48:43
ditto(80 columns)
falken
2016/07/14 13:52:07
Done.
| |
61 | |
62 // Create an in-flight request. | |
63 registration.active.postMessage('wait'); | |
64 | |
65 // Now there is both a controllee and an in-flight request. | |
66 // Initiate an update. | |
67 return registration.update(); | |
68 }) | |
69 .then(() => { | |
70 // Wait for a waiting worker. | |
71 return wait_for_state(t, registration.installing, 'installed'); | |
72 }) | |
73 .then(() => { | |
74 return wait_for_activation_on_dummy_scope(t); | |
75 }) | |
76 .then(() => { | |
77 assert_not_equals(registration.waiting, null); | |
78 assert_not_equals(registration.active, null); | |
79 return Promise.resolve({registration: registration, iframe: iframe}); | |
80 }); | |
81 } | |
82 | |
83 promise_test(t => { | |
84 var scope = 'resources/no-controllee'; | |
85 var worker_url = 'resources/mint-new-worker.php'; | |
86 var registration; | |
87 var iframe; | |
88 var new_worker; | |
89 | |
90 return setup_activation_test(t, scope, worker_url) | |
91 .then(result => { | |
92 registration = result.registration; | |
93 iframe = result.iframe; | |
94 | |
95 // Finish the in-flight request. | |
96 registration.active.postMessage('go'); | |
97 return wait_for_activation_on_dummy_scope(t); | |
98 }) | |
99 .then(() => { | |
100 // The new worker is still waiting. Remove the frame and it should | |
101 // activate. | |
102 new_worker = registration.waiting; | |
103 | |
104 assert_equals(new_worker.state, 'installed'); | |
105 var reached_active = wait_for_state(t, new_worker, 'activating'); | |
106 iframe.remove(); | |
107 return reached_active; | |
108 }) | |
109 .then(() => { | |
110 assert_equals(new_worker, registration.active); | |
111 }); | |
112 }, 'loss of controllees triggers activation'); | |
113 | |
114 promise_test(t => { | |
115 var scope = 'resources/no-request'; | |
116 var worker_url = 'resources/mint-new-worker.php'; | |
117 var registration; | |
118 var iframe; | |
119 var new_worker; | |
120 | |
121 return setup_activation_test(t, scope, worker_url) | |
122 .then(result => { | |
123 registration = result.registration; | |
124 iframe = result.iframe; | |
125 | |
126 // Remove the iframe. | |
127 iframe.remove(); | |
128 return new Promise(resolve => setTimeout(resolve, 0)); | |
129 }) | |
130 .then(() => { | |
131 // Finish the request. | |
132 new_worker = registration.waiting; | |
133 var reached_active = wait_for_state(t, new_worker, 'activating'); | |
134 registration.active.postMessage('go'); | |
135 return reached_active; | |
136 }) | |
137 .then(() => { | |
138 assert_equals(registration.active, new_worker); | |
139 }); | |
140 }, 'finishing a request triggers activation'); | |
141 | |
142 promise_test(t => { | |
143 var scope = 'resources/skip-waiting'; | |
144 var worker_url = 'resources/mint-new-worker.php?skip-waiting'; | |
145 var registration; | |
146 var new_worker; | |
147 | |
148 return setup_activation_test(t, scope, worker_url) | |
149 .then(result => { | |
150 registration = result.registration; | |
151 | |
152 // Finish the request. The iframe does not need to be removed because | |
153 // skipWaiting() was called. | |
154 new_worker = registration.waiting; | |
155 var reached_active = wait_for_state(t, new_worker, 'activating'); | |
156 registration.active.postMessage('go'); | |
157 return reached_active; | |
158 }) | |
159 .then(() => { | |
160 assert_equals(registration.active, new_worker); | |
161 }); | |
162 }, 'skipWaiting bypasses no controllee requirement'); | |
163 </script> | |
OLD | NEW |