Bug 1190324 - Test coverage for notifications extension API, r=kmag
authorbsilverberg <bsilverberg@mozilla.com>
Tue, 29 Mar 2016 15:44:26 -0400
changeset 291358 5851bac416b3be028afc426f6f8f7032c89aa2fb
parent 291357 adc196371da9a14d6b805054ad7278e258a3df33
child 291359 6934dc0c95b8657235da70eb0a37a511998944be
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
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>