tor-browser

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

browser_inspector_highlighter-measure_04.js (4290B)


      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 TEST_URL = "data:text/html;charset=utf-8,measuring tool test";
      8 
      9 const PREFIX = "measuring-tool-";
     10 const HANDLER_PREFIX = "handler-";
     11 const { TYPES } = ChromeUtils.importESModule(
     12  "resource://devtools/shared/highlighters.mjs"
     13 );
     14 const HIGHLIGHTER_TYPE = TYPES.MEASURING;
     15 
     16 const X = 32;
     17 const Y = 20;
     18 const WIDTH = 160;
     19 const HEIGHT = 100;
     20 const X_OFFSET = 15;
     21 const Y_OFFSET = 10;
     22 
     23 const HANDLER_MAP = {
     24  top(areaWidth) {
     25    return { x: Math.round(areaWidth / 2), y: 0 };
     26  },
     27  topright(areaWidth) {
     28    return { x: areaWidth, y: 0 };
     29  },
     30  right(areaWidth, areaHeight) {
     31    return { x: areaWidth, y: Math.round(areaHeight / 2) };
     32  },
     33  bottomright(areaWidth, areaHeight) {
     34    return { x: areaWidth, y: areaHeight };
     35  },
     36  bottom(areaWidth, areaHeight) {
     37    return { x: Math.round(areaWidth / 2), y: areaHeight };
     38  },
     39  bottomleft(areaWidth, areaHeight) {
     40    return { x: 0, y: areaHeight };
     41  },
     42  left(areaWidth, areaHeight) {
     43    return { x: 0, y: Math.round(areaHeight / 2) };
     44  },
     45  topleft() {
     46    return { x: 0, y: 0 };
     47  },
     48 };
     49 
     50 add_task(async function () {
     51  const helper = await openInspectorForURL(TEST_URL).then(
     52    getHighlighterHelperFor(HIGHLIGHTER_TYPE)
     53  );
     54 
     55  const { show, finalize } = helper;
     56 
     57  helper.prefix = PREFIX;
     58 
     59  info("Showing the highlighter");
     60  await show();
     61 
     62  info("Creating the area");
     63  const { mouse } = helper;
     64  await mouse.down(X, Y);
     65  await mouse.move(X + WIDTH, Y + HEIGHT);
     66  await mouse.up();
     67 
     68  await canResizeAreaViaHandlers(helper);
     69 
     70  info("Hiding the highlighter");
     71  await finalize();
     72 });
     73 
     74 async function canResizeAreaViaHandlers(helper) {
     75  const { mouse } = helper;
     76 
     77  for (const handler of Object.keys(HANDLER_MAP)) {
     78    const { x: origHandlerX, y: origHandlerY } = await getHandlerCoords(
     79      helper,
     80      handler
     81    );
     82    const {
     83      x: origAreaX,
     84      y: origAreaY,
     85      width: origAreaWidth,
     86      height: origAreaHeight,
     87    } = await getAreaRect(helper);
     88    const absOrigHandlerX = origHandlerX + origAreaX;
     89    const absOrigHandlerY = origHandlerY + origAreaY;
     90 
     91    const delta = {
     92      x: handler.includes("left") ? X_OFFSET : 0,
     93      y: handler.includes("top") ? Y_OFFSET : 0,
     94      width:
     95        handler.includes("right") || handler.includes("left") ? X_OFFSET : 0,
     96      height:
     97        handler.includes("bottom") || handler.includes("top") ? Y_OFFSET : 0,
     98    };
     99 
    100    if (handler.includes("left")) {
    101      delta.width *= -1;
    102    }
    103    if (handler.includes("top")) {
    104      delta.height *= -1;
    105    }
    106 
    107    // Simulate drag & drop of handler
    108    await mouse.down(absOrigHandlerX, absOrigHandlerY);
    109    await mouse.move(absOrigHandlerX + X_OFFSET, absOrigHandlerY + Y_OFFSET);
    110    await mouse.up();
    111 
    112    const {
    113      x: areaX,
    114      y: areaY,
    115      width: areaWidth,
    116      height: areaHeight,
    117    } = await getAreaRect(helper);
    118    is(
    119      areaX,
    120      origAreaX + delta.x,
    121      `X coordinate of area correct after resizing using ${handler} handler`
    122    );
    123    is(
    124      areaY,
    125      origAreaY + delta.y,
    126      `Y coordinate of area correct after resizing using ${handler} handler`
    127    );
    128    is(
    129      areaWidth,
    130      origAreaWidth + delta.width,
    131      `Width of area correct after resizing using ${handler} handler`
    132    );
    133    is(
    134      areaHeight,
    135      origAreaHeight + delta.height,
    136      `Height of area correct after resizing using ${handler} handler`
    137    );
    138 
    139    const { x: handlerX, y: handlerY } = await getHandlerCoords(
    140      helper,
    141      handler
    142    );
    143    const { x: expectedX, y: expectedY } = HANDLER_MAP[handler](
    144      areaWidth,
    145      areaHeight
    146    );
    147    is(
    148      handlerX,
    149      expectedX,
    150      `X coordinate of ${handler} handler correct after resizing`
    151    );
    152    is(
    153      handlerY,
    154      expectedY,
    155      `Y coordinate of ${handler} handler correct after resizing`
    156    );
    157  }
    158 }
    159 
    160 async function getHandlerCoords({ getElementAttribute }, handler) {
    161  const handlerId = `${HANDLER_PREFIX}${handler}`;
    162  return {
    163    x: Math.round(await getElementAttribute(handlerId, "cx")),
    164    y: Math.round(await getElementAttribute(handlerId, "cy")),
    165  };
    166 }