tor-browser

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

browser_computed_getNodeInfo.js (5507B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Tests various output of the computed-view's getNodeInfo method.
      7 // This method is used by the HighlightersOverlay and TooltipsOverlay on mouseover to
      8 // decide which highlighter or tooltip to show when hovering over a value/name/selector
      9 // if any.
     10 //
     11 // For instance, browser_ruleview_selector-highlighter_01.js and
     12 // browser_ruleview_selector-highlighter_02.js test that the selector
     13 // highlighter appear when hovering over a selector in the rule-view.
     14 // Since the code to make this work for the computed-view is 90% the same,
     15 // there is no need for testing it again here.
     16 // This test however serves as a unit test for getNodeInfo.
     17 
     18 const {
     19  VIEW_NODE_SELECTOR_TYPE,
     20  VIEW_NODE_PROPERTY_TYPE,
     21  VIEW_NODE_VALUE_TYPE,
     22  VIEW_NODE_IMAGE_URL_TYPE,
     23 } = require("resource://devtools/client/inspector/shared/node-types.js");
     24 
     25 const TEST_URI = `
     26  <style type="text/css">
     27    body {
     28      background: red;
     29      color: white;
     30    }
     31    div {
     32      background: green;
     33    }
     34    div div {
     35      background-color: yellow;
     36      background-image: url(chrome://branding/content/icon64.png);
     37      color: red;
     38    }
     39  </style>
     40  <div><div id="testElement">Test element</div></div>
     41 `;
     42 
     43 // Each item in this array must have the following properties:
     44 // - desc {String} will be logged for information
     45 // - getHoveredNode {Generator Function} received the computed-view instance as
     46 //   argument and must return the node to be tested
     47 // - assertNodeInfo {Function} should check the validity of the nodeInfo
     48 //   argument it receives
     49 const TEST_DATA = [
     50  {
     51    desc: "Testing a null node",
     52    getHoveredNode() {
     53      return null;
     54    },
     55    assertNodeInfo(nodeInfo) {
     56      is(nodeInfo, null);
     57    },
     58  },
     59  {
     60    desc: "Testing a useless node",
     61    getHoveredNode(view) {
     62      return view.element;
     63    },
     64    assertNodeInfo(nodeInfo) {
     65      is(nodeInfo, null);
     66    },
     67  },
     68  {
     69    desc: "Testing a property name",
     70    getHoveredNode(view) {
     71      return getComputedViewProperty(view, "color").nameSpan;
     72    },
     73    assertNodeInfo(nodeInfo) {
     74      is(nodeInfo.type, VIEW_NODE_PROPERTY_TYPE);
     75      ok("property" in nodeInfo.value);
     76      ok("value" in nodeInfo.value);
     77      is(nodeInfo.value.property, "color");
     78      is(nodeInfo.value.value, "rgb(255, 0, 0)");
     79    },
     80  },
     81  {
     82    desc: "Testing a property value",
     83    getHoveredNode(view) {
     84      return getComputedViewProperty(view, "color").valueSpan;
     85    },
     86    assertNodeInfo(nodeInfo) {
     87      is(nodeInfo.type, VIEW_NODE_VALUE_TYPE);
     88      ok("property" in nodeInfo.value);
     89      ok("value" in nodeInfo.value);
     90      is(nodeInfo.value.property, "color");
     91      is(nodeInfo.value.value, "rgb(255, 0, 0)");
     92    },
     93  },
     94  {
     95    desc: "Testing an image url",
     96    getHoveredNode(view) {
     97      const { valueSpan } = getComputedViewProperty(view, "background-image");
     98      return valueSpan.querySelector(".theme-link");
     99    },
    100    assertNodeInfo(nodeInfo) {
    101      is(nodeInfo.type, VIEW_NODE_IMAGE_URL_TYPE);
    102      ok("property" in nodeInfo.value);
    103      ok("value" in nodeInfo.value);
    104      is(nodeInfo.value.property, "background-image");
    105      is(nodeInfo.value.value, 'url("chrome://branding/content/icon64.png")');
    106      is(nodeInfo.value.url, "chrome://branding/content/icon64.png");
    107    },
    108  },
    109  {
    110    desc: "Testing a matched rule selector (bestmatch)",
    111    async getHoveredNode(view) {
    112      const el = await getComputedViewMatchedRules(view, "background-color");
    113      return el.querySelector(".bestmatch");
    114    },
    115    assertNodeInfo(nodeInfo) {
    116      is(nodeInfo.type, VIEW_NODE_SELECTOR_TYPE);
    117      is(nodeInfo.value, "div div");
    118    },
    119  },
    120  {
    121    desc: "Testing a matched rule selector (matched)",
    122    async getHoveredNode(view) {
    123      const el = await getComputedViewMatchedRules(view, "background-color");
    124      return el.querySelector(".matched");
    125    },
    126    assertNodeInfo(nodeInfo) {
    127      is(nodeInfo.type, VIEW_NODE_SELECTOR_TYPE);
    128      is(nodeInfo.value, "div");
    129    },
    130  },
    131  {
    132    desc: "Testing a matched rule selector (parentmatch)",
    133    async getHoveredNode(view) {
    134      const el = await getComputedViewMatchedRules(view, "color");
    135      return el.querySelector(".parentmatch");
    136    },
    137    assertNodeInfo(nodeInfo) {
    138      is(nodeInfo.type, VIEW_NODE_SELECTOR_TYPE);
    139      is(nodeInfo.value, "body");
    140    },
    141  },
    142  {
    143    desc: "Testing a matched rule value",
    144    async getHoveredNode(view) {
    145      const el = await getComputedViewMatchedRules(view, "color");
    146      return el.querySelector(".computed-other-property-value");
    147    },
    148    assertNodeInfo(nodeInfo) {
    149      is(nodeInfo.type, VIEW_NODE_VALUE_TYPE);
    150      is(nodeInfo.value.property, "color");
    151      is(nodeInfo.value.value, "red");
    152    },
    153  },
    154  {
    155    desc: "Testing a matched rule stylesheet link",
    156    async getHoveredNode(view) {
    157      const el = await getComputedViewMatchedRules(view, "color");
    158      return el.querySelector(".rule-link .theme-link");
    159    },
    160    assertNodeInfo(nodeInfo) {
    161      is(nodeInfo, null);
    162    },
    163  },
    164 ];
    165 
    166 add_task(async function () {
    167  await addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
    168  const { inspector, view } = await openComputedView();
    169  await selectNode("#testElement", inspector);
    170 
    171  for (const { desc, getHoveredNode, assertNodeInfo } of TEST_DATA) {
    172    info(desc);
    173    const nodeInfo = view.getNodeInfo(await getHoveredNode(view));
    174    assertNodeInfo(nodeInfo);
    175  }
    176 });