tor-browser

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

browser_inspector_highlighter-geometry_06.js (5484B)


      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 the geometry editor resizes properly an element on all sides,
      8 // with different unit measures, and that arrow/handlers are updated correctly.
      9 
     10 const TEST_URL = URL_ROOT + "doc_inspector_highlighter-geometry_01.html";
     11 const ID = "geometry-editor-";
     12 const { TYPES } = ChromeUtils.importESModule(
     13  "resource://devtools/shared/highlighters.mjs"
     14 );
     15 const HIGHLIGHTER_TYPE = TYPES.GEOMETRY;
     16 
     17 const SIDES = ["top", "right", "bottom", "left"];
     18 
     19 // The object below contains all the tests for this unit test.
     20 // The property's name is the test's description, that points to an
     21 // object contains the steps (what side of the geometry editor to drag,
     22 // the amount of pixels) and the expectation.
     23 const TESTS = {
     24  "Drag top's handler along x and y, south-east direction": {
     25    expects: "Only y axis is used to updated the top's element value",
     26    drag: "top",
     27    by: { x: 10, y: 10 },
     28  },
     29  "Drag right's handler along x and y, south-east direction": {
     30    expects: "Only x axis is used to updated the right's element value",
     31    drag: "right",
     32    by: { x: 10, y: 10 },
     33  },
     34  "Drag bottom's handler along x and y, south-east direction": {
     35    expects: "Only y axis is used to updated the bottom's element value",
     36    drag: "bottom",
     37    by: { x: 10, y: 10 },
     38  },
     39  "Drag left's handler along x and y, south-east direction": {
     40    expects: "Only y axis is used to updated the left's element value",
     41    drag: "left",
     42    by: { x: 10, y: 10 },
     43  },
     44  "Drag top's handler along x and y, north-west direction": {
     45    expects: "Only y axis is used to updated the top's element value",
     46    drag: "top",
     47    by: { x: -20, y: -20 },
     48  },
     49  "Drag right's handler along x and y, north-west direction": {
     50    expects: "Only x axis is used to updated the right's element value",
     51    drag: "right",
     52    by: { x: -20, y: -20 },
     53  },
     54  "Drag bottom's handler along x and y, north-west direction": {
     55    expects: "Only y axis is used to updated the bottom's element value",
     56    drag: "bottom",
     57    by: { x: -20, y: -20 },
     58  },
     59  "Drag left's handler along x and y, north-west direction": {
     60    expects: "Only y axis is used to updated the left's element value",
     61    drag: "left",
     62    by: { x: -20, y: -20 },
     63  },
     64 };
     65 
     66 add_task(async function () {
     67  const inspector = await openInspectorForURL(TEST_URL);
     68  const helper = await getHighlighterHelperFor(HIGHLIGHTER_TYPE)(inspector);
     69 
     70  helper.prefix = ID;
     71 
     72  const { show, hide, finalize } = helper;
     73 
     74  info("Showing the highlighter");
     75  await show("#node2");
     76 
     77  for (const desc in TESTS) {
     78    await executeTest(helper, desc, TESTS[desc]);
     79  }
     80 
     81  info("Hiding the highlighter");
     82  await hide();
     83  await finalize();
     84 });
     85 
     86 async function executeTest(helper, desc, data) {
     87  info(desc);
     88 
     89  ok(
     90    await areElementAndHighlighterMovedCorrectly(helper, data.drag, data.by),
     91    data.expects
     92  );
     93 }
     94 
     95 async function areElementAndHighlighterMovedCorrectly(helper, side, by) {
     96  const { mouse, highlightedNode } = helper;
     97 
     98  const { x, y } = await getHandlerCoords(helper, side);
     99 
    100  const dx = x + by.x;
    101  const dy = y + by.y;
    102 
    103  const beforeDragStyle = await highlightedNode.getComputedStyle();
    104 
    105  // simulate drag & drop
    106  await mouse.down(x, y);
    107  await mouse.move(dx, dy);
    108  await mouse.up();
    109 
    110  await reflowContentPage();
    111 
    112  info(`Checking ${side} handler is moved correctly`);
    113  await isHandlerPositionUpdated(helper, side, x, y, by);
    114 
    115  let delta = side === "left" || side === "right" ? by.x : by.y;
    116  delta = delta * (side === "right" || side === "bottom" ? -1 : 1);
    117 
    118  info("Checking element's sides are correct after drag & drop");
    119  return areElementSideValuesCorrect(
    120    highlightedNode,
    121    beforeDragStyle,
    122    side,
    123    delta
    124  );
    125 }
    126 
    127 async function isHandlerPositionUpdated(helper, name, x, y, by) {
    128  const { x: afterDragX, y: afterDragY } = await getHandlerCoords(helper, name);
    129 
    130  if (name === "left" || name === "right") {
    131    is(afterDragX, x + by.x, `${name} handler's x axis updated.`);
    132    is(afterDragY, y, `${name} handler's y axis unchanged.`);
    133  } else {
    134    is(afterDragX, x, `${name} handler's x axis unchanged.`);
    135    is(afterDragY, y + by.y, `${name} handler's y axis updated.`);
    136  }
    137 }
    138 
    139 async function areElementSideValuesCorrect(node, beforeDragStyle, name, delta) {
    140  const afterDragStyle = await node.getComputedStyle();
    141  let isSideCorrect = true;
    142 
    143  for (const side of SIDES) {
    144    const afterValue = Math.round(parseFloat(afterDragStyle[side].value));
    145    const beforeValue = Math.round(parseFloat(beforeDragStyle[side].value));
    146 
    147    if (side === name) {
    148      // `isSideCorrect` is used only as test's return value, not to perform
    149      // the actual test, because with `is` instead of `ok` we gather more
    150      // information in case of failure
    151      isSideCorrect = isSideCorrect && afterValue === beforeValue + delta;
    152 
    153      is(afterValue, beforeValue + delta, `${side} is updated.`);
    154    } else {
    155      isSideCorrect = isSideCorrect && afterValue === beforeValue;
    156 
    157      is(afterValue, beforeValue, `${side} is unchaged.`);
    158    }
    159  }
    160 
    161  return isSideCorrect;
    162 }
    163 
    164 async function getHandlerCoords({ getElementAttribute }, side) {
    165  return {
    166    x: Math.round(await getElementAttribute("handler-" + side, "cx")),
    167    y: Math.round(await getElementAttribute("handler-" + side, "cy")),
    168  };
    169 }