Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <script src="/js-test-resources/js-test.js"></script> | 2 <script src="/js-test-resources/js-test.js"></script> |
| 3 <script> | 3 <script> |
| 4 window.jsTestIsAsync = true; | 4 window.jsTestIsAsync = true; |
| 5 description('Test that a registered Service Worker with an event handler is not garbage collected prematurely'); | 5 description('Test that a registered Service Worker with an event handler is not garbage collected prematurely'); |
|
falken
2014/08/14 13:13:36
replace with 'Test that ServiceWorker and ServiceW
nhiroki
2014/08/14 13:22:08
Done.
| |
| 6 var registrationObservation = null; | |
| 6 var swObservation = null; | 7 var swObservation = null; |
| 7 var scope = 'gc'; | 8 var scope = 'gc'; |
| 8 | 9 |
| 9 if (!window.internals) { | 10 if (!window.internals) { |
| 10 testFailed('This test requires internals.observeGC'); | 11 testFailed('This test requires internals.observeGC'); |
| 11 finishJSTest(); | 12 finishJSTest(); |
| 12 } else { | 13 } else { |
| 13 setup(); | 14 setup(); |
| 14 } | 15 } |
| 15 | 16 |
| 16 function setup() { | 17 function setup() { |
| 17 var worker = '../resources/empty-worker.js'; | 18 var worker = '../resources/empty-worker.js'; |
| 18 unregisterAndRegister(worker, scope).then(onRegister); | 19 unregisterAndRegister(worker, scope).then(onRegister); |
| 19 } | 20 } |
| 20 | 21 |
| 21 function unregisterAndRegister(url, scope) { | 22 function unregisterAndRegister(url, scope) { |
| 22 return navigator.serviceWorker.unregister(scope).then(function() { | 23 return navigator.serviceWorker.unregister(scope).then(function() { |
| 23 return navigator.serviceWorker.register(url, { scope: scope }); | 24 return navigator.serviceWorker.register(url, { scope: scope }); |
| 24 }).catch(function(error) { | 25 }).catch(function(error) { |
| 25 testFailed('Could not register worker: ' + error); | 26 testFailed('Could not register worker: ' + error); |
| 26 finishJSTest(); | 27 finishJSTest(); |
| 27 }); | 28 }); |
| 28 } | 29 } |
| 29 | 30 |
| 30 function onRegister(sw) { | 31 function onRegister(registration) { |
| 32 registrationObservation = internals.observeGC(registration); | |
| 33 registration.addEventListener('updatefound', (function() { | |
| 34 onUpdate(registration.installing); | |
| 35 })); | |
| 36 } | |
| 37 | |
| 38 function onUpdate(sw) { | |
| 31 swObservation = internals.observeGC(sw); | 39 swObservation = internals.observeGC(sw); |
| 32 sw.addEventListener('statechange', onStateChange); | 40 sw.addEventListener('statechange', onStateChange); |
| 33 } | 41 } |
| 34 | 42 |
| 35 function onStateChange(event) { | 43 function onStateChange(event) { |
| 36 // Use setTimeout to ensure a fresh stack with no references to the worker. | 44 // Use setTimeout to ensure a fresh stack with no references to the worker. |
| 37 switch (event.target.state) { | 45 switch (event.target.state) { |
| 38 case 'activated': | 46 case 'activated': |
| 39 setTimeout(unregister, 0); | 47 setTimeout(unregister, 0); |
| 40 break; | 48 break; |
| 41 case 'redundant': | 49 case 'redundant': |
| 42 setTimeout(finish, 0); | 50 setTimeout(finish, 0); |
| 43 break; | 51 break; |
| 44 } | 52 } |
| 45 } | 53 } |
| 46 | 54 |
| 47 function unregister() { | 55 function unregister() { |
| 48 // The worker has an event handler that can still receive the state change | 56 // The worker has an event handler that can still receive the state change |
| 49 // to 'redundant', so it shouldn't be collected yet. | 57 // to 'redundant', so it shouldn't be collected yet. |
| 50 gc(); | 58 gc(); |
| 59 shouldBeFalse('registrationObservation.wasCollected'); | |
| 51 shouldBeFalse('swObservation.wasCollected'); | 60 shouldBeFalse('swObservation.wasCollected'); |
| 52 navigator.serviceWorker.unregister(scope).catch(function(error) { | 61 navigator.serviceWorker.unregister(scope).catch(function(error) { |
| 53 testFailed('Could not unregister worker: ' + error); | 62 testFailed('Could not unregister worker: ' + error); |
| 54 finishJSTest(); | 63 finishJSTest(); |
| 55 }); | 64 }); |
| 56 } | 65 } |
| 57 | 66 |
| 58 function finish() | 67 function finish() |
| 59 { | 68 { |
|
falken
2014/08/14 13:13:36
add comment:
// The worker is 'redundant' but
nhiroki
2014/08/14 13:22:08
Done.
| |
| 60 gc(); | 69 gc(); |
| 61 shouldBeTrue('swObservation.wasCollected'); | 70 shouldBeFalse('registrationObservation.wasCollected'); |
| 71 shouldBeFalse('swObservation.wasCollected'); | |
| 62 finishJSTest(); | 72 finishJSTest(); |
| 63 } | 73 } |
| 64 </script> | 74 </script> |
| OLD | NEW |