test_serviceworkerregistrationinfo.xhtml (6741B)
1 <?xml version="1.0"?> 2 <!-- 3 Any copyright is dedicated to the Public Domain. 4 http://creativecommons.org/publicdomain/zero/1.0/ 5 --> 6 <window title="Test for ServiceWorkerRegistrationInfo" 7 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 8 onload="test();"> 9 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 10 <script type="application/javascript" src="chrome_helpers.js"/> 11 <script type="application/javascript"> 12 <![CDATA[ 13 14 let IFRAME_URL = EXAMPLE_URL + "serviceworkerregistrationinfo_iframe.html"; 15 16 function test() { 17 SimpleTest.waitForExplicitFinish(); 18 19 SpecialPowers.pushPrefEnv({'set': [ 20 ["dom.serviceWorkers.enabled", true], 21 ["dom.serviceWorkers.testing.enabled", true], 22 ]}, function () { 23 (async function() { 24 let iframe = $("iframe"); 25 let promise = waitForIframeLoad(iframe); 26 iframe.src = IFRAME_URL; 27 await promise; 28 29 // The change handler is not guaranteed to be called within the same 30 // tick of the event loop as the one in which the change happened. 31 // Because of this, the exact state of the service worker registration 32 // is only known until the handler returns. 33 // 34 // Because then-handlers are resolved asynchronously, the following 35 // checks are done using callbacks, which are called synchronously 36 // when then handler is called. These callbacks can return a promise, 37 // which is used to resolve the promise returned by the function. 38 39 info("Check that a service worker registration notifies its " + 40 "listeners when its state changes."); 41 promise = waitForRegister(EXAMPLE_URL, function (registration) { 42 is(registration.scriptSpec, ""); 43 ok(registration.installingWorker === null); 44 ok(registration.waitingWorker === null); 45 ok(registration.activeWorker === null); 46 47 return waitForServiceWorkerRegistrationChange(registration, function () { 48 // Got change event for updating (byte-check) 49 ok(registration.installingWorker === null); 50 ok(registration.waitingWorker === null); 51 ok(registration.activeWorker === null); 52 53 return waitForServiceWorkerRegistrationChange(registration, function () { 54 is(registration.scriptSpec, EXAMPLE_URL + "worker.js"); 55 ok(registration.evaluatingWorker !== null); 56 ok(registration.installingWorker === null); 57 ok(registration.waitingWorker === null); 58 ok(registration.activeWorker === null); 59 60 return waitForServiceWorkerRegistrationChange(registration, function () { 61 ok(registration.installingWorker !== null); 62 is(registration.installingWorker.scriptSpec, EXAMPLE_URL + "worker.js"); 63 ok(registration.waitingWorker === null); 64 ok(registration.activeWorker === null); 65 66 return waitForServiceWorkerRegistrationChange(registration, function () { 67 ok(registration.installingWorker === null); 68 ok(registration.waitingWorker !== null); 69 ok(registration.activeWorker === null); 70 71 return waitForServiceWorkerRegistrationChange(registration, function () { 72 // Activating 73 ok(registration.installingWorker === null); 74 ok(registration.waitingWorker === null); 75 ok(registration.activeWorker !== null); 76 77 return waitForServiceWorkerRegistrationChange(registration, function () { 78 // Activated 79 ok(registration.installingWorker === null); 80 ok(registration.waitingWorker === null); 81 ok(registration.activeWorker !== null); 82 83 return registration; 84 }); 85 }); 86 }); 87 }); 88 }); 89 }); 90 }); 91 iframe.contentWindow.postMessage("register", "*"); 92 let registration = await promise; 93 94 promise = waitForServiceWorkerRegistrationChange(registration, function () { 95 // Got change event for updating (byte-check) 96 ok(registration.installingWorker === null); 97 ok(registration.waitingWorker === null); 98 ok(registration.activeWorker !== null); 99 100 return waitForServiceWorkerRegistrationChange(registration, function () { 101 is(registration.scriptSpec, EXAMPLE_URL + "worker2.js"); 102 ok(registration.evaluatingWorker !== null); 103 104 return waitForServiceWorkerRegistrationChange(registration, function () { 105 ok(registration.installingWorker !== null); 106 is(registration.installingWorker.scriptSpec, EXAMPLE_URL + "worker2.js"); 107 ok(registration.waitingWorker === null); 108 ok(registration.activeWorker !== null); 109 110 return waitForServiceWorkerRegistrationChange(registration, function () { 111 ok(registration.installingWorker === null); 112 ok(registration.waitingWorker !== null); 113 ok(registration.activeWorker !== null); 114 115 return waitForServiceWorkerRegistrationChange(registration, function () { 116 // Activating 117 ok(registration.installingWorker === null); 118 ok(registration.waitingWorker === null); 119 ok(registration.activeWorker !== null); 120 121 return waitForServiceWorkerRegistrationChange(registration, function () { 122 // Activated 123 ok(registration.installingWorker === null); 124 ok(registration.waitingWorker === null); 125 ok(registration.activeWorker !== null); 126 127 return registration; 128 }); 129 }); 130 }); 131 }); 132 }); 133 }); 134 iframe.contentWindow.postMessage("register", "*"); 135 await promise; 136 137 iframe.contentWindow.postMessage("unregister", "*"); 138 await waitForUnregister(EXAMPLE_URL); 139 140 SimpleTest.finish(); 141 })(); 142 }); 143 } 144 145 ]]> 146 </script> 147 148 <body xmlns="http://www.w3.org/1999/xhtml"> 149 <p id="display"></p> 150 <div id="content" style="display:none;"></div> 151 <pre id="test"></pre> 152 <iframe id="iframe"></iframe> 153 </body> 154 <label id="test-result"/> 155 </window>