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: const objectStoreData = [ michael@0: { name: "", options: { keyPath: "id", autoIncrement: true } }, michael@0: { name: null, options: { keyPath: "ss" } }, michael@0: { name: undefined, options: { } }, michael@0: { name: "4", options: { autoIncrement: true } }, michael@0: ]; michael@0: michael@0: const indexData = [ michael@0: { name: "", keyPath: "name", options: { unique: true } }, michael@0: { name: null, keyPath: "height", options: { } } michael@0: ]; michael@0: michael@0: const data = [ michael@0: { ss: "237-23-7732", name: "Ann", height: 60 }, michael@0: { ss: "237-23-7733", name: "Bob", height: 65 } michael@0: ]; michael@0: michael@0: let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1); michael@0: request.onerror = errorHandler; michael@0: request.onupgradeneeded = grabEventAndContinueHandler; michael@0: let event = yield undefined; michael@0: michael@0: let db = event.target.result; michael@0: db.onerror = errorHandler; michael@0: michael@0: event.target.onsuccess = continueToNextStep; michael@0: michael@0: for (let objectStoreIndex in objectStoreData) { michael@0: const objectStoreInfo = objectStoreData[objectStoreIndex]; michael@0: let objectStore = db.createObjectStore(objectStoreInfo.name, michael@0: objectStoreInfo.options); michael@0: for (let indexIndex in indexData) { michael@0: const indexInfo = indexData[indexIndex]; michael@0: let index = objectStore.createIndex(indexInfo.name, michael@0: indexInfo.keyPath, michael@0: indexInfo.options); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: ok(true, "Initial setup"); michael@0: michael@0: for (let objectStoreIndex in objectStoreData) { michael@0: const info = objectStoreData[objectStoreIndex]; michael@0: michael@0: for (let indexIndex in indexData) { michael@0: const objectStoreName = objectStoreData[objectStoreIndex].name; michael@0: const indexName = indexData[indexIndex].name; michael@0: michael@0: let objectStore = michael@0: db.transaction(objectStoreName, "readwrite") michael@0: .objectStore(objectStoreName); michael@0: ok(true, "Got objectStore " + objectStoreName); michael@0: michael@0: for (let dataIndex in data) { michael@0: const obj = data[dataIndex]; michael@0: let key; michael@0: if (!info.options.keyPath && !info.options.autoIncrement) { michael@0: key = obj.ss; michael@0: } michael@0: objectStore.add(obj, key); michael@0: } michael@0: michael@0: let index = objectStore.index(indexName); michael@0: ok(true, "Got index " + indexName); michael@0: michael@0: let keyIndex = 0; michael@0: michael@0: index.openCursor().onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (!cursor) { michael@0: continueToNextStep(); michael@0: return; michael@0: } michael@0: michael@0: is(cursor.key, data[keyIndex][indexData[indexIndex].keyPath], michael@0: "Good key"); michael@0: is(cursor.value.ss, data[keyIndex].ss, "Correct ss"); michael@0: is(cursor.value.name, data[keyIndex].name, "Correct name"); michael@0: is(cursor.value.height, data[keyIndex].height, "Correct height"); michael@0: michael@0: if (!keyIndex) { michael@0: let obj = cursor.value; michael@0: obj.updated = true; michael@0: michael@0: cursor.update(obj).onsuccess = function(event) { michael@0: ok(true, "Object updated"); michael@0: cursor.continue(); michael@0: keyIndex++ michael@0: } michael@0: return; michael@0: } michael@0: michael@0: cursor.delete().onsuccess = function(event) { michael@0: ok(true, "Object deleted"); michael@0: cursor.continue(); michael@0: keyIndex++ michael@0: } michael@0: }; michael@0: yield undefined; michael@0: michael@0: is(keyIndex, 2, "Saw all the items"); michael@0: michael@0: keyIndex = 0; michael@0: michael@0: db.transaction(objectStoreName).objectStore(objectStoreName) michael@0: .openCursor() michael@0: .onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (!cursor) { michael@0: continueToNextStep(); michael@0: return; michael@0: } michael@0: michael@0: is(cursor.value.ss, data[keyIndex].ss, "Correct ss"); michael@0: is(cursor.value.name, data[keyIndex].name, "Correct name"); michael@0: is(cursor.value.height, data[keyIndex].height, "Correct height"); michael@0: is(cursor.value.updated, true, "Correct updated flag"); michael@0: michael@0: cursor.continue(); michael@0: keyIndex++; michael@0: }; michael@0: yield undefined; michael@0: michael@0: is(keyIndex, 1, "Saw all the items"); michael@0: michael@0: db.transaction(objectStoreName, "readwrite") michael@0: .objectStore(objectStoreName).clear() michael@0: .onsuccess = continueToNextStep; michael@0: yield undefined; michael@0: michael@0: objectStore = index = null; // Bug 943409 workaround. michael@0: } michael@0: } michael@0: michael@0: finishTest(); michael@0: yield undefined; michael@0: }