tor-browser

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

test_cubicBezier.js (4365B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Tests the CubicBezier API in the CubicBezierWidget module
      7 
      8 var {
      9  CubicBezier,
     10  parseTimingFunction,
     11 } = require("resource://devtools/client/shared/widgets/CubicBezierWidget.js");
     12 
     13 function run_test() {
     14  throwsWhenMissingCoordinates();
     15  throwsWhenIncorrectCoordinates();
     16  convertsStringCoordinates();
     17  coordinatesToStringOutputsAString();
     18  pointGettersReturnPointCoordinatesArrays();
     19  toStringOutputsCubicBezierValue();
     20  toStringOutputsCssPresetValues();
     21  testParseTimingFunction();
     22 }
     23 
     24 function throwsWhenMissingCoordinates() {
     25  do_check_throws(() => {
     26    new CubicBezier();
     27  }, "Throws an exception when coordinates are missing");
     28 }
     29 
     30 function throwsWhenIncorrectCoordinates() {
     31  do_check_throws(() => {
     32    new CubicBezier([]);
     33  }, "Throws an exception when coordinates are incorrect (empty array)");
     34 
     35  do_check_throws(() => {
     36    new CubicBezier([0, 0]);
     37  }, "Throws an exception when coordinates are incorrect (incomplete array)");
     38 
     39  do_check_throws(() => {
     40    new CubicBezier(["a", "b", "c", "d"]);
     41  }, "Throws an exception when coordinates are incorrect (invalid type)");
     42 
     43  do_check_throws(() => {
     44    new CubicBezier([1.5, 0, 1.5, 0]);
     45  }, "Throws an exception when coordinates are incorrect (time range invalid)");
     46 
     47  do_check_throws(() => {
     48    new CubicBezier([-0.5, 0, -0.5, 0]);
     49  }, "Throws an exception when coordinates are incorrect (time range invalid)");
     50 }
     51 
     52 function convertsStringCoordinates() {
     53  info("Converts string coordinates to numbers");
     54  const c = new CubicBezier(["0", "1", ".5", "-2"]);
     55 
     56  Assert.equal(c.coordinates[0], 0);
     57  Assert.equal(c.coordinates[1], 1);
     58  Assert.equal(c.coordinates[2], 0.5);
     59  Assert.equal(c.coordinates[3], -2);
     60 }
     61 
     62 function coordinatesToStringOutputsAString() {
     63  info("coordinates.toString() outputs a string representation");
     64 
     65  let c = new CubicBezier(["0", "1", "0.5", "-2"]);
     66  let string = c.coordinates.toString();
     67  Assert.equal(string, "0,1,.5,-2");
     68 
     69  c = new CubicBezier([1, 1, 1, 1]);
     70  string = c.coordinates.toString();
     71  Assert.equal(string, "1,1,1,1");
     72 }
     73 
     74 function pointGettersReturnPointCoordinatesArrays() {
     75  info("Points getters return arrays of coordinates");
     76 
     77  const c = new CubicBezier([0, 0.2, 0.5, 1]);
     78  Assert.equal(c.P1[0], 0);
     79  Assert.equal(c.P1[1], 0.2);
     80  Assert.equal(c.P2[0], 0.5);
     81  Assert.equal(c.P2[1], 1);
     82 }
     83 
     84 function toStringOutputsCubicBezierValue() {
     85  info("toString() outputs the cubic-bezier() value");
     86 
     87  const c = new CubicBezier([0, 1, 1, 0]);
     88  Assert.equal(c.toString(), "cubic-bezier(0,1,1,0)");
     89 }
     90 
     91 function toStringOutputsCssPresetValues() {
     92  info("toString() outputs the css predefined values");
     93 
     94  let c = new CubicBezier([0, 0, 1, 1]);
     95  Assert.equal(c.toString(), "linear");
     96 
     97  c = new CubicBezier([0.25, 0.1, 0.25, 1]);
     98  Assert.equal(c.toString(), "ease");
     99 
    100  c = new CubicBezier([0.42, 0, 1, 1]);
    101  Assert.equal(c.toString(), "ease-in");
    102 
    103  c = new CubicBezier([0, 0, 0.58, 1]);
    104  Assert.equal(c.toString(), "ease-out");
    105 
    106  c = new CubicBezier([0.42, 0, 0.58, 1]);
    107  Assert.equal(c.toString(), "ease-in-out");
    108 }
    109 
    110 function testParseTimingFunction() {
    111  info("test parseTimingFunction");
    112 
    113  for (const test of ["ease", "linear", "ease-in", "ease-out", "ease-in-out"]) {
    114    ok(parseTimingFunction(test), test);
    115  }
    116 
    117  ok(!parseTimingFunction("something"), "non-function token");
    118  ok(!parseTimingFunction("something()"), "non-cubic-bezier function");
    119  ok(
    120    !parseTimingFunction(
    121      "cubic-bezier(something)",
    122      "cubic-bezier with non-numeric argument"
    123    )
    124  );
    125  ok(!parseTimingFunction("cubic-bezier(1,2,3:7)", "did not see comma"));
    126  ok(!parseTimingFunction("cubic-bezier(1,2,3,7:", "did not see close paren"));
    127  ok(!parseTimingFunction("cubic-bezier(1,2", "early EOF after number"));
    128  ok(!parseTimingFunction("cubic-bezier(1,2,", "early EOF after comma"));
    129  deepEqual(
    130    parseTimingFunction("cubic-bezier(1,2,3,7)"),
    131    [1, 2, 3, 7],
    132    "correct invocation"
    133  );
    134  deepEqual(
    135    parseTimingFunction("cubic-bezier(1,  /* */ 2,3,   7  )"),
    136    [1, 2, 3, 7],
    137    "correct with comments and whitespace"
    138  );
    139 }
    140 
    141 function do_check_throws(cb, details) {
    142  info(details);
    143 
    144  let hasThrown = false;
    145  try {
    146    cb();
    147  } catch (e) {
    148    hasThrown = true;
    149  }
    150 
    151  Assert.ok(hasThrown);
    152 }