tor-browser

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

computed-testcommon.js (7812B)


      1 'use strict';
      2 
      3 /**
      4 * Create test that a CSS property computes to the expected value.
      5 * The document element #target is used to perform the test.
      6 *
      7 * @param {string} property  The name of the CSS property being tested.
      8 * @param {string} specified A specified value for the property.
      9 * @param {string|array} computed  The expected computed value,
     10 *                                 or an array of permitted computed value.
     11 *                                 If omitted, defaults to specified.
     12 * @param {string} titleExtra Additional information to put in test output.
     13 * @param {object} options  Additional test information, such as a custom
     14 *                          comparison function required for color tests.
     15 *                          comparisonFunction is a function that takes two
     16 *                          arguments, actual and expected and contains asserts.
     17 */
     18 function test_computed_value(property, specified, computed, titleExtra, options = {}) {
     19  if (!computed)
     20    computed = specified;
     21 
     22  test(() => {
     23    const target = document.getElementById('target');
     24    assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style");
     25    assert_true(CSS.supports(property, specified), "'" + specified + "' is a supported value for " + property + ".");
     26    target.style[property] = '';
     27    target.style[property] = specified;
     28 
     29    let readValue = getComputedStyle(target)[property];
     30    if (options.comparisonFunction) {
     31      options.comparisonFunction(readValue, computed);
     32    } else if (Array.isArray(computed)) {
     33      assert_in_array(readValue, computed);
     34    } else {
     35      assert_equals(readValue, computed);
     36    }
     37    if (readValue !== specified) {
     38      target.style[property] = '';
     39      target.style[property] = readValue;
     40      assert_equals(getComputedStyle(target)[property], readValue,
     41                    'computed value should round-trip');
     42    }
     43  }, `Property ${property} value '${specified}'${titleExtra ? ' ' + titleExtra : ''}`);
     44 }
     45 
     46 function testComputedValueGreaterOrLowerThan(property, specified, expected, titleExtra) {
     47    test(() => {
     48      const target = document.getElementById('target');
     49      assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style");
     50      assert_true(CSS.supports(property, specified), "'" + specified + "' is a supported value for " + property + ".");
     51      target.style[property] = '';
     52      target.style[property] = specified;
     53      let readValue = parseFloat(getComputedStyle(target)[property]);
     54      assert_true(isFinite(readValue), specified + " expected finite value but got " + readValue)
     55      assert_false(isNaN(readValue),   specified + " expected finite value but got " + readValue)
     56      if (expected > 0)
     57        assert_greater_than_equal(readValue, expected, specified);
     58      else
     59        assert_less_than_equal(readValue, expected, specified);
     60  }, `Property ${property} value '${specified}'${titleExtra ? ' ' + titleExtra : ''}`);
     61 }
     62 
     63 function testTransformValuesCloseTo(specified, epsilon, expectedValue, description)
     64 {
     65    if(!description) {
     66      description = `Property ${specified} value expected same with ${expectedValue} in +/-${epsilon}`
     67    }
     68 
     69    test(function()
     70    {
     71        var targetElement = document.getElementById("target");
     72        targetElement.style.setProperty('transform', "initial");
     73 
     74        /*
     75        Since we are running many consecutive tests on the same
     76        element, then it is necessary to reset its property
     77        to an initial value before actually re-testing it.
     78        */
     79 
     80        targetElement.style.setProperty('transform', specified);
     81 
     82        var computedCalcValue = getComputedStyle(targetElement)['transform'];
     83 
     84        /*
     85        We first strip out the word "matrix" with the
     86        opening parenthesis "(" and the closing
     87        parenthesis ")"
     88        */
     89 
     90        computedCalcValue = computedCalcValue.replace("matrix(", "").replace(")", "");
     91 
     92        /*
     93        Then, we split the string at each comma ","
     94        and store the resulting 6 sub-strings into
     95        tableSplitComputedCalcValue
     96        */
     97 
     98        var tableSplitCalcValue = computedCalcValue.split(",");
     99 
    100        /*
    101        We convert the 6 sub-strings into numerical floating values
    102        so that mathematical operations (subtraction, absolute value,
    103        comparison) can be performed.
    104        */
    105 
    106        tableSplitCalcValue[0] = parseFloat(tableSplitCalcValue[0]);
    107        tableSplitCalcValue[1] = parseFloat(tableSplitCalcValue[1]);
    108        tableSplitCalcValue[2] = parseFloat(tableSplitCalcValue[2]);
    109        tableSplitCalcValue[3] = parseFloat(tableSplitCalcValue[3]);
    110        tableSplitCalcValue[4] = parseFloat(tableSplitCalcValue[4]);
    111        tableSplitCalcValue[5] = parseFloat(tableSplitCalcValue[5]);
    112 
    113        /*
    114        Now, we execute the same steps with the expectedValue
    115        */
    116 
    117        targetElement.style.setProperty('transform', expectedValue);
    118 
    119        var computedExpectedValue = getComputedStyle(targetElement)['transform'];
    120 
    121        /*
    122        We first strip out the word "matrix" with the
    123        opening parenthesis "(" and the closing
    124        parenthesis ")"
    125        */
    126 
    127        computedExpectedValue = computedExpectedValue.replace("matrix(", "").replace(")", "");
    128 
    129        /*
    130        Then, we split the string at each comma ","
    131        and store the resulting 6 sub-strings into
    132        tableSplitComputedCalcValue
    133        */
    134 
    135        var tableSplitExpectedValue = computedExpectedValue.split(",");
    136 
    137        /*
    138        We convert the 6 sub-strings into numerical floating values
    139        so that mathematical operations (subtraction, absolute value,
    140        comparison) can be performed.
    141        */
    142 
    143        tableSplitExpectedValue[0] = parseFloat(tableSplitExpectedValue[0]);
    144        tableSplitExpectedValue[1] = parseFloat(tableSplitExpectedValue[1]);
    145        tableSplitExpectedValue[2] = parseFloat(tableSplitExpectedValue[2]);
    146        tableSplitExpectedValue[3] = parseFloat(tableSplitExpectedValue[3]);
    147        tableSplitExpectedValue[4] = parseFloat(tableSplitExpectedValue[4]);
    148        tableSplitExpectedValue[5] = parseFloat(tableSplitExpectedValue[5]);
    149 
    150        assert_array_approx_equals(tableSplitCalcValue, tableSplitExpectedValue, epsilon);
    151    } , description);
    152 
    153 }
    154 
    155 function test_pseudo_computed_value(pseudo, property, specified, computed, titleExtra) {
    156  if (!computed)
    157    computed = specified;
    158 
    159  test(() => {
    160    assert_true(/^::\w+$/.test(pseudo), pseudo + " doesn't seem to be a pseudo-element");
    161    const styleElement = document.createElement("style");
    162    document.documentElement.appendChild(styleElement);
    163    try {
    164      const {sheet} = styleElement;
    165      sheet.insertRule("#target" + pseudo + "{}");
    166      const {style} = sheet.cssRules[0];
    167      const target = document.getElementById('target');
    168 
    169      assert_true(property in getComputedStyle(target, pseudo), property + " doesn't seem to be supported in the computed style");
    170      assert_true(CSS.supports(property, specified), "'" + specified + "' is a supported value for " + property + ".");
    171      style[property] = specified;
    172 
    173      let readValue = getComputedStyle(target, pseudo)[property];
    174      if (Array.isArray(computed)) {
    175        assert_in_array(readValue, computed);
    176      } else {
    177        assert_equals(readValue, computed);
    178      }
    179      if (readValue !== specified) {
    180        style[property] = '';
    181        style[property] = readValue;
    182        assert_equals(getComputedStyle(target, pseudo)[property], readValue,
    183                      'computed value should round-trip');
    184      }
    185    } finally {
    186      document.documentElement.removeChild(styleElement);
    187    }
    188  }, `Property ${property} value '${specified}' in ${pseudo}${titleExtra ? ' ' + titleExtra : ''}`);
    189 }