summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutconfig/test/browser/browser_observe.js
blob: 1f1ab5d21745cae4779331eddec1ae315658422c (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["test.aboutconfig.modify.boolean", true],
      ["test.aboutconfig.modify.number", 1337],
      [
        "test.aboutconfig.modify.string",
        "the answer to the life the universe and everything",
      ],
    ],
  });

  registerCleanupFunction(() => {
    Services.prefs.clearUserPref(PREF_BOOLEAN_DEFAULT_TRUE);
    Services.prefs.clearUserPref(PREF_NUMBER_DEFAULT_ZERO);
    Services.prefs.clearUserPref(PREF_STRING_DEFAULT_EMPTY);
  });
});

add_task(async function test_observe_add_user_pref_before_search() {
  Assert.equal(
    Services.prefs.getPrefType(PREF_NEW),
    Ci.nsIPrefBranch.PREF_INVALID
  );

  await AboutConfigTest.withNewTab(
    async function () {
      this.bypassWarningButton.click();

      // No results are shown after the warning page is dismissed or bypassed,
      // and newly added preferences should not be displayed.
      Preferences.set(PREF_NEW, true);
      Assert.ok(!this.prefsTable.firstElementChild);
      Preferences.reset(PREF_NEW);
    },
    { dontBypassWarning: true }
  );
});

add_task(async function test_observe_add_user_pref() {
  Assert.equal(
    Services.prefs.getPrefType(PREF_NEW),
    Ci.nsIPrefBranch.PREF_INVALID
  );

  await AboutConfigTest.withNewTab(async function () {
    for (let value of [false, true, "", "value", 0, -10]) {
      // A row should be added when a new preference is added.
      Assert.ok(!this.getRow(PREF_NEW));
      Preferences.set(PREF_NEW, value);
      let row = this.getRow(PREF_NEW);
      Assert.equal(row.value, "" + value);

      // The row should stay when the preference is removed.
      Preferences.reset(PREF_NEW);
      Assert.ok(row.hasClass("deleted"));

      // Re-adding the preference from the interface should restore its value.
      row.editColumnButton.click();
      if (value.constructor.name != "Boolean") {
        row.editColumnButton.click();
      }
      Assert.equal(row.value, "" + value);
      Assert.ok(Preferences.get(PREF_NEW) === value);

      // Filtering again after deleting should remove the row.
      Preferences.reset(PREF_NEW);
      this.showAll();
      Assert.ok(!this.getRow(PREF_NEW));

      // Searching for the preference name should give the ability to add it.
      Preferences.reset(PREF_NEW);
      this.search(PREF_NEW);
      row = this.getRow(PREF_NEW);
      Assert.ok(row.hasClass("deleted"));

      // The row for adding should be reused if the new preference is added.
      Preferences.set(PREF_NEW, value);
      Assert.equal(row.value, "" + value);

      // If a new preference does not match the filter it is not displayed.
      Preferences.reset(PREF_NEW);
      this.search(PREF_NEW + ".extra");
      Assert.ok(!this.getRow(PREF_NEW));
      Preferences.set(PREF_NEW, value);
      Assert.ok(!this.getRow(PREF_NEW));

      // Resetting the filter should display the new preference.
      this.showAll();
      Assert.equal(this.getRow(PREF_NEW).value, "" + value);

      // Reset the preference, then continue by adding a different value.
      Preferences.reset(PREF_NEW);
      this.showAll();
    }
  });
});

add_task(async function test_observe_delete_user_pref() {
  for (let value of [true, "value", -10]) {
    Preferences.set(PREF_NEW, value);
    await AboutConfigTest.withNewTab(async function () {
      // Deleting the preference should keep the row.
      let row = this.getRow(PREF_NEW);
      Preferences.reset(PREF_NEW);
      Assert.ok(row.hasClass("deleted"));

      // Filtering again should remove the row.
      this.showAll();
      Assert.ok(!this.getRow(PREF_NEW));
    });
  }
});

add_task(async function test_observe_reset_user_pref() {
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_BOOLEAN_DEFAULT_TRUE, false]],
  });

  await AboutConfigTest.withNewTab(async function () {
    let row = this.getRow(PREF_BOOLEAN_DEFAULT_TRUE);
    Preferences.reset(PREF_BOOLEAN_DEFAULT_TRUE);
    Assert.ok(!row.hasClass("has-user-value"));
    Assert.equal(row.value, "true");
  });
});

add_task(async function test_observe_modify() {
  await AboutConfigTest.withNewTab(async function () {
    for (let [name, value] of [
      ["test.aboutconfig.modify.boolean", false],
      ["test.aboutconfig.modify.number", -10],
      ["test.aboutconfig.modify.string", "value"],
      [PREF_BOOLEAN_DEFAULT_TRUE, false],
      [PREF_NUMBER_DEFAULT_ZERO, 1],
      [PREF_STRING_DEFAULT_EMPTY, "string"],
    ]) {
      let row = this.getRow(name);
      Assert.notEqual(row.value, "" + value);
      Preferences.set(name, value);
      Assert.equal(row.value, "" + value);

      if (value.constructor.name == "Boolean") {
        continue;
      }

      // Changing the value or removing while editing should not take effect.
      row.editColumnButton.click();
      row.valueInput.value = "42";
      Preferences.reset(name);
      Assert.equal(row.element, this.getRow(name).element);
      Assert.equal(row.valueInput.value, "42");

      // Saving should store the value even if the preference was modified.
      row.editColumnButton.click();
      Assert.equal(row.value, "42");
      Assert.equal(Preferences.get(name), "42");
    }
  });
});