2013-11-20 03:15:02 +04:00
|
|
|
<!--
|
|
|
|
Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
-->
|
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Bug 930348 - test stub Navigator ServiceWorker utilities.</title>
|
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none"></div>
|
|
|
|
<pre id="test"></pre>
|
|
|
|
<script class="testbody" type="text/javascript">
|
|
|
|
|
|
|
|
function simpleRegister() {
|
2014-08-22 03:38:40 +04:00
|
|
|
var p = navigator.serviceWorker.register("worker.js", { scope: "simpleregister/" });
|
2013-11-20 03:15:02 +04:00
|
|
|
ok(p instanceof Promise, "register() should return a Promise");
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
function sameOriginWorker() {
|
|
|
|
p = navigator.serviceWorker.register("http://some-other-origin/worker.js");
|
|
|
|
return p.then(function(w) {
|
|
|
|
ok(false, "Worker from different origin should fail");
|
|
|
|
}, function(e) {
|
|
|
|
ok(e.name === "SecurityError", "Should fail with a SecurityError");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sameOriginScope() {
|
2014-08-22 03:38:40 +04:00
|
|
|
p = navigator.serviceWorker.register("worker.js", { scope: "http://www.example.com/" });
|
2013-11-20 03:15:02 +04:00
|
|
|
return p.then(function(w) {
|
|
|
|
ok(false, "Worker controlling scope for different origin should fail");
|
|
|
|
}, function(e) {
|
|
|
|
ok(e.name === "SecurityError", "Should fail with a SecurityError");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function httpsOnly() {
|
2016-11-18 11:33:33 +03:00
|
|
|
return SpecialPowers.pushPrefEnv({'set': [["dom.serviceWorkers.testing.enabled", false]] })
|
|
|
|
.then(function() {
|
2013-11-20 03:15:02 +04:00
|
|
|
return navigator.serviceWorker.register("/worker.js");
|
|
|
|
}).then(function(w) {
|
|
|
|
ok(false, "non-HTTPS pages cannot register ServiceWorkers");
|
|
|
|
}, function(e) {
|
2018-08-22 18:22:40 +03:00
|
|
|
ok(e.name === "TypeError", "navigator.serviceWorker should be undefined");
|
2014-06-11 20:12:56 +04:00
|
|
|
}).then(function() {
|
2016-11-18 11:33:50 +03:00
|
|
|
return SpecialPowers.popPrefEnv();
|
2013-11-20 03:15:02 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-11 20:12:56 +04:00
|
|
|
function realWorker() {
|
2014-08-22 03:38:40 +04:00
|
|
|
var p = navigator.serviceWorker.register("worker.js", { scope: "realworker" });
|
2014-08-19 17:56:00 +04:00
|
|
|
return p.then(function(wr) {
|
|
|
|
ok(wr instanceof ServiceWorkerRegistration, "Register a ServiceWorker");
|
|
|
|
|
|
|
|
info(wr.scope);
|
2014-08-22 03:38:40 +04:00
|
|
|
ok(wr.scope == (new URL("realworker", document.baseURI)).href, "Scope should match");
|
2014-08-25 09:35:03 +04:00
|
|
|
// active, waiting, installing should return valid worker instances
|
|
|
|
// because the registration is for the realworker scope, so the workers
|
|
|
|
// should be obtained for that scope and not for
|
|
|
|
// test_installation_simple.html
|
2014-12-19 13:00:29 +03:00
|
|
|
var worker = wr.installing;
|
|
|
|
ok(worker && wr.scope.match(/realworker$/) &&
|
|
|
|
worker.scriptURL.match(/worker.js$/), "Valid worker instance should be available.");
|
2015-04-18 19:32:38 +03:00
|
|
|
return wr.unregister().then(function(success) {
|
|
|
|
ok(success, "The worker should be unregistered successfully");
|
|
|
|
}, function(e) {
|
|
|
|
dump("Error unregistering the worker: " + e + "\n");
|
|
|
|
});
|
2014-06-11 20:12:56 +04:00
|
|
|
}, function(e) {
|
2014-07-03 04:48:35 +04:00
|
|
|
info("Error: " + e.name);
|
|
|
|
ok(false, "realWorker Registration should have succeeded!");
|
2014-06-11 20:12:56 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function networkError404() {
|
2014-12-19 13:48:31 +03:00
|
|
|
return navigator.serviceWorker.register("404.js", { scope: "network_error/"}).then(function(w) {
|
2015-02-10 00:47:09 +03:00
|
|
|
ok(false, "404 response should fail with TypeError");
|
2014-06-11 20:12:56 +04:00
|
|
|
}, function(e) {
|
2015-02-10 00:47:09 +03:00
|
|
|
ok(e.name === "TypeError", "404 response should fail with TypeError");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function redirectError() {
|
|
|
|
return navigator.serviceWorker.register("redirect_serviceworker.sjs", { scope: "redirect_error/" }).then(function(swr) {
|
2015-08-20 02:21:25 +03:00
|
|
|
ok(false, "redirection should fail");
|
2015-02-10 00:47:09 +03:00
|
|
|
}, function (e) {
|
2015-08-20 02:21:25 +03:00
|
|
|
ok(e.name === "SecurityError", "redirection should fail with SecurityError");
|
2014-06-11 20:12:56 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-03 04:48:35 +04:00
|
|
|
function parseError() {
|
2014-12-19 13:48:31 +03:00
|
|
|
var p = navigator.serviceWorker.register("parse_error_worker.js", { scope: "parse_error/" });
|
2014-08-19 17:56:00 +04:00
|
|
|
return p.then(function(wr) {
|
2014-07-03 04:48:35 +04:00
|
|
|
ok(false, "Registration should fail with parse error");
|
2014-11-06 01:43:51 +03:00
|
|
|
return navigator.serviceWorker.getRegistration("parse_error/").then(function(swr) {
|
|
|
|
// See https://github.com/slightlyoff/ServiceWorker/issues/547
|
|
|
|
is(swr, undefined, "A failed registration for a scope with no prior controllers should clear itself");
|
|
|
|
});
|
2014-07-03 04:48:35 +04:00
|
|
|
}, function(e) {
|
|
|
|
ok(e instanceof Error, "Registration should fail with parse error");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(nsm): test for parse error when Update step doesn't happen (directly from register).
|
|
|
|
|
2014-07-12 03:00:52 +04:00
|
|
|
function updatefound() {
|
2014-07-24 01:05:08 +04:00
|
|
|
var frame = document.createElement("iframe");
|
|
|
|
frame.setAttribute("id", "simpleregister-frame");
|
|
|
|
frame.setAttribute("src", new URL("simpleregister/index.html", document.baseURI).href);
|
|
|
|
document.body.appendChild(frame);
|
|
|
|
var resolve, reject;
|
|
|
|
var p = new Promise(function(res, rej) {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
});
|
2014-07-12 03:00:52 +04:00
|
|
|
|
2015-04-18 19:32:38 +03:00
|
|
|
var reg;
|
2014-07-24 01:05:08 +04:00
|
|
|
function continueTest() {
|
2015-04-18 19:32:38 +03:00
|
|
|
navigator.serviceWorker.register("worker2.js", { scope: "simpleregister/" })
|
|
|
|
.then(function(r) {
|
|
|
|
reg = r;
|
|
|
|
});;
|
2014-07-24 01:05:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.onmessage = function(e) {
|
2014-08-20 19:40:00 +04:00
|
|
|
if (e.data.type == "ready") {
|
2014-07-24 01:05:08 +04:00
|
|
|
continueTest();
|
2014-08-20 19:40:00 +04:00
|
|
|
} else if (e.data.type == "finish") {
|
2014-07-24 01:05:08 +04:00
|
|
|
window.onmessage = null;
|
|
|
|
// We have to make frame navigate away, otherwise it will call
|
|
|
|
// MaybeStopControlling() when this document is unloaded. At that point
|
2014-10-25 02:11:26 +04:00
|
|
|
// the pref has been disabled, so the ServiceWorkerManager is not available.
|
2014-07-24 01:05:08 +04:00
|
|
|
frame.setAttribute("src", new URL("about:blank").href);
|
2015-04-18 19:32:38 +03:00
|
|
|
reg.unregister().then(function(success) {
|
|
|
|
ok(success, "The worker should be unregistered successfully");
|
|
|
|
resolve();
|
|
|
|
}, function(e) {
|
|
|
|
dump("Error unregistering the worker: " + e + "\n");
|
|
|
|
});
|
2014-08-20 19:40:00 +04:00
|
|
|
} else if (e.data.type == "check") {
|
|
|
|
ok(e.data.status, e.data.msg);
|
2014-07-12 03:00:52 +04:00
|
|
|
}
|
2014-07-24 01:05:08 +04:00
|
|
|
}
|
|
|
|
return p;
|
2014-07-12 03:00:52 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 12:16:03 +04:00
|
|
|
var readyPromiseResolved = false;
|
|
|
|
|
|
|
|
function readyPromise() {
|
|
|
|
var frame = document.createElement("iframe");
|
|
|
|
frame.setAttribute("id", "simpleregister-frame-ready");
|
|
|
|
frame.setAttribute("src", new URL("simpleregister/ready.html", document.baseURI).href);
|
|
|
|
document.body.appendChild(frame);
|
|
|
|
|
|
|
|
var channel = new MessageChannel();
|
|
|
|
frame.addEventListener('load', function() {
|
|
|
|
frame.contentWindow.postMessage('your port!', '*', [channel.port2]);
|
2017-01-17 13:50:25 +03:00
|
|
|
});
|
2014-08-26 12:16:03 +04:00
|
|
|
|
|
|
|
channel.port1.onmessage = function() {
|
|
|
|
readyPromiseResolved = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkReadyPromise() {
|
|
|
|
ok(readyPromiseResolved, "The ready promise has been resolved!");
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2013-11-20 03:15:02 +04:00
|
|
|
function runTest() {
|
|
|
|
simpleRegister()
|
|
|
|
.then(sameOriginWorker)
|
|
|
|
.then(sameOriginScope)
|
|
|
|
.then(httpsOnly)
|
2018-08-22 18:22:40 +03:00
|
|
|
.then(readyPromise)
|
2014-06-11 20:12:56 +04:00
|
|
|
.then(realWorker)
|
2014-07-03 04:48:35 +04:00
|
|
|
.then(networkError404)
|
2015-02-10 00:47:09 +03:00
|
|
|
.then(redirectError)
|
2014-07-03 04:48:35 +04:00
|
|
|
.then(parseError)
|
2014-08-20 19:40:00 +04:00
|
|
|
.then(updatefound)
|
2014-08-26 12:16:03 +04:00
|
|
|
.then(checkReadyPromise)
|
2013-11-20 03:15:02 +04:00
|
|
|
// put more tests here.
|
|
|
|
.then(function() {
|
|
|
|
SimpleTest.finish();
|
|
|
|
}).catch(function(e) {
|
|
|
|
ok(false, "Some test failed with error " + e);
|
|
|
|
SimpleTest.finish();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
SpecialPowers.pushPrefEnv({"set": [
|
2015-03-04 01:30:58 +03:00
|
|
|
["dom.serviceWorkers.exemptFromPerDomainMax", true],
|
2013-11-20 03:15:02 +04:00
|
|
|
["dom.serviceWorkers.enabled", true],
|
2015-06-28 06:19:24 +03:00
|
|
|
["dom.serviceWorkers.testing.enabled", true],
|
|
|
|
["dom.caches.testing.enabled", true],
|
2013-11-20 03:15:02 +04:00
|
|
|
]}, runTest);
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|