summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/notification/desktop-notification
diff options
context:
space:
mode:
Diffstat (limited to 'dom/tests/mochitest/notification/desktop-notification')
-rw-r--r--dom/tests/mochitest/notification/desktop-notification/create_notification.html16
-rw-r--r--dom/tests/mochitest/notification/desktop-notification/moz.build6
-rw-r--r--dom/tests/mochitest/notification/desktop-notification/notification_common.js70
-rw-r--r--dom/tests/mochitest/notification/desktop-notification/test_basic_notification.html50
-rw-r--r--dom/tests/mochitest/notification/desktop-notification/test_basic_notification_click.html53
-rw-r--r--dom/tests/mochitest/notification/desktop-notification/test_leak_windowClose.html34
-rw-r--r--dom/tests/mochitest/notification/desktop-notification/test_notification_tag.html110
-rw-r--r--dom/tests/mochitest/notification/desktop-notification/test_system_principal.xul85
8 files changed, 424 insertions, 0 deletions
diff --git a/dom/tests/mochitest/notification/desktop-notification/create_notification.html b/dom/tests/mochitest/notification/desktop-notification/create_notification.html
new file mode 100644
index 000000000..b0387e4ff
--- /dev/null
+++ b/dom/tests/mochitest/notification/desktop-notification/create_notification.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+<head><meta charset=utf-8>
+ <title>Create a notification</title>
+</head>
+<body>
+<script>
+
+var notification = new Notification("This is a title", {
+ body: "This is a notification body",
+ tag: "sometag",
+ });
+
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/notification/desktop-notification/moz.build b/dom/tests/mochitest/notification/desktop-notification/moz.build
new file mode 100644
index 000000000..28919c271
--- /dev/null
+++ b/dom/tests/mochitest/notification/desktop-notification/moz.build
@@ -0,0 +1,6 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
diff --git a/dom/tests/mochitest/notification/desktop-notification/notification_common.js b/dom/tests/mochitest/notification/desktop-notification/notification_common.js
new file mode 100644
index 000000000..921b1e753
--- /dev/null
+++ b/dom/tests/mochitest/notification/desktop-notification/notification_common.js
@@ -0,0 +1,70 @@
+const MOCK_ALERTS_CID = SpecialPowers.wrap(SpecialPowers.Components).ID("{48068bc2-40ab-4904-8afd-4cdfb3a385f3}");
+const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";
+
+const MOCK_SYSTEM_ALERTS_CID = SpecialPowers.wrap(SpecialPowers.Components).ID("{e86d888c-e41b-4b78-9104-2f2742a532de}");
+const SYSTEM_ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/system-alerts-service;1";
+
+var registrar = SpecialPowers.wrap(SpecialPowers.Components).manager.
+ QueryInterface(SpecialPowers.Ci.nsIComponentRegistrar);
+
+var mockAlertsService = {
+ showAlert: function(alert, alertListener) {
+ // probably should do this async....
+ SpecialPowers.wrap(alertListener).observe(null, "alertshow", alert.cookie);
+
+ if (SpecialPowers.getBoolPref("notification.prompt.testing.click_on_notification") == true) {
+ SpecialPowers.wrap(alertListener).observe(null, "alertclickcallback", alert.cookie);
+ }
+
+ SpecialPowers.wrap(alertListener).observe(null, "alertfinished", alert.cookie);
+ },
+
+ showAlertNotification: function(imageUrl, title, text, textClickable,
+ cookie, alertListener, name, bidi,
+ lang, data) {
+ return this.showAlert({
+ cookie: cookie
+ }, alertListener);
+ },
+
+ QueryInterface: function(aIID) {
+ if (SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsISupports) ||
+ SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIAlertsService)) {
+ return this;
+ }
+ throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE;
+ },
+
+ createInstance: function(aOuter, aIID) {
+ if (aOuter != null) {
+ throw SpecialPowers.Components.results.NS_ERROR_NO_AGGREGATION;
+ }
+ return this.QueryInterface(aIID);
+ }
+};
+mockAlertsService = SpecialPowers.wrapCallbackObject(mockAlertsService);
+
+function setup_notifications(allowPrompt, forceClick, callback) {
+ SpecialPowers.pushPrefEnv({'set': [["notification.prompt.testing", true],
+ ["notification.prompt.testing.allow", allowPrompt],
+ ["notification.prompt.testing.click_on_notification", forceClick]]},
+ callback);
+
+ registrar.registerFactory(MOCK_SYSTEM_ALERTS_CID, "system alerts service",
+ SYSTEM_ALERTS_SERVICE_CONTRACT_ID,
+ mockAlertsService);
+
+ registrar.registerFactory(MOCK_ALERTS_CID, "alerts service",
+ ALERTS_SERVICE_CONTRACT_ID,
+ mockAlertsService);
+}
+
+function reset_notifications() {
+ registrar.unregisterFactory(MOCK_SYSTEM_ALERTS_CID, mockAlertsService);
+ registrar.unregisterFactory(MOCK_ALERTS_CID, mockAlertsService);
+}
+
+function is_feature_enabled() {
+ return navigator.mozNotification && SpecialPowers.getBoolPref("notification.feature.enabled");
+}
+
diff --git a/dom/tests/mochitest/notification/desktop-notification/test_basic_notification.html b/dom/tests/mochitest/notification/desktop-notification/test_basic_notification.html
new file mode 100644
index 000000000..75f3c84b4
--- /dev/null
+++ b/dom/tests/mochitest/notification/desktop-notification/test_basic_notification.html
@@ -0,0 +1,50 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=573588
+-->
+<head>
+ <title>Basic functional test</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="notification_common.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=573588">Basic property tests</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<pre id="test">
+</pre>
+<script type="text/javascript">
+ if (is_feature_enabled()) {
+ SimpleTest.waitForExplicitFinish();
+
+ function showNotifications() {
+ ok(navigator.mozNotification, "test for notification.");
+
+ var notification = navigator.mozNotification.createNotification("test", "test");
+ ok(notification, "test to ensure we can create a notification");
+
+ notification.onclose = function() {
+ ok(true, "notification was display and is now closing");
+ reset_notifications();
+ SimpleTest.finish();
+ };
+
+ notification.onclick = function() {
+ ok(false, "Click should not have been called.");
+ reset_notifications();
+ SimpleTest.finish();
+ };
+
+ notification.show();
+ }
+
+ setup_notifications(true, false, showNotifications);
+ } else {
+ ok(true, "Desktop notifications not enabled.");
+ }
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/notification/desktop-notification/test_basic_notification_click.html b/dom/tests/mochitest/notification/desktop-notification/test_basic_notification_click.html
new file mode 100644
index 000000000..defa0f412
--- /dev/null
+++ b/dom/tests/mochitest/notification/desktop-notification/test_basic_notification_click.html
@@ -0,0 +1,53 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=573588
+-->
+<head>
+ <title>Basic functional test</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="notification_common.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=573588">Basic property tests</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<pre id="test">
+</pre>
+<script type="text/javascript">
+ if (is_feature_enabled()) {
+ SimpleTest.waitForExplicitFinish();
+
+ var click_was_called = false;
+
+ function showNotifications() {
+ ok(navigator.mozNotification, "test for notification.");
+
+ var notification = navigator.mozNotification.createNotification("test", "test");
+ ok(notification, "test to ensure we can create a notification");
+
+ notification.onclose = function() {
+ ok(true, "notification was display and is now closing");
+ ok(click_was_called, "was notification clicked?");
+
+ reset_notifications();
+ SimpleTest.finish();
+ };
+
+ notification.onclick = function() {
+ ok(true, "Click was called. Good.");
+ click_was_called = true;
+ };
+
+ notification.show();
+ }
+
+ setup_notifications(true, true, showNotifications);
+ } else {
+ ok(true, "Desktop notifications not enabled.");
+ }
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/notification/desktop-notification/test_leak_windowClose.html b/dom/tests/mochitest/notification/desktop-notification/test_leak_windowClose.html
new file mode 100644
index 000000000..c0c9699ae
--- /dev/null
+++ b/dom/tests/mochitest/notification/desktop-notification/test_leak_windowClose.html
@@ -0,0 +1,34 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=605309
+-->
+<head>
+ <title>Test for leak when window closes</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="notification_common.js"></script>
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+
+<script>
+if (is_feature_enabled()) {
+ SimpleTest.waitForExplicitFinish();
+
+ function boom()
+ {
+ document.documentElement.focus();
+ var x = navigator.mozNotification;
+ document.documentElement.addEventListener('', function(){x}, false);
+ ok(true, "load callback called");
+ SimpleTest.finish();
+ }
+
+ window.addEventListener("load", boom, false);
+} else {
+ ok(true, "Desktop notifications not enabled.");
+}
+</script>
+</head>
+<body>
+<p> I like to write tests </p>
+</body>
+</html>
diff --git a/dom/tests/mochitest/notification/desktop-notification/test_notification_tag.html b/dom/tests/mochitest/notification/desktop-notification/test_notification_tag.html
new file mode 100644
index 000000000..37bcb16c0
--- /dev/null
+++ b/dom/tests/mochitest/notification/desktop-notification/test_notification_tag.html
@@ -0,0 +1,110 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=782211
+-->
+<head>
+ <title>Bug 782211</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=782211">Bug 782211</a>
+<p id="display"></p>
+<iframe name="sameDomain"></iframe>
+<iframe name="anotherSameDomain"></iframe>
+<iframe name="crossDomain"></iframe>
+<div id="content" style="display: none">
+</div>
+<pre id="test">
+</pre>
+<script type="text/javascript">
+ const MOCK_CID = SpecialPowers.wrap(SpecialPowers.Components).ID("{dbe37e64-d9a3-402c-8d8a-0826c619f7ad}");
+ const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";
+
+ var mockAlertsService = {
+ showAlert: function(alert, alertListener) {
+ notificationsCreated.push(alert.name);
+ if (notificationsCreated.length == 3) {
+ checkNotifications();
+ }
+ },
+
+ showAlertNotification: function(imageUrl, title, text, textClickable,
+ cookie, alertListener, name, dir,
+ lang, data) {
+ this.showAlert({ name: name });
+ },
+
+ QueryInterface: function(aIID) {
+ if (SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsISupports) ||
+ SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIAlertsService)) {
+ return this;
+ }
+ throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE;
+ },
+
+ createInstance: function(aOuter, aIID) {
+ if (aOuter != null) {
+ throw SpecialPowers.Components.results.NS_ERROR_NO_AGGREGATION;
+ }
+ return this.QueryInterface(aIID);
+ }
+ };
+ mockAlertsService = SpecialPowers.wrapCallbackObject(mockAlertsService);
+
+ var notificationsCreated = [];
+ function checkNotifications() {
+ // notifications created by the test1 origin
+ var test1notifications = [];
+ // notifications created by the test2 origin
+ var test2notifications = [];
+ for (var i = 0; i < notificationsCreated.length; i++) {
+ var notificationName = notificationsCreated[i];
+ if (notificationName.indexOf("test1") !== -1) {
+ test1notifications.push(notificationsCreated[i]);
+ } else if (notificationName.indexOf("test2") !== -1) {
+ test2notifications.push(notificationsCreated[i]);
+ }
+ }
+
+ is(test1notifications.length, 2, "2 notifications should be created by test1.example.org:80 origin.");
+ is(test1notifications[0], test1notifications[1], "notification names should be identical.");
+ is(test2notifications.length, 1, "1 notification should be created by test2.example.org:80 origin.");
+
+ // Register original alerts service.
+ SpecialPowers.wrap(SpecialPowers.Components).
+ manager.QueryInterface(SpecialPowers.Ci.nsIComponentRegistrar).
+ unregisterFactory(MOCK_CID, mockAlertsService);
+
+ SimpleTest.finish();
+ }
+
+ if (window.Notification) {
+ SimpleTest.waitForExplicitFinish();
+
+ function showNotifications() {
+ SpecialPowers.wrap(SpecialPowers.Components).
+ manager.QueryInterface(SpecialPowers.Ci.nsIComponentRegistrar).
+ registerFactory(MOCK_CID, "alerts service", ALERTS_SERVICE_CONTRACT_ID, mockAlertsService);
+
+ // Load two frames with the same origin that create notification with the same tag.
+ // Both pages should generate notifications with the same name, and thus the second
+ // notification should replace the first.
+ frames["sameDomain"].location.href = "http://test1.example.org:80/tests/dom/tests/mochitest/notification/create_notification.html";
+ frames["anotherSameDomain"].location.href = "http://test1.example.org:80/tests/dom/tests/mochitest/notification/create_notification.html";
+
+ // Load a frame with a different origin that creates a notification with the same tag.
+ // The notification name should be different and thus no notifications should be replaced.
+ frames["crossDomain"].location.href = "http://test2.example.org:80/tests/dom/tests/mochitest/notification/create_notification.html";
+ }
+
+ SpecialPowers.pushPrefEnv({'set': [["notification.prompt.testing", true],
+ ["notification.prompt.testing.allow", true]]},
+ showNotifications);
+ } else {
+ ok(true, "Notifications are not enabled on the platform.");
+ }
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/notification/desktop-notification/test_system_principal.xul b/dom/tests/mochitest/notification/desktop-notification/test_system_principal.xul
new file mode 100644
index 000000000..1690bc101
--- /dev/null
+++ b/dom/tests/mochitest/notification/desktop-notification/test_system_principal.xul
@@ -0,0 +1,85 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
+<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=874090
+-->
+<window title="Mozilla Bug 874090" onload="runTests()"
+ xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+ <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
+ <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
+
+ <!-- test results are displayed in the html:body -->
+ <body xmlns="http://www.w3.org/1999/xhtml">
+ <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=874090"
+ target="_blank">Mozilla Bug 874090</a>
+ </body>
+
+ <!-- test code goes here -->
+ <script type="application/javascript">
+ <![CDATA[
+ /** Test for Bug 874090 **/
+ const MOCK_CID = Components.ID("{2a0f83c4-8818-4914-a184-f1172b4eaaa7}");
+ const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";
+
+ var mockAlertsService = {
+ showAlert: function(alert, alertListener) {
+ ok(true, "System principal was granted permission and is able to call showAlert.");
+ unregisterMock();
+ SimpleTest.finish();
+ },
+
+ showAlertNotification: function(imageUrl, title, text, textClickable,
+ cookie, alertListener, name, dir, lang, data) {
+ this.showAlert();
+ },
+
+ QueryInterface: function(aIID) {
+ if (aIID.equals(Components.interfaces.nsISupports) ||
+ aIID.equals(Components.interfaces.nsIAlertsService)) {
+ return this;
+ }
+ throw Components.results.NS_ERROR_NO_INTERFACE;
+ },
+
+ createInstance: function(aOuter, aIID) {
+ if (aOuter != null) {
+ throw Components.results.NS_ERROR_NO_AGGREGATION;
+ }
+ return this.QueryInterface(aIID);
+ }
+ };
+
+ function registerMock() {
+ Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar).
+ registerFactory(MOCK_CID, "alerts service", ALERTS_SERVICE_CONTRACT_ID, mockAlertsService);
+ }
+
+ function unregisterMock() {
+ Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar).
+ unregisterFactory(MOCK_CID, mockAlertsService);
+ }
+
+ function runTests() {
+ registerMock();
+
+ is(Notification.permission, "granted", "System principal should be automatically granted permission.");
+
+ Notification.requestPermission(function(permission) {
+ is(permission, "granted", "System principal should be granted permission when calling requestPermission.");
+
+ if (permission == "granted") {
+ // Create a notification and make sure that it is able to call into
+ // the mock alert service to show the notification.
+ new Notification("Hello");
+ } else {
+ unregisterMock();
+ SimpleTest.finish();
+ }
+ });
+ }
+
+ SimpleTest.waitForExplicitFinish();
+ ]]>
+ </script>
+</window>