Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / serviceworker / chromium / service-worker-gc.html
bloba54ab5607b49919667644311483d1c5460786410
1 <!DOCTYPE html>
2 <script src="/js-test-resources/js-test.js"></script>
3 <script src="../resources/test-helpers.js"></script>
4 <script>
5 window.jsTestIsAsync = true;
6 description('Test that ServiceWorker and ServiceWorkerRegistration are not garbage collected prematurely');
7 var registrationObservation = null;
8 var swObservation = null;
9 var scope = base_path() + '../resources/gc';
11 if (!window.internals) {
12 testFailed('This test requires internals.observeGC');
13 finishJSTest();
14 } else {
15 setup();
18 function setup() {
19 var worker = '../resources/empty-worker.js';
20 unregisterAndRegister(worker, scope).then(onRegister);
23 function unregisterAndRegister(url, scope) {
24 return navigator.serviceWorker.getRegistration(scope)
25 .then(function(registration) {
26 if (registration)
27 return registration.unregister();
29 .then(function() {
30 return navigator.serviceWorker.register(url, { scope: scope });
32 .catch(function(error) {
33 testFailed('Could not register worker: ' + error);
34 finishJSTest();
35 });
38 function onRegister(registration) {
39 registrationObservation = internals.observeGC(registration);
40 registration.addEventListener('updatefound', (function() {
41 onUpdate(registration.installing);
42 }));
45 function onUpdate(sw) {
46 swObservation = internals.observeGC(sw);
47 sw.addEventListener('statechange', onStateChange);
50 function onStateChange(event) {
51 // Use setTimeout to ensure a fresh stack with no references to the worker.
52 switch (event.target.state) {
53 case 'activated':
54 setTimeout(unregister, 0);
55 break;
56 case 'redundant':
57 setTimeout(finish, 0);
58 break;
62 function unregister() {
63 // The worker has an event handler that can still receive the state change
64 // to 'redundant', so it shouldn't be collected yet.
65 gc();
66 shouldBeFalse('registrationObservation.wasCollected');
67 shouldBeFalse('swObservation.wasCollected');
68 navigator.serviceWorker.getRegistration(scope)
69 .then(function(registration) {
70 registration.unregister();
72 .catch(function(error) {
73 testFailed('Could not unregister worker: ' + error);
74 finishJSTest();
75 });
78 function finish()
80 // The worker is 'redundant' but the registration holds a reference to it,
81 // so it shouldn't be collected yet.
82 // FIXME: When crbug.com/398355 is fixed, register a new script URL and
83 // once the new worker is activated, check that the old worker is gc'd.
84 gc();
85 shouldBeFalse('registrationObservation.wasCollected');
86 shouldBeFalse('swObservation.wasCollected');
87 finishJSTest();
89 </script>