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 | /* |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | let cps = new ContentPrefInstance(null); |
michael@0 | 7 | |
michael@0 | 8 | function run_test() { |
michael@0 | 9 | testCacheWorks("test1.example.com", "test-pref1"); |
michael@0 | 10 | testHasCachedPrefFunction("test2.example.com", "test-pref2"); |
michael@0 | 11 | testSetCaches("test3.example.com", "test-pref3"); |
michael@0 | 12 | testGetCaches("test4.example.com", "test-pref4"); |
michael@0 | 13 | testRemovePrefs("test5.example.com", "test-pref5"); |
michael@0 | 14 | testTypeConversions("test6.example.com", "test-pref6"); |
michael@0 | 15 | testNonExistingPrefCachesAsUndefined("test7.example.com", "test-pref7"); |
michael@0 | 16 | testCacheEviction("test8.example.com", "test-pref8"); |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | function testCacheWorks(uri, prefName) { |
michael@0 | 20 | const CACHED_VALUE = 3; |
michael@0 | 21 | const NEW_VALUE = 5; |
michael@0 | 22 | |
michael@0 | 23 | cps.setPref(uri, prefName, CACHED_VALUE); |
michael@0 | 24 | do_check_eq(cps.getPref(uri, prefName), CACHED_VALUE); |
michael@0 | 25 | |
michael@0 | 26 | // Now change the value directly through the DB and check |
michael@0 | 27 | // that the cached value is different |
michael@0 | 28 | |
michael@0 | 29 | let groupId = selectValue("SELECT id FROM groups WHERE name = :param1", "id", uri); |
michael@0 | 30 | let settingId = selectValue("SELECT id FROM settings WHERE name = :param1", "id", prefName); |
michael@0 | 31 | let prefId = selectValue("SELECT id FROM prefs WHERE groupID = :param1 AND settingID = :param2", |
michael@0 | 32 | "id", groupId, settingId); |
michael@0 | 33 | |
michael@0 | 34 | let stmt = cps.DBConnection.createStatement("UPDATE prefs SET value = :value WHERE id = :id"); |
michael@0 | 35 | stmt.params.value = NEW_VALUE; |
michael@0 | 36 | stmt.params.id = prefId; |
michael@0 | 37 | stmt.execute(); |
michael@0 | 38 | |
michael@0 | 39 | let dbValue = selectValue("SELECT value FROM prefs WHERE id = :param1", "value", prefId); |
michael@0 | 40 | let cacheValue = cps.getPref(uri, prefName); |
michael@0 | 41 | |
michael@0 | 42 | do_check_eq(dbValue, NEW_VALUE); |
michael@0 | 43 | do_check_eq(cacheValue, CACHED_VALUE); |
michael@0 | 44 | do_check_neq(cacheValue, dbValue); |
michael@0 | 45 | |
michael@0 | 46 | do_test_pending(); |
michael@0 | 47 | cps.getPref(uri, prefName, function (value) { |
michael@0 | 48 | do_check_eq(dbValue, NEW_VALUE); |
michael@0 | 49 | do_check_eq(value, CACHED_VALUE); |
michael@0 | 50 | do_check_neq(value, dbValue); |
michael@0 | 51 | do_test_finished(); |
michael@0 | 52 | }); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | function testHasCachedPrefFunction(uri, prefName) { |
michael@0 | 56 | const STARTING_VALUE = 3; |
michael@0 | 57 | const NEW_VALUE = 5; |
michael@0 | 58 | |
michael@0 | 59 | do_check_false(isCached(uri, prefName)); |
michael@0 | 60 | |
michael@0 | 61 | cps.setPref(uri, prefName, STARTING_VALUE); |
michael@0 | 62 | |
michael@0 | 63 | let groupId = selectValue("SELECT id FROM groups WHERE name = :param1", "id", uri); |
michael@0 | 64 | let settingId = selectValue("SELECT id FROM settings WHERE name = :param1", "id", prefName); |
michael@0 | 65 | let prefId = selectValue("SELECT id FROM prefs WHERE groupID = :param1 AND settingID = :param2", |
michael@0 | 66 | "id", groupId, settingId); |
michael@0 | 67 | |
michael@0 | 68 | do_check_neq(prefId, undefined); |
michael@0 | 69 | |
michael@0 | 70 | let originalValue = selectValue("SELECT value FROM prefs WHERE id = :param1", "value", prefId); |
michael@0 | 71 | do_check_eq(originalValue, STARTING_VALUE); |
michael@0 | 72 | |
michael@0 | 73 | let stmt = cps.DBConnection.createStatement("UPDATE prefs SET value = :value WHERE id = :id"); |
michael@0 | 74 | stmt.params.value = NEW_VALUE; |
michael@0 | 75 | stmt.params.id = prefId; |
michael@0 | 76 | stmt.execute(); |
michael@0 | 77 | |
michael@0 | 78 | let newValue = selectValue("SELECT value FROM prefs WHERE id = :param1", "value", prefId); |
michael@0 | 79 | do_check_eq(newValue, NEW_VALUE); |
michael@0 | 80 | |
michael@0 | 81 | let cachedValue = cps.getPref(uri, prefName); |
michael@0 | 82 | do_check_eq(cachedValue, STARTING_VALUE); |
michael@0 | 83 | do_check_true(isCached(uri, prefName)); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | function testSetCaches(uri, prefName) { |
michael@0 | 87 | cps.setPref(uri, prefName, 0); |
michael@0 | 88 | do_check_true(isCached(uri, prefName)); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | function testRemovePrefs(uri, prefName) { |
michael@0 | 92 | |
michael@0 | 93 | /* removePref */ |
michael@0 | 94 | cps.setPref("www1." + uri, prefName, 1); |
michael@0 | 95 | |
michael@0 | 96 | do_check_eq(cps.getPref("www1." + uri, prefName), 1); |
michael@0 | 97 | |
michael@0 | 98 | cps.removePref("www1." + uri, prefName); |
michael@0 | 99 | |
michael@0 | 100 | do_check_false(isCached("www1." + uri, prefName)); |
michael@0 | 101 | do_check_false(cps.hasPref("www1." + uri, prefName)); |
michael@0 | 102 | do_check_neq(cps.getPref("www1." + uri, prefName), 1); |
michael@0 | 103 | |
michael@0 | 104 | /* removeGroupedPrefs */ |
michael@0 | 105 | cps.setPref("www2." + uri, prefName, 2); |
michael@0 | 106 | cps.setPref("www3." + uri, prefName, 3); |
michael@0 | 107 | |
michael@0 | 108 | do_check_eq(cps.getPref("www2." + uri, prefName), 2); |
michael@0 | 109 | do_check_eq(cps.getPref("www3." + uri, prefName), 3); |
michael@0 | 110 | |
michael@0 | 111 | cps.removeGroupedPrefs(); |
michael@0 | 112 | |
michael@0 | 113 | do_check_false(isCached("www2." + uri, prefName)); |
michael@0 | 114 | do_check_false(isCached("www3." + uri, prefName)); |
michael@0 | 115 | do_check_false(cps.hasPref("www2." + uri, prefName)); |
michael@0 | 116 | do_check_false(cps.hasPref("www3." + uri, prefName)); |
michael@0 | 117 | do_check_neq(cps.getPref("www2." + uri, prefName), 2); |
michael@0 | 118 | do_check_neq(cps.getPref("www3." + uri, prefName), 3); |
michael@0 | 119 | |
michael@0 | 120 | /* removePrefsByName */ |
michael@0 | 121 | cps.setPref("www4." + uri, prefName, 4); |
michael@0 | 122 | cps.setPref("www5." + uri, prefName, 5); |
michael@0 | 123 | |
michael@0 | 124 | do_check_eq(cps.getPref("www4." + uri, prefName), 4); |
michael@0 | 125 | do_check_eq(cps.getPref("www5." + uri, prefName), 5); |
michael@0 | 126 | |
michael@0 | 127 | cps.removePrefsByName(prefName); |
michael@0 | 128 | |
michael@0 | 129 | do_check_false(isCached("www4." + uri, prefName)); |
michael@0 | 130 | do_check_false(isCached("www5." + uri, prefName)); |
michael@0 | 131 | do_check_false(cps.hasPref("www4." + uri, prefName)); |
michael@0 | 132 | do_check_false(cps.hasPref("www5." + uri, prefName)); |
michael@0 | 133 | do_check_neq(cps.getPref("www4." + uri, prefName), 4); |
michael@0 | 134 | do_check_neq(cps.getPref("www5." + uri, prefName), 5); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | function testGetCaches(uri, prefName) { |
michael@0 | 138 | const VALUE = 4; |
michael@0 | 139 | |
michael@0 | 140 | let insertGroup = cps.DBConnection.createStatement("INSERT INTO groups (name) VALUES (:name)"); |
michael@0 | 141 | insertGroup.params.name = uri; |
michael@0 | 142 | insertGroup.execute(); |
michael@0 | 143 | let groupId = cps.DBConnection.lastInsertRowID; |
michael@0 | 144 | |
michael@0 | 145 | let insertSetting = cps.DBConnection.createStatement("INSERT INTO settings (name) VALUES (:name)"); |
michael@0 | 146 | insertSetting.params.name = prefName; |
michael@0 | 147 | insertSetting.execute(); |
michael@0 | 148 | let settingId = cps.DBConnection.lastInsertRowID; |
michael@0 | 149 | |
michael@0 | 150 | let insertPref = cps.DBConnection.createStatement("INSERT INTO prefs (groupID, settingID, value) " + |
michael@0 | 151 | "VALUES (:groupId, :settingId, :value)"); |
michael@0 | 152 | insertPref.params.groupId = groupId; |
michael@0 | 153 | insertPref.params.settingId = settingId; |
michael@0 | 154 | insertPref.params.value = VALUE; |
michael@0 | 155 | insertPref.execute(); |
michael@0 | 156 | let prefId = cps.DBConnection.lastInsertRowID; |
michael@0 | 157 | |
michael@0 | 158 | let dbValue = selectValue("SELECT value FROM prefs WHERE id = :param1", "value", prefId); |
michael@0 | 159 | |
michael@0 | 160 | // First access from service should hit the DB |
michael@0 | 161 | let svcValue = cps.getPref(uri, prefName); |
michael@0 | 162 | |
michael@0 | 163 | // Second time should get the value from cache |
michael@0 | 164 | let cacheValue = cps.getPref(uri, prefName); |
michael@0 | 165 | |
michael@0 | 166 | do_check_eq(VALUE, dbValue); |
michael@0 | 167 | do_check_eq(VALUE, svcValue); |
michael@0 | 168 | do_check_eq(VALUE, cacheValue); |
michael@0 | 169 | |
michael@0 | 170 | do_check_true(isCached(uri, prefName)); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | function testTypeConversions(uri, prefName) { |
michael@0 | 174 | let value; |
michael@0 | 175 | |
michael@0 | 176 | cps.setPref(uri, prefName, true); |
michael@0 | 177 | value = cps.getPref(uri, prefName); |
michael@0 | 178 | do_check_true(value === 1); |
michael@0 | 179 | |
michael@0 | 180 | cps.setPref(uri, prefName, false); |
michael@0 | 181 | value = cps.getPref(uri, prefName); |
michael@0 | 182 | do_check_true(value === 0); |
michael@0 | 183 | |
michael@0 | 184 | cps.setPref(uri, prefName, null); |
michael@0 | 185 | value = cps.getPref(uri, prefName); |
michael@0 | 186 | do_check_true(value === null); |
michael@0 | 187 | |
michael@0 | 188 | cps.setPref(uri, prefName, undefined); |
michael@0 | 189 | value = cps.getPref(uri, prefName); |
michael@0 | 190 | do_check_true(value === null); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | function testNonExistingPrefCachesAsUndefined(uri, prefName) { |
michael@0 | 194 | |
michael@0 | 195 | do_check_false(isCached(uri, prefName)); |
michael@0 | 196 | |
michael@0 | 197 | // Cache the pref |
michael@0 | 198 | let value = cps.getPref(uri, prefName); |
michael@0 | 199 | do_check_true(value === undefined); |
michael@0 | 200 | |
michael@0 | 201 | do_check_true(isCached(uri, prefName)); |
michael@0 | 202 | |
michael@0 | 203 | // Cached pref |
michael@0 | 204 | value = cps.getPref(uri, prefName); |
michael@0 | 205 | do_check_true(value === undefined); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | function testCacheEviction(uri, prefName) { |
michael@0 | 209 | |
michael@0 | 210 | cps.setPref(uri, prefName, 5); |
michael@0 | 211 | do_check_eq(cps.getPref(uri, prefName), 5); |
michael@0 | 212 | do_check_true(isCached(uri, prefName)); |
michael@0 | 213 | |
michael@0 | 214 | // try to evict value from cache by adding various other entries |
michael@0 | 215 | const ENTRIES_TO_ADD = 200; |
michael@0 | 216 | for (let i = 0; i < ENTRIES_TO_ADD; i++) { |
michael@0 | 217 | let uriToAdd = "www" + i + uri; |
michael@0 | 218 | cps.setPref(uriToAdd, prefName, 0); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | do_check_false(isCached(uri, prefName)); |
michael@0 | 222 | |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | function selectValue(stmt, columnName, param1, param2) { |
michael@0 | 226 | let stmt = cps.DBConnection.createStatement(stmt); |
michael@0 | 227 | if (param1) |
michael@0 | 228 | stmt.params.param1 = param1; |
michael@0 | 229 | |
michael@0 | 230 | if (param2) |
michael@0 | 231 | stmt.params.param2 = param2; |
michael@0 | 232 | |
michael@0 | 233 | stmt.executeStep(); |
michael@0 | 234 | let val = stmt.row[columnName]; |
michael@0 | 235 | stmt.reset(); |
michael@0 | 236 | stmt.finalize(); |
michael@0 | 237 | return val; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | function isCached(uri, prefName) { |
michael@0 | 241 | return cps.hasCachedPref(uri, prefName); |
michael@0 | 242 | } |