tor-browser

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

browser_popup_blocker_identity_block.js (8164B)


      1 "use strict";
      2 
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 const { PermissionTestUtils } = ChromeUtils.importESModule(
      8  "resource://testing-common/PermissionTestUtils.sys.mjs"
      9 );
     10 
     11 const baseURL = getRootDirectory(gTestPath).replace(
     12  "chrome://mochitests/content",
     13  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     14  "http://example.com"
     15 );
     16 const URL = baseURL + "popup_blocker2.html";
     17 const URI = Services.io.newURI(URL);
     18 const PRINCIPAL = Services.scriptSecurityManager.createContentPrincipal(
     19  URI,
     20  {}
     21 );
     22 
     23 function openPermissionPopup() {
     24  let promise = BrowserTestUtils.waitForEvent(
     25    window,
     26    "popupshown",
     27    true,
     28    event => event.target == gPermissionPanel._permissionPopup
     29  );
     30  gPermissionPanel._identityPermissionBox.click();
     31  return promise;
     32 }
     33 
     34 function closePermissionPopup() {
     35  let promise = BrowserTestUtils.waitForEvent(
     36    gPermissionPanel._permissionPopup,
     37    "popuphidden"
     38  );
     39  gPermissionPanel._permissionPopup.hidePopup();
     40  return promise;
     41 }
     42 
     43 add_task(async function enable_popup_blocker() {
     44  // Enable popup blocker.
     45  await SpecialPowers.pushPrefEnv({
     46    set: [["dom.disable_open_during_load", true]],
     47  });
     48  await SpecialPowers.pushPrefEnv({
     49    set: [["dom.disable_open_click_delay", 0]],
     50  });
     51 });
     52 
     53 add_task(async function check_blocked_popup_indicator() {
     54  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);
     55 
     56  // Blocked popup indicator should not exist in the identity popup when there are no blocked popups.
     57  await openPermissionPopup();
     58  Assert.equal(document.getElementById("blocked-popup-indicator-item"), null);
     59  await closePermissionPopup();
     60 
     61  // Blocked popup notification icon should be hidden in the identity block when no popups are blocked.
     62  let icon = gPermissionPanel._identityPermissionBox.querySelector(
     63    ".blocked-permission-icon[data-permission-id='popup']"
     64  );
     65  Assert.equal(icon.hasAttribute("showing"), false);
     66 
     67  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
     68    let open = content.document.getElementById("pop");
     69    open.click();
     70  });
     71 
     72  // Wait for popup block.
     73  await TestUtils.waitForCondition(() =>
     74    gBrowser.getNotificationBox().getNotificationWithValue("popup-blocked")
     75  );
     76 
     77  // Check if blocked popup indicator text is visible in the identity popup. It should be visible.
     78  document.getElementById("identity-permission-box").click();
     79  await openPermissionPopup();
     80  await TestUtils.waitForCondition(
     81    () => document.getElementById("blocked-popup-indicator-item") !== null
     82  );
     83 
     84  // Check that the default state is correctly set to "Block".
     85  let menulist = document.getElementById("permission-popup-menulist");
     86  Assert.equal(menulist.value, "0");
     87  Assert.equal(menulist.label, "Block");
     88 
     89  await closePermissionPopup();
     90 
     91  // Check if blocked popup icon is visible in the identity block.
     92  Assert.equal(icon.getAttribute("showing"), "true");
     93 
     94  gBrowser.removeTab(tab);
     95 });
     96 
     97 // Check if clicking on "Show blocked popups" shows blocked popups.
     98 add_task(async function check_popup_showing() {
     99  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);
    100 
    101  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
    102    let open = content.document.getElementById("pop");
    103    open.click();
    104  });
    105 
    106  // Wait for popup block.
    107  await TestUtils.waitForCondition(() =>
    108    gBrowser.getNotificationBox().getNotificationWithValue("popup-blocked")
    109  );
    110 
    111  // Store the popup that opens in this array.
    112  let popup;
    113  function onTabOpen(event) {
    114    popup = event.target;
    115  }
    116  gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen);
    117 
    118  // Open identity popup and click on "Show blocked popups".
    119  await openPermissionPopup();
    120  let e = document.getElementById("blocked-popup-indicator-item");
    121  let text = e.getElementsByTagName("label")[0];
    122  text.click();
    123 
    124  await BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen");
    125  await TestUtils.waitForCondition(
    126    () => popup.linkedBrowser.currentURI.spec != "about:blank"
    127  );
    128 
    129  gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen);
    130 
    131  ok(
    132    popup.linkedBrowser.currentURI.spec.endsWith("popup_blocker_a.html"),
    133    "Popup a"
    134  );
    135 
    136  gBrowser.removeTab(popup);
    137  gBrowser.removeTab(tab);
    138 });
    139 
    140 // Test if changing menulist values of blocked popup indicator changes permission state and popup behavior.
    141 add_task(async function check_permission_state_change() {
    142  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);
    143 
    144  // Initially the permission state is BLOCK for popups (set by the prefs).
    145  let state = SitePermissions.getForPrincipal(
    146    PRINCIPAL,
    147    "popup",
    148    gBrowser
    149  ).state;
    150  Assert.equal(state, SitePermissions.BLOCK);
    151 
    152  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
    153    let open = content.document.getElementById("pop");
    154    open.click();
    155  });
    156 
    157  // Wait for popup block.
    158  await TestUtils.waitForCondition(() =>
    159    gBrowser.getNotificationBox().getNotificationWithValue("popup-blocked")
    160  );
    161 
    162  // Open identity popup and change permission state to allow.
    163  await openPermissionPopup();
    164  let menulist = document.getElementById("permission-popup-menulist");
    165  menulist.menupopup.openPopup(); // Open the allow/block menu
    166  let menuitem = menulist.getElementsByTagName("menuitem")[0];
    167  menuitem.click();
    168  await closePermissionPopup();
    169 
    170  state = SitePermissions.getForPrincipal(PRINCIPAL, "popup", gBrowser).state;
    171  Assert.equal(state, SitePermissions.ALLOW);
    172 
    173  // Store the popup that opens in this array.
    174  let popup;
    175  function onTabOpen(event) {
    176    popup = event.target;
    177  }
    178  gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen);
    179 
    180  // Check if a popup opens.
    181  await Promise.all([
    182    SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    183      let open = content.document.getElementById("pop");
    184      open.click();
    185    }),
    186    BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen"),
    187  ]);
    188  await TestUtils.waitForCondition(
    189    () => popup.linkedBrowser.currentURI.spec != "about:blank"
    190  );
    191 
    192  gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen);
    193 
    194  ok(
    195    popup.linkedBrowser.currentURI.spec.endsWith("popup_blocker_a.html"),
    196    "Popup a"
    197  );
    198 
    199  gBrowser.removeTab(popup);
    200 
    201  // Open identity popup and change permission state to block.
    202  await openPermissionPopup();
    203  menulist = document.getElementById("permission-popup-menulist");
    204  menulist.menupopup.openPopup(); // Open the allow/block menu
    205  menuitem = menulist.getElementsByTagName("menuitem")[1];
    206  menuitem.click();
    207  await closePermissionPopup();
    208 
    209  // Clicking on the "Block" menuitem should remove the permission object(same behavior as UNKNOWN state).
    210  // We have already confirmed that popups are blocked when the permission state is BLOCK.
    211  state = SitePermissions.getForPrincipal(PRINCIPAL, "popup", gBrowser).state;
    212  Assert.equal(state, SitePermissions.BLOCK);
    213 
    214  gBrowser.removeTab(tab);
    215 });
    216 
    217 // Explicitly set the permission to the otherwise default state and check that
    218 // the label still displays correctly.
    219 add_task(async function check_explicit_default_permission() {
    220  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);
    221 
    222  // DENY only works if triggered through Services.perms (it's very edge-casey),
    223  // since SitePermissions.sys.mjs considers setting default permissions to be removal.
    224  PermissionTestUtils.add(URI, "popup", Ci.nsIPermissionManager.DENY_ACTION);
    225 
    226  await openPermissionPopup();
    227  let menulist = document.getElementById("permission-popup-menulist");
    228  Assert.equal(menulist.value, "0");
    229  Assert.equal(menulist.label, "Block");
    230  await closePermissionPopup();
    231 
    232  PermissionTestUtils.add(URI, "popup", Services.perms.ALLOW_ACTION);
    233 
    234  await openPermissionPopup();
    235  menulist = document.getElementById("permission-popup-menulist");
    236  Assert.equal(menulist.value, "1");
    237  Assert.equal(menulist.label, "Allow");
    238  await closePermissionPopup();
    239 
    240  PermissionTestUtils.remove(URI, "popup");
    241  gBrowser.removeTab(tab);
    242 });