Bug 1190324 - Test coverage for notifications extension API, r?kmag draft
authorbsilverberg <bsilverberg@mozilla.com>
Tue, 15 Mar 2016 10:51:24 +0100
changeset 340467 da722c88094afb123d64d7ad8e576fbe8daea7bd
parent 340466 f1c8095df774bf8718eb447d964d76275ca23bd6
child 340528 905abe592025ce9c85055e31347d7f557288f840
push id12971
push userbmo:bob.silverberg@gmail.com
push dateTue, 15 Mar 2016 10:57:24 +0000
reviewerskmag
bugs1190324
milestone48.0a1
Bug 1190324 - Test coverage for notifications extension API, r?kmag Add coverage: * Notification observers being fired with the "alertfinished" topic. * The |create| API method without a notification ID. * The |clear| API method. * The |onClosed| event. MozReview-Commit-ID: 1bJrfF30A7S
toolkit/components/extensions/ext-notifications.js
toolkit/components/extensions/test/mochitest/test_ext_notifications.html
--- a/toolkit/components/extensions/ext-notifications.js
+++ b/toolkit/components/extensions/ext-notifications.js
@@ -81,16 +81,17 @@ extensions.on("shutdown", (type, extensi
 var nextId = 0;
 
 extensions.registerSchemaAPI("notifications", "notifications", (extension, context) => {
   return {
     notifications: {
       create: function(notificationId, options) {
         if (!notificationId) {
           notificationId = nextId++;
+          notificationId = notificationId.toString();
         }
 
         // FIXME: Lots of options still aren't supported, especially
         // buttons.
         let notification = new Notification(extension, notificationId, options);
         notificationsMap.get(extension).set(notificationId, notification);
 
         return Promise.resolve(notificationId);
--- a/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
@@ -7,88 +7,106 @@
   <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 
 <script type="text/javascript">
 "use strict";
 
-add_task(function* test_notifications() {
+add_task(function* test_notification() {
   function backgroundScript() {
-    browser.test.log("running background script");
-
     let opts = {
       type: "basic",
       iconUrl: browser.runtime.getURL("data/a.png"),
       title: "Testing Notification",
       message: "Carry on",
     };
 
     // Test an unimplemented listener.
     browser.notifications.onClicked.addListener(function() {});
 
     browser.notifications.create("5", opts).then(id => {
       browser.test.sendMessage("running", id);
       browser.test.notifyPass("background test passed");
     });
   }
 
-  let extensionData = {
+  let extension = ExtensionTestUtils.loadExtension({
     manifest: {
       permissions: ["notifications"],
     },
-    background: "(" + backgroundScript.toString() + ")()",
-  };
-
-  let extension = ExtensionTestUtils.loadExtension(extensionData);
-  info("load complete");
+    background: `(${backgroundScript})()`,
+  });
   yield extension.startup();
-  info("startup complete");
   let x = yield extension.awaitMessage("running");
-  is(x, "5", "got correct value from extension");
+  is(x, "5", "got correct id from notifications.create");
   yield extension.awaitFinish();
-  info("test complete");
   yield extension.unload();
-  info("extension unloaded successfully");
+});
+
+add_task(function* test_notification_on_closed() {
+  function backgroundScript() {
+    let opts = {
+      type: "basic",
+      iconUrl: browser.runtime.getURL("data/a.png"),
+      title: "Testing Notification",
+      message: "Carry on",
+    };
+
+    // Test onClosed listener.
+    browser.notifications.onClosed.addListener(id => {
+      browser.test.sendMessage("closed", id);
+      browser.test.notifyPass("background test passed");
+    });
+
+    browser.notifications.create("", opts).then(id => {
+      browser.test.sendMessage("running", id);
+      browser.notifications.clear(id);
+    });
+  }
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: ["notifications"],
+    },
+    background: `(${backgroundScript})()`,
+  });
+  yield extension.startup();
+  let x = yield extension.awaitMessage("running");
+  is(x, "0", "got correct id from notifications.create");
+  x = yield extension.awaitMessage("closed");
+  is(x, "0", "got correct id from onClosed listener");
+  yield extension.awaitFinish();
+  yield extension.unload();
 });
 
 add_task(function* test_notifications_empty_getAll() {
   function backgroundScript() {
-    browser.test.log("running background script");
-
     browser.notifications.getAll().then(notifications => {
       browser.test.assertTrue(Array.isArray(notifications),
         "getAll() returned an array");
       browser.test.assertEq(notifications.length, 0, "the array was empty");
       browser.test.notifyPass("getAll empty");
     });
   }
 
-  let extensionData = {
+  let extension = ExtensionTestUtils.loadExtension({
     manifest: {
       permissions: ["notifications"],
     },
-    background: "(" + backgroundScript.toString() + ")()",
-  };
-
-  let extension = ExtensionTestUtils.loadExtension(extensionData);
-  info("load complete");
+    background: `(${backgroundScript})()`,
+  });
   yield extension.startup();
-  info("startup complete");
   yield extension.awaitFinish("getAll empty");
-  info("test complete");
   yield extension.unload();
-  info("extension unloaded successfully");
 });
 
 add_task(function* test_notifications_populated_getAll() {
   function backgroundScript() {
-    browser.test.log("running background script");
-
     let opts = {
       type: "basic",
       iconUrl: browser.runtime.getURL("data/a.png"),
       title: "Testing Notification",
       message: "Carry on",
     };
 
     browser.notifications.create("p1", opts).then(() => {
@@ -103,29 +121,23 @@ add_task(function* test_notifications_po
       browser.test.assertTrue(notifications.includes("p1"),
         "the array contains the first notification");
       browser.test.assertTrue(notifications.includes("p2"),
         "the array contains the second notification");
       browser.test.notifyPass("getAll populated");
     });
   }
 
-  let extensionData = {
+  let extension = ExtensionTestUtils.loadExtension({
     manifest: {
       permissions: ["notifications"],
     },
-    background: "(" + backgroundScript.toString() + ")()",
-  };
-
-  let extension = ExtensionTestUtils.loadExtension(extensionData);
-  info("load complete");
+    background: `(${backgroundScript})()`,
+  });
   yield extension.startup();
-  info("startup complete");
   yield extension.awaitFinish("getAll populated");
-  info("test complete");
   yield extension.unload();
-  info("extension unloaded successfully");
 });
 
 </script>
 
 </body>
 </html>