tor-browser

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

head.js (6613B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 /**
      5 * Helper function for encoding override tests, loads URL, runs check1,
      6 * forces encoding detection, runs check2.
      7 */
      8 function runCharsetTest(url, check1, check2) {
      9  waitForExplicitFinish();
     10 
     11  BrowserTestUtils.openNewForegroundTab(gBrowser, url, true).then(afterOpen);
     12 
     13  function afterOpen() {
     14    BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser).then(
     15      afterChangeCharset
     16    );
     17 
     18    SpecialPowers.spawn(gBrowser.selectedBrowser, [], check1).then(() => {
     19      BrowserCommands.forceEncodingDetection();
     20    });
     21  }
     22 
     23  function afterChangeCharset() {
     24    SpecialPowers.spawn(gBrowser.selectedBrowser, [], check2).then(() => {
     25      gBrowser.removeCurrentTab();
     26      finish();
     27    });
     28  }
     29 }
     30 
     31 /**
     32 * Helper function for charset tests. It loads |url| in a new tab,
     33 * runs |check|.
     34 */
     35 function runCharsetCheck(url, check) {
     36  waitForExplicitFinish();
     37 
     38  BrowserTestUtils.openNewForegroundTab(gBrowser, url, true).then(afterOpen);
     39 
     40  function afterOpen() {
     41    SpecialPowers.spawn(gBrowser.selectedBrowser, [], check).then(() => {
     42      gBrowser.removeCurrentTab();
     43      finish();
     44    });
     45  }
     46 }
     47 
     48 async function pushState(url, frameId) {
     49  info(
     50    `Doing a pushState, expecting to load ${url} ${
     51      frameId ? "in an iframe" : ""
     52    }`
     53  );
     54  let browser = gBrowser.selectedBrowser;
     55  let bc = browser.browsingContext;
     56  if (frameId) {
     57    bc = await SpecialPowers.spawn(bc, [frameId], function (id) {
     58      return content.document.getElementById(id).browsingContext;
     59    });
     60  }
     61  let loaded = BrowserTestUtils.waitForLocationChange(gBrowser, url);
     62  await SpecialPowers.spawn(bc, [url], function (url) {
     63    content.history.pushState({}, "", url);
     64  });
     65  await loaded;
     66  info(`Loaded ${url} ${frameId ? "in an iframe" : ""}`);
     67 }
     68 
     69 async function loadURI(url) {
     70  info(`Doing a top-level loadURI, expecting to load ${url}`);
     71  let browser = gBrowser.selectedBrowser;
     72  let loaded = BrowserTestUtils.browserLoaded(browser, false, url);
     73  BrowserTestUtils.startLoadingURIString(browser, url);
     74  await loaded;
     75  info(`Loaded ${url}`);
     76 }
     77 
     78 async function followLink(url, frameId) {
     79  info(
     80    `Creating and following a link to ${url} ${frameId ? "in an iframe" : ""}`
     81  );
     82  let browser = gBrowser.selectedBrowser;
     83  let bc = browser.browsingContext;
     84  if (frameId) {
     85    bc = await SpecialPowers.spawn(bc, [frameId], function (id) {
     86      return content.document.getElementById(id).browsingContext;
     87    });
     88  }
     89  let loaded = BrowserTestUtils.browserLoaded(browser, !!frameId, url);
     90  await SpecialPowers.spawn(bc, [url], function (url) {
     91    let a = content.document.createElement("a");
     92    a.href = url;
     93    content.document.body.appendChild(a);
     94    a.click();
     95  });
     96  await loaded;
     97  info(`Loaded ${url} ${frameId ? "in an iframe" : ""}`);
     98 }
     99 
    100 async function goForward(url, useFrame = false) {
    101  info(
    102    `Clicking the forward button, expecting to load ${url} ${
    103      useFrame ? "in an iframe" : ""
    104    }`
    105  );
    106  let loaded = BrowserTestUtils.waitForLocationChange(gBrowser, url);
    107  let forwardButton = document.getElementById("forward-button");
    108  forwardButton.click();
    109  await loaded;
    110  info(`Loaded ${url} ${useFrame ? "in an iframe" : ""}`);
    111 }
    112 
    113 async function goBack(url, useFrame = false) {
    114  info(
    115    `Clicking the back button, expecting to load ${url} ${
    116      useFrame ? "in an iframe" : ""
    117    }`
    118  );
    119  let loaded = BrowserTestUtils.waitForLocationChange(gBrowser, url);
    120  let backButton = document.getElementById("back-button");
    121  backButton.click();
    122  await loaded;
    123  info(`Loaded ${url} ${useFrame ? "in an iframe" : ""}`);
    124 }
    125 
    126 function assertBackForwardState(canGoBack, canGoForward) {
    127  let backButton = document.getElementById("back-button");
    128  let forwardButton = document.getElementById("forward-button");
    129 
    130  is(
    131    backButton.disabled,
    132    !canGoBack,
    133    `${gBrowser.currentURI.spec}: back button is ${
    134      canGoBack ? "not" : ""
    135    } disabled`
    136  );
    137  is(
    138    forwardButton.disabled,
    139    !canGoForward,
    140    `${gBrowser.currentURI.spec}: forward button is ${
    141      canGoForward ? "not" : ""
    142    } disabled`
    143  );
    144 }
    145 
    146 class SHListener {
    147  static NewEntry = 0;
    148  static Reload = 1;
    149  static GotoIndex = 2;
    150  static Purge = 3;
    151  static ReplaceEntry = 4;
    152  static async waitForHistory(history, event) {
    153    return new Promise(resolve => {
    154      let listener = {
    155        OnHistoryNewEntry: () => {},
    156        OnHistoryReload: () => {
    157          return true;
    158        },
    159        OnHistoryGotoIndex: () => {},
    160        OnHistoryPurge: () => {},
    161        OnHistoryReplaceEntry: () => {},
    162 
    163        QueryInterface: ChromeUtils.generateQI([
    164          "nsISHistoryListener",
    165          "nsISupportsWeakReference",
    166        ]),
    167      };
    168 
    169      function finish() {
    170        history.removeSHistoryListener(listener);
    171        resolve();
    172      }
    173      switch (event) {
    174        case this.NewEntry:
    175          listener.OnHistoryNewEntry = finish;
    176          break;
    177        case this.Reload:
    178          listener.OnHistoryReload = () => {
    179            finish();
    180            return true;
    181          };
    182          break;
    183        case this.GotoIndex:
    184          listener.OnHistoryGotoIndex = finish;
    185          break;
    186        case this.Purge:
    187          listener.OnHistoryPurge = finish;
    188          break;
    189        case this.ReplaceEntry:
    190          listener.OnHistoryReplaceEntry = finish;
    191          break;
    192      }
    193 
    194      history.addSHistoryListener(listener);
    195    });
    196  }
    197 }
    198 
    199 async function assertMenulist(entries) {
    200  // Wait for the session data to be flushed before continuing the test
    201  await new Promise(resolve =>
    202    SessionStore.getSessionHistory(gBrowser.selectedTab, resolve)
    203  );
    204 
    205  let backButton = document.getElementById("back-button");
    206  let contextMenu = document.getElementById("backForwardMenu");
    207 
    208  info("waiting for the history menu to open");
    209 
    210  let popupShownPromise = BrowserTestUtils.waitForEvent(
    211    contextMenu,
    212    "popupshown"
    213  );
    214  EventUtils.synthesizeMouseAtCenter(backButton, {
    215    type: "contextmenu",
    216    button: 2,
    217  });
    218  await popupShownPromise;
    219 
    220  info("history menu opened");
    221 
    222  let nodes = contextMenu.childNodes;
    223 
    224  is(
    225    nodes.length,
    226    entries.length,
    227    "Has the expected number of contextMenu entries"
    228  );
    229 
    230  for (let i = 0; i < entries.length; i++) {
    231    let node = nodes[i];
    232    is(
    233      node.getAttribute("uri"),
    234      entries[i],
    235      "contextMenu node has the correct uri"
    236    );
    237  }
    238 
    239  let popupHiddenPromise = BrowserTestUtils.waitForEvent(
    240    contextMenu,
    241    "popuphidden"
    242  );
    243  contextMenu.hidePopup();
    244  await popupHiddenPromise;
    245 }