| Index: LayoutTests/http/tests/serviceworker/chromium/service-worker-gc.html
|
| diff --git a/LayoutTests/http/tests/serviceworker/chromium/service-worker-gc.html b/LayoutTests/http/tests/serviceworker/chromium/service-worker-gc.html
|
| index 18e09f603fb74b31cb7594bc4af669a5cb0de62f..9f22b5c81c935e6f43f45ed4a1939d2fec0a9002 100644
|
| --- a/LayoutTests/http/tests/serviceworker/chromium/service-worker-gc.html
|
| +++ b/LayoutTests/http/tests/serviceworker/chromium/service-worker-gc.html
|
| @@ -3,62 +3,90 @@
|
| <script>
|
| window.jsTestIsAsync = true;
|
| description('Test that a registered Service Worker with an event handler is not garbage collected prematurely');
|
| -var swObservation = null;
|
| +var service_worker_observation = null;
|
| +var registration_observation = null;
|
| var scope = 'gc';
|
|
|
| if (!window.internals) {
|
| - testFailed('This test requires internals.observeGC');
|
| - finishJSTest();
|
| + testFailed('This test requires internals.observeGC');
|
| + finishJSTest();
|
| } else {
|
| - setup();
|
| + setup();
|
| }
|
|
|
| -function setup() {
|
| - var worker = '../resources/empty-worker.js';
|
| - unregisterAndRegister(worker, scope).then(onRegister);
|
| +function wait_for_state(worker, state) {
|
| + return new Promise(function(resolve) {
|
| + worker.onstatechange = function() {
|
| + if (worker.state == state)
|
| + resolve(worker);
|
| + }
|
| + });
|
| }
|
|
|
| -function unregisterAndRegister(url, scope) {
|
| - return navigator.serviceWorker.unregister(scope).then(function() {
|
| - return navigator.serviceWorker.register(url, { scope: scope });
|
| - }).catch(function(error) {
|
| - testFailed('Could not register worker: ' + error);
|
| - finishJSTest();
|
| +function wait_for_update(registration) {
|
| + return new Promise(function(resolve) {
|
| + registration.addEventListener('updatefound', function() {
|
| + resolve(registration.installing);
|
| + });
|
| });
|
| }
|
|
|
| -function onRegister(sw) {
|
| - swObservation = internals.observeGC(sw);
|
| - sw.addEventListener('statechange', onStateChange);
|
| -}
|
| +function setup() {
|
| + var worker_1 = '../resources/empty-worker.js';
|
|
|
| -function onStateChange(event) {
|
| - // Use setTimeout to ensure a fresh stack with no references to the worker.
|
| - switch (event.target.state) {
|
| - case 'activated':
|
| - setTimeout(unregister, 0);
|
| - break;
|
| - case 'redundant':
|
| - setTimeout(finish, 0);
|
| - break;
|
| - }
|
| + navigator.serviceWorker.unregister(scope)
|
| + .then(function() {
|
| + return navigator.serviceWorker.register(worker_1, { scope: scope });
|
| + })
|
| + .then(function(registration) {
|
| + registration_observation = internals.observeGC(registration);
|
| + return wait_for_update(registration);
|
| + })
|
| + .then(function(sw) {
|
| + service_worker_observation = internals.observeGC(sw);
|
| + return wait_for_state(sw, 'activated');
|
| + })
|
| + .then(function(sw) {
|
| + // Use setTimeout to ensure a fresh stack with no references to
|
| + // the registration or worker.
|
| + setTimeout(on_activated, 0);
|
| + })
|
| + .catch(function(error) {
|
| + testFailed(error.message || error.name || error);
|
| + finishJSTest();
|
| + });
|
| }
|
|
|
| -function unregister() {
|
| - // The worker has an event handler that can still receive the state change
|
| - // to 'redundant', so it shouldn't be collected yet.
|
| - gc();
|
| - shouldBeFalse('swObservation.wasCollected');
|
| - navigator.serviceWorker.unregister(scope).catch(function(error) {
|
| - testFailed('Could not unregister worker: ' + error);
|
| - finishJSTest();
|
| +function on_activated() {
|
| + // The worker has an event handler that can still receive the state change
|
| + // to 'redundant', so it shouldn't be collected yet.
|
| + gc();
|
| + shouldBeFalse('service_worker_observation.wasCollected');
|
| + shouldBeFalse('registration_observation.wasCollected');
|
| + var worker_2 = '../resources/empty-worker.js?new';
|
| + navigator.serviceWorker.register(worker_2, { scope: scope })
|
| + .then(function(registration) {
|
| + return wait_for_update(registration);
|
| + })
|
| + .then(function(sw) {
|
| + return wait_for_state(sw, 'activated');
|
| + })
|
| + .then(function() {
|
| + // Use setTimeout to ensure a fresh stack with no references to
|
| + // the registration or worker.
|
| + setTimeout(finish, 0);
|
| + })
|
| + .catch(function(error) {
|
| + testFailed(error.message || error.name || error);
|
| + finishJSTest();
|
| });
|
| }
|
|
|
| function finish()
|
| {
|
| gc();
|
| - shouldBeTrue('swObservation.wasCollected');
|
| + shouldBeTrue('service_worker_observation.wasCollected');
|
| + shouldBeFalse('registration_observation.wasCollected');
|
| finishJSTest();
|
| }
|
| </script>
|
|
|