Upstream version 8.37.180.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 <script src="resources/test-helpers.js"></script>
5 <body>
6 <script>
7 (function () {
8     var t = async_test('Service Worker state property and "statechange" event');
9     var currentState = 'test-is-starting';
10     var scope = '/state/*';
11
12     service_worker_unregister_and_register(
13         t, 'resources/worker-no-op.js', scope, onRegister);
14
15     function onRegister(sw) {
16         sw.addEventListener('statechange', t.step_func(onStateChange(sw)));
17         assert_in_array(sw.state, ['parsed', 'installing'],
18                         'the service worker should be in a state up to ' +
19                         '"installing".');
20         checkStateTransition(sw.state);
21     }
22
23     function checkStateTransition(newState) {
24         switch (currentState) {
25         case 'test-is-starting':
26             break; // anything goes
27         case 'parsed':
28             assert_equals(newState, 'installing');
29             break;
30         case 'installing':
31             assert_in_array(newState, ['installed', 'deactivated']);
32             break;
33         case 'installed':
34             assert_in_array(newState, ['activating', 'deactivated']);
35             break;
36         case 'activating':
37             assert_in_array(newState, ['active', 'deactivated']);
38             break;
39         case 'active':
40             assert_equals(newState, 'deactivated');
41             break;
42         case 'deactivated':
43             assert_unreached('a ServiceWorker should not transition out of ' +
44                              'the "deactivated" state');
45             break;
46         default:
47             assert_unreached('should not transition into unknown state "' +
48                              newState + '"');
49             break;
50         }
51         currentState = newState;
52     }
53
54     function onStateChange(expectedTarget) {
55         return function(event) {
56             assert_true(event.target instanceof ServiceWorker,
57                         'the target of the statechange event should be a ' +
58                         'ServiceWorker.');
59             assert_equals(event.target, expectedTarget,
60                           'the target of the statechange event should be ' +
61                           'the installing ServiceWorker');
62             assert_equals(event.type, 'statechange',
63                           'the type of the event should be "statechange".');
64
65             checkStateTransition(event.target.state);
66
67             if (event.target.state == 'active')
68                 service_worker_unregister_and_done(t, scope);
69         };
70     }
71 }());
72 </script>