summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/unit/test_cssColor-03.js
blob: c3ef5a5c2cb33249cf6c4d3cf8c53e12d8395446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test css-color-4 color function syntax and old-style syntax.

"use strict";

var Cu = Components.utils;
var Ci = Components.interfaces;
var Cc = Components.classes;

var {require, loader} = Cu.import("resource://devtools/shared/Loader.jsm", {});
const {colorUtils} = require("devtools/shared/css/color");

loader.lazyGetter(this, "DOMUtils", function () {
  return Cc["@mozilla.org/inspector/dom-utils;1"].getService(Ci.inIDOMUtils);
});

const OLD_STYLE_TESTS = [
  "rgb(255,0,192)",
  "RGB(255,0,192)",
  "RGB(100%,0%,83%)",
  "rgba(255,0,192,0.25)",
  "hsl(120, 100%, 40%)",
  "hsla(120, 100%, 40%, 0.25)",
  "hSlA(240, 100%, 50%, 0.25)",
];

const CSS_COLOR_4_TESTS = [
  "rgb(255.0,0.0,192.0)",
  "RGB(255 0 192)",
  "RGB(100% 0% 83% / 0.5)",
  "RGB(100%,0%,83%,0.5)",
  "RGB(100%,0%,83%,50%)",
  "rgba(255,0,192)",
  "hsl(50deg,15%,25%)",
  "hsl(240 25% 33%)",
  "hsl(50deg 25% 33% / 0.25)",
  "hsl(60 120% 60% / 0.25)",
  "hSlA(5turn 40% 4%)",
];

function run_test() {
  for (let test of OLD_STYLE_TESTS) {
    let ours = colorUtils.colorToRGBA(test, true);
    let platform = DOMUtils.colorToRGBA(test);
    deepEqual(ours, platform, "color " + test + " matches DOMUtils");
    ok(ours !== null, "'" + test + "' is a color");
  }

  for (let test of CSS_COLOR_4_TESTS) {
    let oursOld = colorUtils.colorToRGBA(test, true);
    let oursNew = colorUtils.colorToRGBA(test, false);
    let platform = DOMUtils.colorToRGBA(test);
    notEqual(oursOld, platform, "old style parser for color " + test +
             " should not match DOMUtils");
    ok(oursOld === null, "'" + test + "' is not a color with old parser");
    deepEqual(oursNew, platform, `css-color-4 parser for color ${test} matches DOMUtils`);
    ok(oursNew !== null, "'" + test + "' is a color with css-color-4 parser");
  }
}