Bug 1114554 - Patch 6 - ServiceWorkerRegistration.getNotifications() tests. r=wchen

This commit is contained in:
Nikhil Marathe 2015-05-04 19:10:53 -07:00
parent e0074dc7bd
commit ae34b42c7f
6 changed files with 267 additions and 0 deletions

View File

@ -102,6 +102,10 @@ support-files =
periodic/wait_for_update.html
periodic/unregister.html
notification_constructor_error.js
notification_get_sw.js
notification/register.html
notification/listener.html
notification_alt/register.html
sanitize/frame.html
sanitize/register.html
sanitize/example_check_and_unregister.html
@ -190,6 +194,7 @@ skip-if = toolkit == 'android' # Bug 1163410
[test_sandbox_intercept.html]
[test_notificationclick.html]
[test_notification_constructor_error.html]
[test_notification_get.html]
[test_sanitize.html]
[test_sanitize_domain.html]
[test_service_worker_allowed.html]

View File

@ -0,0 +1,27 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1114554 - proxy to forward messages from SW to test</title>
<script class="testbody" type="text/javascript">
var testWindow = parent;
if (opener) {
testWindow = opener;
}
navigator.serviceWorker.onmessage = function(msg) {
// worker message;
testWindow.postMessage(msg.data, "*");
if (msg.data.type == 'finish') {
window.close();
}
};
</script>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<script>
function done() {
parent.callback();
}
navigator.serviceWorker.ready.then(done);
navigator.serviceWorker.register("../notification_get_sw.js", {scope: "."}).catch(function(e) {
dump("Registration failure " + e.message + "\n");
});
</script>

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<script>
function done() {
parent.callback();
}
navigator.serviceWorker.ready.then(done);
navigator.serviceWorker.register("../notification_get_sw.js", {scope: "."}).catch(function(e) {
dump("Registration failure " + e.message + "\n");
});
</script>

View File

@ -0,0 +1,49 @@
function postAll(data) {
self.clients.matchAll().then(function(clients) {
if (clients.length == 0) {
dump("***************** NO CLIENTS FOUND! Test messages are being lost *******************\n");
}
clients.forEach(function(client) {
client.postMessage(data);
});
});
}
function ok(a, msg) {
postAll({type: 'status', status: !!a, msg: a + ": " + msg });
}
function is(a, b, msg) {
postAll({type: 'status', status: a === b, msg: a + " === " + b + ": " + msg });
}
function done() {
postAll({type: 'finish'});
}
onmessage = function(e) {
dump("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MESSAGE " + e.data + "\n");
var start;
if (e.data == 'create') {
start = registration.showNotification("This is a title");
} else {
start = Promise.resolve();
}
start.then(function() {
dump("CALLING getNotification\n");
registration.getNotifications().then(function(notifications) {
dump("RECD getNotification\n");
is(notifications.length, 1, "There should be one stored notification");
var notification = notifications[0];
if (!notification) {
done();
return;
}
ok(notification instanceof Notification, "Should be a Notification");
is(notification.title, "This is a title", "Title should match");
notification.close();
done();
});
});
}

View File

@ -0,0 +1,164 @@
<!DOCTYPE HTML>
<html>
<head>
<title>ServiceWorkerRegistration.getNotifications() on main thread and worker thread.</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/dom/tests/mochitest/notification/MockServices.js"></script>
<script type="text/javascript" src="/tests/dom/tests/mochitest/notification/NotificationTest.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 type="text/javascript">
SimpleTest.requestFlakyTimeout("untriaged");
function testFrame(src) {
return new Promise(function(resolve, reject) {
var iframe = document.createElement("iframe");
iframe.src = src;
window.callback = function(result) {
iframe.src = "about:blank";
document.body.removeChild(iframe);
iframe = null;
SpecialPowers.exactGC(window, function() {
resolve(result);
});
};
document.body.appendChild(iframe);
});
}
function registerSW() {
return testFrame('notification/register.html').then(function() {
ok(true, "Registered service worker.");
});
}
// To check that the scope is respected when retrieving notifications.
function registerAlternateSWAndAddNotification() {
return testFrame('notification_alt/register.html').then(function() {
ok(true, "Registered alternate service worker.");
return navigator.serviceWorker.getRegistration("./notification_alt/").then(function(reg) {
return reg.showNotification("This is a notification_alt");
});
});
}
function testGet() {
// Non persistent notifications will not show up in getNotification().
var n = new Notification("Scope does not match");
var options = NotificationTest.payload;
return navigator.serviceWorker.getRegistration("./notification/")
.then(function(reg) {
return reg.showNotification("This is a title", options)
.then(function() {
return reg;
});
}).then(function(reg) {
return registerAlternateSWAndAddNotification().then(function() {
return reg;
});
}).then(function(reg) {
return reg.getNotifications();
}).then(function(notifications) {
is(notifications.length, 1, "There should be one stored notification");
var notification = notifications[0];
ok(notification instanceof Notification, "Should be a Notification");
is(notification.title, "This is a title", "Title should match");
for (var key in options) {
if (key === "data") {
ok(NotificationTest.customDataMatches(notification.data),
"data property should match");
continue;
}
is(notification[key], options[key], key + " property should match");
}
notification.close();
}).then(function() {
return navigator.serviceWorker.getRegistration("./notification/").then(function(reg) {
return reg.getNotifications();
});
}).then(function(notifications) {
// Make sure closed notifications are no longer retrieved.
is(notifications.length, 0, "There should be no more stored notifications");
}).catch(function(e) {
ok(false, "Something went wrong " + e.message);
});
}
function testGetWorker() {
todo(false, "navigator.serviceWorker is not available on workers yet");
return Promise.resolve();
}
function waitForSWTests(reg, msg) {
return new Promise(function(resolve, reject) {
var content = document.getElementById("content");
iframe = document.createElement("iframe");
content.appendChild(iframe);
iframe.setAttribute('src', "notification/listener.html");
window.onmessage = function(e) {
if (e.data.type == 'status') {
ok(e.data.status, "Service worker test: " + e.data.msg);
} else if (e.data.type == 'finish') {
content.removeChild(iframe);
resolve();
}
}
iframe.onload = function(e) {
iframe.onload = null;
reg.active.postMessage(msg);
}
});
}
function testGetServiceWorker() {
return navigator.serviceWorker.getRegistration("./notification/")
.then(function(reg) {
return waitForSWTests(reg, 'create');
});
}
// Create a Notification here, make sure ServiceWorker sees it.
function testAcrossThreads() {
return navigator.serviceWorker.getRegistration("./notification/")
.then(function(reg) {
return reg.showNotification("This is a title")
.then(function() {
return reg;
});
}).then(function(reg) {
return waitForSWTests(reg, 'do-not-create');
});
}
SimpleTest.waitForExplicitFinish();
MockServices.register();
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true],
["dom.webnotifications.workers.enabled", true],
["notification.prompt.testing", true],
]}, function() {
registerSW()
.then(testGet)
.then(testGetWorker)
.then(testGetServiceWorker)
.then(testAcrossThreads)
.then(function() {
MockServices.unregister();
SimpleTest.finish();
});
});
</script>
</body>
</html>