2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
7 var t = async_test('Service Worker state property and "statechange" event');
8 var currentState = 'test-is-starting';
10 navigator.serviceWorker.unregister('/state/*').then(register, register);
13 navigator.serviceWorker.register(
14 'resources/state-worker.js', {scope: '/state/*'}
15 ).then(t.step_func(onRegister), t.step_func(onRegisterFail));
18 function onRegisterFail(reason) {
19 assert_unreached('registration should not fail.');
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 ' +
28 checkStateTransition(sw.state);
31 function checkStateTransition(newState) {
32 switch (currentState) {
33 case 'test-is-starting':
34 break; // anything goes
36 assert_equals(newState, 'installing');
39 assert_in_array(newState, ['installed', 'deactivated']);
42 assert_in_array(newState, ['activating', 'deactivated']);
45 assert_in_array(newState, ['active', 'deactivated']);
48 assert_equals(newState, 'deactivated');
51 assert_unreached('a ServiceWorker should not transition out of ' +
52 'the "deactivated" state');
55 assert_unreached('should not transition into unknown state "' +
59 currentState = newState;
62 function onStateChange(event) {
63 assert_true(event.target instanceof ServiceWorker,
64 'the target of the statechange event should be a ' +
66 assert_equals(event.type, 'statechange',
67 'the type of the event should be "statechange".');
69 checkStateTransition(event.target.state);
71 if (event.target.state == 'active') {
72 navigator.serviceWorker.unregister('/state/*').then(done, done);