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: // Blob constructor is not implemented outside of windows yet (Bug 827723). michael@0: if (!this.window) { michael@0: finishTest(); michael@0: yield undefined; michael@0: } michael@0: michael@0: const name = this.window ? window.location.pathname : "Splendid Test"; michael@0: michael@0: const objectStoreName = "Things"; michael@0: michael@0: const blob1 = new Blob(["foo", "bar"], { type: "text/plain" }); michael@0: const blob2 = new Blob(["foobazybar"], { type: "text/plain" }); michael@0: const blob3 = new Blob(["2"], { type: "bogus/" }); michael@0: const str = "The Book of Mozilla"; michael@0: str.type = blob1; michael@0: const arr = [1, 2, 3, 4, 5]; michael@0: michael@0: const objectStoreData = [ michael@0: { key: "1", value: blob1}, michael@0: { key: "2", value: blob2}, michael@0: { key: "3", value: blob3}, michael@0: { key: "4", value: str}, michael@0: { key: "5", value: arr}, michael@0: ]; michael@0: michael@0: const indexData = [ michael@0: { name: "type", keyPath: "type", options: { } }, michael@0: { name: "length", keyPath: "length", options: { unique: true } } michael@0: ]; michael@0: michael@0: const objectStoreDataTypeSort = [ michael@0: { key: "3", value: blob3}, michael@0: { key: "1", value: blob1}, michael@0: { key: "2", value: blob2}, michael@0: ]; michael@0: michael@0: const objectStoreDataLengthSort = [ michael@0: { key: "5", value: arr}, michael@0: //{ key: "4", value: str}, 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: let db = event.target.result; michael@0: michael@0: let objectStore = db.createObjectStore(objectStoreName, { keyPath: null }); michael@0: michael@0: // First, add all our data to the object store. 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: // Now create the indexes. 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: is(objectStore.indexNames.length, indexData.length, "Good index count"); michael@0: yield undefined; michael@0: objectStore = db.transaction(objectStoreName) michael@0: .objectStore(objectStoreName); michael@0: michael@0: // Check global properties to make sure they are correct. michael@0: is(objectStore.indexNames.length, indexData.length, "Good index count"); michael@0: for (let i in indexData) { michael@0: let found = false; michael@0: for (let j = 0; j < objectStore.indexNames.length; j++) { michael@0: if (objectStore.indexNames.item(j) == indexData[i].name) { michael@0: found = true; michael@0: break; michael@0: } michael@0: } michael@0: is(found, true, "objectStore has our index"); michael@0: let index = objectStore.index(indexData[i].name); michael@0: is(index.name, indexData[i].name, "Correct name"); michael@0: is(index.storeName, objectStore.name, "Correct store name"); michael@0: is(index.keyPath, indexData[i].keyPath, "Correct keyPath"); michael@0: is(index.unique, indexData[i].options.unique ? true : false, michael@0: "Correct unique value"); michael@0: } michael@0: michael@0: ok(true, "Test group 1"); michael@0: michael@0: let keyIndex = 0; michael@0: michael@0: request = objectStore.index("type").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, objectStoreDataTypeSort[keyIndex].value.type, michael@0: "Correct key"); michael@0: is(cursor.primaryKey, objectStoreDataTypeSort[keyIndex].key, michael@0: "Correct primary key"); michael@0: ok(!("value" in cursor), "No value"); michael@0: michael@0: cursor.continue(); michael@0: michael@0: is(cursor.key, objectStoreDataTypeSort[keyIndex].value.type, michael@0: "Correct key"); michael@0: is(cursor.primaryKey, objectStoreDataTypeSort[keyIndex].key, michael@0: "Correct value"); michael@0: ok(!("value" in cursor), "No value"); michael@0: michael@0: keyIndex++; michael@0: } michael@0: else { michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, objectStoreDataTypeSort.length, "Saw all the expected keys"); michael@0: michael@0: ok(true, "Test group 2"); michael@0: michael@0: keyIndex = 0; michael@0: michael@0: request = objectStore.index("length").openKeyCursor(null, "next"); 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, objectStoreDataLengthSort[keyIndex].value.length, michael@0: "Correct key"); michael@0: is(cursor.primaryKey, objectStoreDataLengthSort[keyIndex].key, michael@0: "Correct value"); michael@0: michael@0: cursor.continue(); michael@0: michael@0: is(cursor.key, objectStoreDataLengthSort[keyIndex].value.length, michael@0: "Correct key"); michael@0: is(cursor.primaryKey, objectStoreDataLengthSort[keyIndex].key, michael@0: "Correct value"); michael@0: michael@0: keyIndex++; michael@0: } michael@0: else { michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, objectStoreDataLengthSort.length, "Saw all the expected keys"); michael@0: michael@0: finishTest(); michael@0: yield undefined; michael@0: }