Bug 1164304 - Run all fetch tests in the service worker context as well; r=nsm

This brings this mini test suite on par with the one that we
have been using for DOM Cache.
This commit is contained in:
Ehsan Akhgari 2015-05-12 20:46:44 -04:00
Родитель b451be5e0e
Коммит 56613b0aac
4 изменённых файлов: 114 добавлений и 19 удалений

Просмотреть файл

@ -1,12 +1,25 @@
function testScript(script) {
function setupPrefs() {
return new Promise(function(resolve, reject) {
SpecialPowers.pushPrefEnv({
"set": [["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true],
["dom.serviceWorkers.exemptFromPerDomainMax", true]]
}, resolve);
});
}
function workerTest() {
return new Promise(function(resolve, reject) {
var worker = new Worker("worker_wrapper.js");
worker.onmessage = function(event) {
if (event.data.context != "Worker") {
return;
}
if (event.data.type == 'finish') {
resolve();
} else if (event.data.type == 'status') {
ok(event.data.status, "Worker fetch test: " + event.data.msg);
ok(event.data.status, event.data.context + ": " + event.data.msg);
}
}
worker.onerror = function(event) {
@ -17,6 +30,48 @@ function testScript(script) {
});
}
function serviceWorkerTest() {
var isB2G = !navigator.userAgent.includes("Android") &&
/Mobile|Tablet/.test(navigator.userAgent);
if (isB2G) {
// TODO B2G doesn't support running service workers for now due to bug 1137683.
dump("Skipping running the test in SW until bug 1137683 gets fixed.\n");
return Promise.resolve();
}
return new Promise(function(resolve, reject) {
function setupSW(registration) {
var worker = registration.waiting ||
registration.active;
window.addEventListener("message",function onMessage(event) {
if (event.data.context != "ServiceWorker") {
return;
}
if (event.data.type == 'finish') {
window.removeEventListener("message", onMessage);
registration.unregister()
.then(resolve)
.catch(reject);
} else if (event.data.type == 'status') {
ok(event.data.status, event.data.context + ": " + event.data.msg);
}
}, false);
worker.onerror = reject;
var iframe = document.createElement("iframe");
iframe.src = "message_receiver.html";
iframe.onload = function() {
worker.postMessage({ script: script });
};
document.body.appendChild(iframe);
}
navigator.serviceWorker.ready.then(setupSW);
navigator.serviceWorker.register("worker_wrapper.js", {scope: "."});
});
}
function windowTest() {
return new Promise(function(resolve, reject) {
var scriptEl = document.createElement("script");
@ -29,13 +84,19 @@ function testScript(script) {
}
SimpleTest.waitForExplicitFinish();
// We have to run the window and worker tests sequentially since some tests
// set and compare cookies and running in parallel can lead to conflicting
// values.
windowTest()
// We have to run the window, worker and service worker tests sequentially
// since some tests set and compare cookies and running in parallel can lead
// to conflicting values.
setupPrefs()
.then(function() {
return windowTest();
})
.then(function() {
return workerTest();
})
.then(function() {
return serviceWorkerTest();
})
.catch(function(e) {
ok(false, "Some test failed in " + script);
info(e);

Просмотреть файл

@ -0,0 +1,6 @@
<!DOCTYPE html>
<script>
navigator.serviceWorker.onmessage = function(e) {
window.parent.postMessage(e.data, "*");
};
</script>

Просмотреть файл

@ -11,6 +11,7 @@ support-files =
test_response.js
utils.js
worker_wrapper.js
message_receiver.html
[test_headers.html]
[test_headers_mainthread.html]

Просмотреть файл

@ -1,31 +1,58 @@
importScripts("utils.js");
var client;
var context;
function ok(a, msg) {
postMessage({type: 'status', status: !!a, msg: a + ": " + msg });
client.postMessage({type: 'status', status: !!a,
msg: a + ": " + msg, context: context});
}
function is(a, b, msg) {
postMessage({type: 'status', status: a === b, msg: 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;
var done = function() {
postMessage({ type: 'finish' });
function loadTest() {
var done = function() {
client.postMessage({ type: 'finish', context: context });
}
try {
importScripts(data.script);
// runTest() is provided by the test.
runTest().then(done, done);
} catch(e) {
client.postMessage({
type: 'status',
status: false,
msg: 'worker failed to import ' + data.script + "; error: " + e.message,
context: context
});
done();
}
}
try {
importScripts(data.script);
// runTest() is provided by the test.
runTest().then(done, done);
} catch(e) {
postMessage({
type: 'status',
status: false,
msg: 'worker failed to import ' + data.script + "; error: " + e.message
if ("ServiceWorker" in self) {
self.clients.matchAll().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";
loadTest();
});
done();
} else {
client = self;
context = "Worker";
loadTest();
}
});