tor-browser

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

browser_bfcache_copycommand.js (2596B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 function dummyPageURL(domain, query = "") {
      7  return (
      8    getRootDirectory(gTestPath).replace(
      9      "chrome://mochitests/content",
     10      `https://${domain}`
     11    ) +
     12    "dummy_page.html" +
     13    query
     14  );
     15 }
     16 
     17 async function openContextMenu(browser) {
     18  let contextMenu = document.getElementById("contentAreaContextMenu");
     19  let awaitPopupShown = BrowserTestUtils.waitForEvent(
     20    contextMenu,
     21    "popupshown"
     22  );
     23  await BrowserTestUtils.synthesizeMouse(
     24    "body",
     25    1,
     26    1,
     27    {
     28      type: "contextmenu",
     29      button: 2,
     30    },
     31    browser
     32  );
     33  await awaitPopupShown;
     34 }
     35 
     36 async function closeContextMenu() {
     37  let contextMenu = document.getElementById("contentAreaContextMenu");
     38  let awaitPopupHidden = BrowserTestUtils.waitForEvent(
     39    contextMenu,
     40    "popuphidden"
     41  );
     42  contextMenu.hidePopup();
     43  await awaitPopupHidden;
     44 }
     45 
     46 async function testWithDomain(domain) {
     47  // Passing a query to make sure the next load is never a same-document
     48  // navigation.
     49  let dummy = dummyPageURL("example.org", "?start");
     50  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, dummy);
     51  let browser = tab.linkedBrowser;
     52 
     53  let sel = await SpecialPowers.spawn(browser, [], function () {
     54    let sel = content.getSelection();
     55    sel.removeAllRanges();
     56    sel.selectAllChildren(content.document.body);
     57    return sel.toString();
     58  });
     59 
     60  await openContextMenu(browser);
     61 
     62  let copyItem = document.getElementById("context-copy");
     63  ok(!copyItem.disabled, "Copy item should be enabled if text is selected.");
     64 
     65  await closeContextMenu();
     66 
     67  let loaded = BrowserTestUtils.browserLoaded(browser, false, null, true);
     68  BrowserTestUtils.startLoadingURIString(browser, dummyPageURL(domain));
     69  await loaded;
     70  loaded = BrowserTestUtils.waitForLocationChange(gBrowser, dummy);
     71  browser.goBack();
     72  await loaded;
     73 
     74  let sel2 = await SpecialPowers.spawn(browser, [], function () {
     75    return content.getSelection().toString();
     76  });
     77  is(sel, sel2, "Selection should remain when coming out of BFCache.");
     78 
     79  await openContextMenu(browser);
     80 
     81  ok(!copyItem.disabled, "Copy item should be enabled if text is selected.");
     82 
     83  await closeContextMenu();
     84 
     85  await BrowserTestUtils.removeTab(tab);
     86 }
     87 
     88 add_task(async function testValidSameOrigin() {
     89  await testWithDomain("example.org");
     90 });
     91 
     92 add_task(async function testValidCrossOrigin() {
     93  await testWithDomain("example.com");
     94 });
     95 
     96 add_task(async function testInvalid() {
     97  await testWithDomain("this.is.invalid");
     98 });