summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/rules/test/browser_rules_colorpicker-contrast-ratio.js
blob: 6bdb9dd9d327e8bc91b840d708648271b033ee1c (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests color pickers displays expected contrast ratio information.

const TEST_URI = `
  <style type="text/css">
    :root {
      --title-color: #000;
    }

    body {
      color: #eee;
      background-color: #eee;
    }

    h1 {
      color: var(--title-color);
    }

    div {
      color: var(--title-color);
      /* Try to to have consistent results over different platforms:
         - using hardstop-ish gradient so the min and max contrast are computed against white and black background
         - having min-content width will make sure we get the gradient only cover the text, and not the whole screen width
      */
      background-image: linear-gradient(to right, black, white);
      width: min-content;
      font-size: 100px;
    }

    section {
      color: color-mix(in srgb, blue, var(--title-color) 50%);
    }
  </style>
  <h1>Testing the color picker contrast ratio data</h1>
  <div>————</div>
  <section>mixed colors</section>
`;

add_task(async function () {
  await addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
  const { inspector, view } = await openRuleView();

  await checkColorPickerConstrastData({
    view,
    label: "Displays contrast information on color property",
    ruleViewPropertyEl: getRuleViewProperty(view, "body", "color"),
    expectVisibleContrast: true,
    expectedContrastValueResult: "FAIL",
    expectedContrastValueTitle:
      "Does not meet WCAG standards for accessible text. Calculated against background: rgba(238,238,238,1)",
    expectedContrastValueScore: "1.00",
  });

  await checkColorPickerConstrastData({
    view,
    label: "Does not display contrast information on background-color property",
    ruleViewPropertyEl: getRuleViewProperty(view, "body", "background-color"),
    expectVisibleContrast: false,
  });

  await selectNode("h1", inspector);
  await checkColorPickerConstrastData({
    view,
    label: "Displays contrast information on color from CSS variable",
    ruleViewPropertyEl: getRuleViewProperty(view, "h1", "color"),
    expectVisibleContrast: true,
    expectedContrastValueResult: "AAA",
    expectedContrastValueTitle:
      "Meets WCAG AAA standards for accessible text. Calculated against background: rgba(238,238,238,1)",
    expectedContrastValueScore: "18.10",
  });

  await selectNode("div", inspector);
  await checkColorPickerConstrastData({
    view,
    label:
      "Displays range contrast information on color against gradient background",
    ruleViewPropertyEl: getRuleViewProperty(view, "div", "color"),
    expectVisibleContrast: true,
    expectContrastRange: true,
    expectedMinContrastValueResult: "FAIL",
    expectedMinContrastValueTitle:
      "Does not meet WCAG standards for accessible text. Calculated against background: rgba(0,0,0,1)",
    expectedMinContrastValueScore: "1.00",
    expectedMaxContrastValueResult: "AAA",
    expectedMaxContrastValueTitle:
      "Meets WCAG AAA standards for accessible text. Calculated against background: rgba(255,255,255,1)",
    expectedMaxContrastValueScore: "19.77",
  });

  await selectNode("section", inspector);
  await checkColorPickerConstrastData({
    view,
    label:
      "Does not displays contrast information on color within color-mix function (#1)",
    ruleViewPropertyEl: getRuleViewProperty(view, "section", "color"),
    swatchIndex: 0,
    expectVisibleContrast: false,
  });
  await checkColorPickerConstrastData({
    view,
    label:
      "Does not displays contrast information on color within color-mix function (#2)",
    ruleViewPropertyEl: getRuleViewProperty(view, "section", "color"),
    swatchIndex: 1,
    expectVisibleContrast: false,
  });
});

async function checkColorPickerConstrastData({
  view,
  ruleViewPropertyEl,
  label,
  swatchIndex = 0,
  expectVisibleContrast,
  expectedContrastValueResult,
  expectedContrastValueTitle,
  expectedContrastValueScore,
  expectContrastRange = false,
  expectedMinContrastValueResult,
  expectedMinContrastValueTitle,
  expectedMinContrastValueScore,
  expectedMaxContrastValueResult,
  expectedMaxContrastValueTitle,
  expectedMaxContrastValueScore,
}) {
  info(`Checking color picker: "${label}"`);
  const cPicker = view.tooltips.getTooltip("colorPicker");
  ok(cPicker, "The rule-view has an expected colorPicker widget");
  const cPickerPanel = cPicker.tooltip.panel;
  ok(cPickerPanel, "The XUL panel for the color picker exists");

  const colorSwatch = ruleViewPropertyEl.valueSpan.querySelectorAll(
    ".ruleview-colorswatch"
  )[swatchIndex];

  const onColorPickerReady = cPicker.once("ready");
  colorSwatch.click();
  await onColorPickerReady;
  ok(true, "The color picker was displayed");

  const contrastEl = cPickerPanel.querySelector(".spectrum-color-contrast");

  if (!expectVisibleContrast) {
    ok(
      !contrastEl.classList.contains("visible"),
      "Contrast information is not displayed, as expected"
    );
    await hideTooltipAndWaitForRuleViewChanged(cPicker, view);
    return;
  }

  ok(
    contrastEl.classList.contains("visible"),
    "Contrast information is displayed"
  );
  is(
    contrastEl.classList.contains("range"),
    expectContrastRange,
    `Contrast information ${
      expectContrastRange ? "has" : "does not have"
    } a result range`
  );

  if (expectContrastRange) {
    const minContrastValueEl = contrastEl.querySelector(
      ".contrast-ratio-range .contrast-ratio-min .accessibility-contrast-value"
    );
    ok(
      minContrastValueEl.classList.contains(expectedMinContrastValueResult),
      `min contrast value has expected "${expectedMinContrastValueResult}" class`
    );
    // Scores vary from platform to platform, disable for now.
    // This should be re-enabled as part of  Bug 1780736
    // is(
    //   minContrastValueEl.title,
    //   expectedMinContrastValueTitle,
    //   "min contrast value has expected title"
    // );
    // is(
    //   minContrastValueEl.textContent,
    //   expectedMinContrastValueScore,
    //   "min contrast value shows expected score"
    // );

    const maxContrastValueEl = contrastEl.querySelector(
      ".contrast-ratio-range .contrast-ratio-max .accessibility-contrast-value"
    );
    ok(
      maxContrastValueEl.classList.contains(expectedMaxContrastValueResult),
      `max contrast value has expected "${expectedMaxContrastValueResult}" class`
    );
    // Scores vary from platform to platform, disable for now.
    // This should be re-enabled as part of  Bug 1780736
    // is(
    //   maxContrastValueEl.title,
    //   expectedMaxContrastValueTitle,
    //   "max contrast value has expected title"
    // );
    // is(
    //   maxContrastValueEl.textContent,
    //   expectedMaxContrastValueScore,
    //   "max contrast value shows expected score"
    // );
  } else {
    const contrastValueEl = contrastEl.querySelector(
      ".accessibility-contrast-value"
    );
    ok(
      contrastValueEl.classList.contains(expectedContrastValueResult),
      `contrast value has expected "${expectedContrastValueResult}" class`
    );
    is(
      contrastValueEl.title,
      expectedContrastValueTitle,
      "contrast value has expected title"
    );
    is(
      contrastValueEl.textContent,
      expectedContrastValueScore,
      "contrast value shows expected score"
    );
  }

  await hideTooltipAndWaitForRuleViewChanged(cPicker, view);
}