|
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/. */ |
|
4 |
|
5 function run_test() { |
|
6 |
|
7 var cps = new ContentPrefInstance(null); |
|
8 |
|
9 // Make sure disk synchronization checking is turned off by default. |
|
10 var statement = cps.DBConnection.createStatement("PRAGMA synchronous"); |
|
11 statement.executeStep(); |
|
12 do_check_eq(0, statement.getInt32(0)); |
|
13 |
|
14 // These are the different types of aGroup arguments we'll test. |
|
15 var anObject = {"foo":"bar"}; // a simple object |
|
16 var uri = ContentPrefTest.getURI("http://www.example.com/"); // nsIURI |
|
17 var stringURI = "www.example.com"; // typeof = "string" |
|
18 var stringObjectURI = new String("www.example.com"); // typeof = "object" |
|
19 |
|
20 { |
|
21 // First check that all the methods work or don't work. |
|
22 function simple_test_methods(aGroup, shouldThrow) { |
|
23 var prefName = "test.pref.0"; |
|
24 var prefValue = Math.floor(Math.random() * 100); |
|
25 |
|
26 if (shouldThrow) { |
|
27 do_check_thrown(function () { cps.getPref(aGroup, prefName); }); |
|
28 do_check_thrown(function () { cps.setPref(aGroup, prefName, prefValue); }); |
|
29 do_check_thrown(function () { cps.hasPref(aGroup, prefName); }); |
|
30 do_check_thrown(function () { cps.removePref(aGroup, prefName); }); |
|
31 do_check_thrown(function () { cps.getPrefs(aGroup); }); |
|
32 } else { |
|
33 do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined); |
|
34 do_check_true(cps.hasPref(aGroup, prefName)); |
|
35 do_check_eq(cps.getPref(aGroup, prefName), prefValue); |
|
36 do_check_eq(cps.removePref(aGroup, prefName), undefined); |
|
37 do_check_false(cps.hasPref(aGroup, prefName)); |
|
38 } |
|
39 } |
|
40 |
|
41 simple_test_methods(cps, true); // arbitrary nsISupports object, should throw too |
|
42 simple_test_methods(anObject, true); |
|
43 simple_test_methods(uri, false); |
|
44 simple_test_methods(stringURI, false); |
|
45 simple_test_methods(stringObjectURI, false); |
|
46 } |
|
47 |
|
48 { |
|
49 // Now we'll check that each argument produces the same result. |
|
50 function complex_test_methods(aGroup) { |
|
51 var prefName = "test.pref.1"; |
|
52 var prefValue = Math.floor(Math.random() * 100); |
|
53 |
|
54 do_check_eq(cps.setPref(aGroup, prefName, prefValue), undefined); |
|
55 |
|
56 do_check_true(cps.hasPref(uri, prefName)); |
|
57 do_check_true(cps.hasPref(stringURI, prefName)); |
|
58 do_check_true(cps.hasPref(stringObjectURI, prefName)); |
|
59 |
|
60 do_check_eq(cps.getPref(uri, prefName), prefValue); |
|
61 do_check_eq(cps.getPref(stringURI, prefName), prefValue); |
|
62 do_check_eq(cps.getPref(stringObjectURI, prefName), prefValue); |
|
63 |
|
64 do_check_eq(cps.removePref(aGroup, prefName), undefined); |
|
65 |
|
66 do_check_false(cps.hasPref(uri, prefName)); |
|
67 do_check_false(cps.hasPref(stringURI, prefName)); |
|
68 do_check_false(cps.hasPref(stringObjectURI, prefName)); |
|
69 } |
|
70 |
|
71 complex_test_methods(uri); |
|
72 complex_test_methods(stringURI); |
|
73 complex_test_methods(stringObjectURI); |
|
74 } |
|
75 |
|
76 { |
|
77 // test getPrefs returns the same prefs |
|
78 do_check_eq(cps.setPref(stringObjectURI, "test.5", 5), undefined); |
|
79 do_check_eq(cps.setPref(stringURI, "test.2", 2), undefined); |
|
80 do_check_eq(cps.setPref(uri, "test.1", 1), undefined); |
|
81 |
|
82 enumerateAndCheck(cps.getPrefs(uri), 8, ["test.1","test.2","test.5"]); |
|
83 enumerateAndCheck(cps.getPrefs(stringURI), 8, ["test.1","test.2","test.5"]); |
|
84 enumerateAndCheck(cps.getPrefs(stringObjectURI), 8, ["test.1","test.2","test.5"]); |
|
85 |
|
86 do_check_eq(cps.setPref(uri, "test.4", 4), undefined); |
|
87 do_check_eq(cps.setPref(stringObjectURI, "test.0", 0), undefined); |
|
88 |
|
89 enumerateAndCheck(cps.getPrefs(uri), 12, ["test.0","test.1","test.2","test.4","test.5"]); |
|
90 enumerateAndCheck(cps.getPrefs(stringURI), 12, ["test.0","test.1","test.2","test.4","test.5"]); |
|
91 enumerateAndCheck(cps.getPrefs(stringObjectURI), 12, ["test.0","test.1","test.2","test.4","test.5"]); |
|
92 |
|
93 do_check_eq(cps.setPref(stringURI, "test.3", 3), undefined); |
|
94 |
|
95 enumerateAndCheck(cps.getPrefs(uri), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]); |
|
96 enumerateAndCheck(cps.getPrefs(stringURI), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]); |
|
97 enumerateAndCheck(cps.getPrefs(stringObjectURI), 15, ["test.0","test.1","test.2","test.3","test.4","test.5"]); |
|
98 } |
|
99 } |
|
100 |
|
101 function do_check_thrown (aCallback) { |
|
102 var exThrown = false; |
|
103 try { |
|
104 aCallback(); |
|
105 do_throw("NS_ERROR_ILLEGAL_VALUE should have been thrown here"); |
|
106 } catch (e) { |
|
107 do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_VALUE); |
|
108 exThrown = true; |
|
109 } |
|
110 do_check_true(exThrown); |
|
111 } |
|
112 |
|
113 function enumerateAndCheck(prefs, expectedSum, expectedNames) { |
|
114 var enumerator = prefs.enumerator; |
|
115 var sum = 0; |
|
116 while (enumerator.hasMoreElements()) { |
|
117 var property = enumerator.getNext().QueryInterface(Components.interfaces.nsIProperty); |
|
118 sum += parseInt(property.value); |
|
119 |
|
120 // remove the pref name from the list of expected names |
|
121 var index = expectedNames.indexOf(property.name); |
|
122 do_check_true(index >= 0); |
|
123 expectedNames.splice(index, 1); |
|
124 } |
|
125 do_check_eq(sum, expectedSum); |
|
126 // check all pref names have been removed from the array |
|
127 do_check_eq(expectedNames.length, 0); |
|
128 } |