Skip to content
Permalink
Browse files

Bug 1038811 - Push Notifications - Tests. r=nsm

  • Loading branch information...
rmottola committed Aug 28, 2019
1 parent 7f12724 commit cbac313088eedfd3db0eee5f6fe9e38d7e257adb
@@ -534,6 +534,7 @@ let AboutPermissions = {
Services.prefs.addObserver("dom.webnotifications.enabled", this, false);
Services.prefs.addObserver("xpinstall.whitelist.required", this, false);
Services.prefs.addObserver("geo.enabled", this, false);
Services.prefs.addObserver("dom.push.enabled", this, false);
Services.prefs.addObserver("dom.indexedDB.enabled", this, false);
Services.prefs.addObserver("plugins.click_to_play", this, false);
Services.prefs.addObserver("full-screen-api.enabled", this, false);
@@ -687,6 +688,7 @@ let AboutPermissions = {
Services.prefs.removeObserver("dom.webnotifications.enabled", this, false);
Services.prefs.removeObserver("xpinstall.whitelist.required", this, false);
Services.prefs.removeObserver("geo.enabled", this, false);
Services.prefs.removeObserver("dom.push.enabled", this, false);
Services.prefs.removeObserver("dom.indexedDB.enabled", this, false);
Services.prefs.removeObserver("plugins.click_to_play", this, false);
Services.prefs.removeObserver("full-screen-api.enabled", this, false);
@@ -23,6 +23,7 @@ const TEST_PERMS = {
"password": PERM_ALLOW,
"cookie": PERM_ALLOW,
"geo": PERM_UNKNOWN,
"push": PERM_DENY,
"indexedDB": PERM_UNKNOWN,
"popup": PERM_DENY,
"fullscreen" : PERM_UNKNOWN,
@@ -37,7 +38,7 @@ const NO_GLOBAL_ALLOW = [
];

// number of managed permissions in the interface
const TEST_PERMS_COUNT = 8;
const TEST_PERMS_COUNT = 9;

function test() {
waitForExplicitFinish();
@@ -12,3 +12,7 @@ EXTRA_COMPONENTS += [
EXTRA_JS_MODULES += [
'PushService.jsm',
]

MOCHITEST_MANIFESTS += [
'test/mochitest.ini',
]
@@ -0,0 +1,27 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<script>
function waitOnPushMessage(pushSubscription)
{
var p = new Promise(function(res, rej) {
navigator.serviceWorker.onmessage = function(e) {
if (e.data.type == "finished") {
parent.ok(e.data.okay == "yes", "Got a push message.");
res(pushSubscription);
}
};
});
return p;
}
</script>
</head>
<body>

</body>
</html>
@@ -0,0 +1,14 @@
[DEFAULT]
support-files =
worker.js
push-server.sjs
frame.html

[test_has_permissions.html]
skip-if = os == "android" || toolkit == "gonk"
[test_permissions.html]
skip-if = os == "android" || toolkit == "gonk"
[test_register.html]
skip-if = os == "android" || toolkit == "gonk"
[test_multiple_register.html]
skip-if = os == "android" || toolkit == "gonk"
@@ -0,0 +1,26 @@
function debug(str) {
// dump("@@@ push-server " + str + "\n");
}

function handleRequest(request, response)
{
debug("handling request!");

const Cc = Components.classes;
const Ci = Components.interfaces;

let params = request.getHeader("X-Push-Server");
debug("params = " + params);

let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
xhr.open("PUT", params);
xhr.send();
xhr.onload = function(e) {
debug("xhr : " + this.status);
}
xhr.onerror = function(e) {
debug("xhr error: " + e);
}

response.setStatusLine(request.httpVersion, "200", "OK");
}
@@ -0,0 +1,76 @@
<!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");
}
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.");
});
}
function hasPermission(swr) {
var p = new Promise(function(res, rej) {
swr.pushManager.hasPermission().then(
function(status) {
debug("status: " + status);
ok(true, "hasPermission() returned a status");
res(swr);
}, function(error) {
ok(false, "hasPermission failed.");
res(swr);
}
);
});
return p;
}
function runTest() {
start()
.then(hasPermission)
.then(unregister)
.catch(function(e) {
ok(false, "Some test failed with error " + e);
}).then(SimpleTest.finish);
}
SpecialPowers.addPermission('push', false, document);
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, runTest);
SimpleTest.waitForExplicitFinish();
</script>
</body>
</html>
@@ -0,0 +1,128 @@
<!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");
}
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.");
});
}
function setupPushNotification(swr) {
var p = new Promise(function(res, rej) {
swr.pushManager.subscribe().then(
function(pushSubscription) {
ok(true, "successful registered for push notification");
res({swr: swr, pushSubscription: pushSubscription});
}, function(error) {
ok(false, "could not register for push notification");
res(null);
}
);
});
return p;
}
function setupSecondEndpoint(result) {
var p = new Promise(function(res, rej) {
result.swr.pushManager.subscribe().then(
function(pushSubscription) {
ok(result.pushSubscription.endpoint == pushSubscription.endpoint, "setupSecondEndpoint - Got the same endpoint back.");
res(result);
}, function(error) {
ok(false, "could not register for push notification");
res(null);
}
);
});
return p;
}
function getEndpointExpectNull(swr) {
var p = new Promise(function(res, rej) {
swr.pushManager.getSubscription().then(
function(pushSubscription) {
ok(pushSubscription == null, "getEndpoint should return null when app not subscribed.");
res(swr);
}, function(error) {
ok(false, "could not register for push notification");
res(null);
}
);
});
return p;
}
function getEndpoint(result) {
var p = new Promise(function(res, rej) {
result.swr.pushManager.getSubscription().then(
function(pushSubscription) {
ok(result.pushSubscription.endpoint == pushSubscription.endpoint, "getEndpoint - Got the same endpoint back.");
res(pushSubscription);
}, function(error) {
ok(false, "could not register for push notification");
res(null);
}
);
});
return p;
}
function unregisterPushNotification(pushSubscription) {
return pushSubscription.unsubscribe();
}
function runTest() {
start()
.then(getEndpointExpectNull)
.then(setupPushNotification)
.then(setupSecondEndpoint)
.then(getEndpoint)
.then(unregisterPushNotification)
.then(unregister)
.catch(function(e) {
ok(false, "Some test failed with error " + e);
}).then(SimpleTest.finish);
}
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, runTest);
SpecialPowers.addPermission('push', true, document);
SimpleTest.waitForExplicitFinish();
</script>
</body>
</html>

0 comments on commit cbac313

Please sign in to comment.
You can’t perform that action at this time.