michael@0: /** michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: if (!this.window) { michael@0: this.runTest = function() { michael@0: todo(false, "Test disabled in xpcshell test suite for now"); michael@0: finishTest(); michael@0: } michael@0: } michael@0: michael@0: var testGenerator = testSteps(); michael@0: michael@0: function genCheck(key, value, test, options) { michael@0: return function(event) { michael@0: is(JSON.stringify(event.target.result), JSON.stringify(key), michael@0: "correct returned key in " + test); michael@0: if (options && options.store) { michael@0: is(event.target.source, options.store, "correct store in " + test); michael@0: } michael@0: if (options && options.trans) { michael@0: is(event.target.transaction, options.trans, "correct transaction in " + test); michael@0: } michael@0: michael@0: event.target.source.get(key).onsuccess = function(event) { michael@0: is(JSON.stringify(event.target.result), JSON.stringify(value), michael@0: "correct stored value in " + test); michael@0: continueToNextStepSync(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function testSteps() michael@0: { michael@0: const dbname = this.window ? window.location.pathname : "Splendid Test"; michael@0: const RW = "readwrite"; michael@0: let c1 = 1; michael@0: let c2 = 1; michael@0: michael@0: let openRequest = indexedDB.open(dbname, 1); michael@0: openRequest.onerror = errorHandler; michael@0: openRequest.onupgradeneeded = grabEventAndContinueHandler; michael@0: openRequest.onsuccess = unexpectedSuccessHandler; michael@0: let event = yield undefined; michael@0: let db = event.target.result; michael@0: let trans = event.target.transaction; michael@0: michael@0: // Create test stores michael@0: let store1 = db.createObjectStore("store1", { autoIncrement: true }); michael@0: let store2 = db.createObjectStore("store2", { autoIncrement: true, keyPath: "id" }); michael@0: let store3 = db.createObjectStore("store3", { autoIncrement: false }); michael@0: is(store1.autoIncrement, true, "store1 .autoIncrement"); michael@0: is(store2.autoIncrement, true, "store2 .autoIncrement"); michael@0: is(store3.autoIncrement, false, "store3 .autoIncrement"); michael@0: michael@0: store1.createIndex("unique1", "unique", { unique: true }); michael@0: store2.createIndex("unique1", "unique", { unique: true }); michael@0: michael@0: // Test simple inserts michael@0: let test = " for test simple insert" michael@0: store1.add({ foo: "value1" }).onsuccess = michael@0: genCheck(c1++, { foo: "value1" }, "first" + test); michael@0: store1.add({ foo: "value2" }).onsuccess = michael@0: genCheck(c1++, { foo: "value2" }, "second" + test); michael@0: michael@0: yield undefined; michael@0: yield undefined; michael@0: michael@0: store2.put({ bar: "value1" }).onsuccess = michael@0: genCheck(c2, { bar: "value1", id: c2 }, "first in store2" + test, michael@0: { store: store2 }); michael@0: c2++; michael@0: store1.put({ foo: "value3" }).onsuccess = michael@0: genCheck(c1++, { foo: "value3" }, "third" + test, michael@0: { store: store1 }); michael@0: michael@0: yield undefined; michael@0: yield undefined; michael@0: michael@0: store2.get(IDBKeyRange.lowerBound(c2)).onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: is(event.target.result, undefined, "no such value" + test); michael@0: michael@0: // Close version_change transaction michael@0: openRequest.onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: is(event.target, openRequest, "succeeded to open" + test); michael@0: is(event.type, "success", "succeeded to open" + test); michael@0: michael@0: // Test inserting explicit keys michael@0: test = " for test explicit keys"; michael@0: trans = db.transaction("store1", RW); michael@0: trans.objectStore("store1").add({ explicit: 1 }, 100).onsuccess = michael@0: genCheck(100, { explicit: 1 }, "first" + test); michael@0: c1 = 101; michael@0: trans = db.transaction("store1", RW); michael@0: trans.objectStore("store1").add({ explicit: 2 }).onsuccess = michael@0: genCheck(c1++, { explicit: 2 }, "second" + test); michael@0: yield undefined; yield undefined; michael@0: michael@0: trans = db.transaction("store1", RW); michael@0: trans.objectStore("store1").add({ explicit: 3 }, 200).onsuccess = michael@0: genCheck(200, { explicit: 3 }, "third" + test); michael@0: c1 = 201; michael@0: trans.objectStore("store1").add({ explicit: 4 }).onsuccess = michael@0: genCheck(c1++, { explicit: 4 }, "fourth" + test); michael@0: yield undefined; yield undefined; michael@0: michael@0: trans = db.transaction("store1", RW); michael@0: trans.objectStore("store1").add({ explicit: 5 }, 150).onsuccess = michael@0: genCheck(150, { explicit: 5 }, "fifth" + test); michael@0: yield undefined; michael@0: trans.objectStore("store1").add({ explicit: 6 }).onsuccess = michael@0: genCheck(c1++, { explicit: 6 }, "sixth" + test); michael@0: yield undefined; michael@0: michael@0: trans = db.transaction("store1", RW); michael@0: trans.objectStore("store1").add({ explicit: 7 }, "key").onsuccess = michael@0: genCheck("key", { explicit: 7 }, "seventh" + test); michael@0: yield undefined; michael@0: trans.objectStore("store1").add({ explicit: 8 }).onsuccess = michael@0: genCheck(c1++, { explicit: 8 }, "eighth" + test); michael@0: yield undefined; michael@0: michael@0: trans = db.transaction("store1", RW); michael@0: trans.objectStore("store1").add({ explicit: 7 }, [100000]).onsuccess = michael@0: genCheck([100000], { explicit: 7 }, "seventh" + test); michael@0: yield undefined; michael@0: trans.objectStore("store1").add({ explicit: 8 }).onsuccess = michael@0: genCheck(c1++, { explicit: 8 }, "eighth" + test); michael@0: yield undefined; michael@0: michael@0: trans = db.transaction("store1", RW); michael@0: trans.objectStore("store1").add({ explicit: 9 }, -100000).onsuccess = michael@0: genCheck(-100000, { explicit: 9 }, "ninth" + test); michael@0: yield undefined; michael@0: trans.objectStore("store1").add({ explicit: 10 }).onsuccess = michael@0: genCheck(c1++, { explicit: 10 }, "tenth" + test); michael@0: yield undefined; michael@0: michael@0: michael@0: trans = db.transaction("store2", RW); michael@0: trans.objectStore("store2").add({ explicit2: 1, id: 300 }).onsuccess = michael@0: genCheck(300, { explicit2: 1, id: 300 }, "first store2" + test); michael@0: c2 = 301; michael@0: trans = db.transaction("store2", RW); michael@0: trans.objectStore("store2").add({ explicit2: 2 }).onsuccess = michael@0: genCheck(c2, { explicit2: 2, id: c2 }, "second store2" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans = db.transaction("store2", RW); michael@0: trans.objectStore("store2").add({ explicit2: 3, id: 400 }).onsuccess = michael@0: genCheck(400, { explicit2: 3, id: 400 }, "third store2" + test); michael@0: c2 = 401; michael@0: trans.objectStore("store2").add({ explicit2: 4 }).onsuccess = michael@0: genCheck(c2, { explicit2: 4, id: c2 }, "fourth store2" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans = db.transaction("store2", RW); michael@0: trans.objectStore("store2").add({ explicit: 5, id: 150 }).onsuccess = michael@0: genCheck(150, { explicit: 5, id: 150 }, "fifth store2" + test); michael@0: yield undefined; michael@0: trans.objectStore("store2").add({ explicit: 6 }).onsuccess = michael@0: genCheck(c2, { explicit: 6, id: c2 }, "sixth store2" + test); michael@0: c2++; michael@0: yield undefined; michael@0: michael@0: trans = db.transaction("store2", RW); michael@0: trans.objectStore("store2").add({ explicit: 7, id: "key" }).onsuccess = michael@0: genCheck("key", { explicit: 7, id: "key" }, "seventh store2" + test); michael@0: yield undefined; michael@0: trans.objectStore("store2").add({ explicit: 8 }).onsuccess = michael@0: genCheck(c2, { explicit: 8, id: c2 }, "eighth store2" + test); michael@0: c2++; michael@0: yield undefined; michael@0: michael@0: trans = db.transaction("store2", RW); michael@0: trans.objectStore("store2").add({ explicit: 7, id: [100000] }).onsuccess = michael@0: genCheck([100000], { explicit: 7, id: [100000] }, "seventh store2" + test); michael@0: yield undefined; michael@0: trans.objectStore("store2").add({ explicit: 8 }).onsuccess = michael@0: genCheck(c2, { explicit: 8, id: c2 }, "eighth store2" + test); michael@0: c2++; michael@0: yield undefined; michael@0: michael@0: trans = db.transaction("store2", RW); michael@0: trans.objectStore("store2").add({ explicit: 9, id: -100000 }).onsuccess = michael@0: genCheck(-100000, { explicit: 9, id: -100000 }, "ninth store2" + test); michael@0: yield undefined; michael@0: trans.objectStore("store2").add({ explicit: 10 }).onsuccess = michael@0: genCheck(c2, { explicit: 10, id: c2 }, "tenth store2" + test); michael@0: c2++; michael@0: yield undefined; michael@0: michael@0: michael@0: // Test separate transactions doesn't generate overlapping numbers michael@0: test = " for test non-overlapping counts"; michael@0: trans = db.transaction("store1", RW); michael@0: trans2 = db.transaction("store1", RW); michael@0: trans2.objectStore("store1").put({ over: 2 }).onsuccess = michael@0: genCheck(c1 + 1, { over: 2 }, "first" + test, michael@0: { trans: trans2 }); michael@0: trans.objectStore("store1").put({ over: 1 }).onsuccess = michael@0: genCheck(c1, { over: 1 }, "second" + test, michael@0: { trans: trans }); michael@0: c1 += 2; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans = db.transaction("store2", RW); michael@0: trans2 = db.transaction("store2", RW); michael@0: trans2.objectStore("store2").put({ over: 2 }).onsuccess = michael@0: genCheck(c2 + 1, { over: 2, id: c2 + 1 }, "third" + test, michael@0: { trans: trans2 }); michael@0: trans.objectStore("store2").put({ over: 1 }).onsuccess = michael@0: genCheck(c2, { over: 1, id: c2 }, "fourth" + test, michael@0: { trans: trans }); michael@0: c2 += 2; michael@0: yield undefined; yield undefined; michael@0: michael@0: // Test that error inserts doesn't increase generator michael@0: test = " for test error inserts"; michael@0: trans = db.transaction(["store1", "store2"], RW); michael@0: trans.objectStore("store1").add({ unique: 1 }, -1); michael@0: trans.objectStore("store2").add({ unique: 1, id: "unique" }); michael@0: michael@0: trans.objectStore("store1").add({ error: 1, unique: 1 }). michael@0: addEventListener("error", new ExpectError("ConstraintError", true)); michael@0: trans.objectStore("store1").add({ error: 2 }).onsuccess = michael@0: genCheck(c1++, { error: 2 }, "first" + test); michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store2").add({ error: 3, unique: 1 }). michael@0: addEventListener("error", new ExpectError("ConstraintError", true)); michael@0: trans.objectStore("store2").add({ error: 4 }).onsuccess = michael@0: genCheck(c2, { error: 4, id: c2 }, "second" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store1").add({ error: 5, unique: 1 }, 100000). michael@0: addEventListener("error", new ExpectError("ConstraintError", true)); michael@0: trans.objectStore("store1").add({ error: 6 }).onsuccess = michael@0: genCheck(c1++, { error: 6 }, "third" + test); michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store2").add({ error: 7, unique: 1, id: 100000 }). michael@0: addEventListener("error", new ExpectError("ConstraintError", true)); michael@0: trans.objectStore("store2").add({ error: 8 }).onsuccess = michael@0: genCheck(c2, { error: 8, id: c2 }, "fourth" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: // Test that aborts doesn't increase generator michael@0: test = " for test aborted transaction"; michael@0: trans = db.transaction(["store1", "store2"], RW); michael@0: trans.objectStore("store1").add({ abort: 1 }).onsuccess = michael@0: genCheck(c1, { abort: 1 }, "first" + test); michael@0: trans.objectStore("store2").put({ abort: 2 }).onsuccess = michael@0: genCheck(c2, { abort: 2, id: c2 }, "second" + test); michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store1").add({ abort: 3 }, 500).onsuccess = michael@0: genCheck(500, { abort: 3 }, "third" + test); michael@0: trans.objectStore("store2").put({ abort: 4, id: 600 }).onsuccess = michael@0: genCheck(600, { abort: 4, id: 600 }, "fourth" + test); michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store1").add({ abort: 5 }).onsuccess = michael@0: genCheck(501, { abort: 5 }, "fifth" + test); michael@0: trans.objectStore("store2").put({ abort: 6 }).onsuccess = michael@0: genCheck(601, { abort: 6, id: 601 }, "sixth" + test); michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.abort(); michael@0: trans.onabort = grabEventAndContinueHandler; michael@0: event = yield michael@0: is(event.type, "abort", "transaction aborted"); michael@0: is(event.target, trans, "correct transaction aborted"); michael@0: michael@0: trans = db.transaction(["store1", "store2"], RW); michael@0: trans.objectStore("store1").add({ abort: 1 }).onsuccess = michael@0: genCheck(c1++, { abort: 1 }, "re-first" + test); michael@0: trans.objectStore("store2").put({ abort: 2 }).onsuccess = michael@0: genCheck(c2, { abort: 2, id: c2 }, "re-second" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: // Test that delete doesn't decrease generator michael@0: test = " for test delete items" michael@0: trans = db.transaction(["store1", "store2"], RW); michael@0: trans.objectStore("store1").add({ delete: 1 }).onsuccess = michael@0: genCheck(c1++, { delete: 1 }, "first" + test); michael@0: trans.objectStore("store2").put({ delete: 2 }).onsuccess = michael@0: genCheck(c2, { delete: 2, id: c2 }, "second" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store1").delete(c1 - 1).onsuccess = michael@0: grabEventAndContinueHandler; michael@0: trans.objectStore("store2").delete(c2 - 1).onsuccess = michael@0: grabEventAndContinueHandler; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store1").add({ delete: 3 }).onsuccess = michael@0: genCheck(c1++, { delete: 3 }, "first" + test); michael@0: trans.objectStore("store2").put({ delete: 4 }).onsuccess = michael@0: genCheck(c2, { delete: 4, id: c2 }, "second" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store1").delete(c1 - 1).onsuccess = michael@0: grabEventAndContinueHandler; michael@0: trans.objectStore("store2").delete(c2 - 1).onsuccess = michael@0: grabEventAndContinueHandler; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans = db.transaction(["store1", "store2"], RW); michael@0: trans.objectStore("store1").add({ delete: 5 }).onsuccess = michael@0: genCheck(c1++, { delete: 5 }, "first" + test); michael@0: trans.objectStore("store2").put({ delete: 6 }).onsuccess = michael@0: genCheck(c2, { delete: 6, id: c2 }, "second" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: // Test that clears doesn't decrease generator michael@0: test = " for test clear stores"; michael@0: trans = db.transaction(["store1", "store2"], RW); michael@0: trans.objectStore("store1").add({ clear: 1 }).onsuccess = michael@0: genCheck(c1++, { clear: 1 }, "first" + test); michael@0: trans.objectStore("store2").put({ clear: 2 }).onsuccess = michael@0: genCheck(c2, { clear: 2, id: c2 }, "second" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store1").clear().onsuccess = michael@0: grabEventAndContinueHandler; michael@0: trans.objectStore("store2").clear().onsuccess = michael@0: grabEventAndContinueHandler; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store1").add({ clear: 3 }).onsuccess = michael@0: genCheck(c1++, { clear: 3 }, "third" + test); michael@0: trans.objectStore("store2").put({ clear: 4 }).onsuccess = michael@0: genCheck(c2, { clear: 4, id: c2 }, "forth" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans.objectStore("store1").clear().onsuccess = michael@0: grabEventAndContinueHandler; michael@0: trans.objectStore("store2").clear().onsuccess = michael@0: grabEventAndContinueHandler; michael@0: yield undefined; yield undefined; michael@0: michael@0: trans = db.transaction(["store1", "store2"], RW); michael@0: trans.objectStore("store1").add({ clear: 5 }).onsuccess = michael@0: genCheck(c1++, { clear: 5 }, "fifth" + test); michael@0: trans.objectStore("store2").put({ clear: 6 }).onsuccess = michael@0: genCheck(c2, { clear: 6, id: c2 }, "sixth" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: michael@0: // Test that close/reopen doesn't decrease generator michael@0: test = " for test clear stores"; michael@0: trans = db.transaction(["store1", "store2"], RW); michael@0: trans.objectStore("store1").clear().onsuccess = michael@0: grabEventAndContinueHandler; michael@0: trans.objectStore("store2").clear().onsuccess = michael@0: grabEventAndContinueHandler; michael@0: yield undefined; yield undefined; michael@0: db.close(); michael@0: michael@0: SpecialPowers.gc(); michael@0: michael@0: openRequest = indexedDB.open(dbname, 2); michael@0: openRequest.onerror = errorHandler; michael@0: openRequest.onupgradeneeded = grabEventAndContinueHandler; michael@0: openRequest.onsuccess = unexpectedSuccessHandler; michael@0: event = yield undefined; michael@0: db = event.target.result; michael@0: trans = event.target.transaction; michael@0: michael@0: trans.objectStore("store1").add({ reopen: 1 }).onsuccess = michael@0: genCheck(c1++, { reopen: 1 }, "first" + test); michael@0: trans.objectStore("store2").put({ reopen: 2 }).onsuccess = michael@0: genCheck(c2, { reopen: 2, id: c2 }, "second" + test); michael@0: c2++; michael@0: yield undefined; yield undefined; michael@0: michael@0: openRequest.onsuccess = grabEventAndContinueHandler; michael@0: yield undefined; michael@0: michael@0: finishTest(); michael@0: yield undefined; michael@0: }