tor-browser

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

browser_html_tooltip_doorhanger-02.js (2324B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 /* import-globals-from helper_html_tooltip.js */
      4 
      5 "use strict";
      6 
      7 /**
      8 * Test the HTMLTooltip "doorhanger" type's arrow tip is precisely centered on
      9 * the anchor when the anchor is small.
     10 */
     11 
     12 const HTML_NS = "http://www.w3.org/1999/xhtml";
     13 const TEST_URI = CHROME_URL_ROOT + "doc_html_tooltip_doorhanger-02.xhtml";
     14 
     15 const {
     16  HTMLTooltip,
     17 } = require("resource://devtools/client/shared/widgets/tooltip/HTMLTooltip.js");
     18 loadHelperScript("helper_html_tooltip.js");
     19 
     20 let useXulWrapper;
     21 
     22 add_task(async function () {
     23  // Force the toolbox to be 200px high;
     24  await pushPref("devtools.toolbox.footer.height", 200);
     25 
     26  await addTab("about:blank");
     27  const { doc } = await createHost("bottom", TEST_URI);
     28 
     29  info("Run tests for a Tooltip without using a XUL panel");
     30  useXulWrapper = false;
     31  await runTests(doc);
     32 
     33  info("Run tests for a Tooltip with a XUL panel");
     34  useXulWrapper = true;
     35  await runTests(doc);
     36 });
     37 
     38 async function runTests(doc) {
     39  info("Create HTML tooltip");
     40  const tooltip = new HTMLTooltip(doc, { type: "doorhanger", useXulWrapper });
     41  const div = doc.createElementNS(HTML_NS, "div");
     42  div.style.width = "200px";
     43  div.style.height = "35px";
     44  tooltip.panel.appendChild(div);
     45 
     46  const elements = [...doc.querySelectorAll(".anchor")];
     47  for (const el of elements) {
     48    info("Display the tooltip on an anchor.");
     49    await showTooltip(tooltip, el);
     50 
     51    const arrow = tooltip.arrow;
     52    ok(arrow, "Tooltip has an arrow");
     53 
     54    // Get the geometry of the anchor and arrow.
     55    const anchorBounds = el.getBoxQuads({ relativeTo: doc })[0].getBounds();
     56    const arrowBounds = arrow.getBoxQuads({ relativeTo: doc })[0].getBounds();
     57 
     58    // Compare the centers
     59    const center = bounds => bounds.left + bounds.width / 2;
     60    const delta = Math.abs(center(anchorBounds) - center(arrowBounds));
     61    const describeBounds = bounds =>
     62      `${bounds.left}<--[${center(bounds)}]-->${bounds.right}`;
     63    const params =
     64      `anchor: ${describeBounds(anchorBounds)}, ` +
     65      `arrow: ${describeBounds(arrowBounds)}`;
     66    Assert.lessOrEqual(
     67      delta,
     68      1,
     69      `Arrow center is roughly aligned with anchor center (${params})`
     70    );
     71 
     72    await hideTooltip(tooltip);
     73  }
     74 
     75  tooltip.destroy();
     76 }