<!-- Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPEHTML>
<html>
<head>
<title>Bug 930348 - test stub Navigator ServiceWorker utilities.</title>
<script 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() { var p = navigator.serviceWorker.register("worker.js", { scope: "simpleregister/" });
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() {
p = navigator.serviceWorker.register("worker.js", { scope: "http://www.example.com/" });
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 realWorker() { var p = navigator.serviceWorker.register("worker.js", { scope: "realworker" });
return p.then(function(wr) {
ok(wr instanceof ServiceWorkerRegistration, "Register a ServiceWorker");
info(wr.scope);
ok(wr.scope == (new URL("realworker", document.baseURI)).href, "Scope should match");
// 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 var worker = wr.installing;
ok(worker && wr.scope.match(/realworker$/) &&
worker.scriptURL.match(/worker.js$/), "Valid worker instance should be available.");
return wr.unregister().then(function(success) {
ok(success, "The worker should be unregistered successfully");
}, function(e) {
dump("Error unregistering the worker: " + e + "\n");
});
}, function(e) {
info("Error: " + e.name);
ok(false, "realWorker Registration should have succeeded!");
});
}
function networkError404() {
return navigator.serviceWorker.register("404.js", { scope: "network_error/"}).then(function(w) {
ok(false, "404 response should fail with TypeError");
}, function(e) {
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) {
ok(false, "redirection should fail");
}, function (e) {
ok(e.name === "SecurityError", "redirection should fail with SecurityError");
});
}
function parseError() { var p = navigator.serviceWorker.register("parse_error_worker.js", { scope: "parse_error/" });
return p.then(function(wr) {
ok(false, "Registration should fail with parse error");
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");
});
}, 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).
function updatefound() { varframe = 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;
});
var regPromise;
function continueTest() {
regPromise = navigator.serviceWorker.register( "worker2.js", { scope: "simpleregister/" });
}
window.onmessage = function(e) {
if (e.data.type == "ready") {
continueTest();
} else if (e.data.type == "finish") {
window.onmessage = null;
// We have to make frame navigate away, otherwise it will call
// MaybeStopControlling() when this document is unloaded. At that point
// the pref has been disabled, so the ServiceWorkerManager is not available. frame.setAttribute("src", new URL("about:blank").href);
regPromise.then(function(reg) {
reg.unregister().then(function(success) {
ok(success, "The worker should be unregistered successfully");
resolve();
}, function(error) {
dump("Error unregistering the worker: " + error + "\n");
});
});
} else if (e.data.type == "check") {
ok(e.data.status, e.data.msg);
}
}
return p;
}
var readyPromiseResolved = false;
function readyPromise() { varframe = 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]);
});
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.