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