dom/indexedDB/test/unit/test_autoIncrement.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 if (!this.window) {
michael@0 7 this.runTest = function() {
michael@0 8 todo(false, "Test disabled in xpcshell test suite for now");
michael@0 9 finishTest();
michael@0 10 }
michael@0 11 }
michael@0 12
michael@0 13 var testGenerator = testSteps();
michael@0 14
michael@0 15 function genCheck(key, value, test, options) {
michael@0 16 return function(event) {
michael@0 17 is(JSON.stringify(event.target.result), JSON.stringify(key),
michael@0 18 "correct returned key in " + test);
michael@0 19 if (options && options.store) {
michael@0 20 is(event.target.source, options.store, "correct store in " + test);
michael@0 21 }
michael@0 22 if (options && options.trans) {
michael@0 23 is(event.target.transaction, options.trans, "correct transaction in " + test);
michael@0 24 }
michael@0 25
michael@0 26 event.target.source.get(key).onsuccess = function(event) {
michael@0 27 is(JSON.stringify(event.target.result), JSON.stringify(value),
michael@0 28 "correct stored value in " + test);
michael@0 29 continueToNextStepSync();
michael@0 30 }
michael@0 31 }
michael@0 32 }
michael@0 33
michael@0 34 function testSteps()
michael@0 35 {
michael@0 36 const dbname = this.window ? window.location.pathname : "Splendid Test";
michael@0 37 const RW = "readwrite";
michael@0 38 let c1 = 1;
michael@0 39 let c2 = 1;
michael@0 40
michael@0 41 let openRequest = indexedDB.open(dbname, 1);
michael@0 42 openRequest.onerror = errorHandler;
michael@0 43 openRequest.onupgradeneeded = grabEventAndContinueHandler;
michael@0 44 openRequest.onsuccess = unexpectedSuccessHandler;
michael@0 45 let event = yield undefined;
michael@0 46 let db = event.target.result;
michael@0 47 let trans = event.target.transaction;
michael@0 48
michael@0 49 // Create test stores
michael@0 50 let store1 = db.createObjectStore("store1", { autoIncrement: true });
michael@0 51 let store2 = db.createObjectStore("store2", { autoIncrement: true, keyPath: "id" });
michael@0 52 let store3 = db.createObjectStore("store3", { autoIncrement: false });
michael@0 53 is(store1.autoIncrement, true, "store1 .autoIncrement");
michael@0 54 is(store2.autoIncrement, true, "store2 .autoIncrement");
michael@0 55 is(store3.autoIncrement, false, "store3 .autoIncrement");
michael@0 56
michael@0 57 store1.createIndex("unique1", "unique", { unique: true });
michael@0 58 store2.createIndex("unique1", "unique", { unique: true });
michael@0 59
michael@0 60 // Test simple inserts
michael@0 61 let test = " for test simple insert"
michael@0 62 store1.add({ foo: "value1" }).onsuccess =
michael@0 63 genCheck(c1++, { foo: "value1" }, "first" + test);
michael@0 64 store1.add({ foo: "value2" }).onsuccess =
michael@0 65 genCheck(c1++, { foo: "value2" }, "second" + test);
michael@0 66
michael@0 67 yield undefined;
michael@0 68 yield undefined;
michael@0 69
michael@0 70 store2.put({ bar: "value1" }).onsuccess =
michael@0 71 genCheck(c2, { bar: "value1", id: c2 }, "first in store2" + test,
michael@0 72 { store: store2 });
michael@0 73 c2++;
michael@0 74 store1.put({ foo: "value3" }).onsuccess =
michael@0 75 genCheck(c1++, { foo: "value3" }, "third" + test,
michael@0 76 { store: store1 });
michael@0 77
michael@0 78 yield undefined;
michael@0 79 yield undefined;
michael@0 80
michael@0 81 store2.get(IDBKeyRange.lowerBound(c2)).onsuccess = grabEventAndContinueHandler;
michael@0 82 event = yield undefined;
michael@0 83 is(event.target.result, undefined, "no such value" + test);
michael@0 84
michael@0 85 // Close version_change transaction
michael@0 86 openRequest.onsuccess = grabEventAndContinueHandler;
michael@0 87 event = yield undefined;
michael@0 88
michael@0 89 is(event.target, openRequest, "succeeded to open" + test);
michael@0 90 is(event.type, "success", "succeeded to open" + test);
michael@0 91
michael@0 92 // Test inserting explicit keys
michael@0 93 test = " for test explicit keys";
michael@0 94 trans = db.transaction("store1", RW);
michael@0 95 trans.objectStore("store1").add({ explicit: 1 }, 100).onsuccess =
michael@0 96 genCheck(100, { explicit: 1 }, "first" + test);
michael@0 97 c1 = 101;
michael@0 98 trans = db.transaction("store1", RW);
michael@0 99 trans.objectStore("store1").add({ explicit: 2 }).onsuccess =
michael@0 100 genCheck(c1++, { explicit: 2 }, "second" + test);
michael@0 101 yield undefined; yield undefined;
michael@0 102
michael@0 103 trans = db.transaction("store1", RW);
michael@0 104 trans.objectStore("store1").add({ explicit: 3 }, 200).onsuccess =
michael@0 105 genCheck(200, { explicit: 3 }, "third" + test);
michael@0 106 c1 = 201;
michael@0 107 trans.objectStore("store1").add({ explicit: 4 }).onsuccess =
michael@0 108 genCheck(c1++, { explicit: 4 }, "fourth" + test);
michael@0 109 yield undefined; yield undefined;
michael@0 110
michael@0 111 trans = db.transaction("store1", RW);
michael@0 112 trans.objectStore("store1").add({ explicit: 5 }, 150).onsuccess =
michael@0 113 genCheck(150, { explicit: 5 }, "fifth" + test);
michael@0 114 yield undefined;
michael@0 115 trans.objectStore("store1").add({ explicit: 6 }).onsuccess =
michael@0 116 genCheck(c1++, { explicit: 6 }, "sixth" + test);
michael@0 117 yield undefined;
michael@0 118
michael@0 119 trans = db.transaction("store1", RW);
michael@0 120 trans.objectStore("store1").add({ explicit: 7 }, "key").onsuccess =
michael@0 121 genCheck("key", { explicit: 7 }, "seventh" + test);
michael@0 122 yield undefined;
michael@0 123 trans.objectStore("store1").add({ explicit: 8 }).onsuccess =
michael@0 124 genCheck(c1++, { explicit: 8 }, "eighth" + test);
michael@0 125 yield undefined;
michael@0 126
michael@0 127 trans = db.transaction("store1", RW);
michael@0 128 trans.objectStore("store1").add({ explicit: 7 }, [100000]).onsuccess =
michael@0 129 genCheck([100000], { explicit: 7 }, "seventh" + test);
michael@0 130 yield undefined;
michael@0 131 trans.objectStore("store1").add({ explicit: 8 }).onsuccess =
michael@0 132 genCheck(c1++, { explicit: 8 }, "eighth" + test);
michael@0 133 yield undefined;
michael@0 134
michael@0 135 trans = db.transaction("store1", RW);
michael@0 136 trans.objectStore("store1").add({ explicit: 9 }, -100000).onsuccess =
michael@0 137 genCheck(-100000, { explicit: 9 }, "ninth" + test);
michael@0 138 yield undefined;
michael@0 139 trans.objectStore("store1").add({ explicit: 10 }).onsuccess =
michael@0 140 genCheck(c1++, { explicit: 10 }, "tenth" + test);
michael@0 141 yield undefined;
michael@0 142
michael@0 143
michael@0 144 trans = db.transaction("store2", RW);
michael@0 145 trans.objectStore("store2").add({ explicit2: 1, id: 300 }).onsuccess =
michael@0 146 genCheck(300, { explicit2: 1, id: 300 }, "first store2" + test);
michael@0 147 c2 = 301;
michael@0 148 trans = db.transaction("store2", RW);
michael@0 149 trans.objectStore("store2").add({ explicit2: 2 }).onsuccess =
michael@0 150 genCheck(c2, { explicit2: 2, id: c2 }, "second store2" + test);
michael@0 151 c2++;
michael@0 152 yield undefined; yield undefined;
michael@0 153
michael@0 154 trans = db.transaction("store2", RW);
michael@0 155 trans.objectStore("store2").add({ explicit2: 3, id: 400 }).onsuccess =
michael@0 156 genCheck(400, { explicit2: 3, id: 400 }, "third store2" + test);
michael@0 157 c2 = 401;
michael@0 158 trans.objectStore("store2").add({ explicit2: 4 }).onsuccess =
michael@0 159 genCheck(c2, { explicit2: 4, id: c2 }, "fourth store2" + test);
michael@0 160 c2++;
michael@0 161 yield undefined; yield undefined;
michael@0 162
michael@0 163 trans = db.transaction("store2", RW);
michael@0 164 trans.objectStore("store2").add({ explicit: 5, id: 150 }).onsuccess =
michael@0 165 genCheck(150, { explicit: 5, id: 150 }, "fifth store2" + test);
michael@0 166 yield undefined;
michael@0 167 trans.objectStore("store2").add({ explicit: 6 }).onsuccess =
michael@0 168 genCheck(c2, { explicit: 6, id: c2 }, "sixth store2" + test);
michael@0 169 c2++;
michael@0 170 yield undefined;
michael@0 171
michael@0 172 trans = db.transaction("store2", RW);
michael@0 173 trans.objectStore("store2").add({ explicit: 7, id: "key" }).onsuccess =
michael@0 174 genCheck("key", { explicit: 7, id: "key" }, "seventh store2" + test);
michael@0 175 yield undefined;
michael@0 176 trans.objectStore("store2").add({ explicit: 8 }).onsuccess =
michael@0 177 genCheck(c2, { explicit: 8, id: c2 }, "eighth store2" + test);
michael@0 178 c2++;
michael@0 179 yield undefined;
michael@0 180
michael@0 181 trans = db.transaction("store2", RW);
michael@0 182 trans.objectStore("store2").add({ explicit: 7, id: [100000] }).onsuccess =
michael@0 183 genCheck([100000], { explicit: 7, id: [100000] }, "seventh store2" + test);
michael@0 184 yield undefined;
michael@0 185 trans.objectStore("store2").add({ explicit: 8 }).onsuccess =
michael@0 186 genCheck(c2, { explicit: 8, id: c2 }, "eighth store2" + test);
michael@0 187 c2++;
michael@0 188 yield undefined;
michael@0 189
michael@0 190 trans = db.transaction("store2", RW);
michael@0 191 trans.objectStore("store2").add({ explicit: 9, id: -100000 }).onsuccess =
michael@0 192 genCheck(-100000, { explicit: 9, id: -100000 }, "ninth store2" + test);
michael@0 193 yield undefined;
michael@0 194 trans.objectStore("store2").add({ explicit: 10 }).onsuccess =
michael@0 195 genCheck(c2, { explicit: 10, id: c2 }, "tenth store2" + test);
michael@0 196 c2++;
michael@0 197 yield undefined;
michael@0 198
michael@0 199
michael@0 200 // Test separate transactions doesn't generate overlapping numbers
michael@0 201 test = " for test non-overlapping counts";
michael@0 202 trans = db.transaction("store1", RW);
michael@0 203 trans2 = db.transaction("store1", RW);
michael@0 204 trans2.objectStore("store1").put({ over: 2 }).onsuccess =
michael@0 205 genCheck(c1 + 1, { over: 2 }, "first" + test,
michael@0 206 { trans: trans2 });
michael@0 207 trans.objectStore("store1").put({ over: 1 }).onsuccess =
michael@0 208 genCheck(c1, { over: 1 }, "second" + test,
michael@0 209 { trans: trans });
michael@0 210 c1 += 2;
michael@0 211 yield undefined; yield undefined;
michael@0 212
michael@0 213 trans = db.transaction("store2", RW);
michael@0 214 trans2 = db.transaction("store2", RW);
michael@0 215 trans2.objectStore("store2").put({ over: 2 }).onsuccess =
michael@0 216 genCheck(c2 + 1, { over: 2, id: c2 + 1 }, "third" + test,
michael@0 217 { trans: trans2 });
michael@0 218 trans.objectStore("store2").put({ over: 1 }).onsuccess =
michael@0 219 genCheck(c2, { over: 1, id: c2 }, "fourth" + test,
michael@0 220 { trans: trans });
michael@0 221 c2 += 2;
michael@0 222 yield undefined; yield undefined;
michael@0 223
michael@0 224 // Test that error inserts doesn't increase generator
michael@0 225 test = " for test error inserts";
michael@0 226 trans = db.transaction(["store1", "store2"], RW);
michael@0 227 trans.objectStore("store1").add({ unique: 1 }, -1);
michael@0 228 trans.objectStore("store2").add({ unique: 1, id: "unique" });
michael@0 229
michael@0 230 trans.objectStore("store1").add({ error: 1, unique: 1 }).
michael@0 231 addEventListener("error", new ExpectError("ConstraintError", true));
michael@0 232 trans.objectStore("store1").add({ error: 2 }).onsuccess =
michael@0 233 genCheck(c1++, { error: 2 }, "first" + test);
michael@0 234 yield undefined; yield undefined;
michael@0 235
michael@0 236 trans.objectStore("store2").add({ error: 3, unique: 1 }).
michael@0 237 addEventListener("error", new ExpectError("ConstraintError", true));
michael@0 238 trans.objectStore("store2").add({ error: 4 }).onsuccess =
michael@0 239 genCheck(c2, { error: 4, id: c2 }, "second" + test);
michael@0 240 c2++;
michael@0 241 yield undefined; yield undefined;
michael@0 242
michael@0 243 trans.objectStore("store1").add({ error: 5, unique: 1 }, 100000).
michael@0 244 addEventListener("error", new ExpectError("ConstraintError", true));
michael@0 245 trans.objectStore("store1").add({ error: 6 }).onsuccess =
michael@0 246 genCheck(c1++, { error: 6 }, "third" + test);
michael@0 247 yield undefined; yield undefined;
michael@0 248
michael@0 249 trans.objectStore("store2").add({ error: 7, unique: 1, id: 100000 }).
michael@0 250 addEventListener("error", new ExpectError("ConstraintError", true));
michael@0 251 trans.objectStore("store2").add({ error: 8 }).onsuccess =
michael@0 252 genCheck(c2, { error: 8, id: c2 }, "fourth" + test);
michael@0 253 c2++;
michael@0 254 yield undefined; yield undefined;
michael@0 255
michael@0 256 // Test that aborts doesn't increase generator
michael@0 257 test = " for test aborted transaction";
michael@0 258 trans = db.transaction(["store1", "store2"], RW);
michael@0 259 trans.objectStore("store1").add({ abort: 1 }).onsuccess =
michael@0 260 genCheck(c1, { abort: 1 }, "first" + test);
michael@0 261 trans.objectStore("store2").put({ abort: 2 }).onsuccess =
michael@0 262 genCheck(c2, { abort: 2, id: c2 }, "second" + test);
michael@0 263 yield undefined; yield undefined;
michael@0 264
michael@0 265 trans.objectStore("store1").add({ abort: 3 }, 500).onsuccess =
michael@0 266 genCheck(500, { abort: 3 }, "third" + test);
michael@0 267 trans.objectStore("store2").put({ abort: 4, id: 600 }).onsuccess =
michael@0 268 genCheck(600, { abort: 4, id: 600 }, "fourth" + test);
michael@0 269 yield undefined; yield undefined;
michael@0 270
michael@0 271 trans.objectStore("store1").add({ abort: 5 }).onsuccess =
michael@0 272 genCheck(501, { abort: 5 }, "fifth" + test);
michael@0 273 trans.objectStore("store2").put({ abort: 6 }).onsuccess =
michael@0 274 genCheck(601, { abort: 6, id: 601 }, "sixth" + test);
michael@0 275 yield undefined; yield undefined;
michael@0 276
michael@0 277 trans.abort();
michael@0 278 trans.onabort = grabEventAndContinueHandler;
michael@0 279 event = yield
michael@0 280 is(event.type, "abort", "transaction aborted");
michael@0 281 is(event.target, trans, "correct transaction aborted");
michael@0 282
michael@0 283 trans = db.transaction(["store1", "store2"], RW);
michael@0 284 trans.objectStore("store1").add({ abort: 1 }).onsuccess =
michael@0 285 genCheck(c1++, { abort: 1 }, "re-first" + test);
michael@0 286 trans.objectStore("store2").put({ abort: 2 }).onsuccess =
michael@0 287 genCheck(c2, { abort: 2, id: c2 }, "re-second" + test);
michael@0 288 c2++;
michael@0 289 yield undefined; yield undefined;
michael@0 290
michael@0 291 // Test that delete doesn't decrease generator
michael@0 292 test = " for test delete items"
michael@0 293 trans = db.transaction(["store1", "store2"], RW);
michael@0 294 trans.objectStore("store1").add({ delete: 1 }).onsuccess =
michael@0 295 genCheck(c1++, { delete: 1 }, "first" + test);
michael@0 296 trans.objectStore("store2").put({ delete: 2 }).onsuccess =
michael@0 297 genCheck(c2, { delete: 2, id: c2 }, "second" + test);
michael@0 298 c2++;
michael@0 299 yield undefined; yield undefined;
michael@0 300
michael@0 301 trans.objectStore("store1").delete(c1 - 1).onsuccess =
michael@0 302 grabEventAndContinueHandler;
michael@0 303 trans.objectStore("store2").delete(c2 - 1).onsuccess =
michael@0 304 grabEventAndContinueHandler;
michael@0 305 yield undefined; yield undefined;
michael@0 306
michael@0 307 trans.objectStore("store1").add({ delete: 3 }).onsuccess =
michael@0 308 genCheck(c1++, { delete: 3 }, "first" + test);
michael@0 309 trans.objectStore("store2").put({ delete: 4 }).onsuccess =
michael@0 310 genCheck(c2, { delete: 4, id: c2 }, "second" + test);
michael@0 311 c2++;
michael@0 312 yield undefined; yield undefined;
michael@0 313
michael@0 314 trans.objectStore("store1").delete(c1 - 1).onsuccess =
michael@0 315 grabEventAndContinueHandler;
michael@0 316 trans.objectStore("store2").delete(c2 - 1).onsuccess =
michael@0 317 grabEventAndContinueHandler;
michael@0 318 yield undefined; yield undefined;
michael@0 319
michael@0 320 trans = db.transaction(["store1", "store2"], RW);
michael@0 321 trans.objectStore("store1").add({ delete: 5 }).onsuccess =
michael@0 322 genCheck(c1++, { delete: 5 }, "first" + test);
michael@0 323 trans.objectStore("store2").put({ delete: 6 }).onsuccess =
michael@0 324 genCheck(c2, { delete: 6, id: c2 }, "second" + test);
michael@0 325 c2++;
michael@0 326 yield undefined; yield undefined;
michael@0 327
michael@0 328 // Test that clears doesn't decrease generator
michael@0 329 test = " for test clear stores";
michael@0 330 trans = db.transaction(["store1", "store2"], RW);
michael@0 331 trans.objectStore("store1").add({ clear: 1 }).onsuccess =
michael@0 332 genCheck(c1++, { clear: 1 }, "first" + test);
michael@0 333 trans.objectStore("store2").put({ clear: 2 }).onsuccess =
michael@0 334 genCheck(c2, { clear: 2, id: c2 }, "second" + test);
michael@0 335 c2++;
michael@0 336 yield undefined; yield undefined;
michael@0 337
michael@0 338 trans.objectStore("store1").clear().onsuccess =
michael@0 339 grabEventAndContinueHandler;
michael@0 340 trans.objectStore("store2").clear().onsuccess =
michael@0 341 grabEventAndContinueHandler;
michael@0 342 yield undefined; yield undefined;
michael@0 343
michael@0 344 trans.objectStore("store1").add({ clear: 3 }).onsuccess =
michael@0 345 genCheck(c1++, { clear: 3 }, "third" + test);
michael@0 346 trans.objectStore("store2").put({ clear: 4 }).onsuccess =
michael@0 347 genCheck(c2, { clear: 4, id: c2 }, "forth" + test);
michael@0 348 c2++;
michael@0 349 yield undefined; yield undefined;
michael@0 350
michael@0 351 trans.objectStore("store1").clear().onsuccess =
michael@0 352 grabEventAndContinueHandler;
michael@0 353 trans.objectStore("store2").clear().onsuccess =
michael@0 354 grabEventAndContinueHandler;
michael@0 355 yield undefined; yield undefined;
michael@0 356
michael@0 357 trans = db.transaction(["store1", "store2"], RW);
michael@0 358 trans.objectStore("store1").add({ clear: 5 }).onsuccess =
michael@0 359 genCheck(c1++, { clear: 5 }, "fifth" + test);
michael@0 360 trans.objectStore("store2").put({ clear: 6 }).onsuccess =
michael@0 361 genCheck(c2, { clear: 6, id: c2 }, "sixth" + test);
michael@0 362 c2++;
michael@0 363 yield undefined; yield undefined;
michael@0 364
michael@0 365
michael@0 366 // Test that close/reopen doesn't decrease generator
michael@0 367 test = " for test clear stores";
michael@0 368 trans = db.transaction(["store1", "store2"], RW);
michael@0 369 trans.objectStore("store1").clear().onsuccess =
michael@0 370 grabEventAndContinueHandler;
michael@0 371 trans.objectStore("store2").clear().onsuccess =
michael@0 372 grabEventAndContinueHandler;
michael@0 373 yield undefined; yield undefined;
michael@0 374 db.close();
michael@0 375
michael@0 376 SpecialPowers.gc();
michael@0 377
michael@0 378 openRequest = indexedDB.open(dbname, 2);
michael@0 379 openRequest.onerror = errorHandler;
michael@0 380 openRequest.onupgradeneeded = grabEventAndContinueHandler;
michael@0 381 openRequest.onsuccess = unexpectedSuccessHandler;
michael@0 382 event = yield undefined;
michael@0 383 db = event.target.result;
michael@0 384 trans = event.target.transaction;
michael@0 385
michael@0 386 trans.objectStore("store1").add({ reopen: 1 }).onsuccess =
michael@0 387 genCheck(c1++, { reopen: 1 }, "first" + test);
michael@0 388 trans.objectStore("store2").put({ reopen: 2 }).onsuccess =
michael@0 389 genCheck(c2, { reopen: 2, id: c2 }, "second" + test);
michael@0 390 c2++;
michael@0 391 yield undefined; yield undefined;
michael@0 392
michael@0 393 openRequest.onsuccess = grabEventAndContinueHandler;
michael@0 394 yield undefined;
michael@0 395
michael@0 396 finishTest();
michael@0 397 yield undefined;
michael@0 398 }

mercurial