Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/http/tests/serviceworker/activation.html |
| diff --git a/third_party/WebKit/LayoutTests/http/tests/serviceworker/activation.html b/third_party/WebKit/LayoutTests/http/tests/serviceworker/activation.html |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b98fb56d30a4b809228dc9de1fcef49c7138b261 |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/http/tests/serviceworker/activation.html |
| @@ -0,0 +1,151 @@ |
| +<!DOCTYPE html> |
| +<meta charset="utf-8"> |
| +<title>service worker: activation</title> |
| +<script src="../resources/testharness.js"></script> |
| +<script src="../resources/testharnessreport.js"></script> |
| +<script src="resources/test-helpers.js"></script> |
| +<script> |
| +// Returns {registration, iframe}, where |registration| has an active and |
| +// waiting worker. The active worker controls |iframe| and has an inflight |
| +// message event that can be finished by calling |
| +// |registration.active.postMessage('go')|. |
| + |
| +function setup_activation_test(t, scope, worker_url) { |
| + var registration; |
| + var iframe; |
| + |
| + return navigator.serviceWorker.getRegistration(scope) |
| + .then(r => { |
| + if (r) |
| + return r.unregister(); |
| + }) |
| + .then(() => { |
| + // Create an in-scope iframe. Do this prior to registration to avoid |
| + // racing between an update triggered by navigation and the update() |
| + // call below. |
| + return with_iframe(scope); |
| + }) |
| + .then(f => { |
| + iframe = f; |
| + |
| + // Register. |
| + return navigator.serviceWorker.register(worker_url, { scope: scope }); |
| + }) |
| + .then(r => { |
| + registration = r; |
| + add_result_callback(() => registration.unregister); |
| + |
| + // Create an active worker. |
| + return wait_for_state(t, r.installing, 'activated'); |
| + }) |
| + .then(() => { |
| + // Check that the frame was claimed. |
| + assert_not_equals(iframe.contentWindow.navigator.serviceWorker.controller, null); |
| + |
| + // Create an in-flight request. |
| + registration.active.postMessage('wait'); |
| + |
| + // Now there is both a controllee and an in-flight request. |
| + // Initiate an update. |
| + return registration.update(); |
| + }) |
| + .then(() => { |
| + // Wait for a waiting worker. |
| + return wait_for_state(t, registration.installing, 'installed'); |
| + }) |
| + .then(() => { |
| + // To test that the new worker remains waiting until the activation |
| + // conditions are satisfied, pump the event loop. |
| + 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.
|
| + }) |
| + .then(() => { |
| + assert_not_equals(registration.waiting, null); |
| + assert_not_equals(registration.active, null); |
| + return Promise.resolve({registration: registration, iframe: |
| + iframe}); |
| + }); |
| +} |
| + |
| +promise_test(t => { |
| + var scope = 'resources/no-controllee'; |
| + var worker_url = 'resources/mint-new-worker.php'; |
| + var registration; |
| + var iframe; |
| + var new_worker; |
| + |
| + return setup_activation_test(t, scope, worker_url) |
| + .then(result => { |
| + registration = result.registration; |
| + iframe = result.iframe; |
| + |
| + // Finish the in-flight request. |
| + registration.active.postMessage('go'); |
| + |
| + // Pump the event loop. |
| + return new Promise(resolve => setTimeout(resolve, 0)); |
| + }) |
| + .then(() => { |
| + // The new worker is still waiting. Remove the frame and it should |
| + // activate. |
| + new_worker = registration.waiting; |
| + |
| + assert_equals(new_worker.state, 'installed'); |
| + var reached_active = wait_for_state(t, new_worker, 'activating'); |
| + iframe.remove(); |
| + return reached_active; |
| + }) |
| + .then(() => { |
| + assert_equals(new_worker, registration.active); |
| + }); |
| + }, 'loss of controllees triggers activation'); |
| + |
| +promise_test(t => { |
| + var scope = 'resources/no-request'; |
| + var worker_url = 'resources/mint-new-worker.php'; |
| + var registration; |
| + var iframe; |
| + var new_worker; |
| + |
| + return setup_activation_test(t, scope, worker_url) |
| + .then(result => { |
| + registration = result.registration; |
| + iframe = result.iframe; |
| + |
| + // Remove the iframe. |
| + iframe.remove(); |
| + return new Promise(resolve => setTimeout(resolve, 0)); |
| + }) |
| + .then(() => { |
| + // Finish the request. |
| + new_worker = registration.waiting; |
| + var reached_active = wait_for_state(t, new_worker, 'activating'); |
| + registration.active.postMessage('go'); |
| + return reached_active; |
| + }) |
| + .then(() => { |
| + assert_equals(registration.active, new_worker); |
| + }); |
| + }, 'finishing a request triggers activation'); |
| + |
| +promise_test(t => { |
| + var scope = 'resources/skip-waiting'; |
| + var worker_url = 'resources/mint-new-worker.php?skip-waiting'; |
| + var registration; |
| + var new_worker; |
| + |
| + return setup_activation_test(t, scope, worker_url) |
| + .then(result => { |
| + registration = result.registration; |
| + |
| + // Finish the request. The iframe does not need to be removed because |
| + // skipWaiting() was called. |
| + new_worker = registration.waiting; |
| + var reached_active = wait_for_state(t, new_worker, 'activating'); |
| + registration.active.postMessage('go'); |
| + return reached_active; |
| + }) |
| + .then(() => { |
| + assert_equals(registration.active, new_worker); |
| + }); |
| + }, 'skipWaiting bypasses no controllee requirement'); |
| +</script> |