summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_experimental_features_resetall.js
blob: 636374c0572ac01027d69c1243bd817d34ed842b (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// It doesn't matter what two preferences are used here, as long as the first is a built-in
// one that defaults to false and the second defaults to true.
const KNOWN_PREF_1 = "browser.display.use_system_colors";
const KNOWN_PREF_2 = "browser.underline_anchors";

// This test verifies that pressing the reset all button for experimental features
// resets all of the checkboxes to their default state.
add_task(async function testResetAll() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.preferences.experimental", true],
      ["test.featureA", false],
      ["test.featureB", true],
      [KNOWN_PREF_1, false],
      [KNOWN_PREF_2, true],
    ],
  });

  // Add a number of test features.
  const server = new DefinitionServer();
  let definitions = [
    {
      id: "test-featureA",
      preference: "test.featureA",
      defaultValue: false,
    },
    {
      id: "test-featureB",
      preference: "test.featureB",
      defaultValue: true,
    },
    {
      id: "test-featureC",
      preference: KNOWN_PREF_1,
      defaultValue: false,
    },
    {
      id: "test-featureD",
      preference: KNOWN_PREF_2,
      defaultValue: true,
    },
  ];
  for (let { id, preference, defaultValue } of definitions) {
    server.addDefinition({ id, preference, defaultValue, isPublic: true });
  }

  await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    `about:preferences?definitionsUrl=${encodeURIComponent(
      server.definitionsUrl
    )}#paneExperimental`
  );
  let doc = gBrowser.contentDocument;

  await TestUtils.waitForCondition(
    () => doc.getElementById(definitions[definitions.length - 1].id),
    "wait for the first public feature to get added to the DOM"
  );

  // Check the initial state of each feature.
  ok(!Services.prefs.getBoolPref("test.featureA"), "initial state A");
  ok(Services.prefs.getBoolPref("test.featureB"), "initial state B");
  ok(!Services.prefs.getBoolPref(KNOWN_PREF_1), "initial state C");
  ok(Services.prefs.getBoolPref(KNOWN_PREF_2), "initial state D");

  // Modify the state of some of the features.
  doc.getElementById("test-featureC").click();
  doc.getElementById("test-featureD").click();
  ok(!Services.prefs.getBoolPref("test.featureA"), "modified state A");
  ok(Services.prefs.getBoolPref("test.featureB"), "modified state B");
  ok(Services.prefs.getBoolPref(KNOWN_PREF_1), "modified state C");
  ok(!Services.prefs.getBoolPref(KNOWN_PREF_2), "modified state D");

  // State after reset.
  let prefChangedPromise = new Promise(resolve => {
    Services.prefs.addObserver(KNOWN_PREF_2, function observer() {
      Services.prefs.removeObserver(KNOWN_PREF_2, observer);
      resolve();
    });
  });
  doc.getElementById("experimentalCategory-reset").click();
  await prefChangedPromise;

  // The preferences will be reset to the default value for the feature.
  ok(!Services.prefs.getBoolPref("test.featureA"), "after reset state A");
  ok(Services.prefs.getBoolPref("test.featureB"), "after reset state B");
  ok(!Services.prefs.getBoolPref(KNOWN_PREF_1), "after reset state C");
  ok(Services.prefs.getBoolPref(KNOWN_PREF_2), "after reset state D");
  ok(
    !doc.getElementById("test-featureA").checked,
    "after reset checkbox state A"
  );
  ok(
    doc.getElementById("test-featureB").checked,
    "after reset checkbox state B"
  );
  ok(
    !doc.getElementById("test-featureC").checked,
    "after reset checkbox state C"
  );
  ok(
    doc.getElementById("test-featureD").checked,
    "after reset checkbox state D"
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});