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
|
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1265808
-->
<head>
<title>Test for Bug 1265808 - replace Services.prefs</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css"
href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript;version=1.8">
"use strict";
var exports = {}
var module = {exports};
// Add some starter prefs.
localStorage.setItem("Services.prefs:devtools.branch1.somebool", JSON.stringify({
// bool
type: 128,
defaultValue: false,
hasUserValue: false,
userValue: false
}));
localStorage.setItem("Services.prefs:devtools.branch1.somestring", JSON.stringify({
// string
type: 32,
defaultValue: "dinosaurs",
hasUserValue: true,
userValue: "elephants"
}));
localStorage.setItem("Services.prefs:devtools.branch2.someint", JSON.stringify({
// string
type: 64,
defaultValue: -16,
hasUserValue: false,
userValue: null
}));
</script>
<script type="application/javascript;version=1.8"
src="prefs-wrapper.js"></script>
<script type="application/javascript;version=1.8"
src="resource://devtools/client/shared/shim/Services.js"></script>
</head>
<body>
<script type="application/javascript;version=1.8">
"use strict";
function do_tests() {
// We can't load the defaults in this context.
Services._defaultPrefsEnabled = false;
is(Services.prefs.getBoolPref("devtools.branch1.somebool"), false,
"bool pref value");
Services.prefs.setBoolPref("devtools.branch1.somebool", true);
is(Services.prefs.getBoolPref("devtools.branch1.somebool"), true,
"bool pref value after setting");
let threw;
try {
threw = false;
WrappedPrefs.getIntPref("devtools.branch1.somebool");
} catch (e) {
threw = true;
}
ok(threw, "type-checking for bool pref");
try {
threw = false;
Services.prefs.setIntPref("devtools.branch1.somebool", 27);
} catch (e) {
threw = true;
}
ok(threw, "type-checking for setting bool pref");
try {
threw = false;
Services.prefs.setBoolPref("devtools.branch1.somebool", 27);
} catch (e) {
threw = true;
}
ok(threw, "setting bool pref to wrong type");
try {
threw = false;
Services.prefs.getCharPref("devtools.branch2.someint");
} catch (e) {
threw = true;
}
ok(threw, "type-checking for int pref");
try {
threw = false;
Services.prefs.setCharPref("devtools.branch2.someint", "whatever");
} catch (e) {
threw = true;
}
ok(threw, "type-checking for setting int pref");
try {
threw = false;
Services.prefs.setIntPref("devtools.branch2.someint", "whatever");
} catch (e) {
threw = true;
}
ok(threw, "setting int pref to wrong type");
try {
threw = false;
Services.prefs.getBoolPref("devtools.branch1.somestring");
} catch (e) {
threw = true;
}
ok(threw, "type-checking for char pref");
try {
threw = false;
Services.prefs.setBoolPref("devtools.branch1.somestring", true);
} catch (e) {
threw = true;
}
ok(threw, "type-checking for setting char pref");
try {
threw = false;
Services.prefs.setCharPref("devtools.branch1.somestring", true);
} catch (e) {
threw = true;
}
ok(threw, "setting char pref to wrong type");
is(Services.prefs.getPrefType("devtools.branch1.somebool"),
Services.prefs.PREF_BOOL, "type of bool pref");
is(Services.prefs.getPrefType("devtools.branch2.someint"),
Services.prefs.PREF_INT, "type of int pref");
is(Services.prefs.getPrefType("devtools.branch1.somestring"),
Services.prefs.PREF_STRING, "type of string pref");
WrappedPrefs.setBoolPref("devtools.branch1.somebool", true);
ok(WrappedPrefs.getBoolPref("devtools.branch1.somebool"), "set bool pref");
WrappedPrefs.setIntPref("devtools.branch2.someint", -93);
is(WrappedPrefs.getIntPref("devtools.branch2.someint"), -93, "set int pref");
WrappedPrefs.setCharPref("devtools.branch1.somestring", "hello");
is(WrappedPrefs.getCharPref("devtools.branch1.somestring"), "hello",
"set string pref");
Services.prefs.clearUserPref("devtools.branch1.somestring");
is(Services.prefs.getCharPref("devtools.branch1.somestring"), "dinosaurs",
"clear string pref");
ok(Services.prefs.prefHasUserValue("devtools.branch1.somebool"),
"bool pref has user value");
ok(!Services.prefs.prefHasUserValue("devtools.branch1.somestring"),
"string pref does not have user value");
Services.prefs.savePrefFile(null);
ok(true, "saved pref file without error");
let branch0 = Services.prefs.getBranch(null);
let branch1 = Services.prefs.getBranch("devtools.branch1.");
branch1.setCharPref("somestring", "octopus");
Services.prefs.setCharPref("devtools.branch1.somestring", "octopus");
is(Services.prefs.getCharPref("devtools.branch1.somestring"), "octopus",
"set correctly via branch");
is(branch0.getCharPref("devtools.branch1.somestring"), "octopus",
"get via base branch");
is(branch1.getCharPref("somestring"), "octopus", "get via branch");
let notifications = {};
let clearNotificationList = () => { notifications = {}; }
let observer = {
observe: function (subject, topic, data) {
notifications[data] = true;
}
};
branch0.addObserver("devtools.branch1", null, null);
branch0.addObserver("devtools.branch1.", observer, false);
branch1.addObserver("", observer, false);
Services.prefs.setCharPref("devtools.branch1.somestring", "elf owl");
isDeeply(notifications, {
"devtools.branch1.somestring": true,
"somestring": true
}, "notifications sent to two listeners");
clearNotificationList();
Services.prefs.setIntPref("devtools.branch2.someint", 1729);
isDeeply(notifications, {}, "no notifications sent");
clearNotificationList();
branch0.removeObserver("devtools.branch1.", observer);
Services.prefs.setCharPref("devtools.branch1.somestring", "tapir");
isDeeply(notifications, {
"somestring": true
}, "removeObserver worked");
clearNotificationList();
branch0.addObserver("devtools.branch1.somestring", observer, false);
Services.prefs.setCharPref("devtools.branch1.somestring", "northern shoveler");
isDeeply(notifications, {
"devtools.branch1.somestring": true,
"somestring": true
}, "notifications sent to two listeners");
branch0.removeObserver("devtools.branch1.somestring", observer);
// Make sure we update if the pref change comes from somewhere else.
clearNotificationList();
pref("devtools.branch1.someotherstring", "lazuli bunting");
isDeeply(notifications, {
"someotherstring": true
}, "pref worked");
// Regression test for bug 1296427.
pref("devtools.hud.loglimit", 1000);
pref("devtools.hud.loglimit.network", 1000);
// Clean up.
localStorage.clear();
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv(
{"set": [
["devtools.branch1.somestring", "elephants"],
["devtools.branch1.somebool", false],
["devtools.branch2.someint", "-16"],
]},
do_tests);
</script>
</body>
|