tor-browser

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

browser_private_browsing_window.js (6966B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 add_task(async function testOpenBrowserWindow() {
      6  let win = OpenBrowserWindow();
      7  Assert.ok(
      8    !PrivateBrowsingUtils.isWindowPrivate(win),
      9    "OpenBrowserWindow() should open a normal window"
     10  );
     11  await BrowserTestUtils.closeWindow(win);
     12 
     13  win = OpenBrowserWindow({ private: true });
     14  Assert.ok(
     15    PrivateBrowsingUtils.isWindowPrivate(win),
     16    "OpenBrowserWindow({private: true}) should open a private window"
     17  );
     18  await BrowserTestUtils.closeWindow(win);
     19 
     20  win = OpenBrowserWindow({ private: false });
     21  Assert.ok(
     22    !PrivateBrowsingUtils.isWindowPrivate(win),
     23    "OpenBrowserWindow({private: false}) should open a normal window"
     24  );
     25  await BrowserTestUtils.closeWindow(win);
     26 
     27  // In permanent browsing mode.
     28  await SpecialPowers.pushPrefEnv({
     29    set: [["browser.privatebrowsing.autostart", true]],
     30  });
     31 
     32  win = OpenBrowserWindow();
     33  Assert.ok(
     34    PrivateBrowsingUtils.isWindowPrivate(win),
     35    "OpenBrowserWindow() in PBM should open a private window"
     36  );
     37  await BrowserTestUtils.closeWindow(win);
     38 
     39  win = OpenBrowserWindow({ private: true });
     40  Assert.ok(
     41    PrivateBrowsingUtils.isWindowPrivate(win),
     42    "OpenBrowserWindow({private: true}) in PBM should open a private window"
     43  );
     44  await BrowserTestUtils.closeWindow(win);
     45 
     46  win = OpenBrowserWindow({ private: false });
     47  Assert.ok(
     48    PrivateBrowsingUtils.isWindowPrivate(win),
     49    "OpenBrowserWindow({private: false}) in PBM should open a private window"
     50  );
     51  await BrowserTestUtils.closeWindow(win);
     52 
     53  await SpecialPowers.popPrefEnv();
     54 });
     55 
     56 /**
     57 * Check that the "new window" menu items have the expected properties.
     58 *
     59 * @param {Element} newWindowItem - The "new window" item to check.
     60 * @param {Element} privateWindowItem - The "new private window" item to check.
     61 * @param {object} expect - The expected properties.
     62 * @param {boolean} expect.privateVisible - Whether we expect the private item
     63 *   to be visible or not.
     64 * @param {string} expect.newWindowL10nId - The expected string ID used by the
     65 *   "new window" item.
     66 * @param {string} expect.privateWindowL10nId - The expected string ID used by
     67 *   the "new private window" item.
     68 * @param {boolean} [useIsVisible=true] - Whether to test the "true" visibility
     69 *   of the item. Otherwise only the "hidden" attribute is checked.
     70 */
     71 function assertMenuItems(
     72  newWindowItem,
     73  privateWindowItem,
     74  expect,
     75  useIsVisible = true
     76 ) {
     77  Assert.ok(newWindowItem);
     78  Assert.ok(privateWindowItem);
     79 
     80  if (useIsVisible) {
     81    Assert.ok(
     82      BrowserTestUtils.isVisible(newWindowItem),
     83      "New window item should be visible"
     84    );
     85  } else {
     86    // The application menu is not accessible on macOS, just check the hidden
     87    // attribute.
     88    Assert.ok(!newWindowItem.hidden, "New window item should be visible");
     89  }
     90 
     91  Assert.equal(
     92    newWindowItem.getAttribute("key"),
     93    "key_newNavigator",
     94    "New window item should use the same key"
     95  );
     96  Assert.equal(
     97    newWindowItem.getAttribute("data-l10n-id"),
     98    expect.newWindowL10nId
     99  );
    100 
    101  if (!expect.privateVisible) {
    102    if (useIsVisible) {
    103      Assert.ok(
    104        BrowserTestUtils.isHidden(privateWindowItem),
    105        "Private window item should be hidden"
    106      );
    107    } else {
    108      Assert.ok(
    109        privateWindowItem.hidden,
    110        "Private window item should be hidden"
    111      );
    112    }
    113    // Don't check attributes since hidden.
    114  } else {
    115    if (useIsVisible) {
    116      Assert.ok(
    117        BrowserTestUtils.isVisible(privateWindowItem),
    118        "Private window item should be visible"
    119      );
    120    } else {
    121      Assert.ok(
    122        !privateWindowItem.hidden,
    123        "Private window item should be visible"
    124      );
    125    }
    126    Assert.equal(
    127      privateWindowItem.getAttribute("key"),
    128      "key_privatebrowsing",
    129      "Private window item should use the same key"
    130    );
    131    Assert.equal(
    132      privateWindowItem.getAttribute("data-l10n-id"),
    133      expect.privateWindowL10nId
    134    );
    135  }
    136 }
    137 
    138 /**
    139 * Check that a window has the expected "new window" items in the "File" and app
    140 * menus.
    141 *
    142 * @param {Window} win - The window to check.
    143 * @param {boolean} expectBoth - Whether we expect the window to contain both
    144 *   "new window" and "new private window" as separate controls.
    145 */
    146 async function checkWindowMenus(win, expectBoth) {
    147  // Check the File menu.
    148  assertMenuItems(
    149    win.document.getElementById("menu_newNavigator"),
    150    win.document.getElementById("menu_newPrivateWindow"),
    151    {
    152      privateVisible: expectBoth,
    153      // If in permanent private browsing, expect the new window item to use the
    154      // "New private window" string.
    155      newWindowL10nId: expectBoth
    156        ? "menu-file-new-window"
    157        : "menu-file-new-private-window",
    158      privateWindowL10nId: "menu-file-new-private-window",
    159    },
    160    // The file menu is difficult to open cross-platform, so we do not open it
    161    // for this test.
    162    false
    163  );
    164 
    165  // Open the app menu.
    166  let appMenuButton = win.document.getElementById("PanelUI-menu-button");
    167  let appMenu = win.document.getElementById("appMenu-popup");
    168  let menuShown = BrowserTestUtils.waitForEvent(appMenu, "popupshown");
    169  EventUtils.synthesizeMouseAtCenter(appMenuButton, {}, win);
    170  await menuShown;
    171 
    172  // Check the app menu.
    173  assertMenuItems(
    174    win.document.getElementById("appMenu-new-window-button2"),
    175    win.document.getElementById("appMenu-new-private-window-button2"),
    176    {
    177      privateVisible: expectBoth,
    178      // If in permanent private browsing, expect the new window item to use the
    179      // "New private window" string.
    180      newWindowL10nId: expectBoth
    181        ? "appmenuitem-new-window"
    182        : "appmenuitem-new-private-window",
    183      privateWindowL10nId: "appmenuitem-new-private-window",
    184    }
    185  );
    186 
    187  appMenu.hidePopup();
    188 }
    189 
    190 add_task(async function testNewWindowMenuItems() {
    191  // In non-private window, expect both menu items.
    192  let win = await BrowserTestUtils.openNewBrowserWindow({
    193    private: false,
    194  });
    195  await checkWindowMenus(win, true);
    196  Assert.equal(win.gBrowser.currentURI.spec, "about:blank");
    197  await BrowserTestUtils.closeWindow(win);
    198 
    199  // In non-permanent private window, still expect both menu items.
    200  win = await BrowserTestUtils.openNewBrowserWindow({
    201    private: true,
    202  });
    203  await checkWindowMenus(win, true);
    204  Assert.equal(win.gBrowser.currentURI.spec, "about:privatebrowsing");
    205  await BrowserTestUtils.closeWindow(win);
    206 
    207  // In permanent private browsing, expect only one menu item.
    208  await SpecialPowers.pushPrefEnv({
    209    set: [["browser.privatebrowsing.autostart", true]],
    210  });
    211 
    212  win = await BrowserTestUtils.openNewBrowserWindow();
    213  await checkWindowMenus(win, false);
    214  Assert.equal(win.gBrowser.currentURI.spec, "about:blank");
    215  await BrowserTestUtils.closeWindow(win);
    216 
    217  await SpecialPowers.popPrefEnv();
    218 });