tor-browser

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

browser_openWebLinkIn.js (5823B)


      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 "use strict";
      5 
      6 const EXAMPLE_URL = "https://example.org/";
      7 add_task(async function test_open_tab() {
      8  const waitForTabPromise = BrowserTestUtils.waitForNewTab(
      9    gBrowser,
     10    EXAMPLE_URL
     11  );
     12  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
     13    openWebLinkIn(EXAMPLE_URL, "tab", {
     14      resolveOnContentBrowserCreated,
     15    })
     16  );
     17 
     18  const tab = await waitForTabPromise;
     19  is(
     20    contentBrowser,
     21    tab.linkedBrowser,
     22    "We get a content browser that is the tab's linked browser as the result of opening a tab"
     23  );
     24 
     25  BrowserTestUtils.removeTab(tab);
     26 });
     27 
     28 add_task(async function test_open_window() {
     29  const waitForWindowPromise = BrowserTestUtils.waitForNewWindow();
     30  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
     31    openWebLinkIn(EXAMPLE_URL, "window", {
     32      resolveOnContentBrowserCreated,
     33    })
     34  );
     35 
     36  const win = await waitForWindowPromise;
     37  is(
     38    contentBrowser,
     39    win.gBrowser.selectedBrowser,
     40    "We get the content browser for the newly opened window as a result of opening a window"
     41  );
     42 
     43  await BrowserTestUtils.closeWindow(win);
     44 });
     45 
     46 add_task(async function test_open_private_window() {
     47  const waitForWindowPromise = BrowserTestUtils.waitForNewWindow();
     48  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
     49    openWebLinkIn(EXAMPLE_URL, "window", {
     50      resolveOnContentBrowserCreated,
     51      private: true,
     52    })
     53  );
     54 
     55  const win = await waitForWindowPromise;
     56  ok(
     57    PrivateBrowsingUtils.isBrowserPrivate(win),
     58    "The new window is a private window."
     59  );
     60  is(
     61    contentBrowser,
     62    win.gBrowser.selectedBrowser,
     63    "We get the content browser for the newly opened window as a result of opening a private window"
     64  );
     65 
     66  await BrowserTestUtils.closeWindow(win);
     67 });
     68 
     69 add_task(async function test_open_private_tab_from_private_window() {
     70  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
     71    private: true,
     72  });
     73  const waitForTabPromise = BrowserTestUtils.waitForNewTab(
     74    privateWindow.gBrowser,
     75    EXAMPLE_URL
     76  );
     77  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
     78    privateWindow.openWebLinkIn(EXAMPLE_URL, "tab", {
     79      resolveOnContentBrowserCreated,
     80    })
     81  );
     82 
     83  const tab = await waitForTabPromise;
     84  ok(
     85    PrivateBrowsingUtils.isBrowserPrivate(tab),
     86    "The new tab was opened in a private browser."
     87  );
     88  is(
     89    contentBrowser,
     90    tab.linkedBrowser,
     91    "We get a content browser that is the tab's linked browser as the result of opening a private tab in a private window"
     92  );
     93 
     94  await BrowserTestUtils.closeWindow(privateWindow);
     95 });
     96 
     97 add_task(async function test_open_non_private_tab_from_private_window() {
     98  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
     99    private: true,
    100  });
    101 
    102  // Opening this tab from the private window should open it in the non-private window.
    103  const waitForTabPromise = BrowserTestUtils.waitForNewTab(
    104    gBrowser,
    105    EXAMPLE_URL
    106  );
    107 
    108  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
    109    privateWindow.openWebLinkIn(EXAMPLE_URL, "tab", {
    110      forceNonPrivate: true,
    111      resolveOnContentBrowserCreated,
    112    })
    113  );
    114 
    115  const nonPrivateTab = await waitForTabPromise;
    116  ok(
    117    !PrivateBrowsingUtils.isBrowserPrivate(nonPrivateTab),
    118    "The new window isn't a private window."
    119  );
    120  is(
    121    contentBrowser,
    122    nonPrivateTab.linkedBrowser,
    123    "We get a content browser that is the non private tab's linked browser as the result of opening a non private tab from a private window"
    124  );
    125 
    126  BrowserTestUtils.removeTab(nonPrivateTab);
    127  await BrowserTestUtils.closeWindow(privateWindow);
    128 });
    129 
    130 add_task(async function test_open_non_private_tab_from_only_private_window() {
    131  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    132    private: true,
    133  });
    134 
    135  // In this test we'll hide the existing window from window trackers, because
    136  // we want to test that we open a new window when there's only a private
    137  // window.
    138  BrowserTestUtils.concealWindow(window, { signal: testSignal });
    139 
    140  // Opening this tab from the private window should open it in a new non-private window.
    141  const waitForWindowPromise = BrowserTestUtils.waitForNewWindow({
    142    url: EXAMPLE_URL,
    143  });
    144  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
    145    privateWindow.openWebLinkIn(EXAMPLE_URL, "tab", {
    146      forceNonPrivate: true,
    147      resolveOnContentBrowserCreated,
    148    })
    149  );
    150 
    151  const nonPrivateWindow = await waitForWindowPromise;
    152  ok(
    153    !PrivateBrowsingUtils.isBrowserPrivate(nonPrivateWindow),
    154    "The new window isn't a private window."
    155  );
    156  is(
    157    contentBrowser,
    158    nonPrivateWindow.gBrowser.selectedBrowser,
    159    "We get the content browser for the newly opened non private window from a private window, as a result of opening a non private tab."
    160  );
    161 
    162  await BrowserTestUtils.closeWindow(nonPrivateWindow);
    163  await BrowserTestUtils.closeWindow(privateWindow);
    164 });
    165 
    166 add_task(async function test_open_ai_window_tab_in_ai_window() {
    167  await SpecialPowers.pushPrefEnv({
    168    set: [["browser.aiwindow.enabled", true]],
    169  });
    170  let win = await BrowserTestUtils.openNewBrowserWindow({ aiWindow: true });
    171 
    172  let tabPromise = BrowserTestUtils.waitForNewTab(win.gBrowser);
    173  win.BrowserCommands.openTab({});
    174  let tab = await tabPromise;
    175 
    176  Assert.equal(
    177    tab.linkedBrowser.currentURI.spec,
    178    AIWindow.newTabURL,
    179    "New tab in an AI Window should load AIWindow.newTabURL"
    180  );
    181 
    182  await BrowserTestUtils.removeTab(tab);
    183  await BrowserTestUtils.closeWindow(win);
    184  await SpecialPowers.popPrefEnv();
    185 });