gecko-dev/dom/push/test/test_unregister.html

94 строки
2.7 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
Bug 1170817: Push tests.
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/licenses/publicdomain/
-->
<head>
<title>Test for Bug 1170817</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1170817">Mozilla Bug 1170817</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
<script class="testbody" type="text/javascript">
var registration;
function start() {
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: "."})
.then(swr => { registration = swr; return swr; });
}
function unregisterSW() {
return registration.unregister().then(function(result) {
ok(result, "Unregister should return true.");
}, function(e) {
dump("Unregistering the SW failed with " + e + "\n");
});
}
function setupPushNotification(swr) {
return swr.pushManager.subscribe().then(
pushSubscription => {
ok(true, "successful registered for push notification");
return pushSubscription;
}, error => {
ok(false, "could not register for push notification");
});
}
function unregisterPushNotification(pushSubscription) {
return pushSubscription.unsubscribe().then(
result => {
ok(result, "unsubscribe() on existing subscription should return true.");
return pushSubscription;
}, error => {
ok(false, "unsubscribe() should never fail.");
});
}
function unregisterAgain(pushSubscription) {
return pushSubscription.unsubscribe().then(
result => {
ok(!result, "unsubscribe() on previously unsubscribed subscription should return false.");
return pushSubscription;
}, error => {
ok(false, "unsubscribe() should never fail.");
});
}
function runTest() {
start()
.then(setupPushNotification)
.then(unregisterPushNotification)
.then(unregisterAgain)
.then(unregisterSW)
.catch(function(e) {
ok(false, "Some test failed with error " + e);
}).then(SimpleTest.finish);
}
SpecialPowers.pushPrefEnv({"set": [
["dom.push.enabled", true],
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, runTest);
SpecialPowers.addPermission("desktop-notification", true, document);
SimpleTest.waitForExplicitFinish();
</script>
</body>
</html>