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