Bug 1259148 - Test that dismissing the notification permission doorhanger resolves the promise with `default`. r=wchen
authorKit Cambridge <kcambridge@mozilla.com>
Wed, 20 Apr 2016 13:48:36 -0700
changeset 332192 8c72f449815b3969c8ac024b1659e9e4f26c9d72
parent 332191 6c3268c97a6cad35aabf98e0478f51302f3f5eb3
child 332193 0b75a2580c13ee93fcb46bee432fedbeeb59f9aa
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswchen
bugs1259148
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1259148 - Test that dismissing the notification permission doorhanger resolves the promise with `default`. r=wchen MozReview-Commit-ID: ASFqCCrKCQ9
dom/notification/moz.build
dom/notification/test/browser/browser.ini
dom/notification/test/browser/browser_permission_dismiss.js
dom/notification/test/browser/notification.html
--- a/dom/notification/moz.build
+++ b/dom/notification/moz.build
@@ -31,9 +31,10 @@ include('/ipc/chromium/chromium-config.m
 
 FINAL_LIBRARY = 'xul'
 LOCAL_INCLUDES += [
     '/dom/base',
     '/dom/ipc',
     '/dom/workers',
 ]
 
+BROWSER_CHROME_MANIFESTS += ['test/browser/browser.ini']
 XPCSHELL_TESTS_MANIFESTS += ['test/unit/xpcshell.ini']
new file mode 100644
--- /dev/null
+++ b/dom/notification/test/browser/browser.ini
@@ -0,0 +1,2 @@
+[browser_permission_dismiss.js]
+support-files = notification.html
new file mode 100644
--- /dev/null
+++ b/dom/notification/test/browser/browser_permission_dismiss.js
@@ -0,0 +1,113 @@
+"use strict";
+
+const ORIGIN_URI = Services.io.newURI("http://mochi.test:8888", null, null);
+const PERMISSION_NAME = "desktop-notification";
+const PROMPT_ALLOW_BUTTON = -1;
+const PROMPT_BLOCK_BUTTON = 0;
+const TEST_URL = "http://mochi.test:8888/browser/dom/notification/test/browser/notification.html";
+
+/**
+ * Clicks the specified web-notifications prompt button.
+ *
+ * @param {Number} aButtonIndex Number indicating which button to click.
+ *                              See the constants in this file.
+ * @note modified from toolkit/components/passwordmgr/test/browser/head.js
+ */
+function clickDoorhangerButton(aButtonIndex) {
+  ok(true, "Looking for action at index " + aButtonIndex);
+
+  let popup = PopupNotifications.getNotification("web-notifications");
+  let notifications = popup.owner.panel.childNodes;
+  ok(notifications.length > 0, "at least one notification displayed");
+  ok(true, notifications.length + " notification(s)");
+  let notification = notifications[0];
+
+  if (aButtonIndex == -1) {
+    ok(true, "Triggering main action");
+    notification.button.doCommand();
+  } else if (aButtonIndex <= popup.secondaryActions.length) {
+    ok(true, "Triggering secondary action " + aButtonIndex);
+    notification.childNodes[aButtonIndex].doCommand();
+  }
+}
+
+/**
+ * Opens a tab which calls `Notification.requestPermission()` with a callback
+ * argument, calls the `task` function while the permission prompt is open,
+ * and verifies that the expected permission is set.
+ *
+ * @param {Function} task Task function to run to interact with the prompt.
+ * @param {String} permission Expected permission value.
+ * @return {Promise} resolving when the task function is done and the tab
+ *                   closes.
+ */
+function tabWithRequest(task, permission) {
+  Services.perms.remove(ORIGIN_URI, PERMISSION_NAME);
+
+  return BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: TEST_URL,
+  }, function*(browser) {
+    let requestPromise = ContentTask.spawn(browser, {
+      permission
+    }, function*({permission}) {
+      function requestCallback(perm) {
+        is(perm, permission,
+          "Should call the legacy callback with the permission state");
+      }
+      let perm = yield content.window.Notification
+                              .requestPermission(requestCallback);
+      is(perm, permission,
+         "Should resolve the promise with the permission state");
+    });
+
+    yield BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
+    yield task();
+    yield requestPromise;
+  });
+}
+
+add_task(function* setup() {
+  SimpleTest.registerCleanupFunction(() => {
+    Services.perms.remove(ORIGIN_URI, PERMISSION_NAME);
+  });
+});
+
+add_task(function* test_requestPermission_granted() {
+  yield tabWithRequest(function() {
+    clickDoorhangerButton(PROMPT_ALLOW_BUTTON);
+  }, "granted");
+
+  ok(!PopupNotifications.getNotification("web-notifications"),
+     "Should remove the doorhanger notification icon if granted");
+
+  is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+     Services.perms.ALLOW_ACTION,
+     "Check permission in perm. manager");
+});
+
+add_task(function* test_requestPermission_denied() {
+  yield tabWithRequest(function() {
+    clickDoorhangerButton(PROMPT_BLOCK_BUTTON);
+  }, "denied");
+
+  ok(!PopupNotifications.getNotification("web-notifications"),
+     "Should remove the doorhanger notification icon if denied");
+
+  is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+     Services.perms.DENY_ACTION,
+     "Check permission in perm. manager");
+});
+
+add_task(function* test_requestPermission_dismissed() {
+  yield tabWithRequest(function() {
+    PopupNotifications.panel.hidePopup();
+  }, "default");
+
+  ok(!PopupNotifications.getNotification("web-notifications"),
+     "Should remove the doorhanger notification icon if dismissed");
+
+  is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+     Services.perms.UNKNOWN_ACTION,
+     "Check permission in perm. manager");
+});
new file mode 100644
--- /dev/null
+++ b/dom/notification/test/browser/notification.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta charset="utf-8">
+    <title>Notifications test</title>
+  </head>
+
+  <body>
+
+  </body>
+</html>