Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!DOCTYPE html> |
michael@0 | 2 | <html> |
michael@0 | 3 | <!-- |
michael@0 | 4 | https://bugzilla.mozilla.org/show_bug.cgi?id={678695} |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Test for Bug {678695} Settings API</title> |
michael@0 | 8 | <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> |
michael@0 | 9 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 10 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 11 | </head> |
michael@0 | 12 | <body> |
michael@0 | 13 | |
michael@0 | 14 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id={678695}">Mozilla Bug {678695}</a> |
michael@0 | 15 | <p id="display"></p> |
michael@0 | 16 | <div id="content" style="display: none"> |
michael@0 | 17 | |
michael@0 | 18 | </div> |
michael@0 | 19 | <pre id="test"> |
michael@0 | 20 | <script class="testbody" type="text/javascript"> |
michael@0 | 21 | |
michael@0 | 22 | "use strict"; |
michael@0 | 23 | |
michael@0 | 24 | if (SpecialPowers.isMainProcess()) { |
michael@0 | 25 | SpecialPowers.Cu.import("resource://gre/modules/SettingsChangeNotifier.jsm"); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | SpecialPowers.addPermission("settings-read", true, document); |
michael@0 | 29 | SpecialPowers.addPermission("settings-write", true, document); |
michael@0 | 30 | |
michael@0 | 31 | function onUnwantedSuccess() { |
michael@0 | 32 | ok(false, "onUnwantedSuccess: shouldn't get here"); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | function onFailure() { |
michael@0 | 36 | ok(false, "in on Failure!"); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | const wifi = {"net3g.apn": "internet.mnc012.mcc345.gprs"}; |
michael@0 | 40 | const wifi2 = {"net3g.apn": "internet.mnc012.mcc345.test"}; |
michael@0 | 41 | var wifi3 = {"net3g.apn2": "internet.mnc012.mcc345.test3"}; |
michael@0 | 42 | var wifiEnabled = {"wifi.enabled": true}; |
michael@0 | 43 | var wifiDisabled = {"wifi.enabled": false}; |
michael@0 | 44 | var screenBright = {"screen.brightness": 0.7}; |
michael@0 | 45 | var screenBright2 = {"screen.brightness": 0.1}; |
michael@0 | 46 | var wifiNetworks0 = { "wifi.networks[0]": { ssid: "myfreenetwork", mac: "01:23:45:67:89:ab", passwd: "secret"}}; |
michael@0 | 47 | var wifiNetworks1 = { "wifi.networks[1]": { ssid: "myfreenetwork2", mac: "01:23:45:67:89:ab", passwd: "secret2"}}; |
michael@0 | 48 | |
michael@0 | 49 | var combination = { |
michael@0 | 50 | "wifi.enabled": false, |
michael@0 | 51 | "screen.brightness": 0.7, |
michael@0 | 52 | "wifi.networks[0]": { ssid: "myfreenetwork", mac: "01:23:45:67:89:ab", passwd: "secret" }, |
michael@0 | 53 | "test.test": true, |
michael@0 | 54 | "net3g.apn2": "internet.mnc012.mcc345.gprs" |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | function equals(o1, o2) { |
michael@0 | 58 | var k1 = Object.keys(o1).sort(); |
michael@0 | 59 | var k2 = Object.keys(o2).sort(); |
michael@0 | 60 | if (k1.length != k2.length) return false; |
michael@0 | 61 | return k1.zip(k2, function(keyPair) { |
michael@0 | 62 | if(typeof o1[keyPair[0]] == typeof o2[keyPair[1]] == "object"){ |
michael@0 | 63 | return equals(o1[keyPair[0]], o2[keyPair[1]]) |
michael@0 | 64 | } else { |
michael@0 | 65 | return o1[keyPair[0]] == o2[keyPair[1]]; |
michael@0 | 66 | } |
michael@0 | 67 | }).all(); |
michael@0 | 68 | }; |
michael@0 | 69 | |
michael@0 | 70 | function observer1(setting) { |
michael@0 | 71 | is(setting.settingName, "screen.brightness", "Same settingName"); |
michael@0 | 72 | is(setting.settingValue, "0.7", "Same settingvalue"); |
michael@0 | 73 | }; |
michael@0 | 74 | |
michael@0 | 75 | function observer2(setting) { |
michael@0 | 76 | is(setting.settingName, "screen.brightness", "Same settingName"); |
michael@0 | 77 | is(setting.settingValue, "0.7", "Same settingvalue"); |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | function observerWithNext(setting) { |
michael@0 | 81 | is(setting.settingName, "screen.brightness", "Same settingName"); |
michael@0 | 82 | is(setting.settingValue, "0.7", "Same settingvalue"); |
michael@0 | 83 | next(); |
michael@0 | 84 | }; |
michael@0 | 85 | |
michael@0 | 86 | function onsettingschangeWithNext(event) { |
michael@0 | 87 | is(event.settingName, "screen.brightness", "Same settingName"); |
michael@0 | 88 | is(event.settingValue, "0.7", "Same settingvalue"); |
michael@0 | 89 | next(); |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | function check(o1, o2) { |
michael@0 | 93 | is(JSON.stringify(o1), JSON.stringify(o2), "same"); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | var req, req2, req3, req4, req5, req6; |
michael@0 | 97 | var index = 0; |
michael@0 | 98 | |
michael@0 | 99 | var mozSettings = navigator.mozSettings; |
michael@0 | 100 | |
michael@0 | 101 | var steps = [ |
michael@0 | 102 | function () { |
michael@0 | 103 | ok(true, "Deleting database"); |
michael@0 | 104 | var lock = mozSettings.createLock(); |
michael@0 | 105 | req = lock.clear(); |
michael@0 | 106 | req.onsuccess = function () { |
michael@0 | 107 | ok(true, "Deleted the database"); |
michael@0 | 108 | next(); |
michael@0 | 109 | }; |
michael@0 | 110 | }, |
michael@0 | 111 | function () { |
michael@0 | 112 | ok(true, "Setting wifi"); |
michael@0 | 113 | var lock = mozSettings.createLock(); |
michael@0 | 114 | req = lock.set(wifi); |
michael@0 | 115 | req.onsuccess = function () { |
michael@0 | 116 | ok(true, "set done"); |
michael@0 | 117 | } |
michael@0 | 118 | req.onerror = onFailure; |
michael@0 | 119 | |
michael@0 | 120 | var lock2 = mozSettings.createLock(); |
michael@0 | 121 | req2 = lock2.get("net3g.apn"); |
michael@0 | 122 | req2.onsuccess = function () { |
michael@0 | 123 | is(Object.keys(req2.result).length, 1, "length 1"); |
michael@0 | 124 | check(wifi, req2.result); |
michael@0 | 125 | ok(true, "Get net3g.apn Done"); |
michael@0 | 126 | next(); |
michael@0 | 127 | }; |
michael@0 | 128 | req2.onerror = onFailure; |
michael@0 | 129 | }, |
michael@0 | 130 | function () { |
michael@0 | 131 | ok(true, "Change wifi1"); |
michael@0 | 132 | var lock = mozSettings.createLock(); |
michael@0 | 133 | req = lock.set(wifi2); |
michael@0 | 134 | req.onsuccess = function () { |
michael@0 | 135 | ok(true, "Set Done"); |
michael@0 | 136 | }; |
michael@0 | 137 | req.onerror = onFailure; |
michael@0 | 138 | ok(true, "Get changed net3g.apn"); |
michael@0 | 139 | req2 = lock.get("net3g.apn"); |
michael@0 | 140 | req2.onsuccess = function () { |
michael@0 | 141 | is(Object.keys(req2.result).length, 1, "length 1"); |
michael@0 | 142 | check(wifi2, req2.result); |
michael@0 | 143 | ok(true, "Get net3g.apn Done"); |
michael@0 | 144 | next(); |
michael@0 | 145 | }; |
michael@0 | 146 | req2.onerror = onFailure; |
michael@0 | 147 | }, |
michael@0 | 148 | function () { |
michael@0 | 149 | ok(true, "Set Combination"); |
michael@0 | 150 | var lock = mozSettings.createLock(); |
michael@0 | 151 | req3 = lock.set(combination); |
michael@0 | 152 | req3.onsuccess = function () { |
michael@0 | 153 | ok(true, "set done"); |
michael@0 | 154 | req4 = lock.get("net3g.apn2"); |
michael@0 | 155 | req4.onsuccess = function() { |
michael@0 | 156 | ok(true, "Done"); |
michael@0 | 157 | check(combination["net3g.apn2"], req4.result["net3g.apn2"]); |
michael@0 | 158 | next(); |
michael@0 | 159 | } |
michael@0 | 160 | } |
michael@0 | 161 | req3.onerror = onFailure; |
michael@0 | 162 | }, |
michael@0 | 163 | function() { |
michael@0 | 164 | var lock = mozSettings.createLock(); |
michael@0 | 165 | req4 = lock.get("net3g.apn2"); |
michael@0 | 166 | req4.onsuccess = function() { |
michael@0 | 167 | ok(true, "Done"); |
michael@0 | 168 | check(combination["net3g.apn2"], req4.result["net3g.apn2"]); |
michael@0 | 169 | next(); |
michael@0 | 170 | } |
michael@0 | 171 | req4.onerror = onFailure; |
michael@0 | 172 | }, |
michael@0 | 173 | function() { |
michael@0 | 174 | ok(true, "Get unknown key"); |
michael@0 | 175 | var lock = mozSettings.createLock(); |
michael@0 | 176 | req = lock.get("abc.def"); |
michael@0 | 177 | req.onsuccess = function() { |
michael@0 | 178 | is(req.result["abc.def"], undefined, "no result"); |
michael@0 | 179 | next(); |
michael@0 | 180 | }; |
michael@0 | 181 | req.onerror = onFailure; |
michael@0 | 182 | }, |
michael@0 | 183 | function() { |
michael@0 | 184 | ok(true, "adding onsettingchange"); |
michael@0 | 185 | navigator.mozSettings.onsettingchange = onsettingschangeWithNext; |
michael@0 | 186 | var lock = mozSettings.createLock(); |
michael@0 | 187 | req2 = lock.get("screen.brightness"); |
michael@0 | 188 | req2.onsuccess = function() { |
michael@0 | 189 | ok(true, "end adding onsettingchange"); |
michael@0 | 190 | next(); |
michael@0 | 191 | }; |
michael@0 | 192 | req2.onerror = onFailure; |
michael@0 | 193 | }, |
michael@0 | 194 | function() { |
michael@0 | 195 | ok(true, "Test onsettingchange"); |
michael@0 | 196 | var lock = mozSettings.createLock(); |
michael@0 | 197 | req = lock.set(screenBright); |
michael@0 | 198 | req.onsuccess = function () { |
michael@0 | 199 | ok(true, "set done, observer has to call next"); |
michael@0 | 200 | } |
michael@0 | 201 | req.onerror = onFailure; |
michael@0 | 202 | }, |
michael@0 | 203 | function() { |
michael@0 | 204 | ok(true, "delete onsettingschange"); |
michael@0 | 205 | var lock = mozSettings.createLock(); |
michael@0 | 206 | navigator.mozSettings.onsettingchange = null; |
michael@0 | 207 | req = lock.set(screenBright); |
michael@0 | 208 | req.onsuccess = function () { |
michael@0 | 209 | ok(true, "set done"); |
michael@0 | 210 | next(); |
michael@0 | 211 | } |
michael@0 | 212 | req.onerror = onFailure; |
michael@0 | 213 | }, |
michael@0 | 214 | function () { |
michael@0 | 215 | ok(true, "Waiting for all set callbacks"); |
michael@0 | 216 | var lock = mozSettings.createLock(); |
michael@0 | 217 | req = lock.get("screen.brightness"); |
michael@0 | 218 | req.onsuccess = function() { |
michael@0 | 219 | ok(true, "Done"); |
michael@0 | 220 | next(); |
michael@0 | 221 | } |
michael@0 | 222 | req.onerror = onFailure; |
michael@0 | 223 | }, |
michael@0 | 224 | function() { |
michael@0 | 225 | ok(true, "adding Observers 1"); |
michael@0 | 226 | navigator.mozSettings.addObserver("screen.brightness", observer1); |
michael@0 | 227 | navigator.mozSettings.addObserver("screen.brightness", observer1); |
michael@0 | 228 | navigator.mozSettings.addObserver("screen.brightness", observer2); |
michael@0 | 229 | navigator.mozSettings.addObserver("screen.brightness", observerWithNext); |
michael@0 | 230 | var lock = mozSettings.createLock(); |
michael@0 | 231 | req2 = lock.get("screen.brightness"); |
michael@0 | 232 | req2.onsuccess = function() { |
michael@0 | 233 | ok(true, "set observeSetting done!"); |
michael@0 | 234 | next(); |
michael@0 | 235 | }; |
michael@0 | 236 | req2.onerror = onFailure; |
michael@0 | 237 | }, |
michael@0 | 238 | function() { |
michael@0 | 239 | ok(true, "test observers"); |
michael@0 | 240 | var lock = mozSettings.createLock(); |
michael@0 | 241 | req = lock.set(screenBright); |
michael@0 | 242 | req.onsuccess = function () { |
michael@0 | 243 | ok(true, "set done"); |
michael@0 | 244 | } |
michael@0 | 245 | req.onerror = onFailure; |
michael@0 | 246 | }, |
michael@0 | 247 | function() { |
michael@0 | 248 | ok(true, "removing Event Listener"); |
michael@0 | 249 | var lock = mozSettings.createLock(); |
michael@0 | 250 | req = lock.set(screenBright); |
michael@0 | 251 | req.onsuccess = function () { |
michael@0 | 252 | ok(true, "set done"); |
michael@0 | 253 | navigator.mozSettings.removeObserver("screen.brightness", observer2); |
michael@0 | 254 | navigator.mozSettings.removeObserver("screen.brightness", observer1); |
michael@0 | 255 | } |
michael@0 | 256 | req.onerror = onFailure; |
michael@0 | 257 | }, |
michael@0 | 258 | function() { |
michael@0 | 259 | ok(true, "test Event Listener"); |
michael@0 | 260 | var lock = mozSettings.createLock(); |
michael@0 | 261 | req = lock.set(screenBright); |
michael@0 | 262 | req.onsuccess = function () { |
michael@0 | 263 | ok(true, "set done"); |
michael@0 | 264 | } |
michael@0 | 265 | req.onerror = onFailure; |
michael@0 | 266 | }, |
michael@0 | 267 | function() { |
michael@0 | 268 | ok(true, "removing Event Listener"); |
michael@0 | 269 | var lock = mozSettings.createLock(); |
michael@0 | 270 | navigator.mozSettings.removeObserver("screen.brightness", observerWithNext); |
michael@0 | 271 | req = lock.set(screenBright); |
michael@0 | 272 | req.onsuccess = function () { |
michael@0 | 273 | ok(true, "set done"); |
michael@0 | 274 | navigator.mozSettings.removeObserver("screen.brightness", observer2); |
michael@0 | 275 | navigator.mozSettings.removeObserver("screen.brightness", observer1); |
michael@0 | 276 | next(); |
michael@0 | 277 | } |
michael@0 | 278 | req.onerror = onFailure; |
michael@0 | 279 | }, |
michael@0 | 280 | function() { |
michael@0 | 281 | ok(true, "removing Event Listener"); |
michael@0 | 282 | var lock = mozSettings.createLock(); |
michael@0 | 283 | req = lock.get("screen.brightness"); |
michael@0 | 284 | req.onsuccess = function () { |
michael@0 | 285 | ok(true, "get done"); |
michael@0 | 286 | next(); |
michael@0 | 287 | } |
michael@0 | 288 | req.onerror = onFailure; |
michael@0 | 289 | }, |
michael@0 | 290 | function () { |
michael@0 | 291 | ok(true, "Nested test"); |
michael@0 | 292 | var lock = mozSettings.createLock(); |
michael@0 | 293 | req = lock.get("screen.brightness"); |
michael@0 | 294 | req.onsuccess = function () { |
michael@0 | 295 | req3 = lock.set({"screen.brightness": req.result["screen.brightness"] + 1}) |
michael@0 | 296 | req3.onsuccess = function () { |
michael@0 | 297 | req4 = lock.get("screen.brightness"); |
michael@0 | 298 | req4.onsuccess = function() { |
michael@0 | 299 | is(req4.result["screen.brightness"], 1.7, "same Value"); |
michael@0 | 300 | } |
michael@0 | 301 | req4.onerror = onFailure; |
michael@0 | 302 | } |
michael@0 | 303 | req3.onerror = onFailure; |
michael@0 | 304 | }; |
michael@0 | 305 | req.onerror = onFailure; |
michael@0 | 306 | |
michael@0 | 307 | req2 = lock.get("screen.brightness"); |
michael@0 | 308 | req2.onsuccess = function () { |
michael@0 | 309 | is(req2.result["screen.brightness"], 0.7, "same Value"); |
michael@0 | 310 | } |
michael@0 | 311 | req2.onerror = onFailure; |
michael@0 | 312 | |
michael@0 | 313 | var lock2 = mozSettings.createLock(); |
michael@0 | 314 | req5 = lock2.get("screen.brightness"); |
michael@0 | 315 | req5.onsuccess = function () { |
michael@0 | 316 | is(req5.result["screen.brightness"], 1.7, "same Value"); |
michael@0 | 317 | next(); |
michael@0 | 318 | } |
michael@0 | 319 | req5.onerror = onFailure; |
michael@0 | 320 | }, |
michael@0 | 321 | function () { |
michael@0 | 322 | ok(true, "Deleting database"); |
michael@0 | 323 | var lock = mozSettings.createLock(); |
michael@0 | 324 | req = lock.clear(); |
michael@0 | 325 | req.onsuccess = function () { |
michael@0 | 326 | ok(true, "Deleted the database"); |
michael@0 | 327 | }; |
michael@0 | 328 | req.onerror = onFailure; |
michael@0 | 329 | req2 = lock.set(wifi); |
michael@0 | 330 | req2.onsuccess = function () { |
michael@0 | 331 | ok(true, "set done"); |
michael@0 | 332 | } |
michael@0 | 333 | req2.onerror = onFailure; |
michael@0 | 334 | |
michael@0 | 335 | ok(true, "Get all settings"); |
michael@0 | 336 | var lock2 = mozSettings.createLock(); |
michael@0 | 337 | req = lock2.get("*"); |
michael@0 | 338 | req.onsuccess = function () { |
michael@0 | 339 | is(Object.keys(req.result).length, 1, "length 1"); |
michael@0 | 340 | check(wifi, req.result); |
michael@0 | 341 | ok(true, JSON.stringify(req.result)); |
michael@0 | 342 | ok(true, "Get all settings Done"); |
michael@0 | 343 | }; |
michael@0 | 344 | req.onerror = onFailure; |
michael@0 | 345 | |
michael@0 | 346 | req2 = lock2.get("net3g.apn"); |
michael@0 | 347 | req2.onsuccess = function () { |
michael@0 | 348 | is(Object.keys(req2.result).length, 1, "length 1"); |
michael@0 | 349 | check(wifi, req2.result); |
michael@0 | 350 | ok(true, "Get net3g.apn Done"); |
michael@0 | 351 | next(); |
michael@0 | 352 | }; |
michael@0 | 353 | req2.onerror = onFailure; |
michael@0 | 354 | }, |
michael@0 | 355 | function () { |
michael@0 | 356 | ok(true, "Change wifi1"); |
michael@0 | 357 | var lock = mozSettings.createLock(); |
michael@0 | 358 | req = lock.set(wifi2); |
michael@0 | 359 | req.onsuccess = function () { |
michael@0 | 360 | ok(true, "Set Done"); |
michael@0 | 361 | }; |
michael@0 | 362 | req.onerror = onFailure; |
michael@0 | 363 | |
michael@0 | 364 | ok(true, "Get changed net3g.apn"); |
michael@0 | 365 | req2 = lock.get("net3g.apn"); |
michael@0 | 366 | req2.onsuccess = function () { |
michael@0 | 367 | is(Object.keys(req2.result).length, 1, "length 1"); |
michael@0 | 368 | check(wifi2, req2.result); |
michael@0 | 369 | ok(true, "Get net3g.apn Done"); |
michael@0 | 370 | next(); |
michael@0 | 371 | }; |
michael@0 | 372 | req2.onerror = onFailure; |
michael@0 | 373 | }, |
michael@0 | 374 | function () { |
michael@0 | 375 | ok(true, "Test locking"); |
michael@0 | 376 | var lock = mozSettings.createLock(); |
michael@0 | 377 | var lock2 = mozSettings.createLock(); |
michael@0 | 378 | req = lock.set(wifiEnabled); |
michael@0 | 379 | req.onsuccess = function () { |
michael@0 | 380 | ok(true, "Test Locking Done"); |
michael@0 | 381 | }; |
michael@0 | 382 | req.onerror = onFailure; |
michael@0 | 383 | |
michael@0 | 384 | req2 = lock2.set(wifiDisabled); |
michael@0 | 385 | req2.onsuccess = function () { |
michael@0 | 386 | ok(true, "Set Done"); |
michael@0 | 387 | next(); |
michael@0 | 388 | }; |
michael@0 | 389 | req2.onerror = onFailure; |
michael@0 | 390 | }, |
michael@0 | 391 | function () { |
michael@0 | 392 | ok(true, "Test locking result"); |
michael@0 | 393 | var lock = mozSettings.createLock(); |
michael@0 | 394 | req = lock.get("wifi.enabled"); |
michael@0 | 395 | req.onsuccess = function() { |
michael@0 | 396 | check(req.result, wifiDisabled); |
michael@0 | 397 | ok(true, "Test1 locking result done"); |
michael@0 | 398 | next(); |
michael@0 | 399 | } |
michael@0 | 400 | req.onerror = onFailure; |
michael@0 | 401 | }, |
michael@0 | 402 | function () { |
michael@0 | 403 | ok(true, "Test locking heavy"); |
michael@0 | 404 | for (var i=0; i<30; i++) { |
michael@0 | 405 | // only new locks! |
michael@0 | 406 | var lock = mozSettings.createLock(); |
michael@0 | 407 | var obj = {}; |
michael@0 | 408 | obj["wifi.enabled" + i] = true; |
michael@0 | 409 | req = lock.set( obj ); |
michael@0 | 410 | req.onsuccess = function () { |
michael@0 | 411 | ok(true, "Set1 Done"); |
michael@0 | 412 | }; |
michael@0 | 413 | req.onerror = onFailure; |
michael@0 | 414 | }; |
michael@0 | 415 | { |
michael@0 | 416 | var lock2 = mozSettings.createLock(); |
michael@0 | 417 | req2 = lock2.get("*"); |
michael@0 | 418 | req2.onsuccess = function () { |
michael@0 | 419 | is(Object.keys(req2.result).length, 32, "length 12"); |
michael@0 | 420 | ok(true, JSON.stringify(req2.result)); |
michael@0 | 421 | ok(true, "Get all settings Done"); |
michael@0 | 422 | }; |
michael@0 | 423 | req2.onerror = onFailure; |
michael@0 | 424 | } |
michael@0 | 425 | var lock2 = mozSettings.createLock(); |
michael@0 | 426 | var obj = {}; |
michael@0 | 427 | obj["wifi.enabled" + 30] = true; |
michael@0 | 428 | req3 = lock2.set( obj ); |
michael@0 | 429 | req3.onsuccess = function () { |
michael@0 | 430 | ok(true, "Set12 Done"); |
michael@0 | 431 | }; |
michael@0 | 432 | req3.onerror = onFailure; |
michael@0 | 433 | |
michael@0 | 434 | var lock3 = mozSettings.createLock(); |
michael@0 | 435 | // with one lock |
michael@0 | 436 | for (var i = 0; i < 30; i++) { |
michael@0 | 437 | req4 = lock3.get("wifi.enabled" + i); |
michael@0 | 438 | var testObj = {}; |
michael@0 | 439 | testObj["wifi.enabled" + i] = true; |
michael@0 | 440 | req4.onsuccess = function () { |
michael@0 | 441 | check(this.request.result, this.testObj); |
michael@0 | 442 | ok(true, "Get1 Done"); |
michael@0 | 443 | }.bind({testObj: testObj, request: req4}); |
michael@0 | 444 | req4.onerror = onFailure; |
michael@0 | 445 | } |
michael@0 | 446 | |
michael@0 | 447 | ok(true, "start next2!"); |
michael@0 | 448 | var lock4 = mozSettings.createLock(); |
michael@0 | 449 | for (var i=0; i<30; i++) { |
michael@0 | 450 | var obj = {}; |
michael@0 | 451 | obj["wifi.enabled" + i] = false; |
michael@0 | 452 | req4 = lock4.set( obj ); |
michael@0 | 453 | req4.onsuccess = function () { |
michael@0 | 454 | ok(true, "Set2 Done"); |
michael@0 | 455 | }; |
michael@0 | 456 | req4.onerror = onFailure; |
michael@0 | 457 | } |
michael@0 | 458 | var lock5 = mozSettings.createLock(); |
michael@0 | 459 | for (var i=0; i<30; i++) { |
michael@0 | 460 | req5 = lock5.get("wifi.enabled" + i); |
michael@0 | 461 | var testObj = {}; |
michael@0 | 462 | testObj["wifi.enabled" + i] = false; |
michael@0 | 463 | req5.onsuccess = function () { |
michael@0 | 464 | check(this.request.result, this.testObj); |
michael@0 | 465 | ok(true, "Get2 Done"); |
michael@0 | 466 | }.bind({testObj: testObj, request: req5}); |
michael@0 | 467 | req5.onerror = onFailure; |
michael@0 | 468 | } |
michael@0 | 469 | |
michael@0 | 470 | var lock5 = mozSettings.createLock(); |
michael@0 | 471 | req6 = lock5.clear(); |
michael@0 | 472 | req6.onsuccess = function () { |
michael@0 | 473 | ok(true, "Deleted the database"); |
michael@0 | 474 | next(); |
michael@0 | 475 | }; |
michael@0 | 476 | req6.onerror = onFailure; |
michael@0 | 477 | }, |
michael@0 | 478 | function () { |
michael@0 | 479 | ok(true, "reverse Test locking"); |
michael@0 | 480 | var lock2 = mozSettings.createLock(); |
michael@0 | 481 | var lock = mozSettings.createLock(); |
michael@0 | 482 | |
michael@0 | 483 | req = lock.set(wifiEnabled); |
michael@0 | 484 | req.onsuccess = function () { |
michael@0 | 485 | ok(true, "Test Locking Done"); |
michael@0 | 486 | next(); |
michael@0 | 487 | }; |
michael@0 | 488 | req.onerror = onFailure; |
michael@0 | 489 | |
michael@0 | 490 | req2 = lock2.set(wifiDisabled); |
michael@0 | 491 | req2.onsuccess = function () { |
michael@0 | 492 | ok(true, "Set Done"); |
michael@0 | 493 | }; |
michael@0 | 494 | req2.onerror = onFailure; |
michael@0 | 495 | }, |
michael@0 | 496 | function () { |
michael@0 | 497 | ok(true, "Test locking result"); |
michael@0 | 498 | var lock = mozSettings.createLock(); |
michael@0 | 499 | |
michael@0 | 500 | req = lock.get("wifi.enabled"); |
michael@0 | 501 | req.onsuccess = function() { |
michael@0 | 502 | check(req.result, wifiEnabled); |
michael@0 | 503 | ok(true, "Test2 locking result done"); |
michael@0 | 504 | } |
michael@0 | 505 | req.onerror = onFailure; |
michael@0 | 506 | |
michael@0 | 507 | req2 = lock.clear(); |
michael@0 | 508 | req2.onsuccess = function () { |
michael@0 | 509 | ok(true, "Deleted the database"); |
michael@0 | 510 | }; |
michael@0 | 511 | req2.onerror = onFailure; |
michael@0 | 512 | |
michael@0 | 513 | req3 = lock.set(wifi); |
michael@0 | 514 | req3.onsuccess = function () { |
michael@0 | 515 | ok(true, "set done"); |
michael@0 | 516 | next(); |
michael@0 | 517 | } |
michael@0 | 518 | req3.onerror = onFailure; |
michael@0 | 519 | |
michael@0 | 520 | }, |
michael@0 | 521 | function () { |
michael@0 | 522 | ok(true, "Get all settings"); |
michael@0 | 523 | var lock = mozSettings.createLock(); |
michael@0 | 524 | req = lock.get("*"); |
michael@0 | 525 | req.onsuccess = function () { |
michael@0 | 526 | is(Object.keys(req.result).length, 1, "length 1"); |
michael@0 | 527 | check(wifi, req.result); |
michael@0 | 528 | ok(true, "Get all settings Done"); |
michael@0 | 529 | next(); |
michael@0 | 530 | }; |
michael@0 | 531 | req.onerror = onFailure; |
michael@0 | 532 | }, |
michael@0 | 533 | function () { |
michael@0 | 534 | ok(true, "Get net3g.apn"); |
michael@0 | 535 | var lock = mozSettings.createLock(); |
michael@0 | 536 | req = lock.get("net3g.apn"); |
michael@0 | 537 | req.onsuccess = function () { |
michael@0 | 538 | is(Object.keys(req.result).length, 1, "length 1"); |
michael@0 | 539 | check(wifi, req.result); |
michael@0 | 540 | ok(true, "Get net3g.apn Done"); |
michael@0 | 541 | next(); |
michael@0 | 542 | }; |
michael@0 | 543 | req.onerror = onFailure; |
michael@0 | 544 | }, |
michael@0 | 545 | function () { |
michael@0 | 546 | ok(true, "Change wifi2"); |
michael@0 | 547 | var lock = mozSettings.createLock(); |
michael@0 | 548 | req = lock.set(wifi2); |
michael@0 | 549 | req.onsuccess = function () { |
michael@0 | 550 | ok(true, "Set Done"); |
michael@0 | 551 | next(); |
michael@0 | 552 | }; |
michael@0 | 553 | req.onerror = onFailure; |
michael@0 | 554 | }, |
michael@0 | 555 | function () { |
michael@0 | 556 | ok(true, "Get net3g.apn"); |
michael@0 | 557 | var lock = mozSettings.createLock(); |
michael@0 | 558 | req = lock.get("net3g.apn"); |
michael@0 | 559 | req.onsuccess = function () { |
michael@0 | 560 | is(Object.keys(req.result).length, 1, "length 1"); |
michael@0 | 561 | check(wifi2, req.result); |
michael@0 | 562 | ok(true, "Get net3g.apn Done"); |
michael@0 | 563 | next(); |
michael@0 | 564 | }; |
michael@0 | 565 | req.onerror = onFailure; |
michael@0 | 566 | }, |
michael@0 | 567 | function () { |
michael@0 | 568 | ok(true, "Add wifi.enabled"); |
michael@0 | 569 | var lock = mozSettings.createLock(); |
michael@0 | 570 | req = lock.set(wifiEnabled); |
michael@0 | 571 | req.onsuccess = function () { |
michael@0 | 572 | ok(true, "Set Done"); |
michael@0 | 573 | next(); |
michael@0 | 574 | }; |
michael@0 | 575 | req.onerror = onFailure; |
michael@0 | 576 | }, |
michael@0 | 577 | function () { |
michael@0 | 578 | ok(true, "Get Wifi Enabled"); |
michael@0 | 579 | var lock = mozSettings.createLock(); |
michael@0 | 580 | req = lock.get("wifi.enabled"); |
michael@0 | 581 | req.onsuccess = function () { |
michael@0 | 582 | is(Object.keys(req.result).length, 1, "length 1"); |
michael@0 | 583 | check(wifiEnabled, req.result); |
michael@0 | 584 | ok(true, "Get wifi.enabledDone"); |
michael@0 | 585 | next(); |
michael@0 | 586 | }; |
michael@0 | 587 | req.onerror = onFailure; |
michael@0 | 588 | }, |
michael@0 | 589 | function () { |
michael@0 | 590 | ok(true, "Get all"); |
michael@0 | 591 | var lock = mozSettings.createLock(); |
michael@0 | 592 | req = lock.get("*"); |
michael@0 | 593 | req.onsuccess = function () { |
michael@0 | 594 | is(Object.keys(req.result).length, 2, "length 2"); |
michael@0 | 595 | check(wifiEnabled["wifi.enabled"], req.result["wifi.enabled"]); |
michael@0 | 596 | check(wifi2["net3g.apn"], req.result["net3g.apn"]); |
michael@0 | 597 | ok(true, "Get all Done"); |
michael@0 | 598 | next(); |
michael@0 | 599 | }; |
michael@0 | 600 | req.onerror = onFailure; |
michael@0 | 601 | }, |
michael@0 | 602 | function () { |
michael@0 | 603 | ok(true, "Add wifiNetworks"); |
michael@0 | 604 | var lock = mozSettings.createLock(); |
michael@0 | 605 | req = lock.set(wifiNetworks0); |
michael@0 | 606 | req.onsuccess = function () { |
michael@0 | 607 | ok(true, "Set Done"); |
michael@0 | 608 | }; |
michael@0 | 609 | req.onerror = onFailure; |
michael@0 | 610 | |
michael@0 | 611 | req2 = lock.set(wifiNetworks1); |
michael@0 | 612 | req2.onsuccess = function () { |
michael@0 | 613 | ok(true, "Set Done"); |
michael@0 | 614 | next(); |
michael@0 | 615 | }; |
michael@0 | 616 | req2.onerror = onFailure; |
michael@0 | 617 | }, |
michael@0 | 618 | function () { |
michael@0 | 619 | ok(true, "Get Wifi Networks"); |
michael@0 | 620 | var lock = mozSettings.createLock(); |
michael@0 | 621 | req = lock.get("wifi.networks[0]"); |
michael@0 | 622 | req.onsuccess = function () { |
michael@0 | 623 | is(Object.keys(req.result).length, 1, "length 1"); |
michael@0 | 624 | check(wifiNetworks0, req.result); |
michael@0 | 625 | ok(true, "Get wifi.networks[0]"); |
michael@0 | 626 | next(); |
michael@0 | 627 | }; |
michael@0 | 628 | req.onerror = onFailure; |
michael@0 | 629 | }, |
michael@0 | 630 | function() { |
michael@0 | 631 | ok(true, "Clear DB, multiple locks"); |
michael@0 | 632 | var lock4 = mozSettings.createLock(); |
michael@0 | 633 | var lock3 = mozSettings.createLock(); |
michael@0 | 634 | var lock2 = mozSettings.createLock(); |
michael@0 | 635 | var lock = mozSettings.createLock(); |
michael@0 | 636 | var lock6 = mozSettings.createLock(); |
michael@0 | 637 | var lock7 = mozSettings.createLock(); |
michael@0 | 638 | req = lock.clear(); |
michael@0 | 639 | req.onsuccess = function () { |
michael@0 | 640 | ok(true, "Deleted the database"); |
michael@0 | 641 | next(); |
michael@0 | 642 | }; |
michael@0 | 643 | req.onerror = onFailure; |
michael@0 | 644 | }, |
michael@0 | 645 | function () { |
michael@0 | 646 | ok(true, "Add wifiNetworks"); |
michael@0 | 647 | var lock = mozSettings.createLock(); |
michael@0 | 648 | req = lock.set(wifiNetworks0); |
michael@0 | 649 | req.onsuccess = function () { |
michael@0 | 650 | ok(true, "Set Done"); |
michael@0 | 651 | next(); |
michael@0 | 652 | }; |
michael@0 | 653 | req.onerror = onFailure; |
michael@0 | 654 | }, |
michael@0 | 655 | function () { |
michael@0 | 656 | ok(true, "Test set after lock closed"); |
michael@0 | 657 | var lockx = mozSettings.createLock(); |
michael@0 | 658 | var cb = function() { |
michael@0 | 659 | var reqx = null; |
michael@0 | 660 | try { |
michael@0 | 661 | reqx = lockx.set(wifiNetworks0); |
michael@0 | 662 | ok(false, "should have thrown"); |
michael@0 | 663 | } catch (ex) { |
michael@0 | 664 | ok(reqx == null, "request is still null"); |
michael@0 | 665 | ok(true, "Caught Exception"); |
michael@0 | 666 | next(); |
michael@0 | 667 | } |
michael@0 | 668 | } |
michael@0 | 669 | SimpleTest.executeSoon(cb); |
michael@0 | 670 | }, |
michael@0 | 671 | function() { |
michael@0 | 672 | ok(true, "Clear DB"); |
michael@0 | 673 | var lock = mozSettings.createLock(); |
michael@0 | 674 | req = lock.clear(); |
michael@0 | 675 | req.onsuccess = function () { |
michael@0 | 676 | ok(true, "Deleted the database"); |
michael@0 | 677 | next(); |
michael@0 | 678 | }; |
michael@0 | 679 | req.onerror = onFailure; |
michael@0 | 680 | }, |
michael@0 | 681 | function() { |
michael@0 | 682 | ok(true, "Set with multiple arguments"); |
michael@0 | 683 | var lock = mozSettings.createLock(); |
michael@0 | 684 | req = lock.set(combination); |
michael@0 | 685 | req.onsuccess = function () { |
michael@0 | 686 | ok(true, "Set Done"); |
michael@0 | 687 | next(); |
michael@0 | 688 | }; |
michael@0 | 689 | req.onerror = onFailure; |
michael@0 | 690 | }, |
michael@0 | 691 | function() { |
michael@0 | 692 | ok(true, "request argument from multiple set"); |
michael@0 | 693 | var lock = mozSettings.createLock(); |
michael@0 | 694 | req = lock.get("screen.brightness"); |
michael@0 | 695 | req.onsuccess = function () { |
michael@0 | 696 | check(req.result["screen.brightness"], 0.7, "get done"); |
michael@0 | 697 | next(); |
michael@0 | 698 | } |
michael@0 | 699 | req.onerror = onFailure; |
michael@0 | 700 | }, |
michael@0 | 701 | function() { |
michael@0 | 702 | ok(true, "Test closed attribute on a valid lock"); |
michael@0 | 703 | var lock = mozSettings.createLock(); |
michael@0 | 704 | is(lock.closed, false, "closed attribute is false on creation"); |
michael@0 | 705 | req = lock.get("screen.brightness"); |
michael@0 | 706 | req.onsuccess = function () { |
michael@0 | 707 | is(lock.closed, false, "closed attribute is false on success callback"); |
michael@0 | 708 | next(); |
michael@0 | 709 | } |
michael@0 | 710 | req.onerror = onFailure; |
michael@0 | 711 | }, |
michael@0 | 712 | function () { |
michael@0 | 713 | ok(true, "Test closed attribute on invalid lock"); |
michael@0 | 714 | var lockx = mozSettings.createLock(); |
michael@0 | 715 | var cb = function() { |
michael@0 | 716 | var reqx = null; |
michael@0 | 717 | try { |
michael@0 | 718 | reqx = lockx.set(wifiNetworks0); |
michael@0 | 719 | ok(false, "should have thrown"); |
michael@0 | 720 | } catch (ex) { |
michael@0 | 721 | is(lockx.closed, true, "closed attribute is true"); |
michael@0 | 722 | ok(true, "Caught Exception"); |
michael@0 | 723 | next(); |
michael@0 | 724 | } |
michael@0 | 725 | } |
michael@0 | 726 | SimpleTest.executeSoon(cb); |
michael@0 | 727 | }, |
michael@0 | 728 | function() { |
michael@0 | 729 | ok(true, "Clear DB"); |
michael@0 | 730 | var lock = mozSettings.createLock(); |
michael@0 | 731 | req = lock.clear(); |
michael@0 | 732 | req.onsuccess = function () { |
michael@0 | 733 | ok(true, "Deleted the database"); |
michael@0 | 734 | next(); |
michael@0 | 735 | }; |
michael@0 | 736 | req.onerror = onFailure; |
michael@0 | 737 | }, |
michael@0 | 738 | function() { |
michael@0 | 739 | ok(true, "Set object value"); |
michael@0 | 740 | var lock = mozSettings.createLock(); |
michael@0 | 741 | req = lock.set({"setting-obj": {foo: {bar: 23}}}); |
michael@0 | 742 | req.onsuccess = function() { |
michael@0 | 743 | req2 = lock.get("setting-obj"); |
michael@0 | 744 | req2.onsuccess = function(event) { |
michael@0 | 745 | var result = event.target.result["setting-obj"]; |
michael@0 | 746 | ok(result, "Got valid result"); |
michael@0 | 747 | ok(typeof result == "object", "Result is object"); |
michael@0 | 748 | ok("foo" in result && "bar" in result.foo, "Result has properties"); |
michael@0 | 749 | ok(result.foo.bar == 23, "Result properties are set"); |
michael@0 | 750 | next(); |
michael@0 | 751 | }; |
michael@0 | 752 | }; |
michael@0 | 753 | }, |
michael@0 | 754 | function() { |
michael@0 | 755 | ok(true, "Clear DB"); |
michael@0 | 756 | var lock = mozSettings.createLock(); |
michael@0 | 757 | req = lock.clear(); |
michael@0 | 758 | req.onsuccess = function () { |
michael@0 | 759 | ok(true, "Deleted the database"); |
michael@0 | 760 | next(); |
michael@0 | 761 | }; |
michael@0 | 762 | req.onerror = onFailure; |
michael@0 | 763 | }, |
michael@0 | 764 | function () { |
michael@0 | 765 | ok(true, "all done!\n"); |
michael@0 | 766 | SimpleTest.finish(); |
michael@0 | 767 | } |
michael@0 | 768 | ]; |
michael@0 | 769 | |
michael@0 | 770 | function next() { |
michael@0 | 771 | ok(true, "Begin!"); |
michael@0 | 772 | if (index >= steps.length) { |
michael@0 | 773 | ok(false, "Shouldn't get here!"); |
michael@0 | 774 | return; |
michael@0 | 775 | } |
michael@0 | 776 | try { |
michael@0 | 777 | steps[index](); |
michael@0 | 778 | } catch(ex) { |
michael@0 | 779 | ok(false, "Caught exception", ex); |
michael@0 | 780 | } |
michael@0 | 781 | index += 1; |
michael@0 | 782 | } |
michael@0 | 783 | |
michael@0 | 784 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 785 | addLoadEvent(next); |
michael@0 | 786 | </script> |
michael@0 | 787 | </pre> |
michael@0 | 788 | </body> |
michael@0 | 789 | </html> |