2015-04-11 03:19:28 +00:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<!--
|
|
|
|
Bug 1038811: Push tests.
|
|
|
|
|
|
|
|
Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/licenses/publicdomain/
|
|
|
|
|
|
|
|
-->
|
|
|
|
<head>
|
|
|
|
<title>Test for Bug 1038811</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=1038811">Mozilla Bug 1038811</a>
|
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<pre id="test">
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<script class="testbody" type="text/javascript">
|
|
|
|
|
|
|
|
function debug(str) {
|
|
|
|
// console.log(str + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
var controlledFrame;
|
|
|
|
function createControlledIFrame(swr) {
|
|
|
|
var p = new Promise(function(res, rej) {
|
|
|
|
var iframe = document.createElement('iframe');
|
|
|
|
iframe.id = "controlledFrame";
|
2015-09-19 01:15:23 +00:00
|
|
|
iframe.src = "http://mochi.test:8888/tests/dom/push/test/frame.html";
|
2015-04-11 03:19:28 +00:00
|
|
|
|
2015-09-19 01:15:23 +00:00
|
|
|
iframe.onload = function() {
|
|
|
|
res(swr)
|
|
|
|
}
|
2015-04-11 03:19:28 +00:00
|
|
|
controlledFrame = iframe;
|
|
|
|
document.body.appendChild(iframe);
|
|
|
|
});
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:27:32 +00:00
|
|
|
function checkPermissionState(swr) {
|
|
|
|
return swr.pushManager.permissionState().then(function(state) {
|
|
|
|
ok(state === "granted", "permissionState() should resolve to granted.");
|
|
|
|
return swr;
|
|
|
|
}).catch(function(e) {
|
|
|
|
ok(false, "permissionState() should resolve to granted.");
|
|
|
|
return swr;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-11 03:19:28 +00:00
|
|
|
function sendPushToPushServer(pushEndpoint) {
|
|
|
|
// Work around CORS for now.
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', "http://mochi.test:8888/tests/dom/push/test/push-server.sjs", true);
|
2015-09-17 12:10:42 +00:00
|
|
|
xhr.setRequestHeader("X-Push-Method", "PUT");
|
2015-04-11 03:19:28 +00:00
|
|
|
xhr.setRequestHeader("X-Push-Server", pushEndpoint);
|
|
|
|
xhr.onload = function(e) {
|
|
|
|
debug("xhr : " + this.status);
|
|
|
|
}
|
|
|
|
xhr.onerror = function(e) {
|
|
|
|
debug("xhr error: " + e);
|
|
|
|
}
|
|
|
|
xhr.send("version=24601");
|
|
|
|
}
|
|
|
|
|
|
|
|
var registration;
|
|
|
|
|
|
|
|
function start() {
|
|
|
|
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: "."})
|
|
|
|
.then((swr) => registration = swr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function unregister() {
|
|
|
|
return registration.unregister().then(function(result) {
|
|
|
|
ok(result, "Unregister should return true.");
|
2015-04-18 17:08:41 +00:00
|
|
|
}, function(e) {
|
|
|
|
dump("Unregistering the SW failed with " + e + "\n");
|
2015-04-11 03:19:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupPushNotification(swr) {
|
|
|
|
var p = new Promise(function(res, rej) {
|
|
|
|
swr.pushManager.subscribe().then(
|
|
|
|
function(pushSubscription) {
|
|
|
|
ok(true, "successful registered for push notification");
|
|
|
|
res(pushSubscription);
|
|
|
|
}, function(error) {
|
|
|
|
ok(false, "could not register for push notification");
|
|
|
|
res(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unregisterPushNotification(pushSubscription) {
|
|
|
|
controlledFrame.parentNode.removeChild(controlledFrame);
|
|
|
|
controlledFrame = null;
|
|
|
|
return pushSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForPushNotification(pushSubscription) {
|
2015-09-19 01:15:23 +00:00
|
|
|
var p = controlledFrame.contentWindow.waitOnPushMessage();
|
2015-04-11 03:19:28 +00:00
|
|
|
sendPushToPushServer(pushSubscription.endpoint);
|
|
|
|
return p.then(function() {
|
|
|
|
return pushSubscription;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function runTest() {
|
|
|
|
start()
|
|
|
|
.then(createControlledIFrame)
|
2015-07-14 21:27:32 +00:00
|
|
|
.then(checkPermissionState)
|
2015-04-11 03:19:28 +00:00
|
|
|
.then(setupPushNotification)
|
|
|
|
.then(waitForPushNotification)
|
|
|
|
.then(unregisterPushNotification)
|
|
|
|
.then(unregister)
|
|
|
|
.catch(function(e) {
|
|
|
|
ok(false, "Some test failed with error " + e);
|
|
|
|
}).then(SimpleTest.finish);
|
|
|
|
}
|
|
|
|
|
|
|
|
SpecialPowers.pushPrefEnv({"set": [
|
2015-04-13 17:03:24 +00:00
|
|
|
["dom.push.enabled", true],
|
2015-04-11 03:19:28 +00:00
|
|
|
["dom.serviceWorkers.exemptFromPerDomainMax", true],
|
|
|
|
["dom.serviceWorkers.enabled", true],
|
|
|
|
["dom.serviceWorkers.testing.enabled", true]
|
|
|
|
]}, runTest);
|
2015-10-05 23:39:34 +00:00
|
|
|
SpecialPowers.addPermission("desktop-notification", true, document);
|
2015-04-11 03:19:28 +00:00
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|