tor-browser

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

browser_newTabDrop.js (5663B)


      1 /* eslint-disable @microsoft/sdl/no-insecure-url */
      2 
      3 const ANY_URL = undefined;
      4 
      5 const { SearchTestUtils } = ChromeUtils.importESModule(
      6  "resource://testing-common/SearchTestUtils.sys.mjs"
      7 );
      8 
      9 SearchTestUtils.init(this);
     10 
     11 registerCleanupFunction(async function cleanup() {
     12  while (gBrowser.tabs.length > 1) {
     13    BrowserTestUtils.removeTab(gBrowser.tabs[gBrowser.tabs.length - 1]);
     14  }
     15 });
     16 
     17 add_task(async function test_setup() {
     18  // This test opens multiple tabs and some confirm dialogs, that takes long.
     19  requestLongerTimeout(2);
     20 
     21  // Stop search-engine loads from hitting the network
     22  await SearchTestUtils.installSearchExtension(
     23    {
     24      name: "MozSearch",
     25      search_url: "https://example.com/",
     26      search_url_get_params: "q={searchTerms}",
     27    },
     28    { setAsDefault: true }
     29  );
     30 });
     31 
     32 // New Tab Button opens any link.
     33 add_task(async function single_url() {
     34  await dropText("example.com/first", ["http://example.com/first"]);
     35 });
     36 add_task(async function single_url2() {
     37  await dropText("example.com/second", ["http://example.com/second"]);
     38 });
     39 add_task(async function single_url3() {
     40  await dropText("example.com/third", ["http://example.com/third"]);
     41 });
     42 
     43 // Single text/plain item, with multiple links.
     44 add_task(async function multiple_urls() {
     45  await dropText("www.example.com/1\nexample.com/2", [
     46    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     47    "http://www.example.com/1",
     48    "http://example.com/2",
     49  ]);
     50 });
     51 
     52 // Multiple text/plain items, with single and multiple links.
     53 add_task(async function multiple_items_single_and_multiple_links() {
     54  await drop(
     55    [
     56      [{ type: "text/plain", data: "example.com/5" }],
     57      [{ type: "text/plain", data: "example.com/6\nexample.com/7" }],
     58    ],
     59    ["http://example.com/5", "http://example.com/6", "http://example.com/7"]
     60  );
     61 });
     62 
     63 // Single text/x-moz-url item, with multiple links.
     64 // "text/x-moz-url" has titles in even-numbered lines.
     65 add_task(async function single_moz_url_multiple_links() {
     66  await drop(
     67    [
     68      [
     69        {
     70          type: "text/x-moz-url",
     71          data: "example.com/8\nTITLE8\nexample.com/9\nTITLE9",
     72        },
     73      ],
     74    ],
     75    ["http://example.com/8", "http://example.com/9"]
     76  );
     77 });
     78 
     79 // Single item with multiple types.
     80 add_task(async function single_item_multiple_types() {
     81  await drop(
     82    [
     83      [
     84        { type: "text/plain", data: "example.com/10" },
     85        { type: "text/x-moz-url", data: "example.com/11\nTITLE11" },
     86      ],
     87    ],
     88    ["http://example.com/11"]
     89  );
     90 });
     91 
     92 // Warn when too many URLs are dropped.
     93 add_task(async function multiple_tabs_under_max() {
     94  let urls = [];
     95  for (let i = 0; i < 5; i++) {
     96    urls.push("example.com/multi" + i);
     97  }
     98  await dropText(urls.join("\n"), [
     99    "http://example.com/multi0",
    100    "http://example.com/multi1",
    101    "http://example.com/multi2",
    102    "http://example.com/multi3",
    103    "http://example.com/multi4",
    104  ]);
    105 });
    106 add_task(async function multiple_tabs_over_max_accept() {
    107  await SpecialPowers.pushPrefEnv({
    108    set: [["browser.tabs.maxOpenBeforeWarn", 4]],
    109  });
    110 
    111  let confirmPromise = BrowserTestUtils.promiseAlertDialog("accept");
    112 
    113  let urls = [];
    114  for (let i = 0; i < 5; i++) {
    115    urls.push("example.com/accept" + i);
    116  }
    117  await dropText(urls.join("\n"), [
    118    "http://example.com/accept0",
    119    "http://example.com/accept1",
    120    "http://example.com/accept2",
    121    "http://example.com/accept3",
    122    "http://example.com/accept4",
    123  ]);
    124 
    125  await confirmPromise;
    126 
    127  await SpecialPowers.popPrefEnv();
    128 });
    129 add_task(async function multiple_tabs_over_max_cancel() {
    130  await SpecialPowers.pushPrefEnv({
    131    set: [["browser.tabs.maxOpenBeforeWarn", 4]],
    132  });
    133 
    134  let confirmPromise = BrowserTestUtils.promiseAlertDialog("cancel");
    135 
    136  let urls = [];
    137  for (let i = 0; i < 5; i++) {
    138    urls.push("example.com/cancel" + i);
    139  }
    140  await dropText(urls.join("\n"), []);
    141 
    142  await confirmPromise;
    143 
    144  await SpecialPowers.popPrefEnv();
    145 });
    146 
    147 // Open URLs ignoring non-URL.
    148 add_task(async function multiple_urls() {
    149  await dropText(
    150    `
    151    example.com/urls0
    152    example.com/urls1
    153    example.com/urls2
    154    non url0
    155    example.com/urls3
    156    non url1
    157    non url2
    158 `,
    159    [
    160      "http://example.com/urls0",
    161      "http://example.com/urls1",
    162      "http://example.com/urls2",
    163      "http://example.com/urls3",
    164    ]
    165  );
    166 });
    167 
    168 // Open single search if there's no URL.
    169 add_task(async function multiple_text() {
    170  await dropText(
    171    `
    172    non url0
    173    non url1
    174    non url2
    175 `,
    176    [ANY_URL]
    177  );
    178 });
    179 
    180 function dropText(text, expectedURLs) {
    181  return drop([[{ type: "text/plain", data: text }]], expectedURLs);
    182 }
    183 
    184 async function drop(dragData, expectedURLs) {
    185  let dragDataString = JSON.stringify(dragData);
    186  info(
    187    `Starting test for dragData:${dragDataString}; expectedURLs.length:${expectedURLs.length}`
    188  );
    189 
    190  // Since synthesizeDrop triggers the srcElement, need to use another button
    191  // that should be visible.
    192  let dragSrcElement = document.getElementById("back-button");
    193  ok(dragSrcElement, "Back button exists");
    194  let newTabButton = document.getElementById(
    195    gBrowser.tabContainer.overflowing ? "new-tab-button" : "tabs-newtab-button"
    196  );
    197  ok(newTabButton, "New Tab button exists");
    198 
    199  let awaitDrop = BrowserTestUtils.waitForEvent(newTabButton, "drop");
    200 
    201  let loadedPromises = expectedURLs.map(url =>
    202    BrowserTestUtils.waitForNewTab(gBrowser, url, true, true)
    203  );
    204 
    205  EventUtils.synthesizeDrop(
    206    dragSrcElement,
    207    newTabButton,
    208    dragData,
    209    "link",
    210    window
    211  );
    212 
    213  let tabs = await Promise.all(loadedPromises);
    214  for (let tab of tabs) {
    215    BrowserTestUtils.removeTab(tab);
    216  }
    217 
    218  await awaitDrop;
    219  ok(true, "Got drop event");
    220 }