OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <script src="/js-test-resources/js-test.js"></script> |
| 3 <body> |
| 4 <script> |
| 5 window.jsTestIsAsync = true; |
| 6 description('Test that a registered Service Worker with an event handler is not
garbage collected prematurely'); |
| 7 swObservation = null; |
| 8 scope = 'gc'; |
| 9 |
| 10 if (!window.internals) { |
| 11 testFailed('This test requires internals.observeGC'); |
| 12 finishJSTest(); |
| 13 } else { |
| 14 setup(); |
| 15 } |
| 16 |
| 17 function setup() { |
| 18 var worker = 'resources/empty-worker.js'; |
| 19 unregisterAndRegister(worker, scope).then(onRegister); |
| 20 } |
| 21 |
| 22 function unregisterAndRegister(url, scope) { |
| 23 return navigator.serviceWorker.unregister(scope).then(function() { |
| 24 return navigator.serviceWorker.register(url, { scope: scope }); |
| 25 }).catch(function(error) { |
| 26 testFailed('Could not register worker: ' + error); |
| 27 finishJSTest(); |
| 28 }); |
| 29 } |
| 30 |
| 31 function assertServiceWorkerIsNotCollected() { |
| 32 gc(); |
| 33 shouldBeFalse('swObservation.wasCollected') |
| 34 } |
| 35 |
| 36 function onRegister(sw) { |
| 37 swObservation = internals.observeGC(sw); |
| 38 sw.addEventListener('statechange', onStateChange); |
| 39 setTimeout(assertServiceWorkerIsNotCollected, 0); |
| 40 } |
| 41 |
| 42 function onStateChange(event) { |
| 43 assertServiceWorkerIsNotCollected(); |
| 44 |
| 45 if (event.target.state != 'active') |
| 46 return; |
| 47 navigator.serviceWorker.unregister(scope).then(onUnregister); |
| 48 } |
| 49 |
| 50 function onUnregister() |
| 51 { |
| 52 // FIXME: Assert that the ServiceWorker *is* collected when the 'redundant'
state is implemented. |
| 53 assertServiceWorkerIsNotCollected(); |
| 54 |
| 55 finishJSTest(); |
| 56 } |
| 57 </script> |
| 58 </body> |
OLD | NEW |