summaryrefslogtreecommitdiffstats
path: root/toolkit/components/normandy/test/unit/test_PrefUtils.js
blob: 57130d8783c40c61f6f47c3a85f3230dd8b75ec6 (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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { PrefUtils } = ChromeUtils.importESModule(
  "resource://normandy/lib/PrefUtils.sys.mjs"
);

add_task(function getPrefGetsValues() {
  const defaultBranch = Services.prefs.getDefaultBranch("");
  const userBranch = Services.prefs;

  defaultBranch.setBoolPref("test.bool", false);
  userBranch.setBoolPref("test.bool", true);
  defaultBranch.setIntPref("test.int", 1);
  userBranch.setIntPref("test.int", 2);
  defaultBranch.setStringPref("test.string", "default");
  userBranch.setStringPref("test.string", "user");

  equal(
    PrefUtils.getPref("test.bool", { branch: "user" }),
    true,
    "should read user branch bools"
  );
  equal(
    PrefUtils.getPref("test.int", { branch: "user" }),
    2,
    "should read user branch ints"
  );
  equal(
    PrefUtils.getPref("test.string", { branch: "user" }),
    "user",
    "should read user branch strings"
  );

  equal(
    PrefUtils.getPref("test.bool", { branch: "default" }),
    false,
    "should read default branch bools"
  );
  equal(
    PrefUtils.getPref("test.int", { branch: "default" }),
    1,
    "should read default branch ints"
  );
  equal(
    PrefUtils.getPref("test.string", { branch: "default" }),
    "default",
    "should read default branch strings"
  );

  equal(
    PrefUtils.getPref("test.bool"),
    true,
    "should read bools from the user branch by default"
  );
  equal(
    PrefUtils.getPref("test.int"),
    2,
    "should read ints from the user branch by default"
  );
  equal(
    PrefUtils.getPref("test.string"),
    "user",
    "should read strings from the user branch by default"
  );

  equal(
    PrefUtils.getPref("test.does_not_exist"),
    null,
    "Should return null for non-existent prefs by default"
  );
  let defaultValue = Symbol();
  equal(
    PrefUtils.getPref("test.does_not_exist", { defaultValue }),
    defaultValue,
    "Should use the passed default value"
  );
});

// This is an important test because the pref system can behave in strange ways
// when the user branch has a value, but the default branch does not.
add_task(function getPrefHandlesUserValueNoDefaultValue() {
  Services.prefs.setStringPref("test.only-user-value", "user");

  let defaultValue = Symbol();
  equal(
    PrefUtils.getPref("test.only-user-value", {
      branch: "default",
      defaultValue,
    }),
    defaultValue
  );
  equal(PrefUtils.getPref("test.only-user-value", { branch: "default" }), null);
  equal(PrefUtils.getPref("test.only-user-value", { branch: "user" }), "user");
  equal(PrefUtils.getPref("test.only-user-value"), "user");
});

add_task(function getPrefInvalidBranch() {
  Assert.throws(
    () => PrefUtils.getPref("test.pref", { branch: "invalid" }),
    PrefUtils.UnexpectedPreferenceBranch
  );
});

add_task(function setPrefSetsValues() {
  const defaultBranch = Services.prefs.getDefaultBranch("");
  const userBranch = Services.prefs;

  defaultBranch.setIntPref("test.int", 1);
  userBranch.setIntPref("test.int", 2);
  defaultBranch.setStringPref("test.string", "default");
  userBranch.setStringPref("test.string", "user");
  defaultBranch.setBoolPref("test.bool", false);
  userBranch.setBoolPref("test.bool", true);

  PrefUtils.setPref("test.int", 3);
  equal(
    userBranch.getIntPref("test.int"),
    3,
    "the user branch should change for ints"
  );
  PrefUtils.setPref("test.int", 4, { branch: "default" });
  equal(
    userBranch.getIntPref("test.int"),
    3,
    "changing the default branch shouldn't affect the user branch for ints"
  );
  PrefUtils.setPref("test.int", null, { branch: "user" });
  equal(
    userBranch.getIntPref("test.int"),
    4,
    "clearing the user branch should reveal the default value for ints"
  );

  PrefUtils.setPref("test.string", "user override");
  equal(
    userBranch.getStringPref("test.string"),
    "user override",
    "the user branch should change for strings"
  );
  PrefUtils.setPref("test.string", "default override", { branch: "default" });
  equal(
    userBranch.getStringPref("test.string"),
    "user override",
    "changing the default branch shouldn't affect the user branch for strings"
  );
  PrefUtils.setPref("test.string", null, { branch: "user" });
  equal(
    userBranch.getStringPref("test.string"),
    "default override",
    "clearing the user branch should reveal the default value for strings"
  );

  PrefUtils.setPref("test.bool", false);
  equal(
    userBranch.getBoolPref("test.bool"),
    false,
    "the user branch should change for bools"
  );
  // The above effectively unsets the user branch, since it is now the same as the default branch
  PrefUtils.setPref("test.bool", true, { branch: "default" });
  equal(
    userBranch.getBoolPref("test.bool"),
    true,
    "the default branch should change for bools"
  );

  defaultBranch.setBoolPref("test.bool", false);
  userBranch.setBoolPref("test.bool", true);
  equal(
    userBranch.getBoolPref("test.bool"),
    true,
    "the precondition should hold"
  );
  PrefUtils.setPref("test.bool", null, { branch: "user" });
  equal(
    userBranch.getBoolPref("test.bool"),
    false,
    "setting the user branch to null should reveal the default value for bools"
  );
});

add_task(function setPrefInvalidBranch() {
  Assert.throws(
    () => PrefUtils.setPref("test.pref", "value", { branch: "invalid" }),
    PrefUtils.UnexpectedPreferenceBranch
  );
});

add_task(function clearPrefClearsValues() {
  const defaultBranch = Services.prefs.getDefaultBranch("");
  const userBranch = Services.prefs;

  defaultBranch.setStringPref("test.string", "default");
  userBranch.setStringPref("test.string", "user");
  equal(
    userBranch.getStringPref("test.string"),
    "user",
    "the precondition should hold"
  );
  PrefUtils.clearPref("test.string");
  equal(
    userBranch.getStringPref("test.string"),
    "default",
    "clearing the user branch should reveal the default value for bools"
  );

  PrefUtils.clearPref("test.string", { branch: "default" });
  equal(
    userBranch.getStringPref("test.string"),
    "default",
    "clearing the default branch shouldn't do anything"
  );
});

add_task(function clearPrefInvalidBranch() {
  Assert.throws(
    () => PrefUtils.clearPref("test.pref", { branch: "invalid" }),
    PrefUtils.UnexpectedPreferenceBranch
  );
});