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.
michael@0 | 1 | // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | Components.utils.import("resource://gre/modules/SharedPreferences.jsm"); |
michael@0 | 7 | Components.utils.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 8 | |
michael@0 | 9 | let _observerId = 0; |
michael@0 | 10 | |
michael@0 | 11 | function makeObserver() { |
michael@0 | 12 | let deferred = Promise.defer(); |
michael@0 | 13 | |
michael@0 | 14 | let ret = { |
michael@0 | 15 | id: _observerId++, |
michael@0 | 16 | count: 0, |
michael@0 | 17 | promise: deferred.promise, |
michael@0 | 18 | observe: function (subject, topic, data) { |
michael@0 | 19 | ret.count += 1; |
michael@0 | 20 | let msg = { subject: subject, |
michael@0 | 21 | topic: topic, |
michael@0 | 22 | data: data }; |
michael@0 | 23 | deferred.resolve(msg); |
michael@0 | 24 | }, |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | return ret; |
michael@0 | 28 | }; |
michael@0 | 29 | |
michael@0 | 30 | add_task(function test_get_set() { |
michael@0 | 31 | let branch = new SharedPreferences("test"); |
michael@0 | 32 | |
michael@0 | 33 | branch.setBoolPref("boolKey", true); |
michael@0 | 34 | branch.setCharPref("charKey", "string value"); |
michael@0 | 35 | branch.setIntPref("intKey", 1000); |
michael@0 | 36 | |
michael@0 | 37 | do_check_eq(branch.getBoolPref("boolKey"), true); |
michael@0 | 38 | do_check_eq(branch.getCharPref("charKey"), "string value"); |
michael@0 | 39 | do_check_eq(branch.getIntPref("intKey"), 1000); |
michael@0 | 40 | |
michael@0 | 41 | branch.setBoolPref("boolKey", false); |
michael@0 | 42 | branch.setCharPref("charKey", "different string value"); |
michael@0 | 43 | branch.setIntPref("intKey", -2000); |
michael@0 | 44 | |
michael@0 | 45 | do_check_eq(branch.getBoolPref("boolKey"), false); |
michael@0 | 46 | do_check_eq(branch.getCharPref("charKey"), "different string value"); |
michael@0 | 47 | do_check_eq(branch.getIntPref("intKey"), -2000); |
michael@0 | 48 | |
michael@0 | 49 | do_check_eq(typeof(branch.getBoolPref("boolKey")), "boolean"); |
michael@0 | 50 | do_check_eq(typeof(branch.getCharPref("charKey")), "string"); |
michael@0 | 51 | do_check_eq(typeof(branch.getIntPref("intKey")), "number"); |
michael@0 | 52 | }); |
michael@0 | 53 | |
michael@0 | 54 | add_task(function test_default() { |
michael@0 | 55 | let branch = new SharedPreferences(); |
michael@0 | 56 | |
michael@0 | 57 | branch.setBoolPref("boolKey", true); |
michael@0 | 58 | branch.setCharPref("charKey", "string value"); |
michael@0 | 59 | branch.setIntPref("intKey", 1000); |
michael@0 | 60 | |
michael@0 | 61 | do_check_eq(branch.getBoolPref("boolKey"), true); |
michael@0 | 62 | do_check_eq(branch.getCharPref("charKey"), "string value"); |
michael@0 | 63 | do_check_eq(branch.getIntPref("intKey"), 1000); |
michael@0 | 64 | |
michael@0 | 65 | branch.setBoolPref("boolKey", false); |
michael@0 | 66 | branch.setCharPref("charKey", "different string value"); |
michael@0 | 67 | branch.setIntPref("intKey", -2000); |
michael@0 | 68 | |
michael@0 | 69 | do_check_eq(branch.getBoolPref("boolKey"), false); |
michael@0 | 70 | do_check_eq(branch.getCharPref("charKey"), "different string value"); |
michael@0 | 71 | do_check_eq(branch.getIntPref("intKey"), -2000); |
michael@0 | 72 | |
michael@0 | 73 | do_check_eq(typeof(branch.getBoolPref("boolKey")), "boolean"); |
michael@0 | 74 | do_check_eq(typeof(branch.getCharPref("charKey")), "string"); |
michael@0 | 75 | do_check_eq(typeof(branch.getIntPref("intKey")), "number"); |
michael@0 | 76 | }); |
michael@0 | 77 | |
michael@0 | 78 | add_task(function test_multiple_branches() { |
michael@0 | 79 | let branch1 = new SharedPreferences("test1"); |
michael@0 | 80 | let branch2 = new SharedPreferences("test2"); |
michael@0 | 81 | |
michael@0 | 82 | branch1.setBoolPref("boolKey", true); |
michael@0 | 83 | branch2.setBoolPref("boolKey", false); |
michael@0 | 84 | |
michael@0 | 85 | do_check_eq(branch1.getBoolPref("boolKey"), true); |
michael@0 | 86 | do_check_eq(branch2.getBoolPref("boolKey"), false); |
michael@0 | 87 | |
michael@0 | 88 | branch1.setCharPref("charKey", "a value"); |
michael@0 | 89 | branch2.setCharPref("charKey", "a different value"); |
michael@0 | 90 | |
michael@0 | 91 | do_check_eq(branch1.getCharPref("charKey"), "a value"); |
michael@0 | 92 | do_check_eq(branch2.getCharPref("charKey"), "a different value"); |
michael@0 | 93 | }); |
michael@0 | 94 | |
michael@0 | 95 | add_task(function test_add_remove_observer() { |
michael@0 | 96 | let branch = new SharedPreferences("test"); |
michael@0 | 97 | |
michael@0 | 98 | branch.setBoolPref("boolKey", false); |
michael@0 | 99 | do_check_eq(branch.getBoolPref("boolKey"), false); |
michael@0 | 100 | |
michael@0 | 101 | let obs1 = makeObserver(); |
michael@0 | 102 | branch.addObserver("boolKey", obs1); |
michael@0 | 103 | |
michael@0 | 104 | try { |
michael@0 | 105 | branch.setBoolPref("boolKey", true); |
michael@0 | 106 | do_check_eq(branch.getBoolPref("boolKey"), true); |
michael@0 | 107 | |
michael@0 | 108 | let value1 = yield obs1.promise; |
michael@0 | 109 | do_check_eq(obs1.count, 1); |
michael@0 | 110 | |
michael@0 | 111 | do_check_eq(value1.subject, obs1); |
michael@0 | 112 | do_check_eq(value1.topic, "boolKey"); |
michael@0 | 113 | do_check_eq(typeof(value1.data), "boolean"); |
michael@0 | 114 | do_check_eq(value1.data, true); |
michael@0 | 115 | } finally { |
michael@0 | 116 | branch.removeObserver("boolKey", obs1); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | // Make sure the original observer is really gone, or as close as |
michael@0 | 120 | // we: install a second observer, wait for it to be notified, and |
michael@0 | 121 | // then verify the original observer was *not* notified. This |
michael@0 | 122 | // depends, of course, on the order that observers are notified, but |
michael@0 | 123 | // is better than nothing. |
michael@0 | 124 | |
michael@0 | 125 | let obs2 = makeObserver(); |
michael@0 | 126 | branch.addObserver("boolKey", obs2); |
michael@0 | 127 | |
michael@0 | 128 | try { |
michael@0 | 129 | branch.setBoolPref("boolKey", false); |
michael@0 | 130 | do_check_eq(branch.getBoolPref("boolKey"), false); |
michael@0 | 131 | |
michael@0 | 132 | let value2 = yield obs2.promise; |
michael@0 | 133 | do_check_eq(obs2.count, 1); |
michael@0 | 134 | |
michael@0 | 135 | do_check_eq(value2.subject, obs2); |
michael@0 | 136 | do_check_eq(value2.topic, "boolKey"); |
michael@0 | 137 | do_check_eq(typeof(value2.data), "boolean"); |
michael@0 | 138 | do_check_eq(value2.data, false); |
michael@0 | 139 | |
michael@0 | 140 | // Original observer count is preserved. |
michael@0 | 141 | do_check_eq(obs1.count, 1); |
michael@0 | 142 | } finally { |
michael@0 | 143 | branch.removeObserver("boolKey", obs2); |
michael@0 | 144 | } |
michael@0 | 145 | }); |
michael@0 | 146 | |
michael@0 | 147 | add_task(function test_observer_ignores() { |
michael@0 | 148 | let branch = new SharedPreferences("test"); |
michael@0 | 149 | |
michael@0 | 150 | branch.setCharPref("charKey", "first value"); |
michael@0 | 151 | do_check_eq(branch.getCharPref("charKey"), "first value"); |
michael@0 | 152 | |
michael@0 | 153 | let obs = makeObserver(); |
michael@0 | 154 | branch.addObserver("charKey", obs); |
michael@0 | 155 | |
michael@0 | 156 | try { |
michael@0 | 157 | // These should all be ignored. |
michael@0 | 158 | branch.setBoolPref("boolKey", true); |
michael@0 | 159 | branch.setBoolPref("boolKey", false); |
michael@0 | 160 | branch.setIntPref("intKey", -3000); |
michael@0 | 161 | branch.setIntPref("intKey", 4000); |
michael@0 | 162 | |
michael@0 | 163 | branch.setCharPref("charKey", "a value"); |
michael@0 | 164 | let value = yield obs.promise; |
michael@0 | 165 | |
michael@0 | 166 | // Observer should have been notified exactly once. |
michael@0 | 167 | do_check_eq(obs.count, 1); |
michael@0 | 168 | |
michael@0 | 169 | do_check_eq(value.subject, obs); |
michael@0 | 170 | do_check_eq(value.topic, "charKey"); |
michael@0 | 171 | do_check_eq(typeof(value.data), "string"); |
michael@0 | 172 | do_check_eq(value.data, "a value"); |
michael@0 | 173 | } finally { |
michael@0 | 174 | branch.removeObserver("charKey", obs); |
michael@0 | 175 | } |
michael@0 | 176 | }); |
michael@0 | 177 | |
michael@0 | 178 | add_task(function test_observer_ignores_branches() { |
michael@0 | 179 | let branch = new SharedPreferences("test"); |
michael@0 | 180 | |
michael@0 | 181 | branch.setCharPref("charKey", "first value"); |
michael@0 | 182 | do_check_eq(branch.getCharPref("charKey"), "first value"); |
michael@0 | 183 | |
michael@0 | 184 | let obs = makeObserver(); |
michael@0 | 185 | branch.addObserver("charKey", obs); |
michael@0 | 186 | |
michael@0 | 187 | try { |
michael@0 | 188 | // These should all be ignored. |
michael@0 | 189 | let branch2 = new SharedPreferences("test2"); |
michael@0 | 190 | branch2.setCharPref("charKey", "a wrong value"); |
michael@0 | 191 | let branch3 = new SharedPreferences("test.2"); |
michael@0 | 192 | branch3.setCharPref("charKey", "a different wrong value"); |
michael@0 | 193 | |
michael@0 | 194 | // This should not be ignored. |
michael@0 | 195 | branch.setCharPref("charKey", "a value"); |
michael@0 | 196 | |
michael@0 | 197 | let value = yield obs.promise; |
michael@0 | 198 | |
michael@0 | 199 | // Observer should have been notified exactly once. |
michael@0 | 200 | do_check_eq(obs.count, 1); |
michael@0 | 201 | |
michael@0 | 202 | do_check_eq(value.subject, obs); |
michael@0 | 203 | do_check_eq(value.topic, "charKey"); |
michael@0 | 204 | do_check_eq(typeof(value.data), "string"); |
michael@0 | 205 | do_check_eq(value.data, "a value"); |
michael@0 | 206 | } finally { |
michael@0 | 207 | branch.removeObserver("charKey", obs); |
michael@0 | 208 | } |
michael@0 | 209 | }); |
michael@0 | 210 | |
michael@0 | 211 | run_next_test(); |