tor-browser

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

test_shapes_highlighter_helpers.js (5761B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 /**
      5 * Test the helper functions of the shapes highlighter.
      6 */
      7 
      8 "use strict";
      9 
     10 const {
     11  splitCoords,
     12  coordToPercent,
     13  evalCalcExpression,
     14  shapeModeToCssPropertyName,
     15  getCirclePath,
     16  getDecimalPrecision,
     17  getUnit,
     18 } = require("resource://devtools/server/actors/highlighters/shapes.js");
     19 
     20 function run_test() {
     21  test_split_coords();
     22  test_coord_to_percent();
     23  test_eval_calc_expression();
     24  test_shape_mode_to_css_property_name();
     25  test_get_circle_path();
     26  test_get_decimal_precision();
     27  test_get_unit();
     28  run_next_test();
     29 }
     30 
     31 function test_split_coords() {
     32  const tests = [
     33    {
     34      desc: "splitCoords for basic coordinate pair",
     35      expr: "30% 20%",
     36      expected: ["30%", "20%"],
     37    },
     38    {
     39      desc: "splitCoords for coord pair with calc()",
     40      expr: "calc(50px + 20%) 30%",
     41      expected: ["calc(50px\u00a0+\u00a020%)", "30%"],
     42    },
     43  ];
     44 
     45  for (const { desc, expr, expected } of tests) {
     46    deepEqual(splitCoords(expr), expected, desc);
     47  }
     48 }
     49 
     50 function test_coord_to_percent() {
     51  const size = 1000;
     52  const tests = [
     53    {
     54      desc: "coordToPercent for percent value",
     55      expr: "50%",
     56      expected: 50,
     57    },
     58    {
     59      desc: "coordToPercent for px value",
     60      expr: "500px",
     61      expected: 50,
     62    },
     63    {
     64      desc: "coordToPercent for zero value",
     65      expr: "0",
     66      expected: 0,
     67    },
     68  ];
     69 
     70  for (const { desc, expr, expected } of tests) {
     71    equal(coordToPercent(expr, size), expected, desc);
     72  }
     73 }
     74 
     75 function test_eval_calc_expression() {
     76  const size = 1000;
     77  const tests = [
     78    {
     79      desc: "evalCalcExpression with one value",
     80      expr: "50%",
     81      expected: 50,
     82    },
     83    {
     84      desc: "evalCalcExpression with percent and px values",
     85      expr: "50% + 100px",
     86      expected: 60,
     87    },
     88    {
     89      desc: "evalCalcExpression with a zero value",
     90      expr: "0 + 100px",
     91      expected: 10,
     92    },
     93    {
     94      desc: "evalCalcExpression with a negative value",
     95      expr: "-200px+50%",
     96      expected: 30,
     97    },
     98  ];
     99 
    100  for (const { desc, expr, expected } of tests) {
    101    equal(evalCalcExpression(expr, size), expected, desc);
    102  }
    103 }
    104 
    105 function test_shape_mode_to_css_property_name() {
    106  const tests = [
    107    {
    108      desc: "shapeModeToCssPropertyName for clip-path",
    109      expr: "cssClipPath",
    110      expected: "clipPath",
    111    },
    112    {
    113      desc: "shapeModeToCssPropertyName for shape-outside",
    114      expr: "cssShapeOutside",
    115      expected: "shapeOutside",
    116    },
    117  ];
    118 
    119  for (const { desc, expr, expected } of tests) {
    120    equal(shapeModeToCssPropertyName(expr), expected, desc);
    121  }
    122 }
    123 
    124 function test_get_circle_path() {
    125  const tests = [
    126    {
    127      desc: "getCirclePath with size 5, no resizing, no zoom, 1:1 ratio",
    128      size: 5,
    129      cx: 0,
    130      cy: 0,
    131      width: 100,
    132      height: 100,
    133      zoom: 1,
    134      expected: "M-5,0a5,5 0 1,0 10,0a5,5 0 1,0 -10,0",
    135    },
    136    {
    137      desc: "getCirclePath with size 7, resizing, no zoom, 1:1 ratio",
    138      size: 7,
    139      cx: 0,
    140      cy: 0,
    141      width: 200,
    142      height: 200,
    143      zoom: 1,
    144      expected: "M-3.5,0a3.5,3.5 0 1,0 7,0a3.5,3.5 0 1,0 -7,0",
    145    },
    146    {
    147      desc: "getCirclePath with size 5, resizing, zoom, 1:1 ratio",
    148      size: 5,
    149      cx: 0,
    150      cy: 0,
    151      width: 200,
    152      height: 200,
    153      zoom: 2,
    154      expected: "M-1.25,0a1.25,1.25 0 1,0 2.5,0a1.25,1.25 0 1,0 -2.5,0",
    155    },
    156    {
    157      desc: "getCirclePath with size 5, resizing, zoom, non-square ratio",
    158      size: 5,
    159      cx: 0,
    160      cy: 0,
    161      width: 100,
    162      height: 200,
    163      zoom: 2,
    164      expected: "M-2.5,0a2.5,1.25 0 1,0 5,0a2.5,1.25 0 1,0 -5,0",
    165    },
    166  ];
    167 
    168  for (const { desc, size, cx, cy, width, height, zoom, expected } of tests) {
    169    equal(getCirclePath(size, cx, cy, width, height, zoom), expected, desc);
    170  }
    171 }
    172 
    173 function test_get_decimal_precision() {
    174  const tests = [
    175    {
    176      desc: "getDecimalPrecision with px",
    177      expr: "px",
    178      expected: 0,
    179    },
    180    {
    181      desc: "getDecimalPrecision with %",
    182      expr: "%",
    183      expected: 2,
    184    },
    185    {
    186      desc: "getDecimalPrecision with em",
    187      expr: "em",
    188      expected: 2,
    189    },
    190    {
    191      desc: "getDecimalPrecision with undefined",
    192      expr: undefined,
    193      expected: 0,
    194    },
    195    {
    196      desc: "getDecimalPrecision with empty string",
    197      expr: "",
    198      expected: 0,
    199    },
    200  ];
    201 
    202  for (const { desc, expr, expected } of tests) {
    203    equal(getDecimalPrecision(expr), expected, desc);
    204  }
    205 }
    206 
    207 function test_get_unit() {
    208  const tests = [
    209    {
    210      desc: "getUnit with %",
    211      expr: "30%",
    212      expected: "%",
    213    },
    214    {
    215      desc: "getUnit with px",
    216      expr: "400px",
    217      expected: "px",
    218    },
    219    {
    220      desc: "getUnit with em",
    221      expr: "4em",
    222      expected: "em",
    223    },
    224    {
    225      desc: "getUnit with 0",
    226      expr: "0",
    227      expected: "px",
    228    },
    229    {
    230      desc: "getUnit with 0%",
    231      expr: "0%",
    232      expected: "%",
    233    },
    234    {
    235      desc: "getUnit with 0.00%",
    236      expr: "0.00%",
    237      expected: "%",
    238    },
    239    {
    240      desc: "getUnit with 0px",
    241      expr: "0px",
    242      expected: "px",
    243    },
    244    {
    245      desc: "getUnit with 0em",
    246      expr: "0em",
    247      expected: "em",
    248    },
    249    {
    250      desc: "getUnit with calc",
    251      expr: "calc(30px + 5%)",
    252      expected: "px",
    253    },
    254    {
    255      desc: "getUnit with var",
    256      expr: "var(--variable)",
    257      expected: "px",
    258    },
    259    {
    260      desc: "getUnit with closest-side",
    261      expr: "closest-side",
    262      expected: "px",
    263    },
    264    {
    265      desc: "getUnit with farthest-side",
    266      expr: "farthest-side",
    267      expected: "px",
    268    },
    269  ];
    270 
    271  for (const { desc, expr, expected } of tests) {
    272    equal(getUnit(expr), expected, desc);
    273  }
    274 }