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 dataCount = 30; 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: let objectStore = db.createObjectStore("", { keyPath: "key" }); michael@0: objectStore.createIndex("", "index"); michael@0: michael@0: for (let i = 0; i < dataCount; i++) { michael@0: objectStore.add({ key: i, index: i }); michael@0: } michael@0: yield undefined; michael@0: michael@0: function getObjectStore() { michael@0: return db.transaction("").objectStore(""); michael@0: } michael@0: michael@0: function getIndex() { michael@0: return db.transaction("").objectStore("").index(""); michael@0: } michael@0: michael@0: let count = 0; michael@0: michael@0: getObjectStore().openCursor().onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: count++; michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: continueToNextStep(); michael@0: } michael@0: }; michael@0: yield undefined; michael@0: michael@0: is(count, dataCount, "Saw all data"); michael@0: michael@0: count = 0; michael@0: michael@0: getObjectStore().openCursor().onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: is(cursor.primaryKey, count, "Got correct object"); michael@0: if (count) { michael@0: count++; michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: count = 10; michael@0: cursor.advance(10); michael@0: } michael@0: } michael@0: else { michael@0: continueToNextStep(); michael@0: } michael@0: }; michael@0: yield undefined; michael@0: michael@0: is(count, dataCount, "Saw all data"); michael@0: michael@0: count = 0; michael@0: michael@0: getIndex().openCursor().onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: is(cursor.primaryKey, count, "Got correct object"); michael@0: if (count) { michael@0: count++; michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: count = 10; michael@0: cursor.advance(10); michael@0: } michael@0: } michael@0: else { michael@0: continueToNextStep(); michael@0: } michael@0: }; michael@0: yield undefined; michael@0: michael@0: is(count, dataCount, "Saw all data"); michael@0: michael@0: count = 0; michael@0: michael@0: getIndex().openKeyCursor().onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: is(cursor.primaryKey, count, "Got correct object"); michael@0: if (count) { michael@0: count++; michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: count = 10; michael@0: cursor.advance(10); michael@0: } michael@0: } michael@0: else { michael@0: continueToNextStep(); michael@0: } michael@0: }; michael@0: yield undefined; michael@0: michael@0: is(count, dataCount, "Saw all data"); michael@0: michael@0: count = 0; michael@0: michael@0: getObjectStore().openCursor().onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: is(cursor.primaryKey, count, "Got correct object"); michael@0: if (count == 0) { michael@0: cursor.advance(dataCount + 1); michael@0: } michael@0: else { michael@0: ok(false, "Should never get here!"); 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(count, 0, "Saw all data"); michael@0: michael@0: count = dataCount - 1; michael@0: michael@0: getObjectStore().openCursor(null, "prev").onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: is(cursor.primaryKey, count, "Got correct object"); michael@0: count--; michael@0: if (count == dataCount - 2) { michael@0: cursor.advance(10); michael@0: count -= 9; michael@0: } michael@0: else { 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(count, -1, "Saw all data"); michael@0: michael@0: count = dataCount - 1; michael@0: michael@0: getObjectStore().openCursor(null, "prev").onsuccess = function(event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: is(cursor.primaryKey, count, "Got correct object"); michael@0: if (count == dataCount - 1) { michael@0: cursor.advance(dataCount + 1); michael@0: } michael@0: else { michael@0: ok(false, "Should never get here!"); 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(count, dataCount - 1, "Saw all data"); michael@0: michael@0: finishTest(); michael@0: yield undefined; michael@0: }