tor-browser

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

browser_noopener.js (5067B)


      1 "use strict";
      2 
      3 const TESTS = [
      4  { id: "#test1", name: "", opener: true, newWindow: false },
      5  { id: "#test2", name: "", opener: false, newWindow: false },
      6  { id: "#test3", name: "", opener: false, newWindow: false },
      7 
      8  { id: "#test4", name: "uniquename1", opener: true, newWindow: false },
      9  { id: "#test5", name: "uniquename2", opener: false, newWindow: false },
     10  { id: "#test6", name: "uniquename3", opener: false, newWindow: false },
     11 
     12  { id: "#test7", name: "", opener: true, newWindow: false },
     13  { id: "#test8", name: "", opener: false, newWindow: false },
     14  { id: "#test9", name: "", opener: false, newWindow: false },
     15 
     16  { id: "#test10", name: "uniquename1", opener: true, newWindow: false },
     17  { id: "#test11", name: "uniquename2", opener: false, newWindow: false },
     18  { id: "#test12", name: "uniquename3", opener: false, newWindow: false },
     19 ];
     20 
     21 const TEST_URL =
     22  "http://mochi.test:8888/browser/dom/tests/browser/test_noopener_source.html";
     23 const TARGET_URL =
     24  "http://mochi.test:8888/browser/dom/tests/browser/test_noopener_target.html";
     25 
     26 const OPEN_NEWWINDOW_PREF = "browser.link.open_newwindow";
     27 const OPEN_NEWWINDOW = 2;
     28 const OPEN_NEWTAB = 3;
     29 
     30 const NOOPENER_NEWPROC_PREF = "dom.noopener.newprocess.enabled";
     31 
     32 async function doTests(usePrivate, container) {
     33  let alwaysNewWindow =
     34    SpecialPowers.getIntPref(OPEN_NEWWINDOW_PREF) == OPEN_NEWWINDOW;
     35 
     36  let window = await BrowserTestUtils.openNewBrowserWindow({
     37    private: usePrivate,
     38  });
     39 
     40  let tabOpenOptions = {};
     41  if (container) {
     42    tabOpenOptions.userContextId = 1;
     43  }
     44 
     45  for (let test of TESTS) {
     46    const testid = `${test.id} (private=${usePrivate}, container=${container}, alwaysNewWindow=${alwaysNewWindow})`;
     47    let originalTab = BrowserTestUtils.addTab(
     48      window.gBrowser,
     49      TEST_URL,
     50      tabOpenOptions
     51    );
     52    await BrowserTestUtils.browserLoaded(originalTab.linkedBrowser);
     53    await BrowserTestUtils.switchTab(window.gBrowser, originalTab);
     54 
     55    let waitFor;
     56    if (test.newWindow || alwaysNewWindow) {
     57      waitFor = BrowserTestUtils.waitForNewWindow({ url: TARGET_URL });
     58      // Confirm that this window has private browsing set if we're doing a private browsing test
     59    } else {
     60      waitFor = BrowserTestUtils.waitForNewTab(
     61        window.gBrowser,
     62        TARGET_URL,
     63        true
     64      );
     65    }
     66 
     67    BrowserTestUtils.synthesizeMouseAtCenter(
     68      test.id,
     69      {},
     70      window.gBrowser.getBrowserForTab(originalTab)
     71    );
     72 
     73    let tab;
     74    if (test.newWindow || alwaysNewWindow) {
     75      let window = await waitFor;
     76      is(
     77        PrivateBrowsingUtils.isWindowPrivate(window),
     78        usePrivate,
     79        "Private status should match for " + testid
     80      );
     81      tab = window.gBrowser.selectedTab;
     82    } else {
     83      tab = await waitFor;
     84    }
     85 
     86    // Check that the name matches.
     87    await SpecialPowers.spawn(
     88      tab.linkedBrowser,
     89      [test, container, testid],
     90      async (test, container, testid) => {
     91        Assert.equal(
     92          content.document.nodePrincipal.originAttributes.userContextId,
     93          container ? 1 : 0,
     94          `User context ID should match for ${testid}`
     95        );
     96 
     97        Assert.equal(
     98          content.window.name,
     99          test.name,
    100          `Name should match for ${testid}`
    101        );
    102        if (test.opener) {
    103          Assert.ok(
    104            content.window.opener,
    105            `Opener should have been set for ${testid}`
    106          );
    107        } else {
    108          Assert.ok(
    109            !content.window.opener,
    110            `Opener should not have been set for ${testid}`
    111          );
    112        }
    113      }
    114    );
    115 
    116    BrowserTestUtils.removeTab(tab);
    117    BrowserTestUtils.removeTab(originalTab);
    118  }
    119 
    120  window.close();
    121 }
    122 
    123 async function doAllTests() {
    124  // Non-private window
    125  await doTests(false, false);
    126 
    127  // Private window
    128  await doTests(true, false);
    129 
    130  // Non-private window with container
    131  await doTests(false, true);
    132 }
    133 
    134 // This test takes a really long time, especially in debug builds, as it is
    135 // constant starting and stopping processes, and opens a new window ~144 times.
    136 requestLongerTimeout(30);
    137 
    138 add_setup(async function () {
    139  await SpecialPowers.pushPrefEnv({
    140    set: [["test.wait300msAfterTabSwitch", true]],
    141  });
    142 });
    143 
    144 add_task(async function newtab_sameproc() {
    145  await SpecialPowers.pushPrefEnv({
    146    set: [
    147      [OPEN_NEWWINDOW_PREF, OPEN_NEWTAB],
    148      [NOOPENER_NEWPROC_PREF, false],
    149    ],
    150  });
    151  await doAllTests();
    152 });
    153 
    154 add_task(async function newtab_newproc() {
    155  await SpecialPowers.pushPrefEnv({
    156    set: [
    157      [OPEN_NEWWINDOW_PREF, OPEN_NEWTAB],
    158      [NOOPENER_NEWPROC_PREF, true],
    159    ],
    160  });
    161  await doAllTests();
    162 });
    163 
    164 add_task(async function newwindow_sameproc() {
    165  await SpecialPowers.pushPrefEnv({
    166    set: [
    167      [OPEN_NEWWINDOW_PREF, OPEN_NEWWINDOW],
    168      [NOOPENER_NEWPROC_PREF, false],
    169    ],
    170  });
    171  await doAllTests();
    172 });
    173 
    174 add_task(async function newwindow_newproc() {
    175  await SpecialPowers.pushPrefEnv({
    176    set: [
    177      [OPEN_NEWWINDOW_PREF, OPEN_NEWWINDOW],
    178      [NOOPENER_NEWPROC_PREF, true],
    179    ],
    180  });
    181  await doAllTests();
    182 });