tor-browser

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

browser_HCM_telemetry.js (10722B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 Services.scriptloader.loadSubScript(
      8  "chrome://mochitests/content/browser/browser/components/preferences/tests/head.js",
      9  this
     10 );
     11 
     12 const { TelemetryTestUtils } = ChromeUtils.importESModule(
     13  "resource://testing-common/TelemetryTestUtils.sys.mjs"
     14 );
     15 
     16 registerCleanupFunction(reset);
     17 
     18 function pushPref(name, val) {
     19  return SpecialPowers.pushPrefEnv({ set: [[name, val]] });
     20 }
     21 
     22 function refresh() {
     23  // These prefs get taken into account asynchronously.
     24  return new Promise(r =>
     25    requestAnimationFrame(() => requestAnimationFrame(r))
     26  );
     27 }
     28 
     29 async function reset() {
     30  // This (manually) runs after every task in this test suite.
     31  // We have to add this in because the initial state of
     32  // `document_color_use` affects the initial state of
     33  // `foreground_color`/`background_color` which can change our
     34  // starting telem samples. This ensures each tasks makes no lasting
     35  // state changes.
     36  Services.prefs.clearUserPref("browser.display.document_color_use");
     37  Services.prefs.clearUserPref("browser.display.permit_backplate");
     38  Services.prefs.clearUserPref("layout.css.always_underline_links");
     39  Services.prefs.clearUserPref("browser.display.foreground_color");
     40  Services.prefs.clearUserPref("browser.display.background_color");
     41 
     42  await refresh();
     43 
     44  Services.telemetry.clearEvents();
     45  TelemetryTestUtils.assertNumberOfEvents(0);
     46 }
     47 
     48 function verifyBackplate(expectedValue) {
     49  TelemetryTestUtils.assertScalar(
     50    TelemetryTestUtils.getProcessScalars("parent", false, false),
     51    "a11y.backplate",
     52    expectedValue,
     53    "Backplate scalar is logged as " + expectedValue
     54  );
     55 }
     56 
     57 async function verifyAlwaysUnderlineLinks(expectedValue) {
     58  let snapshot = TelemetryTestUtils.getProcessScalars("parent", false, false);
     59  ok(
     60    "a11y.always_underline_links" in snapshot,
     61    "Always underline links was logged."
     62  );
     63  await TestUtils.waitForCondition(() => {
     64    snapshot = TelemetryTestUtils.getProcessScalars("parent", false, false);
     65    return snapshot["a11y.always_underline_links"] == expectedValue;
     66  }, "Always underline links has expected value " + expectedValue);
     67 }
     68 
     69 // The magic numbers below are the uint32_t values representing RGB white
     70 // and RGB black respectively. They're directly captured as nsColors and
     71 // follow the same bit-shift pattern.
     72 function testIsWhite(pref, snapshot) {
     73  ok(pref in snapshot, "Scalar must be present.");
     74  is(snapshot[pref], 4294967295, "Scalar is logged as white");
     75 }
     76 
     77 function testIsBlack(pref, snapshot) {
     78  ok(pref in snapshot, "Scalar must be present.");
     79  is(snapshot[pref], 4278190080, "Scalar is logged as black");
     80 }
     81 
     82 function setForegroundColor(color) {
     83  // Note: we set the foreground and background colors by modifying this pref
     84  // instead of setting the value attribute on the color input direclty.
     85  // This is because setting the value of the input with setAttribute
     86  // doesn't generate the correct event to save the new value to the prefs
     87  // store, so we have to do it ourselves.
     88  return pushPref("browser.display.foreground_color", color);
     89 }
     90 
     91 async function setBackgroundColor(color) {
     92  return pushPref("browser.display.background_color", color);
     93 }
     94 
     95 add_task(async function testInit() {
     96  await openPreferencesViaOpenPreferencesAPI("general", { leaveOpen: true });
     97  const contrastControlRadios =
     98    gBrowser.selectedBrowser.contentDocument.getElementById(
     99      "contrastControlSettings"
    100    );
    101  if (AppConstants.platform == "win") {
    102    is(
    103      contrastControlRadios.value,
    104      0,
    105      "HCM menulist should be set to only with HCM theme on startup for windows"
    106    );
    107 
    108    // Verify correct default value
    109    TelemetryTestUtils.assertKeyedScalar(
    110      TelemetryTestUtils.getProcessScalars("parent", true, true),
    111      "a11y.theme",
    112      "default",
    113      false
    114    );
    115  } else {
    116    is(
    117      contrastControlRadios.value,
    118      1,
    119      "HCM menulist should be set to never on startup for non-windows platforms"
    120    );
    121 
    122    // Verify correct default value
    123    TelemetryTestUtils.assertKeyedScalar(
    124      TelemetryTestUtils.getProcessScalars("parent", true, true),
    125      "a11y.theme",
    126      "always",
    127      false
    128    );
    129 
    130    // We should not have logged any colors
    131    let snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    132    ok(
    133      !("a11y.HCM_foreground" in snapshot),
    134      "Foreground color shouldn't be present."
    135    );
    136    ok(
    137      !("a11y.HCM_background" in snapshot),
    138      "Background color shouldn't be present."
    139    );
    140 
    141    // If we change the colors, our probes should not be updated
    142    await setForegroundColor("#ffffff"); // white
    143    await setBackgroundColor("#000000"); // black
    144 
    145    snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    146    ok(
    147      !("a11y.HCM_foreground" in snapshot),
    148      "Foreground color shouldn't be present."
    149    );
    150    ok(
    151      !("a11y.HCM_background" in snapshot),
    152      "Background color shouldn't be present."
    153    );
    154  }
    155 
    156  await reset();
    157  gBrowser.removeCurrentTab();
    158 });
    159 
    160 add_task(async function testSetAlways() {
    161  await openPreferencesViaOpenPreferencesAPI("general", { leaveOpen: true });
    162  const contrastControlRadios =
    163    gBrowser.selectedBrowser.contentDocument.getElementById(
    164      "contrastControlSettings"
    165    );
    166 
    167  const newOption =
    168    gBrowser.selectedBrowser.contentDocument.getElementById(
    169      "contrastSettingsOn"
    170    );
    171  newOption.click();
    172 
    173  is(contrastControlRadios.value, 2, "HCM menulist should be set to always");
    174 
    175  await refresh();
    176 
    177  // Verify correct initial value
    178  let snapshot = TelemetryTestUtils.getProcessScalars("parent", true, true);
    179  TelemetryTestUtils.assertKeyedScalar(snapshot, "a11y.theme", "never", false);
    180 
    181  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    182  // We should have logged the default foreground and background colors
    183  testIsWhite("a11y.HCM_background", snapshot);
    184  testIsBlack("a11y.HCM_foreground", snapshot);
    185 
    186  await setBackgroundColor("#000000");
    187  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    188  testIsBlack("a11y.HCM_background", snapshot);
    189 
    190  await setForegroundColor("#ffffff");
    191  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    192  testIsWhite("a11y.HCM_foreground", snapshot);
    193 
    194  await reset();
    195  gBrowser.removeCurrentTab();
    196 });
    197 
    198 add_task(async function testSetDefault() {
    199  await openPreferencesViaOpenPreferencesAPI("general", { leaveOpen: true });
    200  const contrastControlRadios =
    201    gBrowser.selectedBrowser.contentDocument.getElementById(
    202      "contrastControlSettings"
    203    );
    204 
    205  const newOption = gBrowser.selectedBrowser.contentDocument.getElementById(
    206    "contrastSettingsAuto"
    207  );
    208  newOption.click();
    209 
    210  is(contrastControlRadios.value, 0, "HCM menulist should be set to default");
    211 
    212  await refresh();
    213 
    214  // Verify correct initial value
    215  TelemetryTestUtils.assertKeyedScalar(
    216    TelemetryTestUtils.getProcessScalars("parent", true, true),
    217    "a11y.theme",
    218    "default",
    219    false
    220  );
    221 
    222  // We should not have logged any colors
    223  let snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    224  ok(
    225    !("a11y.HCM_foreground" in snapshot),
    226    "Foreground color shouldn't be present."
    227  );
    228  ok(
    229    !("a11y.HCM_background" in snapshot),
    230    "Background color shouldn't be present."
    231  );
    232 
    233  // If we change the colors, our probes should not be updated anywhere
    234  await setForegroundColor("#ffffff"); // white
    235  await setBackgroundColor("#000000"); // black
    236 
    237  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    238  ok(
    239    !("a11y.HCM_foreground" in snapshot),
    240    "Foreground color shouldn't be present."
    241  );
    242  ok(
    243    !("a11y.HCM_background" in snapshot),
    244    "Background color shouldn't be present."
    245  );
    246 
    247  await reset();
    248  gBrowser.removeCurrentTab();
    249 });
    250 
    251 add_task(async function testSetNever() {
    252  await openPreferencesViaOpenPreferencesAPI("general", { leaveOpen: true });
    253  const contrastControlRadios =
    254    gBrowser.selectedBrowser.contentDocument.getElementById(
    255      "contrastControlSettings"
    256    );
    257 
    258  const newOption = gBrowser.selectedBrowser.contentDocument.getElementById(
    259    "contrastSettingsOff"
    260  );
    261  newOption.click();
    262 
    263  is(contrastControlRadios.value, 1, "HCM menulist should be set to never");
    264 
    265  await refresh();
    266 
    267  // Verify correct initial value
    268  TelemetryTestUtils.assertKeyedScalar(
    269    TelemetryTestUtils.getProcessScalars("parent", true, true),
    270    "a11y.theme",
    271    "always",
    272    false
    273  );
    274 
    275  // We should not have logged any colors
    276  let snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    277  ok(
    278    !("a11y.HCM_foreground" in snapshot),
    279    "Foreground color shouldn't be present."
    280  );
    281  ok(
    282    !("a11y.HCM_background" in snapshot),
    283    "Background color shouldn't be present."
    284  );
    285 
    286  // If we change the colors, our probes should not be updated anywhere
    287  await setForegroundColor("#ffffff"); // white
    288  await setBackgroundColor("#000000"); // black
    289 
    290  snapshot = TelemetryTestUtils.getProcessScalars("parent", false, true);
    291  ok(
    292    !("a11y.HCM_foreground" in snapshot),
    293    "Foreground color shouldn't be present."
    294  );
    295  ok(
    296    !("a11y.HCM_background" in snapshot),
    297    "Background color shouldn't be present."
    298  );
    299 
    300  await reset();
    301  gBrowser.removeCurrentTab();
    302 });
    303 
    304 add_task(async function testBackplate() {
    305  is(
    306    Services.prefs.getBoolPref("browser.display.permit_backplate"),
    307    true,
    308    "Backplate is init'd to true"
    309  );
    310 
    311  await pushPref("browser.display.permit_backplate", false);
    312 
    313  // Verify correct recorded value
    314  verifyBackplate(false);
    315 
    316  await pushPref("browser.display.permit_backplate", true);
    317 
    318  // Verify correct recorded value
    319  verifyBackplate(true);
    320 
    321  await reset();
    322 });
    323 
    324 add_task(async function testAlwaysUnderlineLinks() {
    325  const expectedInitVal = false;
    326  await verifyAlwaysUnderlineLinks(expectedInitVal);
    327  await openPreferencesViaOpenPreferencesAPI("general", { leaveOpen: true });
    328  const checkbox = gBrowser.selectedBrowser.contentDocument.getElementById(
    329    "alwaysUnderlineLinks"
    330  );
    331  is(
    332    checkbox.checked,
    333    expectedInitVal,
    334    "Always underline links checkbox has correct initial state"
    335  );
    336  checkbox.click();
    337 
    338  is(
    339    checkbox.checked,
    340    !expectedInitVal,
    341    "Always underline links checkbox should be modified"
    342  );
    343  is(
    344    Services.prefs.getBoolPref("layout.css.always_underline_links"),
    345    !expectedInitVal,
    346    "Always underline links pref reflects new value."
    347  );
    348  await verifyAlwaysUnderlineLinks(!expectedInitVal);
    349  await reset();
    350  gBrowser.removeCurrentTab();
    351 });