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 name = this.window ? window.location.pathname : "Splendid Test"; michael@0: michael@0: const objectStoreName = "People"; michael@0: michael@0: const objectStoreData = [ michael@0: { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, michael@0: { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, michael@0: { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }, michael@0: { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, michael@0: { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, michael@0: { key: "237-23-7737", value: { name: "Pat", height: 65 } }, michael@0: { key: "237-23-7738", value: { name: "Mel", height: 66, weight: {} } } michael@0: ]; michael@0: michael@0: const badObjectStoreData = [ michael@0: { key: "237-23-7739", value: { name: "Rob", height: 65 } }, michael@0: { key: "237-23-7740", value: { name: "Jen", height: 66, weight: {} } } michael@0: ]; michael@0: michael@0: const indexData = [ michael@0: { name: "weight", keyPath: "weight", options: { unique: false } } michael@0: ]; michael@0: michael@0: const objectStoreDataWeightSort = [ michael@0: { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, michael@0: { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, michael@0: { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, michael@0: { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, michael@0: { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } } michael@0: ]; michael@0: michael@0: let request = indexedDB.open(name, 1); michael@0: request.onerror = errorHandler; michael@0: request.onupgradeneeded = grabEventAndContinueHandler; michael@0: request.onsuccess = grabEventAndContinueHandler; michael@0: let event = yield undefined; michael@0: michael@0: let db = event.target.result; michael@0: michael@0: let objectStore = db.createObjectStore(objectStoreName, { } ); michael@0: michael@0: let addedData = 0; michael@0: for (let i in objectStoreData) { michael@0: request = objectStore.add(objectStoreData[i].value, michael@0: objectStoreData[i].key); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function(event) { michael@0: if (++addedData == objectStoreData.length) { michael@0: testGenerator.send(event); michael@0: } michael@0: } michael@0: } michael@0: event = yield undefined; michael@0: michael@0: for (let i in indexData) { michael@0: objectStore.createIndex(indexData[i].name, indexData[i].keyPath, michael@0: indexData[i].options); michael@0: } michael@0: michael@0: addedData = 0; michael@0: for (let i in badObjectStoreData) { michael@0: request = objectStore.add(badObjectStoreData[i].value, michael@0: badObjectStoreData[i].key); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function(event) { michael@0: if (++addedData == badObjectStoreData.length) { michael@0: executeSoon(function() { testGenerator.next() }); michael@0: } michael@0: } michael@0: } michael@0: yield undefined; michael@0: yield undefined; michael@0: michael@0: objectStore = db.transaction(objectStoreName) michael@0: .objectStore(objectStoreName); michael@0: michael@0: let keyIndex = 0; michael@0: michael@0: request = objectStore.index("weight").openKeyCursor(); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function (event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: is(cursor.key, objectStoreDataWeightSort[keyIndex].value.weight, michael@0: "Correct key"); michael@0: is(cursor.primaryKey, objectStoreDataWeightSort[keyIndex].key, michael@0: "Correct value"); michael@0: keyIndex++; michael@0: michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, objectStoreDataWeightSort.length, "Saw all weights"); michael@0: michael@0: keyIndex = 0; michael@0: michael@0: request = objectStore.openCursor(); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function (event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: keyIndex++; michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, objectStoreData.length + badObjectStoreData.length, michael@0: "Saw all people"); michael@0: michael@0: finishTest(); michael@0: yield undefined; michael@0: }