Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
5 const PREF_INVALID = 0;
6 const PREF_BOOL = 128;
7 const PREF_INT = 64;
8 const PREF_STRING = 32;
10 const MAX_PREF_LENGTH = 1 * 1024 * 1024;
12 function makeList(a)
13 {
14 var o = {};
15 for(var i=0; i<a.length; i++)
16 {
17 o[a[i]] = '';
18 }
19 return o;
20 }
22 function run_test() {
24 var ps = Cc["@mozilla.org/preferences-service;1"].
25 getService(Ci.nsIPrefService);
27 var pb2= Cc["@mozilla.org/preferences-service;1"].
28 getService(Ci.nsIPrefBranch);
30 var pb = Cc["@mozilla.org/preferences-service;1"].
31 getService(Ci.nsIPrefBranch);
33 //**************************************************************************//
34 // Nullsafety
36 do_check_throws(function() {
37 pb.getPrefType(null); }, Cr.NS_ERROR_INVALID_ARG);
38 do_check_throws(function() {
39 pb.getBoolPref(null); }, Cr.NS_ERROR_INVALID_ARG);
40 do_check_throws(function() {
41 pb.setBoolPref(null, false); }, Cr.NS_ERROR_INVALID_ARG);
42 do_check_throws(function() {
43 pb.getIntPref(null); }, Cr.NS_ERROR_INVALID_ARG);
44 do_check_throws(function() {
45 pb.setIntPref(null, 0); }, Cr.NS_ERROR_INVALID_ARG);
46 do_check_throws(function() {
47 pb.getCharPref(null); }, Cr.NS_ERROR_INVALID_ARG);
48 do_check_throws(function() {
49 pb.setCharPref(null, null); }, Cr.NS_ERROR_INVALID_ARG);
50 do_check_throws(function() {
51 pb.getComplexValue(null, Components.interfaces.nsISupportsString); }, Cr.NS_ERROR_INVALID_ARG);
52 do_check_throws(function() {
53 pb.setComplexValue(null, Components.interfaces.nsISupportsString, pb); }, Cr.NS_ERROR_INVALID_ARG);
54 do_check_throws(function() {
55 pb.clearUserPref(null); }, Cr.NS_ERROR_INVALID_ARG);
56 do_check_throws(function() {
57 pb.prefHasUserValue(null); }, Cr.NS_ERROR_INVALID_ARG);
58 do_check_throws(function() {
59 pb.lockPref(null); }, Cr.NS_ERROR_INVALID_ARG);
60 do_check_throws(function() {
61 pb.prefIsLocked(null); }, Cr.NS_ERROR_INVALID_ARG);
62 do_check_throws(function() {
63 pb.unlockPref(null); }, Cr.NS_ERROR_INVALID_ARG);
64 do_check_throws(function() {
65 pb.deleteBranch(null); }, Cr.NS_ERROR_INVALID_ARG);
66 do_check_throws(function() {
67 pb.getChildList(null); }, Cr.NS_ERROR_INVALID_ARG);
69 //**************************************************************************//
70 // Nonexisting user preferences
72 do_check_eq(pb.prefHasUserValue("UserPref.nonexistent.hasUserValue"), false);
73 pb.clearUserPref("UserPref.nonexistent.clearUserPref"); // shouldn't throw
74 do_check_eq(pb.getPrefType("UserPref.nonexistent.getPrefType"), PREF_INVALID);
75 do_check_eq(pb.root, "");
77 // bool...
78 do_check_throws(function() {
79 pb.getBoolPref("UserPref.nonexistent.getBoolPref");}, Cr.NS_ERROR_UNEXPECTED);
80 pb.setBoolPref("UserPref.nonexistent.setBoolPref", false);
81 do_check_eq(pb.getBoolPref("UserPref.nonexistent.setBoolPref"), false);
83 // int...
84 do_check_throws(function() {
85 pb.getIntPref("UserPref.nonexistent.getIntPref");}, Cr.NS_ERROR_UNEXPECTED);
86 pb.setIntPref("UserPref.nonexistent.setIntPref", 5);
87 do_check_eq(pb.getIntPref("UserPref.nonexistent.setIntPref"), 5);
89 // char
90 do_check_throws(function() {
91 pb.getCharPref("UserPref.nonexistent.getCharPref");}, Cr.NS_ERROR_UNEXPECTED);
92 pb.setCharPref("UserPref.nonexistent.setCharPref", "_test");
93 do_check_eq(pb.getCharPref("UserPref.nonexistent.setCharPref"), "_test");
95 //**************************************************************************//
96 // Existing user Prefs and data integrity test (round-trip match)
98 pb.setBoolPref("UserPref.existing.bool", true);
99 pb.setIntPref("UserPref.existing.int", 23);
100 pb.setCharPref("UserPref.existing.char", "hey");
102 // getPref should return the pref value
103 do_check_eq(pb.getBoolPref("UserPref.existing.bool"), true);
104 do_check_eq(pb.getIntPref("UserPref.existing.int"), 23);
105 do_check_eq(pb.getCharPref("UserPref.existing.char"), "hey");
107 // setPref should not complain and should change the value of the pref
108 pb.setBoolPref("UserPref.existing.bool", false);
109 do_check_eq(pb.getBoolPref("UserPref.existing.bool"), false);
110 pb.setIntPref("UserPref.existing.int", 24);
111 do_check_eq(pb.getIntPref("UserPref.existing.int"), 24);
112 pb.setCharPref("UserPref.existing.char", "hej då!");
113 do_check_eq(pb.getCharPref("UserPref.existing.char"), "hej då!");
115 // prefHasUserValue should return true now
116 do_check_true(pb.prefHasUserValue("UserPref.existing.bool"));
117 do_check_true(pb.prefHasUserValue("UserPref.existing.int"));
118 do_check_true(pb.prefHasUserValue("UserPref.existing.char"));
120 // clearUserPref should remove the pref
121 pb.clearUserPref("UserPref.existing.bool");
122 do_check_false(pb.prefHasUserValue("UserPref.existing.bool"));
123 pb.clearUserPref("UserPref.existing.int");
124 do_check_false(pb.prefHasUserValue("UserPref.existing.int"));
125 pb.clearUserPref("UserPref.existing.char");
126 do_check_false(pb.prefHasUserValue("UserPref.existing.char"));
128 //**************************************************************************//
129 // Large value test
131 let largeStr = new Array(MAX_PREF_LENGTH + 1).join('x');
132 pb.setCharPref("UserPref.large.char", largeStr);
133 largeStr += 'x';
134 do_check_throws(function() {
135 pb.setCharPref("UserPref.large.char", largeStr); }, Cr.NS_ERROR_ILLEGAL_VALUE);
137 //**************************************************************************//
138 // getPrefType test
140 // bool...
141 pb.setBoolPref("UserPref.getPrefType.bool", true);
142 do_check_eq(pb.getPrefType("UserPref.getPrefType.bool"), PREF_BOOL);
144 // int...
145 pb.setIntPref("UserPref.getPrefType.int", -234);
146 do_check_eq(pb.getPrefType("UserPref.getPrefType.int"), PREF_INT);
148 // char...
149 pb.setCharPref("UserPref.getPrefType.char", "testing1..2");
150 do_check_eq(pb.getPrefType("UserPref.getPrefType.char"), PREF_STRING);
152 //**************************************************************************//
153 // getBranch tests
155 do_check_eq(ps.root, "");
157 // bool ...
158 pb.setBoolPref("UserPref.root.boolPref", true);
159 let pb_1 = ps.getBranch("UserPref.root.");
160 do_check_eq(pb_1.getBoolPref("boolPref"), true);
161 let pb_2 = ps.getBranch("UserPref.root.boolPref");
162 do_check_eq(pb_2.getBoolPref(""), true);
163 pb_2.setBoolPref(".anotherPref", false);
164 let pb_3 = ps.getBranch("UserPref.root.boolPre");
165 do_check_eq(pb_3.getBoolPref("f.anotherPref"), false);
167 // int ...
168 pb.setIntPref("UserPref.root.intPref", 23);
169 let pb_1 = ps.getBranch("UserPref.root.");
170 do_check_eq(pb_1.getIntPref("intPref"), 23);
171 let pb_2 = ps.getBranch("UserPref.root.intPref");
172 do_check_eq(pb_2.getIntPref(""), 23);
173 pb_2.setIntPref(".anotherPref", 69);
174 let pb_3 = ps.getBranch("UserPref.root.intPre");
175 do_check_eq(pb_3.getIntPref("f.anotherPref"), 69);
177 // char...
178 pb.setCharPref("UserPref.root.charPref", "_char");
179 let pb_1 = ps.getBranch("UserPref.root.");
180 do_check_eq(pb_1.getCharPref("charPref"), "_char");
181 let pb_2 = ps.getBranch("UserPref.root.charPref");
182 do_check_eq(pb_2.getCharPref(""), "_char");
183 pb_2.setCharPref(".anotherPref", "_another");
184 let pb_3 = ps.getBranch("UserPref.root.charPre");
185 do_check_eq(pb_3.getCharPref("f.anotherPref"), "_another");
187 //**************************************************************************//
188 // getChildlist tests
190 // get an already set prefBranch
191 pb1 = ps.getBranch("UserPref.root.");
192 let prefList = pb1.getChildList("");
193 do_check_eq(prefList.length, 6);
195 // check for specific prefs in the array : the order is not important
196 do_check_true("boolPref" in makeList(prefList));
197 do_check_true("intPref" in makeList(prefList));
198 do_check_true("charPref" in makeList(prefList));
199 do_check_true("boolPref.anotherPref" in makeList(prefList));
200 do_check_true("intPref.anotherPref" in makeList(prefList));
201 do_check_true("charPref.anotherPref" in makeList(prefList));
203 //**************************************************************************//
204 // Default branch tests
206 // bool...
207 pb1 = ps.getDefaultBranch("");
208 pb1.setBoolPref("DefaultPref.bool", true);
209 do_check_eq(pb1.getBoolPref("DefaultPref.bool"), true);
210 do_check_false(pb1.prefHasUserValue("DefaultPref.bool"));
211 ps.setBoolPref("DefaultPref.bool", false);
212 do_check_true(pb1.prefHasUserValue("DefaultPref.bool"));
213 do_check_eq(ps.getBoolPref("DefaultPref.bool"), false);
215 // int...
216 pb1 = ps.getDefaultBranch("");
217 pb1.setIntPref("DefaultPref.int", 100);
218 do_check_eq(pb1.getIntPref("DefaultPref.int"), 100);
219 do_check_false(pb1.prefHasUserValue("DefaultPref.int"));
220 ps.setIntPref("DefaultPref.int", 50);
221 do_check_true(pb1.prefHasUserValue("DefaultPref.int"));
222 do_check_eq(ps.getIntPref("DefaultPref.int"), 50);
224 // char...
225 pb1 = ps.getDefaultBranch("");
226 pb1.setCharPref("DefaultPref.char", "_default");
227 do_check_eq(pb1.getCharPref("DefaultPref.char"), "_default");
228 do_check_false(pb1.prefHasUserValue("DefaultPref.char"));
229 ps.setCharPref("DefaultPref.char", "_user");
230 do_check_true(pb1.prefHasUserValue("DefaultPref.char"));
231 do_check_eq(ps.getCharPref("DefaultPref.char"), "_user");
233 //**************************************************************************//
234 // pref Locking/Unlocking tests
236 // locking and unlocking a nonexistent pref should throw
237 do_check_throws(function() {
238 ps.lockPref("DefaultPref.nonexistent");}, Cr.NS_ERROR_UNEXPECTED);
239 do_check_throws(function() {
240 ps.unlockPref("DefaultPref.nonexistent");}, Cr.NS_ERROR_UNEXPECTED);
242 // getting a locked pref branch should return the "default" value
243 do_check_false(ps.prefIsLocked("DefaultPref.char"));
244 ps.lockPref("DefaultPref.char");
245 do_check_eq(ps.getCharPref("DefaultPref.char"), "_default");
246 do_check_true(ps.prefIsLocked("DefaultPref.char"));
248 // getting an unlocked pref branch should return the "user" value
249 ps.unlockPref("DefaultPref.char");
250 do_check_eq(ps.getCharPref("DefaultPref.char"), "_user");
251 do_check_false(ps.prefIsLocked("DefaultPref.char"));
253 // setting the "default" value to a user pref branch should
254 // make prefHasUserValue return false (documented behavior)
255 ps.setCharPref("DefaultPref.char", "_default");
256 do_check_false(pb1.prefHasUserValue("DefaultPref.char"));
258 // unlocking and locking multiple times shouldn't throw
259 ps.unlockPref("DefaultPref.char");
260 ps.lockPref("DefaultPref.char");
261 ps.lockPref("DefaultPref.char");
263 //**************************************************************************//
264 // resetBranch test
266 // NOT IMPLEMENTED YET in module/libpref. So we're not testing !
267 // uncomment the following if resetBranch ever gets implemented.
268 /*ps.resetBranch("DefaultPref");
269 do_check_eq(ps.getBoolPref("DefaultPref.bool"), true);
270 do_check_eq(ps.getIntPref("DefaultPref.int"), 100);
271 do_check_eq(ps.getCharPref("DefaultPref.char"), "_default");*/
273 //**************************************************************************//
274 // deleteBranch tests
276 // TODO : Really, this should throw!, by documentation.
277 // do_check_throws(function() {
278 // ps.deleteBranch("UserPref.nonexistent.deleteBranch");}, Cr.NS_ERROR_UNEXPECTED);
280 ps.deleteBranch("DefaultPref");
281 pb = ps.getBranch("DefaultPref");
282 pb1 = ps.getDefaultBranch("DefaultPref");
284 // getting prefs on deleted user branches should throw
285 do_check_throws(function() {
286 pb.getBoolPref("DefaultPref.bool");}, Cr.NS_ERROR_UNEXPECTED);
287 do_check_throws(function() {
288 pb.getIntPref("DefaultPref.int");}, Cr.NS_ERROR_UNEXPECTED);
289 do_check_throws(function() {
290 pb.getCharPref("DefaultPref.char");}, Cr.NS_ERROR_UNEXPECTED);
292 // getting prefs on deleted default branches should throw
293 do_check_throws(function() {
294 pb1.getBoolPref("DefaultPref.bool");}, Cr.NS_ERROR_UNEXPECTED);
295 do_check_throws(function() {
296 pb1.getIntPref("DefaultPref.int");}, Cr.NS_ERROR_UNEXPECTED);
297 do_check_throws(function() {
298 pb1.getCharPref("DefaultPref.char");}, Cr.NS_ERROR_UNEXPECTED);
300 //**************************************************************************//
301 // savePrefFile & readPrefFile tests
303 // set some prefs
304 ps.setBoolPref("ReadPref.bool", true);
305 ps.setIntPref("ReadPref.int", 230);
306 ps.setCharPref("ReadPref.char", "hello");
308 // save those prefs in a file
309 let savePrefFile = do_get_cwd();
310 savePrefFile.append("data");
311 savePrefFile.append("savePref.js");
312 if (savePrefFile.exists())
313 savePrefFile.remove(false);
314 savePrefFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
315 ps.savePrefFile(savePrefFile);
316 ps.resetPrefs();
318 // load a preexisting pref file
319 let prefFile = do_get_file("data/testPref.js");
320 ps.readUserPrefs(prefFile);
322 // former prefs should have been replaced/lost
323 do_check_throws(function() {
324 do_check_eq(pb.getBoolPref("ReadPref.bool"));}, Cr.NS_ERROR_UNEXPECTED);
325 do_check_throws(function() {
326 do_check_eq(pb.getIntPref("ReadPref.int"));}, Cr.NS_ERROR_UNEXPECTED);
327 do_check_throws(function() {
328 do_check_eq(pb.getCharPref("ReadPref.char"));}, Cr.NS_ERROR_UNEXPECTED);
330 // loaded prefs should read ok.
331 pb = ps.getBranch("testPref.");
332 do_check_eq(pb.getBoolPref("bool1"), true);
333 do_check_eq(pb.getBoolPref("bool2"), false);
334 do_check_eq(pb.getIntPref("int1"), 23);
335 do_check_eq(pb.getIntPref("int2"), -1236);
336 do_check_eq(pb.getCharPref("char1"), "_testPref");
337 do_check_eq(pb.getCharPref("char2"), "älskar");
339 // loading our former savePrefFile should allow us to read former prefs
340 ps.readUserPrefs(savePrefFile);
341 // cleanup the file now we don't need it
342 savePrefFile.remove(false);
343 do_check_eq(ps.getBoolPref("ReadPref.bool"), true);
344 do_check_eq(ps.getIntPref("ReadPref.int"), 230);
345 do_check_eq(ps.getCharPref("ReadPref.char"), "hello");
347 // ... and still be able to access "prior-to-readUserPrefs" preferences
348 do_check_eq(pb.getBoolPref("bool1"), true);
349 do_check_eq(pb.getBoolPref("bool2"), false);
350 do_check_eq(pb.getIntPref("int1"), 23);
352 //**************************************************************************//
353 // preference Observers
355 // an observer...
356 var observer = {
357 QueryInterface: function QueryInterface(aIID) {
358 if (aIID.equals(Ci.nsIObserver) ||
359 aIID.equals(Ci.nsISupports))
360 return this;
361 throw Components.results.NS_NOINTERFACE;
362 },
364 observe: function observe(aSubject, aTopic, aState) {
365 do_check_eq(aTopic, "nsPref:changed");
366 do_check_eq(aState, "ReadPref.int");
367 do_check_eq(ps.getIntPref(aState), 76);
368 ps.removeObserver("ReadPref.int", this);
370 // notification received, we may go on...
371 do_test_finished();
372 }
373 }
375 pb2.addObserver("ReadPref.int", observer, false);
376 ps.setIntPref("ReadPref.int", 76);
378 // test will continue upon notification...
379 do_test_pending();
381 // removed observer should not fire
382 pb2.removeObserver("ReadPref.int", observer);
383 ps.setIntPref("ReadPref.int", 32);
385 // let's test observers once more with a non-root prefbranch
386 pb2.getBranch("ReadPref.");
387 pb2.addObserver("int", observer, false);
388 ps.setIntPref("ReadPref.int", 76);
390 // test will complete upon notification...
391 do_test_pending();
392 }