OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <script src="/js-test-resources/js-test.js"></script> | |
3 <body> | |
4 <script> | |
5 (function() { | |
6 window.jsTestIsAsync = true; | |
7 description('Test that a registered Service Worker with an event handler is not garbage collected'); | |
dominicc (has gone to gerrit)
2014/06/13 19:55:13
Maybe add '...prematurely'?
falken
2014/06/13 22:34:18
Done.
| |
8 var worker = 'resources/empty-worker.js'; | |
9 var scope = 'gc'; | |
10 swObservation = null; | |
11 | |
12 unregister_and_register(worker, scope).then(onRegister); | |
13 | |
14 function unregister_and_register(url, scope) { | |
15 return navigator.serviceWorker.unregister(scope).then(function() { | |
16 return navigator.serviceWorker.register(url, { scope: scope }); | |
17 }).catch(function(error) { | |
18 debug('Could not register worker: ' + error); | |
19 finishJSTest(); | |
20 }); | |
21 } | |
22 | |
23 function collectGarbage(shouldBeCollected, description) { | |
dominicc (has gone to gerrit)
2014/06/13 19:55:13
I'm not sure this function is pulling its weight.
falken
2014/06/13 22:34:18
I tried but there's a couple problems with that:
1
| |
24 gc(); | |
25 if (swObservation) { | |
dominicc (has gone to gerrit)
2014/06/13 19:55:13
Would it be better to fail if this is not set up?
falken
2014/06/13 22:34:18
I agree this is making the test more complex and c
| |
26 debug(description); | |
27 if (shouldBeCollected) | |
28 shouldBeTrue('swObservation.wasCollected'); | |
29 else | |
30 shouldBeFalse('swObservation.wasCollected'); | |
31 } | |
32 } | |
33 | |
34 function onUnregister() { | |
35 // FIXME: The expectation should be reversed, but our implementation cur rently fails it. | |
36 // When properly implemented, the SW not leak here. It may be better to move this test into a separate, targeted file. | |
37 collectGarbage(false, 'Service Worker should not leak after unregistrati on'); | |
38 finishJSTest(); | |
39 } | |
40 | |
41 function onRegister(sw) { | |
42 sw.addEventListener('statechange', onStateChange); | |
43 if (window.internals) | |
44 swObservation = internals.observeGC(sw); | |
dominicc (has gone to gerrit)
2014/06/13 19:55:13
Is this a global just so shouldBe works?
falken
2014/06/13 22:34:18
That, and onRegister needs it.
| |
45 setTimeout(collectGarbage.bind(undefined, false, 'Service Worker should not be gc\'d after registration'), 0); | |
46 } | |
47 | |
48 function onStateChange(event) { | |
49 collectGarbage(false, 'Service Worker should not be gc\'d in ' + event.t arget.state + ' state'); | |
dominicc (has gone to gerrit)
2014/06/13 19:55:13
If it is garbage collected, won't event.target be
falken
2014/06/13 22:34:18
From what I've seen on the timing out test, if we
| |
50 if (event.target.state != 'active') | |
51 return; | |
52 navigator.serviceWorker.unregister(scope).then(onUnregister); | |
53 } | |
54 }()); | |
55 </script> | |
56 </body> | |
OLD | NEW |