tor-browser

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

browser_inspector_highlighter-cancel.js (2660B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 // Test that canceling the element picker zooms back on the focused element. Bug 1224304.
      8 
      9 const TEST_URL = URL_ROOT + "doc_inspector_long-divs.html";
     10 
     11 const isMac = Services.appinfo.OS === "Darwin";
     12 const TESTS = [
     13  {
     14    key: "VK_ESCAPE",
     15    options: {},
     16    // Escape would open the split console if the toolbox was focused
     17    focusToolbox: false,
     18  },
     19  {
     20    key: "C",
     21    options: {
     22      metaKey: isMac,
     23      ctrlKey: !isMac,
     24      shiftKey: true,
     25    },
     26    // Test with the toolbox focused to check the client side event listener.
     27    focusToolbox: true,
     28  },
     29  {
     30    key: "C",
     31    options: {
     32      metaKey: isMac,
     33      ctrlKey: !isMac,
     34      shiftKey: true,
     35    },
     36    // Test with the content page focused to check the actor's event listener.
     37    focusToolbox: false,
     38  },
     39 ];
     40 
     41 for (const { key, options, focusToolbox } of TESTS) {
     42  add_task(async function () {
     43    info(`Testing cancel shortcut: ${key} with toolbox focus: ${focusToolbox}`);
     44 
     45    const { inspector, toolbox, highlighterTestFront } =
     46      await openInspectorForURL(TEST_URL);
     47    await selectAndHighlightNode("#focus-here", inspector);
     48    ok(
     49      await highlighterTestFront.assertHighlightedNode("#focus-here"),
     50      "The highlighter focuses on div#focus-here"
     51    );
     52    ok(
     53      isSelectedMarkupNodeInView(inspector),
     54      "The currently selected node is on the screen."
     55    );
     56 
     57    await startPicker(toolbox);
     58 
     59    await hoverElement(inspector, "#zoom-here");
     60    ok(
     61      !isSelectedMarkupNodeInView(inspector),
     62      "The currently selected node is off the screen."
     63    );
     64 
     65    if (focusToolbox) {
     66      toolbox.win.focus();
     67    }
     68 
     69    await cancelPickerByShortcut(toolbox, key, options);
     70    ok(
     71      isSelectedMarkupNodeInView(inspector),
     72      "The currently selected node is focused back on the screen."
     73    );
     74 
     75    is(
     76      await highlighterTestFront.isHighlighting(),
     77      false,
     78      "The highlighter was hidden"
     79    );
     80  });
     81 }
     82 
     83 async function cancelPickerByShortcut(toolbox, key, options) {
     84  info("Key pressed. Waiting for picker to be canceled.");
     85  const onStopped = toolbox.nodePicker.once("picker-node-canceled");
     86  EventUtils.synthesizeKey(key, options, toolbox.win);
     87  return onStopped;
     88 }
     89 
     90 function isSelectedMarkupNodeInView(inspector) {
     91  const selectedNodeContainer = inspector.markup._selectedContainer.elt;
     92  const bounds = selectedNodeContainer.getBoundingClientRect();
     93  return bounds.top > 0 && bounds.bottom > 0;
     94 }