tor-browser

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

browser_webconsole_context_menu_open_url.js (3906B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that the Open URL in new Tab menu item is displayed for links in text messages
      5 // and network logs and that they work as expected.
      6 
      7 "use strict";
      8 
      9 const TEST_URI =
     10  "http://example.com/browser/devtools/client/webconsole/" +
     11  "test/browser/test-console.html";
     12 const TEST_URI2 = "http://example.com/";
     13 
     14 add_task(async function () {
     15  // Enable net messages in the console for this test.
     16  await pushPref("devtools.webconsole.filter.net", true);
     17 
     18  const hud = await openNewTabAndConsole(TEST_URI);
     19  await clearOutput(hud);
     20 
     21  info("Test Open URL menu item for text log");
     22 
     23  info("Logging a text message in the content window");
     24  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     25    content.wrappedJSObject.console.log("simple text message");
     26  });
     27  let message = await waitFor(() =>
     28    findConsoleAPIMessage(hud, "simple text message")
     29  );
     30  ok(message, "Text log found in the console");
     31 
     32  info("Open and check the context menu for the logged text message");
     33  let menuPopup = await openContextMenu(hud, message);
     34  let openUrlItem = menuPopup.querySelector("#console-menu-open-url");
     35  ok(!openUrlItem, "Open URL menu item is not available");
     36 
     37  await hideContextMenu(hud);
     38 
     39  info("Test Open URL menu item for a text message containing a link");
     40  await ContentTask.spawn(gBrowser.selectedBrowser, TEST_URI2, url => {
     41    content.wrappedJSObject.console.log("Visit", url);
     42  });
     43 
     44  info("Open context menu for the link");
     45  message = await waitFor(() => findConsoleAPIMessage(hud, "Visit"));
     46  const urlNode = message.querySelector("a.url");
     47  menuPopup = await openContextMenu(hud, urlNode);
     48  openUrlItem = menuPopup.querySelector("#console-menu-open-url");
     49  ok(openUrlItem, "Open URL menu item is available");
     50 
     51  info("Click on Open URL menu item and wait for new tab to open");
     52  let currentTab = gBrowser.selectedTab;
     53  const onTabLoaded = BrowserTestUtils.waitForNewTab(gBrowser, TEST_URI2, true);
     54  openUrlItem.click();
     55  let newTab = await onTabLoaded;
     56  ok(newTab, "The expected tab was opened.");
     57  is(
     58    newTab._tPos,
     59    currentTab._tPos + 1,
     60    "The new tab was opened in the position to the right of the current tab"
     61  );
     62  is(gBrowser.selectedTab, currentTab, "The tab was opened in the background");
     63 
     64  await clearOutput(hud);
     65 
     66  info("Test Open URL menu item for network log");
     67 
     68  info("Reload the content window to produce a network log");
     69  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     70    content.wrappedJSObject.location.reload();
     71  });
     72  message = await waitFor(() => findNetworkMessage(hud, "test-console.html"));
     73  ok(message, "Network log found in the console");
     74 
     75  info("Open and check the context menu for the logged network message");
     76  menuPopup = await openContextMenu(hud, message);
     77  openUrlItem = menuPopup.querySelector("#console-menu-open-url");
     78  ok(openUrlItem, "Open URL menu item is available");
     79 
     80  currentTab = gBrowser.selectedTab;
     81  const tabLoaded = listenToTabLoad();
     82  info("Click on Open URL menu item and wait for new tab to open");
     83  openUrlItem.click();
     84  await hideContextMenu(hud);
     85  newTab = await tabLoaded;
     86  const newTabHref = newTab.linkedBrowser.currentURI.spec;
     87  is(newTabHref, TEST_URI, "Tab was opened with the expected URL");
     88 
     89  info("Remove the new tab and select the previous tab back");
     90  gBrowser.removeTab(newTab);
     91  gBrowser.selectedTab = currentTab;
     92 });
     93 
     94 /**
     95 * Simple helper to wrap a tab load listener in a promise.
     96 */
     97 function listenToTabLoad() {
     98  return new Promise(resolve => {
     99    gBrowser.tabContainer.addEventListener(
    100      "TabOpen",
    101      function (evt) {
    102        const newTab = evt.target;
    103        BrowserTestUtils.browserLoaded(newTab.linkedBrowser).then(() =>
    104          resolve(newTab)
    105        );
    106      },
    107      { capture: true, once: true }
    108    );
    109  });
    110 }