summaryrefslogtreecommitdiffstats
path: root/dom/notification/test/browser/browser_permission_dismiss.js
blob: 72b2db28270e7a7f1d1338593f49860756bf858a (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"use strict";

const { PermissionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PermissionTestUtils.sys.mjs"
);

const ORIGIN_URI = Services.io.newURI("https://example.com");
const PERMISSION_NAME = "desktop-notification";
const PROMPT_ALLOW_BUTTON = -1;
const PROMPT_NOT_NOW_BUTTON = 0;
const PROMPT_NEVER_BUTTON = 1;
const TEST_URL =
  "https://example.com/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, browser) {
  let popup = PopupNotifications.getNotification("web-notifications", browser);
  let notifications = popup.owner.panel.childNodes;
  ok(notifications.length, "at least one notification displayed");
  ok(true, notifications.length + " notification(s)");
  let notification = notifications[0];

  if (aButtonIndex == PROMPT_ALLOW_BUTTON) {
    ok(true, "Triggering main action (allow the permission)");
    notification.button.doCommand();
  } else if (aButtonIndex == PROMPT_NEVER_BUTTON) {
    ok(true, "Triggering secondary action (deny the permission permanently)");
    notification.menupopup.querySelector("menuitem").doCommand();
  } else {
    ok(true, "Triggering secondary action (deny the permission temporarily)");
    notification.secondaryButton.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,
  browser = window.gBrowser,
  privateWindow = false
) {
  clearPermission(ORIGIN_URI, PERMISSION_NAME, privateWindow);

  return BrowserTestUtils.withNewTab(
    {
      gBrowser: browser,
      url: TEST_URL,
    },
    async function (linkedBrowser) {
      let requestPromise = SpecialPowers.spawn(
        linkedBrowser,
        [
          {
            permission,
          },
        ],
        async function ({ permission }) {
          function requestCallback(perm) {
            is(
              perm,
              permission,
              "Should call the legacy callback with the permission state"
            );
          }
          let perm = await content.window.Notification.requestPermission(
            requestCallback
          );
          is(
            perm,
            permission,
            "Should resolve the promise with the permission state"
          );
        }
      );

      await task(linkedBrowser);
      await requestPromise;
    }
  );
}

function clearPermission(origin, permissionName, isPrivate) {
  let principal = Services.scriptSecurityManager.createContentPrincipal(
    origin,
    isPrivate ? { privateBrowsingId: 1 } : {} /* attrs */
  );
  PermissionTestUtils.remove(principal, permissionName);
}

add_setup(async function () {
  Services.prefs.setBoolPref(
    "dom.webnotifications.requireuserinteraction",
    false
  );
  Services.prefs.setBoolPref(
    "permissions.desktop-notification.notNow.enabled",
    true
  );
  SimpleTest.registerCleanupFunction(() => {
    Services.prefs.clearUserPref("dom.webnotifications.requireuserinteraction");
    Services.prefs.clearUserPref(
      "permissions.desktop-notification.notNow.enabled"
    );

    clearPermission(ORIGIN_URI, PERMISSION_NAME, false /* private origin */);
    clearPermission(ORIGIN_URI, PERMISSION_NAME, true /* private origin */);
  });
});

add_task(async function test_requestPermission_granted() {
  await tabWithRequest(async function (linkedBrowser) {
    await BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
    clickDoorhangerButton(PROMPT_ALLOW_BUTTON, linkedBrowser);
  }, "granted");

  ok(
    !PopupNotifications.getNotification("web-notifications"),
    "Should remove the doorhanger notification icon if granted"
  );

  is(
    PermissionTestUtils.testPermission(ORIGIN_URI, PERMISSION_NAME),
    Services.perms.ALLOW_ACTION,
    "Check permission in perm. manager"
  );
});

add_task(async function test_requestPermission_denied_temporarily() {
  await tabWithRequest(async function (linkedBrowser) {
    await BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
    clickDoorhangerButton(PROMPT_NOT_NOW_BUTTON, linkedBrowser);
  }, "default");

  ok(
    !PopupNotifications.getNotification("web-notifications"),
    "Should remove the doorhanger notification icon if denied"
  );

  is(
    PermissionTestUtils.testPermission(ORIGIN_URI, PERMISSION_NAME),
    Services.perms.UNKNOWN_ACTION,
    "Check permission in perm. manager"
  );
});

add_task(async function test_requestPermission_denied_permanently() {
  await tabWithRequest(async function (linkedBrowser) {
    await BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
    clickDoorhangerButton(PROMPT_NEVER_BUTTON, linkedBrowser);
  }, "denied");

  ok(
    !PopupNotifications.getNotification("web-notifications"),
    "Should remove the doorhanger notification icon if denied"
  );

  is(
    PermissionTestUtils.testPermission(ORIGIN_URI, PERMISSION_NAME),
    Services.perms.DENY_ACTION,
    "Check permission in perm. manager"
  );
});

add_task(
  async function test_requestPermission_defaultPrivateNotificationsPref() {
    ok(
      !SpecialPowers.getBoolPref(
        "dom.webnotifications.privateBrowsing.enableDespiteLimitations"
      ),
      "Pref should be default disabled"
    );
  }
);

add_task(async function test_requestPermission_privateNotifications() {
  async function run(perm) {
    let privateWindow = await BrowserTestUtils.openNewBrowserWindow({
      private: true,
    });

    await tabWithRequest(
      async linkedBrowser => {
        if (perm != Services.perms.UNKNOWN_ACTION) {
          await BrowserTestUtils.waitForEvent(
            privateWindow.PopupNotifications.panel,
            "popupshown"
          );

          clickDoorhangerButton(PROMPT_ALLOW_BUTTON, linkedBrowser);
        }
      },
      perm == Services.perms.ALLOW_ACTION ? "granted" : "denied",
      privateWindow.gBrowser,
      true /* privateWindow */
    );

    ok(
      !PopupNotifications.getNotification(
        "web-notifications",
        privateWindow.gBrowser
      ),
      "doorhanger should have been removed in all cases by now"
    );

    await BrowserTestUtils.closeWindow(privateWindow);
  }

  await run(Services.perms.UNKNOWN_ACTION);

  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.webnotifications.privateBrowsing.enableDespiteLimitations", true],
    ],
  });
  await run(Services.perms.ALLOW_ACTION);
});