Bug 1352711 - Add a notifications.onShown event and use it to fix an intermittent in test_ext_notifications.html, r?mixedpuppy draft
authorBob Silverberg <bsilverberg@mozilla.com>
Wed, 02 Aug 2017 04:44:10 -0400
changeset 619586 3ba1b2ce83ace38d015bde079c05cc17ce1a4fe4
parent 619065 ef9a0f01e4f68214f0ff8f4631783b8a0e075a82
child 640447 32c8e88adf1bef4ba8ccbde33205e735eba1ddd4
push id71731
push userbmo:bob.silverberg@gmail.com
push dateWed, 02 Aug 2017 08:45:26 +0000
reviewersmixedpuppy
bugs1352711
milestone56.0a1
Bug 1352711 - Add a notifications.onShown event and use it to fix an intermittent in test_ext_notifications.html, r?mixedpuppy MozReview-Commit-ID: GC9poUR4jnu
toolkit/components/extensions/ext-notifications.js
toolkit/components/extensions/schemas/notifications.json
toolkit/components/extensions/test/mochitest/test_ext_notifications.html
--- a/toolkit/components/extensions/ext-notifications.js
+++ b/toolkit/components/extensions/ext-notifications.js
@@ -57,21 +57,26 @@ Notification.prototype = {
       notifications.delete(this.id);
     };
 
     // Don't try to emit events if the extension has been unloaded
     if (!notifications) {
       return;
     }
 
-    if (topic === "alertclickcallback") {
-      emitAndDelete("clicked");
-    }
-    if (topic === "alertfinished") {
-      emitAndDelete("closed");
+    switch (topic) {
+      case "alertclickcallback":
+        emitAndDelete("clicked");
+        break;
+      case "alertfinished":
+        emitAndDelete("closed");
+        break;
+      case "alertshow":
+        notifications.emit("shown", data);
+        break;
     }
   },
 };
 
 this.notifications = class extends ExtensionAPI {
   constructor(extension) {
     super(extension);
 
@@ -151,14 +156,25 @@ this.notifications = class extends Exten
           };
 
           notificationsMap.get(extension).on("clicked", listener);
           return () => {
             notificationsMap.get(extension).off("clicked", listener);
           };
         }).api(),
 
+        onShown: new EventManager(context, "notifications.onShown", fire => {
+          let listener = (event, notificationId) => {
+            fire.async(notificationId, true);
+          };
+
+          notificationsMap.get(extension).on("shown", listener);
+          return () => {
+            notificationsMap.get(extension).off("shown", listener);
+          };
+        }).api(),
+
         // Intend to implement this later: https://bugzilla.mozilla.org/show_bug.cgi?id=1190681
         onButtonClicked: ignoreEvent(context, "notifications.onButtonClicked"),
       },
     };
   }
 };
--- a/toolkit/components/extensions/schemas/notifications.json
+++ b/toolkit/components/extensions/schemas/notifications.json
@@ -406,12 +406,24 @@
       },
       {
         "name": "onShowSettings",
         "unsupported": true,
         "type": "function",
         "description": "Fired when the user clicked on a link for the app's notification settings.",
         "parameters": [
         ]
+      },
+      {
+        "name": "onShown",
+        "type": "function",
+        "description": "Fired when the notification is shown.",
+        "parameters": [
+          {
+            "type": "string",
+            "name": "notificationId",
+            "description": "The notificationId of the shown notification."
+          }
+        ]
       }
     ]
   }
 ]
--- a/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
@@ -82,46 +82,46 @@ add_task(async function test_notificatio
   is(x, "5", "got correct id from onClosed listener");
   x = await extension.awaitMessage("running");
   is(x, "5", "got correct id from notifications.create");
   await extension.awaitFinish();
   await extension.unload();
 });
 
 add_task(async function test_notification_clear() {
-  async function background() {
+  function background() {
     let opts = {
       type: "basic",
       title: "Testing Notification",
       message: "Carry on",
     };
 
-    browser.notifications.onClosed.addListener(id => {
-      browser.test.sendMessage("closed", id);
+    let createdId = "99";
+
+    browser.notifications.onShown.addListener(async id => {
+      browser.test.assertEq(createdId, id, "onShown received the expected id.");
+      let wasCleared = await browser.notifications.clear(id);
+      browser.test.assertTrue(wasCleared, "notifications.clear returned true.");
     });
 
-    let id = await browser.notifications.create("99", opts);
+    browser.notifications.onClosed.addListener(id => {
+      browser.test.assertEq(createdId, id, "onClosed received the expected id.");
+      browser.test.notifyPass("background test passed");
+    });
 
-    let wasCleared = await browser.notifications.clear(id);
-    browser.test.sendMessage("cleared", wasCleared);
-
-    browser.test.notifyPass("background test passed");
+    browser.notifications.create(createdId, opts);
   }
 
   let extension = ExtensionTestUtils.loadExtension({
     manifest: {
       permissions: ["notifications"],
     },
     background,
   });
   await extension.startup();
-  let x = await extension.awaitMessage("closed");
-  is(x, "99", "got correct id from onClosed listener");
-  x = await extension.awaitMessage("cleared");
-  is(x, true, "got correct boolean from notifications.clear");
   await extension.awaitFinish();
   await extension.unload();
 });
 
 add_task(async function test_notifications_empty_getAll() {
   async function background() {
     let notifications = await browser.notifications.getAll();