state.https.html (2731B)
1 <!DOCTYPE html> 2 <script src="/resources/testharness.js"></script> 3 <script src="/resources/testharnessreport.js"></script> 4 <script src="resources/test-helpers.sub.js"></script> 5 <body> 6 <script> 7 promise_test(function (t) { 8 var currentState = 'test-is-starting'; 9 var scope = 'resources/state/'; 10 11 return service_worker_unregister_and_register( 12 t, 'resources/empty-worker.js', scope) 13 .then(function(registration) { 14 t.add_cleanup(function() { 15 return service_worker_unregister(t, scope); 16 }); 17 18 var sw = registration.installing; 19 20 assert_equals(sw.state, 'installing', 21 'the service worker should be in "installing" state.'); 22 checkStateTransition(sw.state); 23 return onStateChange(sw); 24 }); 25 26 function checkStateTransition(newState) { 27 switch (currentState) { 28 case 'test-is-starting': 29 break; // anything goes 30 case 'installing': 31 assert_in_array(newState, ['installed', 'redundant']); 32 break; 33 case 'installed': 34 assert_in_array(newState, ['activating', 'redundant']); 35 break; 36 case 'activating': 37 assert_in_array(newState, ['activated', 'redundant']); 38 break; 39 case 'activated': 40 assert_equals(newState, 'redundant'); 41 break; 42 case 'redundant': 43 assert_unreached('a ServiceWorker should not transition out of ' + 44 'the "redundant" 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 new Promise(function(resolve) { 56 expectedTarget.addEventListener('statechange', resolve); 57 }).then(function(event) { 58 assert_true(event.target instanceof ServiceWorker, 59 'the target of the statechange event should be a ' + 60 'ServiceWorker.'); 61 assert_equals(event.target, expectedTarget, 62 'the target of the statechange event should be ' + 63 'the installing ServiceWorker'); 64 assert_equals(event.type, 'statechange', 65 'the type of the event should be "statechange".'); 66 67 checkStateTransition(event.target.state); 68 69 if (event.target.state != 'activated') 70 return onStateChange(expectedTarget); 71 }); 72 } 73 }, 'Service Worker state property and "statechange" event'); 74 </script>