summaryrefslogtreecommitdiffstats
path: root/dom/notification/test/browser/browser_permission_dismiss.js
blob: de655870bdcf521898464cfb673912133daed00e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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");
});