summaryrefslogtreecommitdiffstats
path: root/dom/notification/test
diff options
context:
space:
mode:
Diffstat (limited to 'dom/notification/test')
-rw-r--r--dom/notification/test/browser/browser.ini2
-rw-r--r--dom/notification/test/browser/browser_permission_dismiss.js113
-rw-r--r--dom/notification/test/browser/notification.html11
-rw-r--r--dom/notification/test/unit/common_test_notificationdb.js60
-rw-r--r--dom/notification/test/unit/test_notificationdb.js310
-rw-r--r--dom/notification/test/unit/test_notificationdb_bug1024090.js56
-rw-r--r--dom/notification/test/unit/xpcshell.ini7
7 files changed, 559 insertions, 0 deletions
diff --git a/dom/notification/test/browser/browser.ini b/dom/notification/test/browser/browser.ini
new file mode 100644
index 000000000..8a357c1a1
--- /dev/null
+++ b/dom/notification/test/browser/browser.ini
@@ -0,0 +1,2 @@
+[browser_permission_dismiss.js]
+support-files = notification.html
diff --git a/dom/notification/test/browser/browser_permission_dismiss.js b/dom/notification/test/browser/browser_permission_dismiss.js
new file mode 100644
index 000000000..de655870b
--- /dev/null
+++ b/dom/notification/test/browser/browser_permission_dismiss.js
@@ -0,0 +1,113 @@
+"use strict";
+
+const ORIGIN_URI = Services.io.newURI("http://mochi.test:8888", null, null);
+const PERMISSION_NAME = "desktop-notification";
+const PROMPT_ALLOW_BUTTON = -1;
+const PROMPT_BLOCK_BUTTON = 0;
+const TEST_URL = "http://mochi.test:8888/browser/dom/notification/test/browser/notification.html";
+
+/**
+ * Clicks the specified web-notifications prompt button.
+ *
+ * @param {Number} aButtonIndex Number indicating which button to click.
+ * See the constants in this file.
+ * @note modified from toolkit/components/passwordmgr/test/browser/head.js
+ */
+function clickDoorhangerButton(aButtonIndex) {
+ ok(true, "Looking for action at index " + aButtonIndex);
+
+ let popup = PopupNotifications.getNotification("web-notifications");
+ let notifications = popup.owner.panel.childNodes;
+ ok(notifications.length > 0, "at least one notification displayed");
+ ok(true, notifications.length + " notification(s)");
+ let notification = notifications[0];
+
+ if (aButtonIndex == -1) {
+ ok(true, "Triggering main action");
+ notification.button.doCommand();
+ } else if (aButtonIndex <= popup.secondaryActions.length) {
+ ok(true, "Triggering secondary action " + aButtonIndex);
+ notification.childNodes[aButtonIndex].doCommand();
+ }
+}
+
+/**
+ * Opens a tab which calls `Notification.requestPermission()` with a callback
+ * argument, calls the `task` function while the permission prompt is open,
+ * and verifies that the expected permission is set.
+ *
+ * @param {Function} task Task function to run to interact with the prompt.
+ * @param {String} permission Expected permission value.
+ * @return {Promise} resolving when the task function is done and the tab
+ * closes.
+ */
+function tabWithRequest(task, permission) {
+ Services.perms.remove(ORIGIN_URI, PERMISSION_NAME);
+
+ return BrowserTestUtils.withNewTab({
+ gBrowser,
+ url: TEST_URL,
+ }, function*(browser) {
+ let requestPromise = ContentTask.spawn(browser, {
+ permission
+ }, function*({permission}) {
+ function requestCallback(perm) {
+ is(perm, permission,
+ "Should call the legacy callback with the permission state");
+ }
+ let perm = yield content.window.Notification
+ .requestPermission(requestCallback);
+ is(perm, permission,
+ "Should resolve the promise with the permission state");
+ });
+
+ yield BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
+ yield task();
+ yield requestPromise;
+ });
+}
+
+add_task(function* setup() {
+ SimpleTest.registerCleanupFunction(() => {
+ Services.perms.remove(ORIGIN_URI, PERMISSION_NAME);
+ });
+});
+
+add_task(function* test_requestPermission_granted() {
+ yield tabWithRequest(function() {
+ clickDoorhangerButton(PROMPT_ALLOW_BUTTON);
+ }, "granted");
+
+ ok(!PopupNotifications.getNotification("web-notifications"),
+ "Should remove the doorhanger notification icon if granted");
+
+ is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+ Services.perms.ALLOW_ACTION,
+ "Check permission in perm. manager");
+});
+
+add_task(function* test_requestPermission_denied() {
+ yield tabWithRequest(function() {
+ clickDoorhangerButton(PROMPT_BLOCK_BUTTON);
+ }, "denied");
+
+ ok(!PopupNotifications.getNotification("web-notifications"),
+ "Should remove the doorhanger notification icon if denied");
+
+ is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+ Services.perms.DENY_ACTION,
+ "Check permission in perm. manager");
+});
+
+add_task(function* test_requestPermission_dismissed() {
+ yield tabWithRequest(function() {
+ PopupNotifications.panel.hidePopup();
+ }, "default");
+
+ ok(!PopupNotifications.getNotification("web-notifications"),
+ "Should remove the doorhanger notification icon if dismissed");
+
+ is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+ Services.perms.UNKNOWN_ACTION,
+ "Check permission in perm. manager");
+});
diff --git a/dom/notification/test/browser/notification.html b/dom/notification/test/browser/notification.html
new file mode 100644
index 000000000..0ceeb8ea4
--- /dev/null
+++ b/dom/notification/test/browser/notification.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <title>Notifications test</title>
+ </head>
+
+ <body>
+
+ </body>
+</html>
diff --git a/dom/notification/test/unit/common_test_notificationdb.js b/dom/notification/test/unit/common_test_notificationdb.js
new file mode 100644
index 000000000..116c7836a
--- /dev/null
+++ b/dom/notification/test/unit/common_test_notificationdb.js
@@ -0,0 +1,60 @@
+"use strict";
+
+var Cu = Components.utils;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+
+XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
+ "@mozilla.org/childprocessmessagemanager;1",
+ "nsIMessageSender");
+
+function getNotificationObject(app, id, tag) {
+ return {
+ origin: "https://" + app + ".gaiamobile.org/",
+ id: id,
+ title: app + "Notification:" + Date.now(),
+ dir: "auto",
+ lang: "",
+ body: app + " notification body",
+ tag: tag || "",
+ icon: "icon.png"
+ };
+}
+
+var systemNotification =
+ getNotificationObject("system", "{2bc883bf-2809-4432-b0f4-f54e10372764}");
+
+var calendarNotification =
+ getNotificationObject("calendar", "{d8d11299-a58e-429b-9a9a-57c562982fbf}");
+
+// Helper to start the NotificationDB
+function startNotificationDB() {
+ Cu.import("resource://gre/modules/NotificationDB.jsm");
+}
+
+// Helper function to add a listener, send message and treat the reply
+function addAndSend(msg, reply, callback, payload, runNext = true) {
+ let handler = {
+ receiveMessage: function(message) {
+ if (message.name === reply) {
+ cpmm.removeMessageListener(reply, handler);
+ callback(message);
+ if (runNext) {
+ run_next_test();
+ }
+ }
+ }
+ };
+ cpmm.addMessageListener(reply, handler);
+ cpmm.sendAsyncMessage(msg, payload);
+}
+
+// helper fonction, comparing two notifications
+function compareNotification(notif1, notif2) {
+ // retrieved notification should be the second one sent
+ for (let prop in notif1) {
+ // compare each property
+ do_check_eq(notif1[prop], notif2[prop]);
+ }
+}
diff --git a/dom/notification/test/unit/test_notificationdb.js b/dom/notification/test/unit/test_notificationdb.js
new file mode 100644
index 000000000..d631bd034
--- /dev/null
+++ b/dom/notification/test/unit/test_notificationdb.js
@@ -0,0 +1,310 @@
+"use strict";
+
+function run_test() {
+ do_get_profile();
+ startNotificationDB();
+ run_next_test();
+}
+
+// Get one notification, none exists
+add_test(function test_get_none() {
+ let requestID = 0;
+ let msgReply = "Notification:GetAll:Return:OK";
+ let msgHandler = function(message) {
+ do_check_eq(requestID, message.data.requestID);
+ do_check_eq(0, message.data.notifications.length);
+ };
+
+ addAndSend("Notification:GetAll", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ requestID: requestID
+ });
+});
+
+// Store one notification
+add_test(function test_send_one() {
+ let requestID = 1;
+ let msgReply = "Notification:Save:Return:OK";
+ let msgHandler = function(message) {
+ do_check_eq(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Save", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ notification: systemNotification,
+ requestID: requestID
+ });
+});
+
+// Get one notification, one exists
+add_test(function test_get_one() {
+ let requestID = 2;
+ let msgReply = "Notification:GetAll:Return:OK";
+ let msgHandler = function(message) {
+ do_check_eq(requestID, message.data.requestID);
+ do_check_eq(1, message.data.notifications.length);
+ // compare the content
+ compareNotification(systemNotification, message.data.notifications[0]);
+ };
+
+ addAndSend("Notification:GetAll", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ requestID: requestID
+ });
+});
+
+// Delete one notification
+add_test(function test_delete_one() {
+ let requestID = 3;
+ let msgReply = "Notification:Delete:Return:OK";
+ let msgHandler = function(message) {
+ do_check_eq(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: systemNotification.id,
+ requestID: requestID
+ });
+});
+
+// Get one notification, none exists
+add_test(function test_get_none_again() {
+ let requestID = 4;
+ let msgReply = "Notification:GetAll:Return:OK";
+ let msgHandler = function(message) {
+ do_check_eq(requestID, message.data.requestID);
+ do_check_eq(0, message.data.notifications.length);
+ };
+
+ addAndSend("Notification:GetAll", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ requestID: requestID
+ });
+});
+
+// Delete one notification that do not exists anymore
+add_test(function test_delete_one_nonexistent() {
+ let requestID = 5;
+ let msgReply = "Notification:Delete:Return:OK";
+ let msgHandler = function(message) {
+ do_check_eq(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: systemNotification.id,
+ requestID: requestID
+ });
+});
+
+// Store two notifications with the same id
+add_test(function test_send_two_get_one() {
+ let requestID = 6;
+ let calls = 0;
+
+ let msgGetReply = "Notification:GetAll:Return:OK";
+ let msgGetHandler = function(message) {
+ do_check_eq(requestID + 2, message.data.requestID);
+ do_check_eq(1, message.data.notifications.length);
+ // compare the content
+ compareNotification(systemNotification, message.data.notifications[0]);
+ };
+
+ let msgSaveReply = "Notification:Save:Return:OK";
+ let msgSaveHandler = function(message) {
+ calls += 1;
+ if (calls === 2) {
+ addAndSend("Notification:GetAll", msgGetReply, msgGetHandler, {
+ origin: systemNotification.origin,
+ requestID: (requestID + 2)
+ });
+ }
+ };
+
+ addAndSend("Notification:Save", msgSaveReply, msgSaveHandler, {
+ origin: systemNotification.origin,
+ notification: systemNotification,
+ requestID: requestID
+ }, false);
+
+ addAndSend("Notification:Save", msgSaveReply, msgSaveHandler, {
+ origin: systemNotification.origin,
+ notification: systemNotification,
+ requestID: (requestID + 1)
+ }, false);
+});
+
+// Delete previous notification
+add_test(function test_delete_previous() {
+ let requestID = 8;
+ let msgReply = "Notification:Delete:Return:OK";
+ let msgHandler = function(message) {
+ do_check_eq(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: systemNotification.id,
+ requestID: requestID
+ });
+});
+
+// Store two notifications from same origin with the same tag
+add_test(function test_send_two_get_one() {
+ let requestID = 10;
+ let tag = "voicemail";
+
+ let systemNotification1 =
+ getNotificationObject("system", "{f271f9ee-3955-4c10-b1f2-af552fb270ee}", tag);
+ let systemNotification2 =
+ getNotificationObject("system", "{8ef9a628-f0f4-44b4-820d-c117573c33e3}", tag);
+
+ let msgGetReply = "Notification:GetAll:Return:OK";
+ let msgGetNotifHandler = {
+ receiveMessage: function(message) {
+ if (message.name === msgGetReply) {
+ cpmm.removeMessageListener(msgGetReply, msgGetNotifHandler);
+ let notifications = message.data.notifications;
+ // same tag, so replaced
+ do_check_eq(1, notifications.length);
+ // compare the content
+ compareNotification(systemNotification2, notifications[0]);
+ run_next_test();
+ }
+ }
+ };
+
+ cpmm.addMessageListener(msgGetReply, msgGetNotifHandler);
+
+ let msgSaveReply = "Notification:Save:Return:OK";
+ let msgSaveCalls = 0;
+ let msgSaveHandler = function(message) {
+ msgSaveCalls++;
+ // Once both request have been sent, trigger getall
+ if (msgSaveCalls === 2) {
+ cpmm.sendAsyncMessage("Notification:GetAll", {
+ origin: systemNotification1.origin,
+ requestID: message.data.requestID + 2 // 12, 13
+ });
+ }
+ };
+
+ addAndSend("Notification:Save", msgSaveReply, msgSaveHandler, {
+ origin: systemNotification1.origin,
+ notification: systemNotification1,
+ requestID: requestID // 10
+ }, false);
+
+ addAndSend("Notification:Save", msgSaveReply, msgSaveHandler, {
+ origin: systemNotification2.origin,
+ notification: systemNotification2,
+ requestID: (requestID + 1) // 11
+ }, false);
+});
+
+// Delete previous notification
+add_test(function test_delete_previous() {
+ let requestID = 15;
+ let msgReply = "Notification:Delete:Return:OK";
+ let msgHandler = function(message) {
+ do_check_eq(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: "{8ef9a628-f0f4-44b4-820d-c117573c33e3}",
+ requestID: requestID
+ });
+});
+
+// Store two notifications from two origins with the same tag
+add_test(function test_send_two_get_two() {
+ let requestID = 20;
+ let tag = "voicemail";
+
+ let systemNotification1 = systemNotification;
+ systemNotification1.tag = tag;
+
+ let calendarNotification2 = calendarNotification;
+ calendarNotification2.tag = tag;
+
+ let msgGetReply = "Notification:GetAll:Return:OK";
+ let msgGetCalls = 0;
+ let msgGetHandler = {
+ receiveMessage: function(message) {
+ if (message.name === msgGetReply) {
+ msgGetCalls++;
+ let notifications = message.data.notifications;
+
+ // one notification per origin
+ do_check_eq(1, notifications.length);
+
+ // first call should be system notification
+ if (msgGetCalls === 1) {
+ compareNotification(systemNotification1, notifications[0]);
+ }
+
+ // second and last call should be calendar notification
+ if (msgGetCalls === 2) {
+ cpmm.removeMessageListener(msgGetReply, msgGetHandler);
+ compareNotification(calendarNotification2, notifications[0]);
+ run_next_test();
+ }
+ }
+ }
+ };
+ cpmm.addMessageListener(msgGetReply, msgGetHandler);
+
+ let msgSaveReply = "Notification:Save:Return:OK";
+ let msgSaveCalls = 0;
+ let msgSaveHandler = {
+ receiveMessage: function(message) {
+ if (message.name === msgSaveReply) {
+ msgSaveCalls++;
+ if (msgSaveCalls === 2) {
+ cpmm.removeMessageListener(msgSaveReply, msgSaveHandler);
+
+ // Trigger getall for each origin
+ cpmm.sendAsyncMessage("Notification:GetAll", {
+ origin: systemNotification1.origin,
+ requestID: message.data.requestID + 1 // 22
+ });
+
+ cpmm.sendAsyncMessage("Notification:GetAll", {
+ origin: calendarNotification2.origin,
+ requestID: message.data.requestID + 2 // 23
+ });
+ }
+ }
+ }
+ };
+ cpmm.addMessageListener(msgSaveReply, msgSaveHandler);
+
+ cpmm.sendAsyncMessage("Notification:Save", {
+ origin: systemNotification1.origin,
+ notification: systemNotification1,
+ requestID: requestID // 20
+ });
+
+ cpmm.sendAsyncMessage("Notification:Save", {
+ origin: calendarNotification2.origin,
+ notification: calendarNotification2,
+ requestID: (requestID + 1) // 21
+ });
+});
+
+// Cleanup previous notification
+add_test(function test_delete_previous() {
+ let requestID = 25;
+ let msgReply = "Notification:Delete:Return:OK";
+ let msgHandler = function(message) {
+ do_check_eq(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: "{2bc883bf-2809-4432-b0f4-f54e10372764}",
+ requestID: requestID
+ });
+});
diff --git a/dom/notification/test/unit/test_notificationdb_bug1024090.js b/dom/notification/test/unit/test_notificationdb_bug1024090.js
new file mode 100644
index 000000000..68dfb164c
--- /dev/null
+++ b/dom/notification/test/unit/test_notificationdb_bug1024090.js
@@ -0,0 +1,56 @@
+"use strict";
+
+function run_test() {
+ do_get_profile();
+ run_next_test();
+}
+
+/// For bug 1024090: test edge case of notificationstore.json
+add_test(function test_bug1024090_purge() {
+ Cu.import("resource://gre/modules/osfile.jsm");
+ const NOTIFICATION_STORE_PATH =
+ OS.Path.join(OS.Constants.Path.profileDir, "notificationstore.json");
+ let cleanup = OS.File.writeAtomic(NOTIFICATION_STORE_PATH, "");
+ cleanup.then(
+ function onSuccess() {
+ ok(true, "Notification database cleaned.");
+ },
+ function onError(reason) {
+ ok(false, "Notification database error when cleaning: " + reason);
+ }
+ ).then(function next() {
+ do_print("Cleanup steps completed: " + NOTIFICATION_STORE_PATH);
+ startNotificationDB();
+ run_next_test();
+ });
+});
+
+// Store one notification
+add_test(function test_bug1024090_send_one() {
+ let requestID = 1;
+ let msgReply = "Notification:Save:Return:OK";
+ let msgHandler = function(message) {
+ equal(requestID, message.data.requestID, "Checking requestID");
+ };
+
+ addAndSend("Notification:Save", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ notification: systemNotification,
+ requestID: requestID
+ });
+});
+
+// Get one notification, one exists
+add_test(function test_bug1024090_get_one() {
+ let requestID = 2;
+ let msgReply = "Notification:GetAll:Return:OK";
+ let msgHandler = function(message) {
+ equal(requestID, message.data.requestID, "Checking requestID");
+ equal(1, message.data.notifications.length, "One notification stored");
+ };
+
+ addAndSend("Notification:GetAll", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ requestID: requestID
+ });
+});
diff --git a/dom/notification/test/unit/xpcshell.ini b/dom/notification/test/unit/xpcshell.ini
new file mode 100644
index 000000000..63ec4019a
--- /dev/null
+++ b/dom/notification/test/unit/xpcshell.ini
@@ -0,0 +1,7 @@
+[DEFAULT]
+head = common_test_notificationdb.js
+tail =
+skip-if = toolkit == 'android'
+
+[test_notificationdb.js]
+[test_notificationdb_bug1024090.js]