tor-browser

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

browser_inspector_highlighter-measure-keybinding.js (4991B)


      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 const IS_OSX = Services.appinfo.OS === "Darwin";
      8 const VK_MOD = IS_OSX ? "VK_META" : "VK_CONTROL";
      9 const TEST_URL = "data:text/html;charset=utf-8,measuring tool test";
     10 
     11 const PREFIX = "measuring-tool-";
     12 const HANDLER_PREFIX = "handler-";
     13 const HIGHLIGHTED_HANDLER_CLASSNAME = "highlight";
     14 const { TYPES } = ChromeUtils.importESModule(
     15  "resource://devtools/shared/highlighters.mjs"
     16 );
     17 const HIGHLIGHTER_TYPE = TYPES.MEASURING;
     18 
     19 const SMALL_DELTA = 1;
     20 const LARGE_DELTA = 10;
     21 
     22 add_task(async function () {
     23  const helper = await openInspectorForURL(TEST_URL).then(
     24    getHighlighterHelperFor(HIGHLIGHTER_TYPE)
     25  );
     26 
     27  const { show, finalize } = helper;
     28 
     29  helper.prefix = PREFIX;
     30 
     31  info("Showing the highlighter");
     32  await show();
     33 
     34  info("Creating the area");
     35  const { mouse } = helper;
     36  await mouse.down(32, 20);
     37  await mouse.move(32 + 160, 20 + 100);
     38  await mouse.up();
     39 
     40  const arrow_tests = [
     41    {
     42      key: "VK_LEFT",
     43      shift: false,
     44      delta: -SMALL_DELTA,
     45      move: "dx",
     46      resize: "dw",
     47    },
     48    {
     49      key: "VK_LEFT",
     50      shift: true,
     51      delta: -LARGE_DELTA,
     52      move: "dx",
     53      resize: "dw",
     54    },
     55    {
     56      key: "VK_RIGHT",
     57      shift: false,
     58      delta: SMALL_DELTA,
     59      move: "dx",
     60      resize: "dw",
     61    },
     62    {
     63      key: "VK_RIGHT",
     64      shift: true,
     65      delta: LARGE_DELTA,
     66      move: "dx",
     67      resize: "dw",
     68    },
     69    {
     70      key: "VK_UP",
     71      shift: false,
     72      delta: -SMALL_DELTA,
     73      move: "dy",
     74      resize: "dh",
     75    },
     76    {
     77      key: "VK_UP",
     78      shift: true,
     79      delta: -LARGE_DELTA,
     80      move: "dy",
     81      resize: "dh",
     82    },
     83    {
     84      key: "VK_DOWN",
     85      shift: false,
     86      delta: SMALL_DELTA,
     87      move: "dy",
     88      resize: "dh",
     89    },
     90    {
     91      key: "VK_DOWN",
     92      shift: true,
     93      delta: LARGE_DELTA,
     94      move: "dy",
     95      resize: "dh",
     96    },
     97  ];
     98 
     99  for (const { key, shift, delta, move, resize } of arrow_tests) {
    100    await canMoveAreaViaKeybindings(helper, key, shift, { [move]: delta });
    101    await canResizeAreaViaKeybindings(helper, key, shift, { [resize]: delta });
    102  }
    103 
    104  // Test handler highlighting on Ctrl/Command hold
    105  await handlerShouldNotBeHighlighted(helper);
    106  BrowserTestUtils.synthesizeKey(
    107    VK_MOD,
    108    { type: "keydown" },
    109    gBrowser.selectedBrowser
    110  );
    111  await handlerShouldBeHighlighted(helper);
    112  BrowserTestUtils.synthesizeKey("VK_LEFT", {}, gBrowser.selectedBrowser);
    113  await handlerShouldBeHighlighted(helper);
    114  BrowserTestUtils.synthesizeKey(
    115    VK_MOD,
    116    { type: "keyup" },
    117    gBrowser.selectedBrowser
    118  );
    119  await handlerShouldNotBeHighlighted(helper);
    120 
    121  info("Hiding the highlighter");
    122  await finalize();
    123 });
    124 
    125 async function canMoveAreaViaKeybindings(helper, key, shiftHeld, deltas) {
    126  const { dx = 0, dy = 0 } = deltas;
    127 
    128  const {
    129    x: origAreaX,
    130    y: origAreaY,
    131    width: origAreaWidth,
    132    height: origAreaHeight,
    133  } = await getAreaRect(helper);
    134 
    135  const eventOptions = shiftHeld ? { shiftKey: true } : {};
    136  BrowserTestUtils.synthesizeKey(key, eventOptions, gBrowser.selectedBrowser);
    137 
    138  const {
    139    x: areaX,
    140    y: areaY,
    141    width: areaWidth,
    142    height: areaHeight,
    143  } = await getAreaRect(helper);
    144 
    145  is(areaX, origAreaX + dx, "X coordinate correct after moving");
    146  is(areaY, origAreaY + dy, "Y coordinate correct after moving");
    147  is(areaWidth, origAreaWidth, "Width unchanged after moving");
    148  is(areaHeight, origAreaHeight, "Height unchanged after moving");
    149 }
    150 
    151 async function canResizeAreaViaKeybindings(helper, key, shiftHeld, deltas) {
    152  const { dw = 0, dh = 0 } = deltas;
    153 
    154  const {
    155    x: origAreaX,
    156    y: origAreaY,
    157    width: origAreaWidth,
    158    height: origAreaHeight,
    159  } = await getAreaRect(helper);
    160 
    161  const eventOptions = IS_OSX ? { metaKey: true } : { ctrlKey: true };
    162  if (shiftHeld) {
    163    eventOptions.shiftKey = true;
    164  }
    165  BrowserTestUtils.synthesizeKey(key, eventOptions, gBrowser.selectedBrowser);
    166 
    167  const {
    168    x: areaX,
    169    y: areaY,
    170    width: areaWidth,
    171    height: areaHeight,
    172  } = await getAreaRect(helper);
    173 
    174  is(areaX, origAreaX, "X coordinate unchanged after resizing");
    175  is(areaY, origAreaY, "Y coordinate unchanged after resizing");
    176  is(areaWidth, origAreaWidth + dw, "Width correct after resizing");
    177  is(areaHeight, origAreaHeight + dh, "Height correct after resizing");
    178 }
    179 
    180 async function handlerIsHighlighted(helper) {
    181  const klass = await helper.getElementAttribute(
    182    `${HANDLER_PREFIX}topleft`,
    183    "class"
    184  );
    185  return klass.includes(HIGHLIGHTED_HANDLER_CLASSNAME);
    186 }
    187 
    188 async function handlerShouldBeHighlighted(helper) {
    189  ok(await handlerIsHighlighted(helper), "Origin handler is highlighted");
    190 }
    191 
    192 async function handlerShouldNotBeHighlighted(helper) {
    193  ok(
    194    !(await handlerIsHighlighted(helper)),
    195    "Origin handler is not highlighted"
    196  );
    197 }