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