Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / state.html
1 <!DOCTYPE html>
2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <body>
5 <script>
6 (function () {
7     var t = async_test('Service Worker state property and "statechange" event');
8     var currentState = 'test-is-starting';
9
10     navigator.serviceWorker.unregister('/state/*').then(register, register);
11
12     function register() {
13         navigator.serviceWorker.register(
14             'resources/state-worker.js', {scope: '/state/*'}
15         ).then(t.step_func(onRegister), t.step_func(onRegisterFail));
16     }
17
18     function onRegisterFail(reason) {
19         assert_unreached('registration should not fail.');
20         t.done();
21     }
22
23     function onRegister(sw) {
24         sw.addEventListener('statechange', t.step_func(onStateChange));
25         assert_in_array(sw.state, ['parsed', 'installing'],
26                         'the service worker should be in a state up to ' +
27                         '"installing".');
28         checkStateTransition(sw.state);
29     }
30
31     function checkStateTransition(newState) {
32         switch (currentState) {
33         case 'test-is-starting':
34             break; // anything goes
35         case 'parsed':
36             assert_equals(newState, 'installing');
37             break;
38         case 'installing':
39             assert_in_array(newState, ['installed', 'deactivated']);
40             break;
41         case 'installed':
42             assert_in_array(newState, ['activating', 'deactivated']);
43             break;
44         case 'activating':
45             assert_in_array(newState, ['active', 'deactivated']);
46             break;
47         case 'active':
48             assert_equals(newState, 'deactivated');
49             break;
50         case 'deactivated':
51             assert_unreached('a ServiceWorker should not transition out of ' +
52                              'the "deactivated" state');
53             break;
54         default:
55             assert_unreached('should not transition into unknown state "' +
56                              newState + '"');
57             break;
58         }
59         currentState = newState;
60     }
61
62     function onStateChange(event) {
63         assert_true(event.target instanceof ServiceWorker,
64                     'the target of the statechange event should be a ' +
65                     'ServiceWorker.');
66         assert_equals(event.type, 'statechange',
67                       'the type of the event should be "statechange".');
68
69         checkStateTransition(event.target.state);
70
71         if (event.target.state == 'active') {
72             navigator.serviceWorker.unregister('/state/*').then(done, done);
73         }
74     }
75
76     function done() {
77         t.done();
78     }
79 }());
80 </script>