tor-browser

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

browser_html_tooltip-03.js (2689B)


      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 * This is the sanity test for the HTMLTooltip focus
      9 */
     10 
     11 const HTML_NS = "http://www.w3.org/1999/xhtml";
     12 const TEST_URI = CHROME_URL_ROOT + "doc_html_tooltip-03.xhtml";
     13 
     14 const {
     15  HTMLTooltip,
     16 } = require("resource://devtools/client/shared/widgets/tooltip/HTMLTooltip.js");
     17 loadHelperScript("helper_html_tooltip.js");
     18 
     19 let useXulWrapper;
     20 
     21 add_task(async function () {
     22  await addTab("about:blank");
     23  const { doc } = await createHost("bottom", TEST_URI);
     24 
     25  info("Run tests for a Tooltip without using a XUL panel");
     26  useXulWrapper = false;
     27  await runTests(doc);
     28 
     29  info("Run tests for a Tooltip with a XUL panel");
     30  useXulWrapper = true;
     31  await runTests(doc);
     32 });
     33 
     34 async function runTests(doc) {
     35  await focusNode(doc, "#box4-input");
     36  ok(doc.activeElement.closest("#box4-input"), "Focus is in the #box4-input");
     37 
     38  info("Test a tooltip will not take focus");
     39  const tooltip = await createTooltip(doc);
     40 
     41  await showTooltip(tooltip, doc.getElementById("box1"));
     42  ok(
     43    doc.activeElement.closest("#box4-input"),
     44    "Focus is still in the #box4-input"
     45  );
     46 
     47  await hideTooltip(tooltip);
     48  await blurNode(doc, "#box4-input");
     49 
     50  tooltip.destroy();
     51 }
     52 
     53 /**
     54 * Fpcus the node corresponding to the provided selector in the provided document. Returns
     55 * a promise that will resolve when receiving the focus event on the node.
     56 */
     57 function focusNode(doc, selector) {
     58  const node = doc.querySelector(selector);
     59  const onFocus = once(node, "focus");
     60  node.focus();
     61  return onFocus;
     62 }
     63 
     64 /**
     65 * Blur the node corresponding to the provided selector in the provided document. Returns
     66 * a promise that will resolve when receiving the blur event on the node.
     67 */
     68 function blurNode(doc, selector) {
     69  const node = doc.querySelector(selector);
     70  const onBlur = once(node, "blur");
     71  node.blur();
     72  return onBlur;
     73 }
     74 
     75 /**
     76 * Create an HTMLTooltip instance.
     77 *
     78 * @param {Document} doc
     79 *        Document in which the tooltip should be created
     80 * @return {Promise} promise that will resolve the HTMLTooltip instance created when the
     81 *         tooltip content will be ready.
     82 */
     83 function createTooltip(doc) {
     84  const tooltip = new HTMLTooltip(doc, { useXulWrapper });
     85  const div = doc.createElementNS(HTML_NS, "div");
     86  div.classList.add("tooltip-content");
     87  div.style.height = "50px";
     88 
     89  const input = doc.createElementNS(HTML_NS, "input");
     90  input.setAttribute("type", "text");
     91  div.appendChild(input);
     92 
     93  tooltip.panel.appendChild(div);
     94  tooltip.setContentSize({ width: 150, height: 50 });
     95  return tooltip;
     96 }