2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <script src="resources/test-helpers.js"></script>
8 var t = async_test('Service Worker state property and "statechange" event');
9 var currentState = 'test-is-starting';
10 var scope = 'resources/state/';
12 service_worker_unregister_and_register(
13 t, 'resources/empty-worker.js', scope)
14 .then(t.step_func(function(registration) {
15 return wait_for_update(t, registration);
17 .then(t.step_func(function(sw) {
18 sw.addEventListener('statechange', t.step_func(onStateChange(sw)));
19 assert_equals(sw.state, 'installing',
20 'the service worker should be in "installing" state.');
21 checkStateTransition(sw.state);
23 .catch(unreached_rejection(t));
25 function checkStateTransition(newState) {
26 switch (currentState) {
27 case 'test-is-starting':
28 break; // anything goes
30 assert_in_array(newState, ['installed', 'redundant']);
33 assert_in_array(newState, ['activating', 'redundant']);
36 assert_in_array(newState, ['activated', 'redundant']);
39 assert_equals(newState, 'redundant');
42 assert_unreached('a ServiceWorker should not transition out of ' +
43 'the "redundant" state');
46 assert_unreached('should not transition into unknown state "' +
50 currentState = newState;
53 function onStateChange(expectedTarget) {
54 return function(event) {
55 assert_true(event.target instanceof ServiceWorker,
56 'the target of the statechange event should be a ' +
58 assert_equals(event.target, expectedTarget,
59 'the target of the statechange event should be ' +
60 'the installing ServiceWorker');
61 assert_equals(event.type, 'statechange',
62 'the type of the event should be "statechange".');
64 checkStateTransition(event.target.state);
66 if (event.target.state == 'activated')
67 service_worker_unregister_and_done(t, scope);