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: let name = this.window ? window.location.pathname : "Splendid Test"; 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: michael@0: let event = yield undefined; michael@0: michael@0: let db = event.target.result; michael@0: db.onerror = errorHandler; michael@0: michael@0: for each (let autoIncrement in [false, true]) { michael@0: let objectStore = michael@0: db.createObjectStore(autoIncrement, { keyPath: "id", michael@0: autoIncrement: autoIncrement }); michael@0: michael@0: for (let i = 0; i < 10; i++) { michael@0: objectStore.add({ id: i, index: i }); michael@0: } michael@0: michael@0: for each (let unique in [false, true]) { michael@0: objectStore.createIndex(unique, "index", { unique: unique }); michael@0: } michael@0: michael@0: for (let i = 10; i < 20; i++) { michael@0: objectStore.add({ id: i, index: i }); michael@0: } michael@0: } michael@0: michael@0: event = yield undefined; michael@0: is(event.type, "success", "expect a success event"); michael@0: michael@0: for each (let autoIncrement in [false, true]) { michael@0: let objectStore = db.transaction(autoIncrement) michael@0: .objectStore(autoIncrement); michael@0: michael@0: objectStore.count().onsuccess = grabEventAndContinueHandler; michael@0: let event = yield undefined; michael@0: michael@0: is(event.target.result, 20, "Correct number of entries in objectStore"); michael@0: michael@0: let objectStoreCount = event.target.result; michael@0: let indexCount = event.target.result; michael@0: michael@0: for each (let unique in [false, true]) { michael@0: let index = db.transaction(autoIncrement, "readwrite") michael@0: .objectStore(autoIncrement) michael@0: .index(unique); michael@0: michael@0: index.count().onsuccess = grabEventAndContinueHandler; michael@0: let event = yield undefined; michael@0: michael@0: is(event.target.result, indexCount, michael@0: "Correct number of entries in index"); michael@0: michael@0: let modifiedEntry = unique ? 5 : 10; michael@0: let keyRange = IDBKeyRange.only(modifiedEntry); michael@0: michael@0: let sawEntry = false; michael@0: index.openCursor(keyRange).onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: sawEntry = true; michael@0: is(cursor.key, modifiedEntry, "Correct key"); michael@0: michael@0: cursor.value.index = unique ? 30 : 35; michael@0: cursor.update(cursor.value).onsuccess = function(event) { michael@0: cursor.continue(); michael@0: } michael@0: } michael@0: else { michael@0: continueToNextStep(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(sawEntry, true, "Saw entry for key value " + modifiedEntry); michael@0: michael@0: // Recount index. Shouldn't change. michael@0: index = db.transaction(autoIncrement, "readwrite") michael@0: .objectStore(autoIncrement) michael@0: .index(unique); michael@0: michael@0: index.count().onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: is(event.target.result, indexCount, michael@0: "Correct number of entries in index"); michael@0: michael@0: modifiedEntry = unique ? 30 : 35; michael@0: keyRange = IDBKeyRange.only(modifiedEntry); michael@0: michael@0: sawEntry = false; michael@0: index.openCursor(keyRange).onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: sawEntry = true; michael@0: is(cursor.key, modifiedEntry, "Correct key"); michael@0: michael@0: delete cursor.value.index; michael@0: cursor.update(cursor.value).onsuccess = function(event) { michael@0: indexCount--; michael@0: cursor.continue(); michael@0: } michael@0: } michael@0: else { michael@0: continueToNextStep(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(sawEntry, true, "Saw entry for key value " + modifiedEntry); michael@0: michael@0: // Recount objectStore. Should be unchanged. michael@0: objectStore = db.transaction(autoIncrement, "readwrite") michael@0: .objectStore(autoIncrement); michael@0: michael@0: objectStore.count().onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: is(event.target.result, objectStoreCount, michael@0: "Correct number of entries in objectStore"); michael@0: michael@0: // Recount index. Should be one item less. michael@0: index = objectStore.index(unique); michael@0: michael@0: index.count().onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: is(event.target.result, indexCount, michael@0: "Correct number of entries in index"); michael@0: michael@0: modifiedEntry = objectStoreCount - 1; michael@0: michael@0: objectStore.delete(modifiedEntry).onsuccess = michael@0: grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: objectStoreCount--; michael@0: indexCount--; michael@0: michael@0: objectStore.count().onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: is(event.target.result, objectStoreCount, michael@0: "Correct number of entries in objectStore"); michael@0: michael@0: index.count().onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: is(event.target.result, indexCount, michael@0: "Correct number of entries in index"); michael@0: michael@0: index = event = null; // Bug 943409 workaround. michael@0: } michael@0: objectStore = event = null; // Bug 943409 workaround. michael@0: } michael@0: michael@0: finishTest(); michael@0: event = db = request = null; // Bug 943409 workaround. michael@0: yield undefined; michael@0: }