Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / chromium / service-worker-gc.html
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';
10
11 if (!window.internals) {
12     testFailed('This test requires internals.observeGC');
13     finishJSTest();
14 } else {
15     setup();
16 }
17
18 function setup() {
19     var worker = '../resources/empty-worker.js';
20     unregisterAndRegister(worker, scope).then(onRegister);
21 }
22
23 function unregisterAndRegister(url, scope) {
24     return navigator.serviceWorker.getRegistration(scope)
25         .then(function(registration) {
26             if (registration)
27               return registration.unregister();
28           })
29         .then(function() {
30             return navigator.serviceWorker.register(url, { scope: scope });
31           })
32         .catch(function(error) {
33             testFailed('Could not register worker: ' + error);
34             finishJSTest();
35           });
36 }
37
38 function onRegister(registration) {
39     registrationObservation = internals.observeGC(registration);
40     registration.addEventListener('updatefound', (function() {
41         onUpdate(registration.installing);
42     }));
43 }
44
45 function onUpdate(sw) {
46     swObservation = internals.observeGC(sw);
47     sw.addEventListener('statechange', onStateChange);
48 }
49
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;
59     }
60 }
61
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();
71         })
72       .catch(function(error) {
73           testFailed('Could not unregister worker: ' + error);
74           finishJSTest();
75         });
76 }
77
78 function finish()
79 {
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();
88 }
89 </script>