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 = 'resources/there/is/no/there/there'; | |
nhiroki
2016/07/14 14:24:50
Maybe "var registration;" is necessary?
falken
2016/07/14 14:54:19
Done.
| |
18 return navigator.serviceWorker.register('resources/empty-worker.js', | |
19 { scope: dummy_scope }) | |
20 .then(r => { | |
21 registration = r; | |
22 return wait_for_state(t, registration.installing, 'activated'); | |
23 }) | |
24 .then(() => registration.unregister()); | |
25 } | |
26 | |
27 // Returns {registration, iframe}, where |registration| has an active and | |
28 // waiting worker. The active worker controls |iframe| and has an inflight | |
29 // message event that can be finished by calling | |
30 // |registration.active.postMessage('go')|. | |
31 function setup_activation_test(t, scope, worker_url) { | |
32 var registration; | |
33 var iframe; | |
34 | |
35 return navigator.serviceWorker.getRegistration(scope) | |
36 .then(r => { | |
37 if (r) | |
38 return r.unregister(); | |
39 }) | |
40 .then(() => { | |
41 // Create an in-scope iframe. Do this prior to registration to avoid | |
42 // racing between an update triggered by navigation and the update() | |
43 // call below. | |
44 return with_iframe(scope); | |
45 }) | |
46 .then(f => { | |
47 iframe = f; | |
48 | |
49 // Create an active worker. | |
50 return navigator.serviceWorker.register(worker_url, { scope: scope }); | |
51 }) | |
52 .then(r => { | |
53 registration = r; | |
54 add_result_callback(() => registration.unregister); | |
55 | |
56 return wait_for_state(t, r.installing, 'activated'); | |
57 }) | |
58 .then(() => { | |
59 // Check that the frame was claimed. | |
60 assert_not_equals( | |
61 iframe.contentWindow.navigator.serviceWorker.controller, null); | |
62 | |
63 // Create an in-flight request. | |
64 registration.active.postMessage('wait'); | |
65 | |
66 // Now there is both a controllee and an in-flight request. | |
67 // Initiate an update. | |
68 return registration.update(); | |
69 }) | |
70 .then(() => { | |
71 // Wait for a waiting worker. | |
72 return wait_for_state(t, registration.installing, 'installed'); | |
73 }) | |
74 .then(() => { | |
75 return wait_for_activation_on_dummy_scope(t); | |
76 }) | |
77 .then(() => { | |
78 assert_not_equals(registration.waiting, null); | |
79 assert_not_equals(registration.active, null); | |
80 return Promise.resolve({registration: registration, iframe: iframe}); | |
81 }); | |
82 } | |
83 | |
84 promise_test(t => { | |
85 var scope = 'resources/no-controllee'; | |
86 var worker_url = 'resources/mint-new-worker.php'; | |
87 var registration; | |
88 var iframe; | |
89 var new_worker; | |
90 | |
91 return setup_activation_test(t, scope, worker_url) | |
92 .then(result => { | |
93 registration = result.registration; | |
94 iframe = result.iframe; | |
95 | |
96 // Finish the in-flight request. | |
97 registration.active.postMessage('go'); | |
98 return wait_for_activation_on_dummy_scope(t); | |
99 }) | |
100 .then(() => { | |
101 // The new worker is still waiting. Remove the frame and it should | |
102 // activate. | |
103 new_worker = registration.waiting; | |
104 | |
105 assert_equals(new_worker.state, 'installed'); | |
106 var reached_active = wait_for_state(t, new_worker, 'activating'); | |
107 iframe.remove(); | |
108 return reached_active; | |
109 }) | |
110 .then(() => { | |
111 assert_equals(new_worker, registration.active); | |
112 }); | |
113 }, 'loss of controllees triggers activation'); | |
114 | |
115 promise_test(t => { | |
116 var scope = 'resources/no-request'; | |
117 var worker_url = 'resources/mint-new-worker.php'; | |
118 var registration; | |
119 var iframe; | |
120 var new_worker; | |
121 | |
122 return setup_activation_test(t, scope, worker_url) | |
123 .then(result => { | |
124 registration = result.registration; | |
125 iframe = result.iframe; | |
126 | |
127 // Remove the iframe. | |
128 iframe.remove(); | |
129 return new Promise(resolve => setTimeout(resolve, 0)); | |
130 }) | |
131 .then(() => { | |
132 // Finish the request. | |
133 new_worker = registration.waiting; | |
134 var reached_active = wait_for_state(t, new_worker, 'activating'); | |
135 registration.active.postMessage('go'); | |
136 return reached_active; | |
137 }) | |
138 .then(() => { | |
139 assert_equals(registration.active, new_worker); | |
140 }); | |
141 }, 'finishing a request triggers activation'); | |
142 | |
143 promise_test(t => { | |
144 var scope = 'resources/skip-waiting'; | |
145 var worker_url = 'resources/mint-new-worker.php?skip-waiting'; | |
146 var registration; | |
147 var new_worker; | |
148 | |
149 return setup_activation_test(t, scope, worker_url) | |
150 .then(result => { | |
151 registration = result.registration; | |
152 | |
153 // Finish the request. The iframe does not need to be removed because | |
154 // skipWaiting() was called. | |
155 new_worker = registration.waiting; | |
156 var reached_active = wait_for_state(t, new_worker, 'activating'); | |
157 registration.active.postMessage('go'); | |
158 return reached_active; | |
159 }) | |
160 .then(() => { | |
161 assert_equals(registration.active, new_worker); | |
162 }); | |
163 }, 'skipWaiting bypasses no controllee requirement'); | |
164 </script> | |
OLD | NEW |