Bug 1259148 - Test that the Notification.requestPermission callback is called upon dismissal. r=kitcambridge,wchen
MozReview-Commit-ID: 20XUizFRZvr
--- 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,146 @@
+"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();
+ }
+}
+
+/**
+ * Open a tab which calls Notification.requestPermission() with a callback
+ * argument and calls the `task` function when the permission prompt is open.
+ *
+ * @param {Function} task Task function to run to interact with the prompt.
+ * @param {String} finalPermission the value of the callback argument indicating
+ * the final permission.
+ * @param {Boolean} [expectingDefaultFirst = false] whether callbacks with
+ * "default" are expected before
+ * a final answer.
+ * @return {Promise} resolving when then task function is done and the tab
+ * closes. The value is the array of callback values.
+ */
+function* tabWithRequest(task, finalPermission, expectingDefaultFirst = false) {
+ Services.perms.remove(ORIGIN_URI, PERMISSION_NAME);
+
+ return BrowserTestUtils.withNewTab({
+ gBrowser,
+ url: TEST_URL,
+ }, function*(browser) {
+ let requestPromise = ContentTask.spawn(browser, {
+ expectingDefaultFirst,
+ finalPermission,
+ }, function*({expectingDefaultFirst, finalPermission}) {
+ function permHelper() {
+ return new Promise((resolve, reject) => {
+ let permsReceived = [];
+ content.window.Notification.requestPermission(function requestCallback(perm) {
+ info("received callback with perm: " + perm);
+ permsReceived.push(perm);
+
+ if (expectingDefaultFirst && perm == "default") {
+ return;
+ }
+
+ if (perm != finalPermission) {
+ reject(`Unexpected ${perm} received`);
+ return;
+ }
+
+ resolve(permsReceived);
+ });
+ });
+ }
+
+ let perms = yield permHelper();
+ return perms;
+ });
+
+ yield BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
+ yield task();
+ let perms = yield requestPromise;
+ return perms;
+ });
+}
+
+add_task(function* setup() {
+ SimpleTest.registerCleanupFunction(() => {
+ Services.perms.remove(ORIGIN_URI, PERMISSION_NAME);
+ });
+});
+
+add_task(function* test_requestPermission_granted() {
+ let perms = yield tabWithRequest(function*() {
+ clickDoorhangerButton(PROMPT_ALLOW_BUTTON);
+ }, "granted", true);
+
+ is(perms[0], "granted", "check granted");
+ is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+ Services.perms.ALLOW_ACTION,
+ "Check permission in perm. manager");
+});
+
+add_task(function* test_requestPermission_denied() {
+ let perms = yield tabWithRequest(function*() {
+ clickDoorhangerButton(PROMPT_BLOCK_BUTTON);
+ }, "denied", true);
+
+ is(perms[0], "denied", "check denied");
+ is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+ Services.perms.DENY_ACTION,
+ "Check permission in perm. manager");
+});
+
+add_task(function* test_requestPermission_minimize_granted() {
+ let perms = yield tabWithRequest(function*() {
+ PopupNotifications.panel.hidePopup();
+ PopupNotifications.getNotification("web-notifications").reshow();
+ yield BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
+ clickDoorhangerButton(PROMPT_ALLOW_BUTTON);
+ }, "granted", true);
+
+ is(perms[0], "default", "check default");
+ is(perms[1], "granted", "check granted");
+ is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+ Services.perms.ALLOW_ACTION,
+ "Check permission in perm. manager");
+});
+
+add_task(function* test_requestPermission_minimize_denied() {
+ let perms = yield tabWithRequest(function*() {
+ PopupNotifications.panel.hidePopup();
+ PopupNotifications.getNotification("web-notifications").reshow();
+ yield BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
+ clickDoorhangerButton(PROMPT_BLOCK_BUTTON);
+ }, "denied", true);
+
+ is(perms[0], "default", "check default");
+ is(perms[1], "denied", "check denied");
+ is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
+ Services.perms.DENY_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>