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: var testGenerator = testSteps(); michael@0: michael@0: function testSteps() michael@0: { michael@0: // Test object stores michael@0: michael@0: let name = this.window ? window.location.pathname : "Splendid Test"; michael@0: let openRequest = indexedDB.open(name, 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: db.onerror = errorHandler; michael@0: let tests = michael@0: [{ add: { x: 1, id: 1 }, michael@0: indexes:[{ v: 1, k: 1 }] }, michael@0: { add: { x: [2, 3], id: 2 }, michael@0: indexes:[{ v: 1, k: 1 }, michael@0: { v: 2, k: 2 }, michael@0: { v: 3, k: 2 }] }, michael@0: { put: { x: [2, 4], id: 1 }, michael@0: indexes:[{ v: 2, k: 1 }, michael@0: { v: 2, k: 2 }, michael@0: { v: 3, k: 2 }, michael@0: { v: 4, k: 1 }] }, michael@0: { add: { x: [5, 6, 5, -2, 3], id: 3 }, michael@0: indexes:[{ v:-2, k: 3 }, michael@0: { v: 2, k: 1 }, michael@0: { v: 2, k: 2 }, michael@0: { v: 3, k: 2 }, michael@0: { v: 3, k: 3 }, michael@0: { v: 4, k: 1 }, michael@0: { v: 5, k: 3 }, michael@0: { v: 6, k: 3 }] }, michael@0: { delete: IDBKeyRange.bound(1, 3), michael@0: indexes:[] }, michael@0: { put: { x: ["food", {}, false, undefined, /x/, [73, false]], id: 2 }, michael@0: indexes:[{ v: "food", k: 2 }] }, michael@0: { add: { x: [{}, /x/, -12, "food", null, [false], undefined], id: 3 }, michael@0: indexes:[{ v: -12, k: 3 }, michael@0: { v: "food", k: 2 }, michael@0: { v: "food", k: 3 }] }, michael@0: { put: { x: [], id: 2 }, michael@0: indexes:[{ v: -12, k: 3 }, michael@0: { v: "food", k: 3 }] }, michael@0: { put: { x: { y: 3 }, id: 3 }, michael@0: indexes:[] }, michael@0: { add: { x: false, id: 7 }, michael@0: indexes:[] }, michael@0: { delete: IDBKeyRange.lowerBound(0), michael@0: indexes:[] }, michael@0: ]; michael@0: michael@0: let store = db.createObjectStore("mystore", { keyPath: "id" }); michael@0: let index = store.createIndex("myindex", "x", { multiEntry: true }); michael@0: is(index.multiEntry, true, "index created with multiEntry"); michael@0: michael@0: let i; michael@0: for (i = 0; i < tests.length; ++i) { michael@0: let test = tests[i]; michael@0: let testName = " for " + JSON.stringify(test); michael@0: let req; michael@0: if (test.add) { michael@0: req = store.add(test.add); michael@0: } michael@0: else if (test.put) { michael@0: req = store.put(test.put); michael@0: } michael@0: else if (test.delete) { michael@0: req = store.delete(test.delete); michael@0: } michael@0: else { michael@0: ok(false, "borked test"); michael@0: } michael@0: req.onsuccess = grabEventAndContinueHandler; michael@0: let e = yield undefined; michael@0: michael@0: req = index.openKeyCursor(); michael@0: req.onsuccess = grabEventAndContinueHandler; michael@0: for (let j = 0; j < test.indexes.length; ++j) { michael@0: e = yield undefined; michael@0: is(req.result.key, test.indexes[j].v, "found expected index key at index " + j + testName); michael@0: is(req.result.primaryKey, test.indexes[j].k, "found expected index primary key at index " + j + testName); michael@0: req.result.continue(); michael@0: } michael@0: e = yield undefined; michael@0: is(req.result, undefined, "exhausted indexes"); michael@0: michael@0: let tempIndex = store.createIndex("temp index", "x", { multiEntry: true }); michael@0: req = tempIndex.openKeyCursor(); michael@0: req.onsuccess = grabEventAndContinueHandler; michael@0: for (let j = 0; j < test.indexes.length; ++j) { michael@0: e = yield undefined; michael@0: is(req.result.key, test.indexes[j].v, "found expected temp index key at index " + j + testName); michael@0: is(req.result.primaryKey, test.indexes[j].k, "found expected temp index primary key at index " + j + testName); michael@0: req.result.continue(); michael@0: } michael@0: e = yield undefined; michael@0: is(req.result, undefined, "exhausted temp index"); michael@0: store.deleteIndex("temp index"); michael@0: } michael@0: michael@0: // Unique indexes michael@0: tests = michael@0: [{ add: { x: 1, id: 1 }, michael@0: indexes:[{ v: 1, k: 1 }] }, michael@0: { add: { x: [2, 3], id: 2 }, michael@0: indexes:[{ v: 1, k: 1 }, michael@0: { v: 2, k: 2 }, michael@0: { v: 3, k: 2 }] }, michael@0: { put: { x: [2, 4], id: 3 }, michael@0: fail: true }, michael@0: { put: { x: [1, 4], id: 1 }, michael@0: indexes:[{ v: 1, k: 1 }, michael@0: { v: 2, k: 2 }, michael@0: { v: 3, k: 2 }, michael@0: { v: 4, k: 1 }] }, michael@0: { add: { x: [5, 0, 5, 5, 5], id: 3 }, michael@0: indexes:[{ v: 0, k: 3 }, michael@0: { v: 1, k: 1 }, michael@0: { v: 2, k: 2 }, michael@0: { v: 3, k: 2 }, michael@0: { v: 4, k: 1 }, michael@0: { v: 5, k: 3 }] }, michael@0: { delete: IDBKeyRange.bound(1, 2), michael@0: indexes:[{ v: 0, k: 3 }, michael@0: { v: 5, k: 3 }] }, michael@0: { add: { x: [0, 6], id: 8 }, michael@0: fail: true }, michael@0: { add: { x: 5, id: 8 }, michael@0: fail: true }, michael@0: { put: { x: 0, id: 8 }, michael@0: fail: true }, michael@0: ]; michael@0: michael@0: store.deleteIndex("myindex"); michael@0: index = store.createIndex("myindex", "x", { multiEntry: true, unique: true }); michael@0: is(index.multiEntry, true, "index created with multiEntry"); michael@0: michael@0: let i; michael@0: let indexes; michael@0: for (i = 0; i < tests.length; ++i) { michael@0: let test = tests[i]; michael@0: let testName = " for " + JSON.stringify(test); michael@0: let req; michael@0: if (test.add) { michael@0: req = store.add(test.add); michael@0: } michael@0: else if (test.put) { michael@0: req = store.put(test.put); michael@0: } michael@0: else if (test.delete) { michael@0: req = store.delete(test.delete); michael@0: } michael@0: else { michael@0: ok(false, "borked test"); michael@0: } michael@0: michael@0: if (!test.fail) { michael@0: req.onsuccess = grabEventAndContinueHandler; michael@0: let e = yield undefined; michael@0: indexes = test.indexes; michael@0: } michael@0: else { michael@0: req.onsuccess = unexpectedSuccessHandler; michael@0: req.onerror = grabEventAndContinueHandler; michael@0: ok(true, "waiting for error"); michael@0: let e = yield undefined; michael@0: ok(true, "got error: " + e.type); michael@0: e.preventDefault(); michael@0: e.stopPropagation(); michael@0: } michael@0: michael@0: let e; michael@0: req = index.openKeyCursor(); michael@0: req.onsuccess = grabEventAndContinueHandler; michael@0: for (let j = 0; j < indexes.length; ++j) { michael@0: e = yield undefined; michael@0: is(req.result.key, indexes[j].v, "found expected index key at index " + j + testName); michael@0: is(req.result.primaryKey, indexes[j].k, "found expected index primary key at index " + j + testName); michael@0: req.result.continue(); michael@0: } michael@0: e = yield undefined; michael@0: is(req.result, undefined, "exhausted indexes"); michael@0: michael@0: let tempIndex = store.createIndex("temp index", "x", { multiEntry: true, unique: true }); michael@0: req = tempIndex.openKeyCursor(); michael@0: req.onsuccess = grabEventAndContinueHandler; michael@0: for (let j = 0; j < indexes.length; ++j) { michael@0: e = yield undefined; michael@0: is(req.result.key, indexes[j].v, "found expected temp index key at index " + j + testName); michael@0: is(req.result.primaryKey, indexes[j].k, "found expected temp index primary key at index " + j + testName); michael@0: req.result.continue(); michael@0: } michael@0: e = yield undefined; michael@0: is(req.result, undefined, "exhausted temp index"); michael@0: store.deleteIndex("temp index"); michael@0: } michael@0: michael@0: michael@0: openRequest.onsuccess = grabEventAndContinueHandler; michael@0: yield undefined; michael@0: michael@0: let trans = db.transaction(["mystore"], "readwrite"); michael@0: store = trans.objectStore("mystore"); michael@0: index = store.index("myindex"); michael@0: is(index.multiEntry, true, "index still is multiEntry"); michael@0: trans.oncomplete = grabEventAndContinueHandler; michael@0: yield undefined; michael@0: michael@0: finishTest(); michael@0: yield undefined; michael@0: }