Bug 1190324 - Test coverage for notifications extension API, r?kmag draft
authorbsilverberg <bsilverberg@mozilla.com>
Tue, 29 Mar 2016 15:44:26 -0400
changeset 345578 7ce97832e03815c3f5dbe7fefcb301049b67aaf8
parent 345577 57f88cec6f638bfef1db4c2fe1025aca4a4d656c
child 345802 5c3b6c8cc60c5631b26e940a987f0910ec022645
push id14117
push userbmo:bob.silverberg@gmail.com
push dateTue, 29 Mar 2016 19:48:41 +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: WlWwXCCCAm
toolkit/components/extensions/test/mochitest/test_ext_notifications.html
--- a/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
@@ -7,87 +7,137 @@
   <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",
+      title: "Testing Notification",
+      message: "Carry on",
+    };
+
+    browser.notifications.create("", opts).then(id => {
+      browser.test.sendMessage("running", id);
+      browser.test.notifyPass("background test passed");
+    });
+  }
 
+  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");
+  yield extension.awaitFinish();
+  yield extension.unload();
+});
+
+add_task(function* test_notification_on_closed() {
+  function backgroundScript() {
     let opts = {
       type: "basic",
       title: "Testing Notification",
       message: "Carry on",
     };
 
-    // Test an unimplemented listener.
-    browser.notifications.onClicked.addListener(function() {});
+    // Test onClosed listener.
+    browser.notifications.onClosed.addListener(id => {
+      browser.test.sendMessage("closed", id);
+      browser.test.notifyPass("background test passed");
+    });
 
     browser.notifications.create("5", opts).then(id => {
+      return browser.notifications.create("5", opts);
+    }).then(id => {
       browser.test.sendMessage("running", id);
+    });
+  }
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: ["notifications"],
+    },
+    background: `(${backgroundScript})()`,
+  });
+  yield extension.startup();
+  let x = yield extension.awaitMessage("closed");
+  is(x, "5", "got correct id from onClosed listener");
+  x = yield extension.awaitMessage("running");
+  is(x, "5", "got correct id from notifications.create");
+  yield extension.awaitFinish();
+  yield extension.unload();
+});
+
+add_task(function* test_notification_clear() {
+  function backgroundScript() {
+    let opts = {
+      type: "basic",
+      title: "Testing Notification",
+      message: "Carry on",
+    };
+
+    browser.notifications.onClosed.addListener(id => {
+      browser.test.sendMessage("closed", id);
+    });
+
+    browser.notifications.create("99", opts).then(id => {
+      return browser.notifications.clear(id);
+    }).then(wasCleared => {
+      browser.test.sendMessage("cleared", wasCleared);
       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");
+  let x = yield extension.awaitMessage("closed");
+  is(x, "99", "got correct id from onClosed listener");
+  x = yield extension.awaitMessage("cleared");
+  is(x, true, "got correct boolean from notifications.clear");
   yield extension.awaitFinish();
-  info("test complete");
   yield extension.unload();
-  info("extension unloaded successfully");
 });
 
 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",
       title: "Testing Notification",
       message: "Carry on",
     };
 
     browser.notifications.create("p1", opts).then(() => {
       return browser.notifications.create("p2", opts);
@@ -101,29 +151,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>