tor-browser

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

browser_inspector_highlighter-measure_03.js (3481B)


      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 
     21 const HANDLER_MAP = {
     22  top(areaWidth) {
     23    return { x: Math.round(areaWidth / 2), y: 0 };
     24  },
     25  topright(areaWidth) {
     26    return { x: areaWidth, y: 0 };
     27  },
     28  right(areaWidth, areaHeight) {
     29    return { x: areaWidth, y: Math.round(areaHeight / 2) };
     30  },
     31  bottomright(areaWidth, areaHeight) {
     32    return { x: areaWidth, y: areaHeight };
     33  },
     34  bottom(areaWidth, areaHeight) {
     35    return { x: Math.round(areaWidth / 2), y: areaHeight };
     36  },
     37  bottomleft(areaWidth, areaHeight) {
     38    return { x: 0, y: areaHeight };
     39  },
     40  left(areaWidth, areaHeight) {
     41    return { x: 0, y: Math.round(areaHeight / 2) };
     42  },
     43  topleft() {
     44    return { x: 0, y: 0 };
     45  },
     46 };
     47 
     48 add_task(async function () {
     49  const helper = await openInspectorForURL(TEST_URL).then(
     50    getHighlighterHelperFor(HIGHLIGHTER_TYPE)
     51  );
     52 
     53  const { show, finalize } = helper;
     54 
     55  helper.prefix = PREFIX;
     56 
     57  info("Showing the highlighter");
     58  await show();
     59 
     60  await areHandlersHiddenByDefault(helper);
     61  await areHandlersHiddenOnAreaCreation(helper);
     62  await areHandlersCorrectlyShownAfterAreaCreation(helper);
     63 
     64  info("Hiding the highlighter");
     65  await finalize();
     66 });
     67 
     68 async function areHandlersHiddenByDefault({ isElementHidden, mouse }) {
     69  info("Checking that highlighter's handlers are hidden by default");
     70 
     71  await mouse.down(X, Y);
     72 
     73  for (const handler of Object.keys(HANDLER_MAP)) {
     74    const hidden = await isElementHidden(`${HANDLER_PREFIX}${handler}`);
     75    ok(hidden, `${handler} handler is hidden by default`);
     76  }
     77 }
     78 
     79 async function areHandlersHiddenOnAreaCreation({ isElementHidden, mouse }) {
     80  info("Checking that highlighter's handlers are hidden while area creation");
     81 
     82  await mouse.move(X + WIDTH, Y + HEIGHT);
     83 
     84  for (const handler of Object.keys(HANDLER_MAP)) {
     85    const hidden = await isElementHidden(`${HANDLER_PREFIX}${handler}`);
     86    ok(hidden, `${handler} handler is still hidden on area creation`);
     87  }
     88 }
     89 
     90 async function areHandlersCorrectlyShownAfterAreaCreation(helper) {
     91  info("Checking that highlighter's handlers are shown after area creation");
     92 
     93  const { isElementHidden, mouse } = helper;
     94 
     95  await mouse.up();
     96 
     97  for (const handler of Object.keys(HANDLER_MAP)) {
     98    const hidden = await isElementHidden(`${HANDLER_PREFIX}${handler}`);
     99    ok(!hidden, `${handler} handler is shown after area creation`);
    100 
    101    const { x: handlerX, y: handlerY } = await getHandlerCoords(
    102      helper,
    103      handler
    104    );
    105    const { x: expectedX, y: expectedY } = HANDLER_MAP[handler](WIDTH, HEIGHT);
    106    is(handlerX, expectedX, `x coordinate of ${handler} handler is correct`);
    107    is(handlerY, expectedY, `y coordinate of ${handler} handler is correct`);
    108  }
    109 }
    110 
    111 async function getHandlerCoords({ getElementAttribute }, handler) {
    112  const handlerId = `${HANDLER_PREFIX}${handler}`;
    113  return {
    114    x: Math.round(await getElementAttribute(handlerId, "cx")),
    115    y: Math.round(await getElementAttribute(handlerId, "cy")),
    116  };
    117 }