| OLD | NEW |
| (Empty) |
| 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 = 'resources/state/'; | |
| 11 | |
| 12 service_worker_unregister_and_register( | |
| 13 t, 'resources/empty-worker.js', scope) | |
| 14 .then(t.step_func(function(registration) { | |
| 15 var sw = registration.installing; | |
| 16 sw.addEventListener('statechange', t.step_func(onStateChange(sw))); | |
| 17 assert_equals(sw.state, 'installing', | |
| 18 'the service worker should be in "installing" state.'); | |
| 19 checkStateTransition(sw.state); | |
| 20 })) | |
| 21 .catch(unreached_rejection(t)); | |
| 22 | |
| 23 function checkStateTransition(newState) { | |
| 24 switch (currentState) { | |
| 25 case 'test-is-starting': | |
| 26 break; // anything goes | |
| 27 case 'installing': | |
| 28 assert_in_array(newState, ['installed', 'redundant']); | |
| 29 break; | |
| 30 case 'installed': | |
| 31 assert_in_array(newState, ['activating', 'redundant']); | |
| 32 break; | |
| 33 case 'activating': | |
| 34 assert_in_array(newState, ['activated', 'redundant']); | |
| 35 break; | |
| 36 case 'activated': | |
| 37 assert_equals(newState, 'redundant'); | |
| 38 break; | |
| 39 case 'redundant': | |
| 40 assert_unreached('a ServiceWorker should not transition out of ' + | |
| 41 'the "redundant" state'); | |
| 42 break; | |
| 43 default: | |
| 44 assert_unreached('should not transition into unknown state "' + | |
| 45 newState + '"'); | |
| 46 break; | |
| 47 } | |
| 48 currentState = newState; | |
| 49 } | |
| 50 | |
| 51 function onStateChange(expectedTarget) { | |
| 52 return function(event) { | |
| 53 assert_true(event.target instanceof ServiceWorker, | |
| 54 'the target of the statechange event should be a ' + | |
| 55 'ServiceWorker.'); | |
| 56 assert_equals(event.target, expectedTarget, | |
| 57 'the target of the statechange event should be ' + | |
| 58 'the installing ServiceWorker'); | |
| 59 assert_equals(event.type, 'statechange', | |
| 60 'the type of the event should be "statechange".'); | |
| 61 | |
| 62 checkStateTransition(event.target.state); | |
| 63 | |
| 64 if (event.target.state == 'activated') | |
| 65 service_worker_unregister_and_done(t, scope); | |
| 66 }; | |
| 67 } | |
| 68 }()); | |
| 69 </script> | |
| OLD | NEW |