Skip to content

Commit

Permalink
Bug 931249 - patch 3 - tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmottola committed Jul 1, 2019
1 parent af8ae34 commit eb9769c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dom/workers/test/serviceworkers/importscript.sjs
@@ -0,0 +1,9 @@
var counter = 0;
function handleRequest(request, response) {
if (!counter) {
response.setHeader("Content-Type", "application/javascript", false);
response.write("callByScript();");
} else {
response.write("no cache no party!");
}
}
23 changes: 23 additions & 0 deletions dom/workers/test/serviceworkers/importscript_worker.js
@@ -0,0 +1,23 @@
var counter = 0;
function callByScript() {
++counter;
}

importScripts(['importscript.sjs']);
importScripts(['importscript.sjs']);

onmessage = function(e) {
self.clients.matchAll().then(function(res) {
if (!res.length) {
dump("ERROR: no clients are currently controlled.\n");
}

try {
importScript(['importscript.sjs']);
res[0].postMessage("KO");
return;
} catch(e) {}

res[0].postMessage(counter == 2 ? "OK" : "KO");
});
};
3 changes: 3 additions & 0 deletions dom/workers/test/serviceworkers/mochitest.ini
Expand Up @@ -55,6 +55,8 @@ support-files =
source_message_posting_worker.js
scope/scope_worker.js
redirect_serviceworker.sjs
importscript.sjs
importscript_worker.js

[test_unregister.html]
skip-if = true # Bug 1133805
Expand Down Expand Up @@ -82,3 +84,4 @@ skip-if = true # Bug 1133805
[test_match_all_client_id.html]
[test_sandbox_intercept.html]
[test_request_context.html]
[test_importscript.html]
67 changes: 67 additions & 0 deletions dom/workers/test/serviceworkers/test_importscript.html
@@ -0,0 +1,67 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Test service worker - script cache policy</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<div id="content"></div>
<script class="testbody" type="text/javascript">
function start() {
return navigator.serviceWorker.register("importscript_worker.js",
{ scope: "./sw_clients/" })
.then((swr) => registration = swr);
}

function unregister() {
return registration.unregister().then(function(result) {
ok(result, "Unregister should return true.");
});
}

function testPostMessage(swr) {
var p = new Promise(function(res, rej) {
window.onmessage = function(e) {
if (e.data === "READY") {
swr.active.postMessage("do magic");
return;
}

ok(e.data === "OK", "Worker posted the correct value: " + e.data);
res();
}
});

var content = document.getElementById("content");
ok(content, "Parent exists.");

iframe = document.createElement("iframe");
iframe.setAttribute('src', "sw_clients/service_worker_controlled.html");
content.appendChild(iframe);

return p.then(() => content.removeChild(iframe));
}

function runTest() {
start()
.then(testPostMessage)
.then(unregister)
.catch(function(e) {
ok(false, "Some test failed with error " + e);
}).then(SimpleTest.finish);
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, runTest);
</script>
</pre>
</body>
</html>

0 comments on commit eb9769c

Please sign in to comment.