tor-browser

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

browser_markup_links_07.js (5668B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Tests that a middle-click or meta/ctrl-click on links in attributes actually
      7 // do follows the link.
      8 
      9 const TEST_URL = URL_ROOT_SSL + "doc_markup_links.html";
     10 const TEST_URL_BASE = URL_ROOT_SSL + "resources/doc_markup_links_base.html";
     11 
     12 add_task(async function () {
     13  const { inspector } = await openInspectorForURL(TEST_URL);
     14 
     15  info("Select a node with a URI attribute");
     16  await selectNode("video", inspector);
     17 
     18  info("Find the link element from the markup-view");
     19  let { editor } = await getContainerForSelector("video", inspector);
     20  let linkEl = editor.attrElements.get("poster").querySelector(".link");
     21 
     22  info("Follow the link with middle-click and wait for the new tab to open");
     23  await followLinkWaitForTab(
     24    linkEl,
     25    false,
     26    URL_ROOT_SSL + "doc_markup_tooltip.png"
     27  );
     28 
     29  info("Follow the link with meta/ctrl-click and wait for the new tab to open");
     30  await followLinkWaitForTab(
     31    linkEl,
     32    true,
     33    URL_ROOT_SSL + "doc_markup_tooltip.png"
     34  );
     35 
     36  info("Check that simple click does not open a tab");
     37  const onTabOpened = once(gBrowser.tabContainer, "TabOpen");
     38  const onTimeout = wait(1000).then(() => "TIMEOUT");
     39  EventUtils.synthesizeMouseAtCenter(linkEl, {}, linkEl.ownerGlobal);
     40  const res = await Promise.race([onTabOpened, onTimeout]);
     41  is(res, "TIMEOUT", "Tab was not opened on simple click");
     42 
     43  info("Select a node with a IDREF attribute");
     44  await selectNode("label", inspector);
     45 
     46  info("Find the link element from the markup-view that contains the ref");
     47  ({ editor } = await getContainerForSelector("label", inspector));
     48  linkEl = editor.attrElements.get("for").querySelector(".link");
     49 
     50  info("Follow link with middle-click, wait for new node to be selected.");
     51  await followLinkWaitForNewNode(linkEl, false, inspector, "name");
     52 
     53  // We have to re-select the label as the link switched the currently selected node.
     54  await selectNode("label", inspector);
     55 
     56  info("Follow link with ctrl/meta-click, wait for new node to be selected.");
     57  await followLinkWaitForNewNode(linkEl, true, inspector, "name");
     58 
     59  info("Find the label for the element whose id starts with a number");
     60  await selectNode(`label[for="${CSS.escape("3d")}"]`, inspector);
     61  ({ editor } = await getContainerForSelector(
     62    `label[for="${CSS.escape("3d")}"]`,
     63    inspector
     64  ));
     65  linkEl = editor.attrElements.get("for").querySelectorAll(".link")[0];
     66  await followLinkWaitForNewNode(linkEl, true, inspector, "3d");
     67 
     68  info("Select a node with an invalid IDREF attribute");
     69  await selectNode("output", inspector);
     70 
     71  info("Find the link element from the markup-view that contains the ref");
     72  ({ editor } = await getContainerForSelector("output", inspector));
     73  linkEl = editor.attrElements.get("for").querySelectorAll(".link")[2];
     74 
     75  info("Try to follow link wiith middle-click, check no new node selected");
     76  await followLinkNoNewNode(linkEl, false, inspector);
     77 
     78  info("Try to follow link wiith meta/ctrl-click, check no new node selected");
     79  await followLinkNoNewNode(linkEl, true, inspector);
     80 });
     81 
     82 add_task(async function testDocumentWithBaseAttribute() {
     83  const { inspector } = await openInspectorForURL(TEST_URL_BASE);
     84 
     85  info("Select a node with a URI attribute");
     86  await selectNode("img", inspector);
     87 
     88  info("Find the link element from the markup-view");
     89  const { editor } = await getContainerForSelector("img", inspector);
     90  const linkEl = editor.attrElements.get("src").querySelector(".link");
     91 
     92  info("Follow the link with middle-click and wait for the new tab to open");
     93  await followLinkWaitForTab(
     94    linkEl,
     95    false,
     96    URL_ROOT_SSL + "doc_markup_tooltip.png"
     97  );
     98 
     99  info("Follow the link with meta/ctrl-click and wait for the new tab to open");
    100  await followLinkWaitForTab(
    101    linkEl,
    102    true,
    103    URL_ROOT_SSL + "doc_markup_tooltip.png"
    104  );
    105 });
    106 
    107 function performMouseDown(linkEl, metactrl) {
    108  const evt = linkEl.ownerDocument.createEvent("MouseEvents");
    109 
    110  let button = -1;
    111 
    112  if (metactrl) {
    113    info("Performing Meta/Ctrl+Left Click");
    114    button = 0;
    115  } else {
    116    info("Performing Middle Click");
    117    button = 1;
    118  }
    119 
    120  evt.initMouseEvent(
    121    "mousedown",
    122    true,
    123    true,
    124    linkEl.ownerDocument.defaultView,
    125    1,
    126    0,
    127    0,
    128    0,
    129    0,
    130    metactrl,
    131    false,
    132    false,
    133    metactrl,
    134    button,
    135    null
    136  );
    137 
    138  linkEl.dispatchEvent(evt);
    139 }
    140 
    141 async function followLinkWaitForTab(linkEl, isMetaClick, expectedTabURI) {
    142  const onTabOpened = once(gBrowser.tabContainer, "TabOpen");
    143  performMouseDown(linkEl, isMetaClick);
    144  const { target } = await onTabOpened;
    145  await BrowserTestUtils.browserLoaded(target.linkedBrowser);
    146  ok(true, "A new tab opened");
    147  is(
    148    target.linkedBrowser.currentURI.spec,
    149    expectedTabURI,
    150    "The URL for the new tab is correct"
    151  );
    152  gBrowser.removeTab(target);
    153 }
    154 
    155 async function followLinkWaitForNewNode(
    156  linkEl,
    157  isMetaClick,
    158  inspector,
    159  expectedSelectedNodeId
    160 ) {
    161  const onSelection = inspector.selection.once("new-node-front");
    162  performMouseDown(linkEl, isMetaClick);
    163  await onSelection;
    164 
    165  ok(true, "A new node was selected");
    166  is(
    167    inspector.selection.nodeFront.id,
    168    expectedSelectedNodeId,
    169    "The right node was selected"
    170  );
    171 }
    172 
    173 async function followLinkNoNewNode(linkEl, isMetaClick, inspector) {
    174  const onFailed = inspector.markup.once("idref-attribute-link-failed");
    175  performMouseDown(linkEl, isMetaClick);
    176  await onFailed;
    177 
    178  ok(true, "The node selection failed");
    179  is(
    180    inspector.selection.nodeFront.tagName.toLowerCase(),
    181    "output",
    182    "The <output> node is still selected"
    183  );
    184 }