forked from mirrors/gecko-dev
After analyzing test_request.html it became clear that it relied on two false assumptions: 1. It assumed that a service worker is notified that it's being installed before any client can interact with it. 2. It assumed that a service worker will always be installed upon registration, if it was unregistered before. The first assumption is not backed by the spec; it seems that the opposite behavior is the correct one (https://github.com/w3c/ServiceWorker/issues/1347). The second assumption ignores the possibility of resurrection, where a service worker is re- registered before getting uninstalled. This commit addresses both problems by not relying on the installation phase, instead passing the script URL as a search parameter and loading it at service worker script evaluation time. It then runs the test function in response to a message from the client. Differential Revision: https://phabricator.services.mozilla.com/D3879 --HG-- extra : moz-landing-system : lando
71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
importScripts("utils.js");
|
|
|
|
function getScriptUrl() {
|
|
return new URL(location.href).searchParams.get('script');
|
|
}
|
|
|
|
importScripts(getScriptUrl());
|
|
|
|
var client;
|
|
var context;
|
|
|
|
function ok(a, msg) {
|
|
client.postMessage({type: 'status', status: !!a,
|
|
msg: a + ": " + msg, context: context});
|
|
}
|
|
|
|
function is(a, b, msg) {
|
|
client.postMessage({type: 'status', status: a === b,
|
|
msg: a + " === " + b + ": " + msg, context: context});
|
|
}
|
|
|
|
addEventListener('message', function workerWrapperOnMessage(e) {
|
|
removeEventListener('message', workerWrapperOnMessage);
|
|
var data = e.data;
|
|
|
|
function runTestAndReportToClient(event) {
|
|
var done = function(res) {
|
|
client.postMessage({ type: 'finish', context: context });
|
|
return res;
|
|
}
|
|
|
|
try {
|
|
// runTest() is provided by the test.
|
|
var result = runTest().then(done, done);
|
|
if ('waitUntil' in event) {
|
|
event.waitUntil(result);
|
|
}
|
|
} catch(e) {
|
|
client.postMessage({
|
|
type: 'status',
|
|
status: false,
|
|
msg: 'worker failed to run ' + data.script + "; error: " + e.message,
|
|
context: context
|
|
});
|
|
done();
|
|
}
|
|
}
|
|
|
|
if ("ServiceWorker" in self) {
|
|
// Fetch requests from a service worker are not intercepted.
|
|
self.isSWPresent = false;
|
|
|
|
e.waitUntil(self.clients.matchAll({ includeUncontrolled: true }).then(function(clients) {
|
|
for (var i = 0; i < clients.length; ++i) {
|
|
if (clients[i].url.indexOf("message_receiver.html") > -1) {
|
|
client = clients[i];
|
|
break;
|
|
}
|
|
}
|
|
if (!client) {
|
|
dump("We couldn't find the message_receiver window, the test will fail\n");
|
|
}
|
|
context = "ServiceWorker";
|
|
runTestAndReportToClient(e);
|
|
}));
|
|
} else {
|
|
client = self;
|
|
context = "Worker";
|
|
runTestAndReportToClient(e);
|
|
}
|
|
});
|