summaryrefslogtreecommitdiffstats
path: root/accessible/tests/browser/telemetry/browser_HCM_telemetry.js
blob: 23600d51c4996a0cb4e9b7dbc6f31b57df737eff (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/browser/components/preferences/tests/head.js",
  this
);

const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

registerCleanupFunction(() => {
  reset();
});

function reset() {
  // This (manually) runs after every task in this test suite.
  // We have to add this in because the initial state of
  // `document_color_use` affects the initial state of
  // `foreground_color`/`background_color` which can change our
  // starting telem samples. This ensures each tasks makes no lasting
  // state changes.
  Services.prefs.clearUserPref("browser.display.document_color_use");
  Services.prefs.clearUserPref("browser.display.permit_backplate");
  Services.prefs.clearUserPref("browser.display.use_system_colors");
  Services.telemetry.clearEvents();
  TelemetryTestUtils.assertNumberOfEvents(0);
  Services.prefs.clearUserPref("browser.display.foreground_color");
  Services.prefs.clearUserPref("browser.display.background_color");
}

async function openColorsDialog() {
  await openPreferencesViaOpenPreferencesAPI("general", { leaveOpen: true });
  const colorsButton =
    gBrowser.selectedBrowser.contentDocument.getElementById("colors");

  const dialogOpened = promiseLoadSubDialog(
    "chrome://browser/content/preferences/dialogs/colors.xhtml"
  );
  colorsButton.doCommand();

  return dialogOpened;
}

async function closeColorsDialog(dialogWin) {
  const dialogClosed = BrowserTestUtils.waitForEvent(dialogWin, "unload");
  const button = dialogWin.document
    .getElementById("ColorsDialog")
    .getButton("accept");
  button.focus();
  button.doCommand();
  return dialogClosed;
}

function verifyBackplate(expectedValue) {
  TelemetryTestUtils.assertScalar(
    TelemetryTestUtils.getProcessScalars("parent", false, true),
    "a11y.backplate",
    expectedValue,
    "Backplate scalar is logged as " + expectedValue
  );
}

function verifyUseSystemColors(expectedValue) {
  const snapshot = TelemetryTestUtils.getProcessScalars("parent", false, false);
  ok("a11y.use_system_colors" in snapshot, "System color usage was logged.");
  TelemetryTestUtils.assertScalar(
    snapshot,
    "a11y.use_system_colors",
    expectedValue,
    "System colors scalar is logged as " + expectedValue
  );
}

// The magic numbers below are the uint32_t values representing RGB white
// and RGB black respectively. They're directly captured as nsColors and
// follow the same bit-shift pattern.
function testIsWhite(pref, snapshot) {
  ok(pref in snapshot, "Scalar must be present.");
  is(snapshot[pref], 4294967295, "Scalar is logged as white");
}

function testIsBlack(pref, snapshot) {
  ok(pref in snapshot, "Scalar must be present.");
  is(snapshot[pref], 4278190080, "Scalar is logged as black");
}

async function setForegroundColor(color) {
  // Note: we set the foreground and background colors by modifying this pref
  // instead of setting the value attribute on the color input direclty.
  // This is because setting the value of the input with setAttribute
  // doesn't generate the correct event to save the new value to the prefs
  // store, so we have to do it ourselves.
  Services.prefs.setStringPref("browser.display.foreground_color", color);
}

async function setBackgroundColor(color) {
  Services.prefs.setStringPref("browser.display.background_color", color);
}

add_task(async function testInit() {
  const dialogWin = await openColorsDialog();
  const menulistHCM = dialogWin.document.getElementById("useDocumentColors");
  if (AppConstants.platform == "win") {
    is(
      Services.prefs.getBoolPref("browser.display.use_system_colors"),
      true,
      "Use system colours pref is init'd correctly"
    );
    verifyUseSystemColors(true);

    is(
      menulistHCM.value,
      "0",
      "HCM menulist should be set to only with HCM theme on startup for windows"
    );

    // Verify correct default value
    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "a11y.theme",
      "default",
      false
    );
  } else {
    is(
      Services.prefs.getBoolPref("browser.display.use_system_colors"),
      false,
      "Use system colours pref is init'd correctly"
    );
    verifyUseSystemColors(false);

    is(
      menulistHCM.value,
      "1",
      "HCM menulist should be set to never on startup for non-windows platforms"
    );

    // Verify correct default value
    TelemetryTestUtils.assertKeyedScalar(
      TelemetryTestUtils.getProcessScalars("parent", true, true),
      "a11y.theme",
      "always",
      false
    );

    await closeColorsDialog(dialogWin);

    // We should not have logged any colors
    let snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    ok(
      !("a11y.HCM_foreground" in snapshot),
      "Foreground color shouldn't be present."
    );
    ok(
      !("a11y.HCM_background" in snapshot),
      "Background color shouldn't be present."
    );

    // If we change the colors, our probes should not be updated
    await setForegroundColor("#ffffff"); // white
    await setBackgroundColor("#000000"); // black

    snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    ok(
      !("a11y.HCM_foreground" in snapshot),
      "Foreground color shouldn't be present."
    );
    ok(
      !("a11y.HCM_background" in snapshot),
      "Background color shouldn't be present."
    );
  }

  reset();
  gBrowser.removeCurrentTab();
});

add_task(async function testSetAlways() {
  const dialogWin = await openColorsDialog();
  const menulistHCM = dialogWin.document.getElementById("useDocumentColors");

  menulistHCM.doCommand();
  const newOption = dialogWin.document.getElementById("documentColorAlways");
  newOption.click();

  is(menulistHCM.value, "2", "HCM menulist should be set to always");

  await closeColorsDialog(dialogWin);

  // Verify correct initial value
  let snapshot = TelemetryTestUtils.getProcessScalars("parent", true, true);
  TelemetryTestUtils.assertKeyedScalar(snapshot, "a11y.theme", "never", false);

  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
  // We should have logged the default foreground and background colors
  testIsWhite("a11y.HCM_background", snapshot);
  testIsBlack("a11y.HCM_foreground", snapshot);

  // If we change the colors, our probes update on non-windows platforms.
  // On windows, useSystemColors is on by default, and so the values we set here
  // will not be written to our telemetry probes, because they capture
  // used colors, not the values of browser.foreground/background_color directly.

  setBackgroundColor("#000000");
  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
  if (AppConstants.platform == "win") {
    testIsWhite("a11y.HCM_background", snapshot);
  } else {
    testIsBlack("a11y.HCM_background", snapshot);
  }

  setForegroundColor("#ffffff");
  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
  if (AppConstants.platform == "win") {
    testIsBlack("a11y.HCM_foreground", snapshot);
  } else {
    testIsWhite("a11y.HCM_foreground", snapshot);
  }

  reset();
  gBrowser.removeCurrentTab();
});

add_task(async function testSetDefault() {
  const dialogWin = await openColorsDialog();
  const menulistHCM = dialogWin.document.getElementById("useDocumentColors");

  menulistHCM.doCommand();
  const newOption = dialogWin.document.getElementById("documentColorAutomatic");
  newOption.click();

  is(menulistHCM.value, "0", "HCM menulist should be set to default");

  await closeColorsDialog(dialogWin);

  // Verify correct initial value
  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "a11y.theme",
    "default",
    false
  );

  // We should not have logged any colors
  let snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
  ok(
    !("a11y.HCM_foreground" in snapshot),
    "Foreground color shouldn't be present."
  );
  ok(
    !("a11y.HCM_background" in snapshot),
    "Background color shouldn't be present."
  );

  // If we change the colors, our probes should not be updated anywhere
  await setForegroundColor("#ffffff"); // white
  await setBackgroundColor("#000000"); // black

  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
  ok(
    !("a11y.HCM_foreground" in snapshot),
    "Foreground color shouldn't be present."
  );
  ok(
    !("a11y.HCM_background" in snapshot),
    "Background color shouldn't be present."
  );

  reset();
  gBrowser.removeCurrentTab();
});

add_task(async function testSetNever() {
  const dialogWin = await openColorsDialog();
  const menulistHCM = dialogWin.document.getElementById("useDocumentColors");

  menulistHCM.doCommand();
  const newOption = dialogWin.document.getElementById("documentColorNever");
  newOption.click();

  is(menulistHCM.value, "1", "HCM menulist should be set to never");

  await closeColorsDialog(dialogWin);

  // Verify correct initial value
  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    "a11y.theme",
    "always",
    false
  );

  // We should not have logged any colors
  let snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
  ok(
    !("a11y.HCM_foreground" in snapshot),
    "Foreground color shouldn't be present."
  );
  ok(
    !("a11y.HCM_background" in snapshot),
    "Background color shouldn't be present."
  );

  // If we change the colors, our probes should not be updated anywhere
  await setForegroundColor("#ffffff"); // white
  await setBackgroundColor("#000000"); // black

  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
  ok(
    !("a11y.HCM_foreground" in snapshot),
    "Foreground color shouldn't be present."
  );
  ok(
    !("a11y.HCM_background" in snapshot),
    "Background color shouldn't be present."
  );

  reset();
  gBrowser.removeCurrentTab();
});

add_task(async function testBackplate() {
  is(
    Services.prefs.getBoolPref("browser.display.permit_backplate"),
    true,
    "Backplate is init'd to true"
  );

  Services.prefs.setBoolPref("browser.display.permit_backplate", false);
  // Verify correct recorded value
  verifyBackplate(false);

  Services.prefs.setBoolPref("browser.display.permit_backplate", true);
  // Verify correct recorded value
  verifyBackplate(true);
});

add_task(async function testSystemColors() {
  let expectedInitVal = false;
  if (AppConstants.platform == "win") {
    expectedInitVal = true;
  }

  const dialogWin = await openColorsDialog();
  const checkbox = dialogWin.document.getElementById("browserUseSystemColors");
  checkbox.click();

  is(
    checkbox.checked,
    !expectedInitVal,
    "System colors checkbox should be modified"
  );

  await closeColorsDialog(dialogWin);

  verifyUseSystemColors(!expectedInitVal);

  reset();
  gBrowser.removeCurrentTab();
});