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