1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
<html>
<head>
<title>Bug 1251238 - track service worker install time</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
</head>
<iframe id="iframe"></iframe>
<body>
<script type="text/javascript">
const State = {
BYTECHECK: -1,
PARSED: Ci.nsIServiceWorkerInfo.STATE_PARSED,
INSTALLING: Ci.nsIServiceWorkerInfo.STATE_INSTALLING,
INSTALLED: Ci.nsIServiceWorkerInfo.STATE_INSTALLED,
ACTIVATING: Ci.nsIServiceWorkerInfo.STATE_ACTIVATING,
ACTIVATED: Ci.nsIServiceWorkerInfo.STATE_ACTIVATED,
REDUNDANT: Ci.nsIServiceWorkerInfo.STATE_REDUNDANT
};
let swm = Cc["@mozilla.org/serviceworkers/manager;1"].
getService(Ci.nsIServiceWorkerManager);
let EXAMPLE_URL = "https://example.com/chrome/dom/serviceworkers/test/";
let swrlistener = null;
let registrationInfo = null;
// Use it to keep the sw after unregistration.
let astrayServiceWorkerInfo = null;
let expectedResults = [
{
// Speacial state for verifying update since we will do the byte-check
// first.
state: State.BYTECHECK, installedTimeRecorded: false,
activatedTimeRecorded: false, redundantTimeRecorded: false
},
{
state: State.PARSED, installedTimeRecorded: false,
activatedTimeRecorded: false, redundantTimeRecorded: false
},
{
state: State.INSTALLING, installedTimeRecorded: false,
activatedTimeRecorded: false, redundantTimeRecorded: false
},
{
state: State.INSTALLED, installedTimeRecorded: true,
activatedTimeRecorded: false, redundantTimeRecorded: false
},
{
state: State.ACTIVATING, installedTimeRecorded: true,
activatedTimeRecorded: false, redundantTimeRecorded: false
},
{
state: State.ACTIVATED, installedTimeRecorded: true,
activatedTimeRecorded: true, redundantTimeRecorded: false
},
// When first being marked as unregistered (but the worker can remain
// actively controlling pages)
{
state: State.ACTIVATED, installedTimeRecorded: true,
activatedTimeRecorded: true, redundantTimeRecorded: false
},
// When cleared (when idle)
{
state: State.REDUNDANT, installedTimeRecorded: true,
activatedTimeRecorded: true, redundantTimeRecorded: true
},
];
function waitForRegister(aScope, aCallback) {
return new Promise(function (aResolve) {
let listener = {
onRegister (aRegistration) {
if (aRegistration.scope !== aScope) {
return;
}
swm.removeListener(listener);
registrationInfo = aRegistration;
aResolve();
}
};
swm.addListener(listener);
});
}
function waitForUnregister(aScope) {
return new Promise(function (aResolve) {
let listener = {
onUnregister (aRegistration) {
if (aRegistration.scope !== aScope) {
return;
}
swm.removeListener(listener);
aResolve();
}
};
swm.addListener(listener);
});
}
function register() {
info("Register a ServiceWorker in the iframe");
let iframe = document.querySelector("iframe");
iframe.src = EXAMPLE_URL + "serviceworkerinfo_iframe.html";
let promise = new Promise(function(aResolve) {
iframe.onload = aResolve;
});
return promise.then(function() {
iframe.contentWindow.postMessage("register", "*");
return waitForRegister(EXAMPLE_URL);
})
}
function verifyServiceWorkTime(aSWRInfo, resolve) {
let expectedResult = expectedResults.shift();
ok(!!expectedResult, "We should be able to get test from expectedResults");
info("Check the ServiceWorker time in its state is " + expectedResult.state);
// Get serviceWorkerInfo from swrInfo or get the astray one which we hold.
let swInfo = aSWRInfo.evaluatingWorker ||
aSWRInfo.installingWorker ||
aSWRInfo.waitingWorker ||
aSWRInfo.activeWorker ||
astrayServiceWorkerInfo;
ok(!!aSWRInfo.lastUpdateTime,
"We should do the byte-check and update the update timeStamp");
if (!swInfo) {
is(expectedResult.state, State.BYTECHECK,
"We shouldn't get sw when we are notified for first time updating");
return;
}
ok(!!swInfo);
is(expectedResult.state, swInfo.state,
"The service worker's state should be " + swInfo.state + ", but got " +
expectedResult.state);
is(expectedResult.installedTimeRecorded, !!swInfo.installedTime,
"InstalledTime should be recorded when their state is greater than " +
"INSTALLING");
is(expectedResult.activatedTimeRecorded, !!swInfo.activatedTime,
"ActivatedTime should be recorded when their state is greater than " +
"ACTIVATING");
is(expectedResult.redundantTimeRecorded, !!swInfo.redundantTime,
"RedundantTime should be recorded when their state is REDUNDANT");
// We need to hold sw to avoid losing it since we'll unregister the swr later.
if (expectedResult.state === State.ACTIVATED) {
astrayServiceWorkerInfo = aSWRInfo.activeWorker;
// Resolve the promise for testServiceWorkerInfo after sw is activated.
resolve();
}
}
function testServiceWorkerInfo() {
info("Listen onChange event and verify service worker's information");
let promise_resolve;
let promise = new Promise(aResolve => promise_resolve = aResolve);
swrlistener = {
onChange: () => {
verifyServiceWorkTime(registrationInfo, promise_resolve);
}
};
registrationInfo.addListener(swrlistener);
return promise;
}
async function testHttpCacheUpdateTime() {
let iframe = document.querySelector("iframe");
let reg = await iframe.contentWindow.navigator.serviceWorker.getRegistration();
let lastUpdateTime = registrationInfo.lastUpdateTime;
await reg.update();
is(lastUpdateTime, registrationInfo.lastUpdateTime,
"The update time should not change when SW script is read from http cache.");
}
function unregister() {
info("Unregister the ServiceWorker");
let iframe = document.querySelector("iframe");
iframe.contentWindow.postMessage("unregister", "*");
return waitForUnregister(EXAMPLE_URL);
}
function cleanAll() {
return new Promise((aResolve, aReject) => {
is(expectedResults.length, 0, "All the tests should be tested");
registrationInfo.removeListener(swrlistener);
swm = null;
swrlistener = null;
registrationInfo = null;
astrayServiceWorkerInfo = null;
aResolve();
})
}
function runTest() {
return Promise.resolve()
.then(register)
.then(testServiceWorkerInfo)
.then(testHttpCacheUpdateTime)
.then(unregister)
.catch(aError => ok(false, "Some test failed with error " + aError))
.then(cleanAll)
.then(SimpleTest.finish);
}
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, runTest);
</script>
</body>
</html>
|