summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/browser_filter-editor-02.js
blob: 2a69f5265f36f698970cad1be0a236b67a007a66 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that the Filter Editor Widget renders filters correctly

const {
  CSSFilterEditorWidget,
} = require("resource://devtools/client/shared/widgets/FilterWidget.js");

const STRINGS_URI = "devtools/client/locales/filterwidget.properties";
const L10N = new LocalizationHelper(STRINGS_URI);

const TEST_URI = CHROME_URL_ROOT + "doc_filter-editor-01.html";

add_task(async function () {
  const { doc } = await createHost("bottom", TEST_URI);

  const TEST_DATA = [
    {
      cssValue:
        "blur(2px) contrast(200%) hue-rotate(20.2deg) drop-shadow(5px 5px black)",
      expected: [
        {
          label: "blur",
          value: "2",
          unit: "px",
        },
        {
          label: "contrast",
          value: "200",
          unit: "%",
        },
        {
          label: "hue-rotate",
          value: "20.2",
          unit: "deg",
        },
        {
          label: "drop-shadow",
          value: "5px 5px black",
          unit: null,
        },
      ],
    },
    {
      cssValue: "hue-rotate(420.2deg)",
      expected: [
        {
          label: "hue-rotate",
          value: "420.2",
          unit: "deg",
        },
      ],
    },
    {
      cssValue: "url(example.svg)",
      expected: [
        {
          label: "url",
          value: "example.svg",
          unit: null,
        },
      ],
    },
    {
      cssValue: "none",
      expected: [],
    },
  ];

  const container = doc.querySelector("#filter-container");
  const widget = new CSSFilterEditorWidget(container, "none");

  info("Test rendering of different types");

  for (const { cssValue, expected } of TEST_DATA) {
    widget.setCssValue(cssValue);

    if (cssValue === "none") {
      const text = container.querySelector("#filters").textContent;
      Assert.greater(
        text.indexOf(L10N.getStr("emptyFilterList")),
        -1,
        "Contains |emptyFilterList| string when given value 'none'"
      );
      Assert.greater(
        text.indexOf(L10N.getStr("addUsingList")),
        -1,
        "Contains |addUsingList| string when given value 'none'"
      );
      continue;
    }
    const filters = container.querySelectorAll(".filter");
    testRenderedFilters(filters, expected);
  }
  widget.destroy();
});

function testRenderedFilters(filters, expected) {
  for (const [index, filter] of [...filters].entries()) {
    const [name, value] = filter.children,
      label = name.children[1],
      [input, unit] = value.children;

    const eq = expected[index];
    is(label.textContent, eq.label, "Label should match");
    is(input.value, eq.value, "Values should match");
    if (eq.unit) {
      is(unit.textContent, eq.unit, "Unit should match");
    }
  }
}