tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_permission_dismiss.js (6889B)


      1 "use strict";
      2 
      3 const { PermissionTestUtils } = ChromeUtils.importESModule(
      4  "resource://testing-common/PermissionTestUtils.sys.mjs"
      5 );
      6 
      7 const ORIGIN_URI = Services.io.newURI("https://example.com");
      8 const PERMISSION_NAME = "desktop-notification";
      9 const PROMPT_ALLOW_BUTTON = -1;
     10 const PROMPT_NOT_NOW_BUTTON = 0;
     11 const PROMPT_NEVER_BUTTON = 1;
     12 const TEST_URL =
     13  "https://example.com/browser/dom/notification/test/browser/notification.html";
     14 
     15 /**
     16 * Clicks the specified web-notifications prompt button.
     17 *
     18 * @param {number} aButtonIndex Number indicating which button to click.
     19 *                              See the constants in this file.
     20 * Note: modified from toolkit/components/passwordmgr/test/browser/head.js
     21 */
     22 function clickDoorhangerButton(aButtonIndex, browser) {
     23  let popup = PopupNotifications.getNotification("web-notifications", browser);
     24  let notifications = popup.owner.panel.childNodes;
     25  ok(notifications.length, "at least one notification displayed");
     26  ok(true, notifications.length + " notification(s)");
     27  let notification = notifications[0];
     28 
     29  if (aButtonIndex == PROMPT_ALLOW_BUTTON) {
     30    ok(true, "Triggering main action (allow the permission)");
     31    notification.button.doCommand();
     32  } else if (aButtonIndex == PROMPT_NEVER_BUTTON) {
     33    ok(true, "Triggering secondary action (deny the permission permanently)");
     34    notification.menupopup.querySelector("menuitem").doCommand();
     35  } else {
     36    ok(true, "Triggering secondary action (deny the permission temporarily)");
     37    notification.secondaryButton.doCommand();
     38  }
     39 }
     40 
     41 /**
     42 * Opens a tab which calls `Notification.requestPermission()` with a callback
     43 * argument, calls the `task` function while the permission prompt is open,
     44 * and verifies that the expected permission is set.
     45 *
     46 * @param {Function} task Task function to run to interact with the prompt.
     47 * @param {string} permission Expected permission value.
     48 * @return {Promise} resolving when the task function is done and the tab
     49 *                   closes.
     50 */
     51 function tabWithRequest(
     52  task,
     53  permission,
     54  browser = window.gBrowser,
     55  privateWindow = false
     56 ) {
     57  clearPermission(ORIGIN_URI, PERMISSION_NAME, privateWindow);
     58 
     59  return BrowserTestUtils.withNewTab(
     60    {
     61      gBrowser: browser,
     62      url: TEST_URL,
     63    },
     64    async function (linkedBrowser) {
     65      let requestPromise = SpecialPowers.spawn(
     66        linkedBrowser,
     67        [
     68          {
     69            permission,
     70          },
     71        ],
     72        async function ({ permission }) {
     73          function requestCallback(perm) {
     74            is(
     75              perm,
     76              permission,
     77              "Should call the legacy callback with the permission state"
     78            );
     79          }
     80          let perm =
     81            await content.window.Notification.requestPermission(
     82              requestCallback
     83            );
     84          is(
     85            perm,
     86            permission,
     87            "Should resolve the promise with the permission state"
     88          );
     89        }
     90      );
     91 
     92      await task(linkedBrowser);
     93      await requestPromise;
     94    }
     95  );
     96 }
     97 
     98 function clearPermission(origin, permissionName, isPrivate) {
     99  let principal = Services.scriptSecurityManager.createContentPrincipal(
    100    origin,
    101    isPrivate ? { privateBrowsingId: 1 } : {} /* attrs */
    102  );
    103  PermissionTestUtils.remove(principal, permissionName);
    104 }
    105 
    106 add_setup(async function () {
    107  Services.prefs.setBoolPref(
    108    "dom.webnotifications.requireuserinteraction",
    109    false
    110  );
    111  Services.prefs.setBoolPref(
    112    "permissions.desktop-notification.notNow.enabled",
    113    true
    114  );
    115  SimpleTest.registerCleanupFunction(() => {
    116    Services.prefs.clearUserPref("dom.webnotifications.requireuserinteraction");
    117    Services.prefs.clearUserPref(
    118      "permissions.desktop-notification.notNow.enabled"
    119    );
    120 
    121    clearPermission(ORIGIN_URI, PERMISSION_NAME, false /* private origin */);
    122    clearPermission(ORIGIN_URI, PERMISSION_NAME, true /* private origin */);
    123  });
    124 });
    125 
    126 add_task(async function test_requestPermission_granted() {
    127  await tabWithRequest(async function (linkedBrowser) {
    128    await BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
    129    clickDoorhangerButton(PROMPT_ALLOW_BUTTON, linkedBrowser);
    130  }, "granted");
    131 
    132  ok(
    133    !PopupNotifications.getNotification("web-notifications"),
    134    "Should remove the doorhanger notification icon if granted"
    135  );
    136 
    137  is(
    138    PermissionTestUtils.testPermission(ORIGIN_URI, PERMISSION_NAME),
    139    Services.perms.ALLOW_ACTION,
    140    "Check permission in perm. manager"
    141  );
    142 });
    143 
    144 add_task(async function test_requestPermission_denied_temporarily() {
    145  await tabWithRequest(async function (linkedBrowser) {
    146    await BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
    147    clickDoorhangerButton(PROMPT_NOT_NOW_BUTTON, linkedBrowser);
    148  }, "default");
    149 
    150  ok(
    151    !PopupNotifications.getNotification("web-notifications"),
    152    "Should remove the doorhanger notification icon if denied"
    153  );
    154 
    155  is(
    156    PermissionTestUtils.testPermission(ORIGIN_URI, PERMISSION_NAME),
    157    Services.perms.UNKNOWN_ACTION,
    158    "Check permission in perm. manager"
    159  );
    160 });
    161 
    162 add_task(async function test_requestPermission_denied_permanently() {
    163  await tabWithRequest(async function (linkedBrowser) {
    164    await BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
    165    clickDoorhangerButton(PROMPT_NEVER_BUTTON, linkedBrowser);
    166  }, "denied");
    167 
    168  ok(
    169    !PopupNotifications.getNotification("web-notifications"),
    170    "Should remove the doorhanger notification icon if denied"
    171  );
    172 
    173  is(
    174    PermissionTestUtils.testPermission(ORIGIN_URI, PERMISSION_NAME),
    175    Services.perms.DENY_ACTION,
    176    "Check permission in perm. manager"
    177  );
    178 });
    179 
    180 add_task(
    181  async function test_requestPermission_defaultPrivateNotificationsPref() {
    182    ok(
    183      SpecialPowers.getBoolPref("dom.webnotifications.privateBrowsing.enabled"),
    184      "Pref should be default enabled"
    185    );
    186  }
    187 );
    188 
    189 add_task(async function test_requestPermission_privateNotifications() {
    190  async function run(perm) {
    191    let privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    192      private: true,
    193    });
    194 
    195    await tabWithRequest(
    196      async linkedBrowser => {
    197        if (perm != Services.perms.UNKNOWN_ACTION) {
    198          await BrowserTestUtils.waitForEvent(
    199            privateWindow.PopupNotifications.panel,
    200            "popupshown"
    201          );
    202 
    203          clickDoorhangerButton(PROMPT_ALLOW_BUTTON, linkedBrowser);
    204        }
    205      },
    206      perm == Services.perms.ALLOW_ACTION ? "granted" : "denied",
    207      privateWindow.gBrowser,
    208      true /* privateWindow */
    209    );
    210 
    211    ok(
    212      !PopupNotifications.getNotification(
    213        "web-notifications",
    214        privateWindow.gBrowser
    215      ),
    216      "doorhanger should have been removed in all cases by now"
    217    );
    218 
    219    await BrowserTestUtils.closeWindow(privateWindow);
    220  }
    221 
    222  await run(Services.perms.ALLOW_ACTION);
    223 });