Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | function run_test() { |
michael@0 | 6 | //**************************************************************************// |
michael@0 | 7 | // Database Creation, Schema Migration, and Backup |
michael@0 | 8 | |
michael@0 | 9 | // Note: in these tests we use createInstance instead of getService |
michael@0 | 10 | // so we can instantiate the service multiple times and make it run |
michael@0 | 11 | // its database initialization code each time. |
michael@0 | 12 | |
michael@0 | 13 | // Create a new database. |
michael@0 | 14 | { |
michael@0 | 15 | ContentPrefTest.deleteDatabase(); |
michael@0 | 16 | |
michael@0 | 17 | // Get the service and make sure it has a ready database connection. |
michael@0 | 18 | let cps = Cc["@mozilla.org/content-pref/service;1"]. |
michael@0 | 19 | createInstance(Ci.nsIContentPrefService); |
michael@0 | 20 | do_check_true(cps.DBConnection.connectionReady); |
michael@0 | 21 | cps.DBConnection.close(); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | // Open an existing database. |
michael@0 | 25 | { |
michael@0 | 26 | let dbFile = ContentPrefTest.deleteDatabase(); |
michael@0 | 27 | |
michael@0 | 28 | let cps = Cc["@mozilla.org/content-pref/service;1"]. |
michael@0 | 29 | createInstance(Ci.nsIContentPrefService); |
michael@0 | 30 | cps.DBConnection.close(); |
michael@0 | 31 | do_check_true(dbFile.exists()); |
michael@0 | 32 | |
michael@0 | 33 | // Get the service and make sure it has a ready database connection. |
michael@0 | 34 | cps = Cc["@mozilla.org/content-pref/service;1"]. |
michael@0 | 35 | createInstance(Ci.nsIContentPrefService); |
michael@0 | 36 | do_check_true(cps.DBConnection.connectionReady); |
michael@0 | 37 | cps.DBConnection.close(); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | // Open an empty database. |
michael@0 | 41 | { |
michael@0 | 42 | let dbFile = ContentPrefTest.deleteDatabase(); |
michael@0 | 43 | |
michael@0 | 44 | // Create an empty database. |
michael@0 | 45 | let dbService = Cc["@mozilla.org/storage/service;1"]. |
michael@0 | 46 | getService(Ci.mozIStorageService); |
michael@0 | 47 | let dbConnection = dbService.openDatabase(dbFile); |
michael@0 | 48 | do_check_eq(dbConnection.schemaVersion, 0); |
michael@0 | 49 | dbConnection.close(); |
michael@0 | 50 | do_check_true(dbFile.exists()); |
michael@0 | 51 | |
michael@0 | 52 | // Get the service and make sure it has created the schema. |
michael@0 | 53 | let cps = Cc["@mozilla.org/content-pref/service;1"]. |
michael@0 | 54 | createInstance(Ci.nsIContentPrefService); |
michael@0 | 55 | do_check_neq(cps.DBConnection.schemaVersion, 0); |
michael@0 | 56 | cps.DBConnection.close(); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | // Open a corrupted database. |
michael@0 | 60 | { |
michael@0 | 61 | let dbFile = ContentPrefTest.deleteDatabase(); |
michael@0 | 62 | let backupDBFile = ContentPrefTest.deleteBackupDatabase(); |
michael@0 | 63 | |
michael@0 | 64 | // Create a corrupted database. |
michael@0 | 65 | let foStream = Cc["@mozilla.org/network/file-output-stream;1"]. |
michael@0 | 66 | createInstance(Ci.nsIFileOutputStream); |
michael@0 | 67 | foStream.init(dbFile, 0x02 | 0x08 | 0x20, 0666, 0); |
michael@0 | 68 | let garbageData = "garbage that makes SQLite think the file is corrupted"; |
michael@0 | 69 | foStream.write(garbageData, garbageData.length); |
michael@0 | 70 | foStream.close(); |
michael@0 | 71 | |
michael@0 | 72 | // Get the service and make sure it backs up and recreates the database. |
michael@0 | 73 | let cps = Cc["@mozilla.org/content-pref/service;1"]. |
michael@0 | 74 | createInstance(Ci.nsIContentPrefService); |
michael@0 | 75 | do_check_true(backupDBFile.exists()); |
michael@0 | 76 | do_check_true(cps.DBConnection.connectionReady); |
michael@0 | 77 | |
michael@0 | 78 | cps.DBConnection.close(); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | // Open a database with a corrupted schema. |
michael@0 | 82 | { |
michael@0 | 83 | let dbFile = ContentPrefTest.deleteDatabase(); |
michael@0 | 84 | let backupDBFile = ContentPrefTest.deleteBackupDatabase(); |
michael@0 | 85 | |
michael@0 | 86 | // Create an empty database and set the schema version to a number |
michael@0 | 87 | // that will trigger a schema migration that will fail. |
michael@0 | 88 | let dbService = Cc["@mozilla.org/storage/service;1"]. |
michael@0 | 89 | getService(Ci.mozIStorageService); |
michael@0 | 90 | let dbConnection = dbService.openDatabase(dbFile); |
michael@0 | 91 | dbConnection.schemaVersion = -1; |
michael@0 | 92 | dbConnection.close(); |
michael@0 | 93 | do_check_true(dbFile.exists()); |
michael@0 | 94 | |
michael@0 | 95 | // Get the service and make sure it backs up and recreates the database. |
michael@0 | 96 | let cps = Cc["@mozilla.org/content-pref/service;1"]. |
michael@0 | 97 | createInstance(Ci.nsIContentPrefService); |
michael@0 | 98 | do_check_true(backupDBFile.exists()); |
michael@0 | 99 | do_check_true(cps.DBConnection.connectionReady); |
michael@0 | 100 | |
michael@0 | 101 | cps.DBConnection.close(); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | |
michael@0 | 105 | // Now get the content pref service for real for use by the rest of the tests. |
michael@0 | 106 | let cps = new ContentPrefInstance(null); |
michael@0 | 107 | |
michael@0 | 108 | var uri = ContentPrefTest.getURI("http://www.example.com/"); |
michael@0 | 109 | |
michael@0 | 110 | // Make sure disk synchronization checking is turned off by default. |
michael@0 | 111 | var statement = cps.DBConnection.createStatement("PRAGMA synchronous"); |
michael@0 | 112 | statement.executeStep(); |
michael@0 | 113 | do_check_eq(0, statement.getInt32(0)); |
michael@0 | 114 | |
michael@0 | 115 | //**************************************************************************// |
michael@0 | 116 | // Nonexistent Pref |
michael@0 | 117 | |
michael@0 | 118 | do_check_eq(cps.getPref(uri, "test.nonexistent.getPref"), undefined); |
michael@0 | 119 | do_check_eq(cps.setPref(uri, "test.nonexistent.setPref", 5), undefined); |
michael@0 | 120 | do_check_false(cps.hasPref(uri, "test.nonexistent.hasPref")); |
michael@0 | 121 | do_check_eq(cps.removePref(uri, "test.nonexistent.removePref"), undefined); |
michael@0 | 122 | |
michael@0 | 123 | |
michael@0 | 124 | //**************************************************************************// |
michael@0 | 125 | // Existing Pref |
michael@0 | 126 | |
michael@0 | 127 | cps.setPref(uri, "test.existing", 5); |
michael@0 | 128 | |
michael@0 | 129 | // getPref should return the pref value |
michael@0 | 130 | do_check_eq(cps.getPref(uri, "test.existing"), 5); |
michael@0 | 131 | |
michael@0 | 132 | // setPref should return undefined and change the value of the pref |
michael@0 | 133 | do_check_eq(cps.setPref(uri, "test.existing", 6), undefined); |
michael@0 | 134 | do_check_eq(cps.getPref(uri, "test.existing"), 6); |
michael@0 | 135 | |
michael@0 | 136 | // hasPref should return true |
michael@0 | 137 | do_check_true(cps.hasPref(uri, "test.existing")); |
michael@0 | 138 | |
michael@0 | 139 | // removePref should return undefined and remove the pref |
michael@0 | 140 | do_check_eq(cps.removePref(uri, "test.existing"), undefined); |
michael@0 | 141 | do_check_false(cps.hasPref(uri, "test.existing")); |
michael@0 | 142 | |
michael@0 | 143 | |
michael@0 | 144 | //**************************************************************************// |
michael@0 | 145 | // Round-Trip Data Integrity |
michael@0 | 146 | |
michael@0 | 147 | // Make sure pref values remain the same from setPref to getPref. |
michael@0 | 148 | |
michael@0 | 149 | cps.setPref(uri, "test.data-integrity.integer", 5); |
michael@0 | 150 | do_check_eq(cps.getPref(uri, "test.data-integrity.integer"), 5); |
michael@0 | 151 | |
michael@0 | 152 | cps.setPref(uri, "test.data-integrity.float", 5.5); |
michael@0 | 153 | do_check_eq(cps.getPref(uri, "test.data-integrity.float"), 5.5); |
michael@0 | 154 | |
michael@0 | 155 | cps.setPref(uri, "test.data-integrity.boolean", true); |
michael@0 | 156 | do_check_eq(cps.getPref(uri, "test.data-integrity.boolean"), true); |
michael@0 | 157 | |
michael@0 | 158 | cps.setPref(uri, "test.data-integrity.string", "test"); |
michael@0 | 159 | do_check_eq(cps.getPref(uri, "test.data-integrity.string"), "test"); |
michael@0 | 160 | |
michael@0 | 161 | cps.setPref(uri, "test.data-integrity.null", null); |
michael@0 | 162 | do_check_eq(cps.getPref(uri, "test.data-integrity.null"), null); |
michael@0 | 163 | |
michael@0 | 164 | // XXX Test arbitrary binary data. |
michael@0 | 165 | |
michael@0 | 166 | // Make sure hasPref and removePref work on all data types. |
michael@0 | 167 | |
michael@0 | 168 | do_check_true(cps.hasPref(uri, "test.data-integrity.integer")); |
michael@0 | 169 | do_check_true(cps.hasPref(uri, "test.data-integrity.float")); |
michael@0 | 170 | do_check_true(cps.hasPref(uri, "test.data-integrity.boolean")); |
michael@0 | 171 | do_check_true(cps.hasPref(uri, "test.data-integrity.string")); |
michael@0 | 172 | do_check_true(cps.hasPref(uri, "test.data-integrity.null")); |
michael@0 | 173 | |
michael@0 | 174 | do_check_eq(cps.removePref(uri, "test.data-integrity.integer"), undefined); |
michael@0 | 175 | do_check_eq(cps.removePref(uri, "test.data-integrity.float"), undefined); |
michael@0 | 176 | do_check_eq(cps.removePref(uri, "test.data-integrity.boolean"), undefined); |
michael@0 | 177 | do_check_eq(cps.removePref(uri, "test.data-integrity.string"), undefined); |
michael@0 | 178 | do_check_eq(cps.removePref(uri, "test.data-integrity.null"), undefined); |
michael@0 | 179 | |
michael@0 | 180 | do_check_false(cps.hasPref(uri, "test.data-integrity.integer")); |
michael@0 | 181 | do_check_false(cps.hasPref(uri, "test.data-integrity.float")); |
michael@0 | 182 | do_check_false(cps.hasPref(uri, "test.data-integrity.boolean")); |
michael@0 | 183 | do_check_false(cps.hasPref(uri, "test.data-integrity.string")); |
michael@0 | 184 | do_check_false(cps.hasPref(uri, "test.data-integrity.null")); |
michael@0 | 185 | |
michael@0 | 186 | |
michael@0 | 187 | //**************************************************************************// |
michael@0 | 188 | // getPrefs |
michael@0 | 189 | |
michael@0 | 190 | cps.setPref(uri, "test.getPrefs.a", 1); |
michael@0 | 191 | cps.setPref(uri, "test.getPrefs.b", 2); |
michael@0 | 192 | cps.setPref(uri, "test.getPrefs.c", 3); |
michael@0 | 193 | |
michael@0 | 194 | var prefs = cps.getPrefs(uri); |
michael@0 | 195 | do_check_true(prefs.hasKey("test.getPrefs.a")); |
michael@0 | 196 | do_check_eq(prefs.get("test.getPrefs.a"), 1); |
michael@0 | 197 | do_check_true(prefs.hasKey("test.getPrefs.b")); |
michael@0 | 198 | do_check_eq(prefs.get("test.getPrefs.b"), 2); |
michael@0 | 199 | do_check_true(prefs.hasKey("test.getPrefs.c")); |
michael@0 | 200 | do_check_eq(prefs.get("test.getPrefs.c"), 3); |
michael@0 | 201 | |
michael@0 | 202 | |
michael@0 | 203 | //**************************************************************************// |
michael@0 | 204 | // Site-Specificity |
michael@0 | 205 | |
michael@0 | 206 | // These are all different sites, and setting a pref for one of them |
michael@0 | 207 | // shouldn't set it for the others. |
michael@0 | 208 | var uri1 = ContentPrefTest.getURI("http://www.domain1.com/"); |
michael@0 | 209 | var uri2 = ContentPrefTest.getURI("http://foo.domain1.com/"); |
michael@0 | 210 | var uri3 = ContentPrefTest.getURI("http://domain1.com/"); |
michael@0 | 211 | var uri4 = ContentPrefTest.getURI("http://www.domain2.com/"); |
michael@0 | 212 | |
michael@0 | 213 | cps.setPref(uri1, "test.site-specificity.uri1", 5); |
michael@0 | 214 | do_check_false(cps.hasPref(uri2, "test.site-specificity.uri1")); |
michael@0 | 215 | do_check_false(cps.hasPref(uri3, "test.site-specificity.uri1")); |
michael@0 | 216 | do_check_false(cps.hasPref(uri4, "test.site-specificity.uri1")); |
michael@0 | 217 | |
michael@0 | 218 | cps.setPref(uri2, "test.site-specificity.uri2", 5); |
michael@0 | 219 | do_check_false(cps.hasPref(uri1, "test.site-specificity.uri2")); |
michael@0 | 220 | do_check_false(cps.hasPref(uri3, "test.site-specificity.uri2")); |
michael@0 | 221 | do_check_false(cps.hasPref(uri4, "test.site-specificity.uri2")); |
michael@0 | 222 | |
michael@0 | 223 | cps.setPref(uri3, "test.site-specificity.uri3", 5); |
michael@0 | 224 | do_check_false(cps.hasPref(uri1, "test.site-specificity.uri3")); |
michael@0 | 225 | do_check_false(cps.hasPref(uri2, "test.site-specificity.uri3")); |
michael@0 | 226 | do_check_false(cps.hasPref(uri4, "test.site-specificity.uri3")); |
michael@0 | 227 | |
michael@0 | 228 | cps.setPref(uri4, "test.site-specificity.uri4", 5); |
michael@0 | 229 | do_check_false(cps.hasPref(uri1, "test.site-specificity.uri4")); |
michael@0 | 230 | do_check_false(cps.hasPref(uri2, "test.site-specificity.uri4")); |
michael@0 | 231 | do_check_false(cps.hasPref(uri3, "test.site-specificity.uri4")); |
michael@0 | 232 | |
michael@0 | 233 | |
michael@0 | 234 | //**************************************************************************// |
michael@0 | 235 | // Observers |
michael@0 | 236 | |
michael@0 | 237 | var specificObserver = { |
michael@0 | 238 | interfaces: [Ci.nsIContentPrefObserver, Ci.nsISupports], |
michael@0 | 239 | |
michael@0 | 240 | QueryInterface: function ContentPrefTest_QueryInterface(iid) { |
michael@0 | 241 | if (!this.interfaces.some( function(v) { return iid.equals(v) } )) |
michael@0 | 242 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 243 | return this; |
michael@0 | 244 | }, |
michael@0 | 245 | |
michael@0 | 246 | numTimesSetCalled: 0, |
michael@0 | 247 | onContentPrefSet: function specificObserver_onContentPrefSet(group, name, value) { |
michael@0 | 248 | ++this.numTimesSetCalled; |
michael@0 | 249 | do_check_eq(group, "www.example.com"); |
michael@0 | 250 | do_check_eq(name, "test.observer.1"); |
michael@0 | 251 | do_check_eq(value, "test value"); |
michael@0 | 252 | }, |
michael@0 | 253 | |
michael@0 | 254 | numTimesRemovedCalled: 0, |
michael@0 | 255 | onContentPrefRemoved: function specificObserver_onContentPrefRemoved(group, name) { |
michael@0 | 256 | ++this.numTimesRemovedCalled; |
michael@0 | 257 | do_check_eq(group, "www.example.com"); |
michael@0 | 258 | do_check_eq(name, "test.observer.1"); |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | }; |
michael@0 | 262 | |
michael@0 | 263 | var genericObserver = { |
michael@0 | 264 | interfaces: [Ci.nsIContentPrefObserver, Ci.nsISupports], |
michael@0 | 265 | |
michael@0 | 266 | QueryInterface: function ContentPrefTest_QueryInterface(iid) { |
michael@0 | 267 | if (!this.interfaces.some( function(v) { return iid.equals(v) } )) |
michael@0 | 268 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 269 | return this; |
michael@0 | 270 | }, |
michael@0 | 271 | |
michael@0 | 272 | numTimesSetCalled: 0, |
michael@0 | 273 | onContentPrefSet: function genericObserver_onContentPrefSet(group, name, value) { |
michael@0 | 274 | ++this.numTimesSetCalled; |
michael@0 | 275 | do_check_eq(group, "www.example.com"); |
michael@0 | 276 | if (name != "test.observer.1" && name != "test.observer.2") |
michael@0 | 277 | do_throw("genericObserver.onContentPrefSet: " + |
michael@0 | 278 | "name not in (test.observer.1, test.observer.2)"); |
michael@0 | 279 | do_check_eq(value, "test value"); |
michael@0 | 280 | }, |
michael@0 | 281 | |
michael@0 | 282 | numTimesRemovedCalled: 0, |
michael@0 | 283 | onContentPrefRemoved: function genericObserver_onContentPrefRemoved(group, name) { |
michael@0 | 284 | ++this.numTimesRemovedCalled; |
michael@0 | 285 | do_check_eq(group, "www.example.com"); |
michael@0 | 286 | if (name != "test.observer.1" && name != "test.observer.2") |
michael@0 | 287 | do_throw("genericObserver.onContentPrefSet: " + |
michael@0 | 288 | "name not in (test.observer.1, test.observer.2)"); |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | }; |
michael@0 | 292 | |
michael@0 | 293 | // Make sure we can add observers, observers get notified about changes, |
michael@0 | 294 | // specific observers only get notified about changes to the specific setting, |
michael@0 | 295 | // and generic observers get notified about changes to all settings. |
michael@0 | 296 | cps.addObserver("test.observer.1", specificObserver); |
michael@0 | 297 | cps.addObserver(null, genericObserver); |
michael@0 | 298 | cps.setPref(uri, "test.observer.1", "test value"); |
michael@0 | 299 | cps.setPref(uri, "test.observer.2", "test value"); |
michael@0 | 300 | cps.removePref(uri, "test.observer.1"); |
michael@0 | 301 | cps.removePref(uri, "test.observer.2"); |
michael@0 | 302 | do_check_eq(specificObserver.numTimesSetCalled, 1); |
michael@0 | 303 | do_check_eq(genericObserver.numTimesSetCalled, 2); |
michael@0 | 304 | do_check_eq(specificObserver.numTimesRemovedCalled, 1); |
michael@0 | 305 | do_check_eq(genericObserver.numTimesRemovedCalled, 2); |
michael@0 | 306 | |
michael@0 | 307 | // Make sure we can remove observers and they don't get notified |
michael@0 | 308 | // about changes anymore. |
michael@0 | 309 | cps.removeObserver("test.observer.1", specificObserver); |
michael@0 | 310 | cps.removeObserver(null, genericObserver); |
michael@0 | 311 | cps.setPref(uri, "test.observer.1", "test value"); |
michael@0 | 312 | cps.removePref(uri, "test.observer.1", "test value"); |
michael@0 | 313 | do_check_eq(specificObserver.numTimesSetCalled, 1); |
michael@0 | 314 | do_check_eq(genericObserver.numTimesSetCalled, 2); |
michael@0 | 315 | do_check_eq(specificObserver.numTimesRemovedCalled, 1); |
michael@0 | 316 | do_check_eq(genericObserver.numTimesRemovedCalled, 2); |
michael@0 | 317 | |
michael@0 | 318 | |
michael@0 | 319 | //**************************************************************************// |
michael@0 | 320 | // Get/Remove Prefs By Name |
michael@0 | 321 | |
michael@0 | 322 | { |
michael@0 | 323 | var anObserver = { |
michael@0 | 324 | interfaces: [Ci.nsIContentPrefObserver, Ci.nsISupports], |
michael@0 | 325 | |
michael@0 | 326 | QueryInterface: function ContentPrefTest_QueryInterface(iid) { |
michael@0 | 327 | if (!this.interfaces.some( function(v) { return iid.equals(v) } )) |
michael@0 | 328 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 329 | return this; |
michael@0 | 330 | }, |
michael@0 | 331 | |
michael@0 | 332 | onContentPrefSet: function anObserver_onContentPrefSet(group, name, value) { |
michael@0 | 333 | }, |
michael@0 | 334 | |
michael@0 | 335 | expectedDomains: [], |
michael@0 | 336 | numTimesRemovedCalled: 0, |
michael@0 | 337 | onContentPrefRemoved: function anObserver_onContentPrefRemoved(group, name) { |
michael@0 | 338 | ++this.numTimesRemovedCalled; |
michael@0 | 339 | |
michael@0 | 340 | // remove the domain from the list of expected domains |
michael@0 | 341 | var index = this.expectedDomains.indexOf(group); |
michael@0 | 342 | do_check_true(index >= 0); |
michael@0 | 343 | this.expectedDomains.splice(index, 1); |
michael@0 | 344 | } |
michael@0 | 345 | }; |
michael@0 | 346 | |
michael@0 | 347 | var uri1 = ContentPrefTest.getURI("http://www.domain1.com/"); |
michael@0 | 348 | var uri2 = ContentPrefTest.getURI("http://foo.domain1.com/"); |
michael@0 | 349 | var uri3 = ContentPrefTest.getURI("http://domain1.com/"); |
michael@0 | 350 | var uri4 = ContentPrefTest.getURI("http://www.domain2.com/"); |
michael@0 | 351 | |
michael@0 | 352 | cps.setPref(uri1, "test.byname.1", 1); |
michael@0 | 353 | cps.setPref(uri1, "test.byname.2", 2); |
michael@0 | 354 | cps.setPref(uri2, "test.byname.1", 4); |
michael@0 | 355 | cps.setPref(uri3, "test.byname.3", 8); |
michael@0 | 356 | cps.setPref(uri4, "test.byname.1", 16); |
michael@0 | 357 | cps.setPref(null, "test.byname.1", 32); |
michael@0 | 358 | cps.setPref(null, "test.byname.2", false); |
michael@0 | 359 | |
michael@0 | 360 | function enumerateAndCheck(testName, expectedSum, expectedDomains) { |
michael@0 | 361 | var prefsByName = cps.getPrefsByName(testName); |
michael@0 | 362 | var enumerator = prefsByName.enumerator; |
michael@0 | 363 | var sum = 0; |
michael@0 | 364 | while (enumerator.hasMoreElements()) { |
michael@0 | 365 | var property = enumerator.getNext().QueryInterface(Components.interfaces.nsIProperty); |
michael@0 | 366 | sum += parseInt(property.value); |
michael@0 | 367 | |
michael@0 | 368 | // remove the domain from the list of expected domains |
michael@0 | 369 | var index = expectedDomains.indexOf(property.name); |
michael@0 | 370 | do_check_true(index >= 0); |
michael@0 | 371 | expectedDomains.splice(index, 1); |
michael@0 | 372 | } |
michael@0 | 373 | do_check_eq(sum, expectedSum); |
michael@0 | 374 | // check all domains have been removed from the array |
michael@0 | 375 | do_check_eq(expectedDomains.length, 0); |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | enumerateAndCheck("test.byname.1", 53, |
michael@0 | 379 | ["foo.domain1.com", null, "www.domain1.com", "www.domain2.com"]); |
michael@0 | 380 | enumerateAndCheck("test.byname.2", 2, ["www.domain1.com", null]); |
michael@0 | 381 | enumerateAndCheck("test.byname.3", 8, ["domain1.com"]); |
michael@0 | 382 | |
michael@0 | 383 | cps.addObserver("test.byname.1", anObserver); |
michael@0 | 384 | anObserver.expectedDomains = ["foo.domain1.com", null, "www.domain1.com", "www.domain2.com"]; |
michael@0 | 385 | |
michael@0 | 386 | cps.removePrefsByName("test.byname.1"); |
michael@0 | 387 | do_check_false(cps.hasPref(uri1, "test.byname.1")); |
michael@0 | 388 | do_check_false(cps.hasPref(uri2, "test.byname.1")); |
michael@0 | 389 | do_check_false(cps.hasPref(uri3, "test.byname.1")); |
michael@0 | 390 | do_check_false(cps.hasPref(uri4, "test.byname.1")); |
michael@0 | 391 | do_check_false(cps.hasPref(null, "test.byname.1")); |
michael@0 | 392 | do_check_true(cps.hasPref(uri1, "test.byname.2")); |
michael@0 | 393 | do_check_true(cps.hasPref(uri3, "test.byname.3")); |
michael@0 | 394 | |
michael@0 | 395 | do_check_eq(anObserver.numTimesRemovedCalled, 4); |
michael@0 | 396 | do_check_eq(anObserver.expectedDomains.length, 0); |
michael@0 | 397 | |
michael@0 | 398 | cps.removeObserver("test.byname.1", anObserver); |
michael@0 | 399 | |
michael@0 | 400 | // Clean up after ourselves |
michael@0 | 401 | cps.removePref(uri1, "test.byname.2"); |
michael@0 | 402 | cps.removePref(uri3, "test.byname.3"); |
michael@0 | 403 | cps.removePref(null, "test.byname.2"); |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | |
michael@0 | 407 | //**************************************************************************// |
michael@0 | 408 | // Clear Private Data Pref Removal |
michael@0 | 409 | |
michael@0 | 410 | { |
michael@0 | 411 | let uri1 = ContentPrefTest.getURI("http://www.domain1.com/"); |
michael@0 | 412 | let uri2 = ContentPrefTest.getURI("http://www.domain2.com/"); |
michael@0 | 413 | let uri3 = ContentPrefTest.getURI("http://www.domain3.com/"); |
michael@0 | 414 | |
michael@0 | 415 | let dbConnection = cps.DBConnection; |
michael@0 | 416 | |
michael@0 | 417 | let prefCount = dbConnection.createStatement("SELECT COUNT(*) AS count FROM prefs"); |
michael@0 | 418 | |
michael@0 | 419 | let groupCount = dbConnection.createStatement("SELECT COUNT(*) AS count FROM groups"); |
michael@0 | 420 | |
michael@0 | 421 | // Add some prefs for multiple domains. |
michael@0 | 422 | cps.setPref(uri1, "test.removeAllGroups", 1); |
michael@0 | 423 | cps.setPref(uri2, "test.removeAllGroups", 2); |
michael@0 | 424 | cps.setPref(uri3, "test.removeAllGroups", 3); |
michael@0 | 425 | |
michael@0 | 426 | // Add a global pref. |
michael@0 | 427 | cps.setPref(null, "test.removeAllGroups", 1); |
michael@0 | 428 | |
michael@0 | 429 | // Make sure there are some prefs and groups in the database. |
michael@0 | 430 | prefCount.executeStep(); |
michael@0 | 431 | do_check_true(prefCount.row.count > 0); |
michael@0 | 432 | prefCount.reset(); |
michael@0 | 433 | groupCount.executeStep(); |
michael@0 | 434 | do_check_true(groupCount.row.count > 0); |
michael@0 | 435 | groupCount.reset(); |
michael@0 | 436 | |
michael@0 | 437 | // Remove all prefs and groups from the database using the same routine |
michael@0 | 438 | // the Clear Private Data dialog uses. |
michael@0 | 439 | cps.removeGroupedPrefs(); |
michael@0 | 440 | |
michael@0 | 441 | // Make sure there are no longer any groups in the database and the only pref |
michael@0 | 442 | // is the global one. |
michael@0 | 443 | prefCount.executeStep(); |
michael@0 | 444 | do_check_true(prefCount.row.count == 1); |
michael@0 | 445 | prefCount.reset(); |
michael@0 | 446 | groupCount.executeStep(); |
michael@0 | 447 | do_check_true(groupCount.row.count == 0); |
michael@0 | 448 | groupCount.reset(); |
michael@0 | 449 | let globalPref = dbConnection.createStatement("SELECT groupID FROM prefs"); |
michael@0 | 450 | globalPref.executeStep(); |
michael@0 | 451 | do_check_true(globalPref.row.groupID == null); |
michael@0 | 452 | globalPref.reset(); |
michael@0 | 453 | } |
michael@0 | 454 | } |