summaryrefslogtreecommitdiffstats
path: root/dom/notification/test/unit
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--dom/notification/test/unit/head_notificationdb.js61
-rw-r--r--dom/notification/test/unit/test_notificationdb.js340
-rw-r--r--dom/notification/test/unit/test_notificationdb_bug1024090.js59
-rw-r--r--dom/notification/test/unit/test_notificationdb_migration.js129
-rw-r--r--dom/notification/test/unit/xpcshell.ini7
5 files changed, 596 insertions, 0 deletions
diff --git a/dom/notification/test/unit/head_notificationdb.js b/dom/notification/test/unit/head_notificationdb.js
new file mode 100644
index 0000000000..1b23d88729
--- /dev/null
+++ b/dom/notification/test/unit/head_notificationdb.js
@@ -0,0 +1,61 @@
+"use strict";
+
+var { XPCOMUtils } = ChromeUtils.importESModule(
+ "resource://gre/modules/XPCOMUtils.sys.mjs"
+);
+
+function getNotificationObject(app, id, tag, includeScope) {
+ const origin = `https://${app}.gaiamobile.org/`;
+ return {
+ origin,
+ id,
+ title: app + "Notification:" + Date.now(),
+ dir: "auto",
+ lang: "",
+ body: app + " notification body",
+ tag: tag || "",
+ icon: "icon.png",
+ serviceWorkerRegistrationScope: includeScope ? origin : undefined,
+ };
+}
+
+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() {
+ ChromeUtils.importESModule("resource://gre/modules/NotificationDB.sys.mjs");
+}
+
+// Helper function to add a listener, send message and treat the reply
+function addAndSend(msg, reply, callback, payload, runNext = true) {
+ let handler = {
+ receiveMessage(message) {
+ if (message.name === reply) {
+ Services.cpmm.removeMessageListener(reply, handler);
+ callback(message);
+ if (runNext) {
+ run_next_test();
+ }
+ }
+ },
+ };
+ Services.cpmm.addMessageListener(reply, handler);
+ Services.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
+ Assert.equal(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 0000000000..33397f87f3
--- /dev/null
+++ b/dom/notification/test/unit/test_notificationdb.js
@@ -0,0 +1,340 @@
+"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) {
+ Assert.equal(requestID, message.data.requestID);
+ Assert.equal(0, message.data.notifications.length);
+ };
+
+ addAndSend("Notification:GetAll", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ requestID,
+ });
+});
+
+// Store one notification
+add_test(function test_send_one() {
+ let requestID = 1;
+ let msgReply = "Notification:Save:Return:OK";
+ let msgHandler = function (message) {
+ Assert.equal(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Save", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ notification: systemNotification,
+ 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) {
+ Assert.equal(requestID, message.data.requestID);
+ Assert.equal(1, message.data.notifications.length);
+ // compare the content
+ compareNotification(systemNotification, message.data.notifications[0]);
+ };
+
+ addAndSend("Notification:GetAll", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ requestID,
+ });
+});
+
+// Delete one notification
+add_test(function test_delete_one() {
+ let requestID = 3;
+ let msgReply = "Notification:Delete:Return:OK";
+ let msgHandler = function (message) {
+ Assert.equal(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: systemNotification.id,
+ 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) {
+ Assert.equal(requestID, message.data.requestID);
+ Assert.equal(0, message.data.notifications.length);
+ };
+
+ addAndSend("Notification:GetAll", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ 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) {
+ Assert.equal(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: systemNotification.id,
+ 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) {
+ Assert.equal(requestID + 2, message.data.requestID);
+ Assert.equal(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,
+ },
+ 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) {
+ Assert.equal(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: systemNotification.id,
+ 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(message) {
+ if (message.name === msgGetReply) {
+ Services.cpmm.removeMessageListener(msgGetReply, msgGetNotifHandler);
+ let notifications = message.data.notifications;
+ // same tag, so replaced
+ Assert.equal(1, notifications.length);
+ // compare the content
+ compareNotification(systemNotification2, notifications[0]);
+ run_next_test();
+ }
+ },
+ };
+
+ Services.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) {
+ Services.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, // 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) {
+ Assert.equal(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: "{8ef9a628-f0f4-44b4-820d-c117573c33e3}",
+ 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(message) {
+ if (message.name === msgGetReply) {
+ msgGetCalls++;
+ let notifications = message.data.notifications;
+
+ // one notification per origin
+ Assert.equal(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) {
+ Services.cpmm.removeMessageListener(msgGetReply, msgGetHandler);
+ compareNotification(calendarNotification2, notifications[0]);
+ run_next_test();
+ }
+ }
+ },
+ };
+ Services.cpmm.addMessageListener(msgGetReply, msgGetHandler);
+
+ let msgSaveReply = "Notification:Save:Return:OK";
+ let msgSaveCalls = 0;
+ let msgSaveHandler = {
+ receiveMessage(message) {
+ if (message.name === msgSaveReply) {
+ msgSaveCalls++;
+ if (msgSaveCalls === 2) {
+ Services.cpmm.removeMessageListener(msgSaveReply, msgSaveHandler);
+
+ // Trigger getall for each origin
+ Services.cpmm.sendAsyncMessage("Notification:GetAll", {
+ origin: systemNotification1.origin,
+ requestID: message.data.requestID + 1, // 22
+ });
+
+ Services.cpmm.sendAsyncMessage("Notification:GetAll", {
+ origin: calendarNotification2.origin,
+ requestID: message.data.requestID + 2, // 23
+ });
+ }
+ }
+ },
+ };
+ Services.cpmm.addMessageListener(msgSaveReply, msgSaveHandler);
+
+ Services.cpmm.sendAsyncMessage("Notification:Save", {
+ origin: systemNotification1.origin,
+ notification: systemNotification1,
+ requestID, // 20
+ });
+
+ Services.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) {
+ Assert.equal(requestID, message.data.requestID);
+ };
+
+ addAndSend("Notification:Delete", msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ id: "{2bc883bf-2809-4432-b0f4-f54e10372764}",
+ 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 0000000000..37dee0a639
--- /dev/null
+++ b/dom/notification/test/unit/test_notificationdb_bug1024090.js
@@ -0,0 +1,59 @@
+"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() {
+ const NOTIFICATION_STORE_PATH = PathUtils.join(
+ PathUtils.profileDir,
+ "notificationstore"
+ );
+ let cleanup = IOUtils.remove(NOTIFICATION_STORE_PATH, { recursive: true });
+ cleanup
+ .then(
+ function onSuccess() {
+ ok(true, "Notification database cleaned.");
+ },
+ function onError(reason) {
+ ok(false, "Notification database error when cleaning: " + reason);
+ }
+ )
+ .then(function next() {
+ info("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,
+ });
+});
+
+// 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,
+ });
+});
diff --git a/dom/notification/test/unit/test_notificationdb_migration.js b/dom/notification/test/unit/test_notificationdb_migration.js
new file mode 100644
index 0000000000..2b38e8d43a
--- /dev/null
+++ b/dom/notification/test/unit/test_notificationdb_migration.js
@@ -0,0 +1,129 @@
+"use strict";
+
+const { AppConstants } = ChromeUtils.importESModule(
+ "resource://gre/modules/AppConstants.sys.mjs"
+);
+
+const fooNotification = getNotificationObject(
+ "foo",
+ "a4f1d54a-98b7-4231-9120-5afc26545bad",
+ null,
+ true
+);
+const barNotification = getNotificationObject(
+ "bar",
+ "a4f1d54a-98b7-4231-9120-5afc26545bad",
+ "baz",
+ true
+);
+const msg = "Notification:GetAll";
+const msgReply = "Notification:GetAll:Return:OK";
+
+do_get_profile();
+const OLD_STORE_PATH = PathUtils.join(
+ PathUtils.profileDir,
+ "notificationstore.json"
+);
+
+let nextRequestID = 0;
+
+// Create the old datastore and populate it with data before we initialize
+// the notification database so it has data to migrate. This is a setup step,
+// not a test, but it seems like we need to do it in a test function
+// rather than in run_test() because the test runner doesn't handle async steps
+// in run_test().
+add_task(
+ {
+ skip_if: () => !AppConstants.MOZ_NEW_NOTIFICATION_STORE,
+ },
+ async function test_create_old_datastore() {
+ const notifications = {
+ [fooNotification.origin]: {
+ [fooNotification.id]: fooNotification,
+ },
+ [barNotification.origin]: {
+ [barNotification.id]: barNotification,
+ },
+ };
+
+ await IOUtils.writeJSON(OLD_STORE_PATH, notifications);
+
+ startNotificationDB();
+ }
+);
+
+add_test(
+ {
+ skip_if: () => !AppConstants.MOZ_NEW_NOTIFICATION_STORE,
+ },
+ function test_get_system_notification() {
+ const requestID = nextRequestID++;
+ const msgHandler = function (message) {
+ Assert.equal(requestID, message.data.requestID);
+ Assert.equal(0, message.data.notifications.length);
+ };
+
+ addAndSend(msg, msgReply, msgHandler, {
+ origin: systemNotification.origin,
+ requestID,
+ });
+ }
+);
+
+add_test(
+ {
+ skip_if: () => !AppConstants.MOZ_NEW_NOTIFICATION_STORE,
+ },
+ function test_get_foo_notification() {
+ const requestID = nextRequestID++;
+ const msgHandler = function (message) {
+ Assert.equal(requestID, message.data.requestID);
+ Assert.equal(1, message.data.notifications.length);
+ Assert.deepEqual(
+ fooNotification,
+ message.data.notifications[0],
+ "Notification data migrated"
+ );
+ };
+
+ addAndSend(msg, msgReply, msgHandler, {
+ origin: fooNotification.origin,
+ requestID,
+ });
+ }
+);
+
+add_test(
+ {
+ skip_if: () => !AppConstants.MOZ_NEW_NOTIFICATION_STORE,
+ },
+ function test_get_bar_notification() {
+ const requestID = nextRequestID++;
+ const msgHandler = function (message) {
+ Assert.equal(requestID, message.data.requestID);
+ Assert.equal(1, message.data.notifications.length);
+ Assert.deepEqual(
+ barNotification,
+ message.data.notifications[0],
+ "Notification data migrated"
+ );
+ };
+
+ addAndSend(msg, msgReply, msgHandler, {
+ origin: barNotification.origin,
+ requestID,
+ });
+ }
+);
+
+add_task(
+ {
+ skip_if: () => !AppConstants.MOZ_NEW_NOTIFICATION_STORE,
+ },
+ async function test_old_datastore_deleted() {
+ Assert.ok(
+ !(await IOUtils.exists(OLD_STORE_PATH)),
+ "old datastore no longer exists"
+ );
+ }
+);
diff --git a/dom/notification/test/unit/xpcshell.ini b/dom/notification/test/unit/xpcshell.ini
new file mode 100644
index 0000000000..7b43951aa1
--- /dev/null
+++ b/dom/notification/test/unit/xpcshell.ini
@@ -0,0 +1,7 @@
+[DEFAULT]
+head = head_notificationdb.js
+skip-if = toolkit == 'android'
+
+[test_notificationdb.js]
+[test_notificationdb_bug1024090.js]
+[test_notificationdb_migration.js]