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: const keys = [1, -1, 0, 10, 2000, "q", "z", "two", "b", "a"]; michael@0: const sortedKeys = [-1, 0, 1, 10, 2000, "a", "b", "q", "two", "z"]; michael@0: michael@0: is(keys.length, sortedKeys.length, "Good key setup"); michael@0: michael@0: let request = indexedDB.open(name, 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: michael@0: let objectStore = db.createObjectStore("autoIncrement", michael@0: { autoIncrement: true }); michael@0: michael@0: request = objectStore.openCursor(); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function (event) { michael@0: ok(!event.target.result, "No results"); michael@0: testGenerator.next(); michael@0: } michael@0: yield undefined; michael@0: michael@0: objectStore = db.createObjectStore("autoIncrementKeyPath", michael@0: { keyPath: "foo", michael@0: autoIncrement: true }); michael@0: michael@0: request = objectStore.openCursor(); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function (event) { michael@0: ok(!event.target.result, "No results"); michael@0: testGenerator.next(); michael@0: } michael@0: yield undefined; michael@0: michael@0: objectStore = db.createObjectStore("keyPath", { keyPath: "foo" }); michael@0: michael@0: request = objectStore.openCursor(); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function (event) { michael@0: ok(!event.target.result, "No results"); michael@0: testGenerator.next(); michael@0: } michael@0: yield undefined; michael@0: michael@0: objectStore = db.createObjectStore("foo"); michael@0: michael@0: request = objectStore.openCursor(); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function (event) { michael@0: ok(!event.target.result, "No results"); michael@0: testGenerator.next(); michael@0: } michael@0: yield undefined; michael@0: michael@0: let keyIndex = 0; michael@0: michael@0: for (let i in keys) { michael@0: request = objectStore.add("foo", keys[i]); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function(event) { michael@0: if (++keyIndex == keys.length) { michael@0: testGenerator.next(); michael@0: } michael@0: }; michael@0: } michael@0: yield undefined; 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: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: cursor.continue(); michael@0: michael@0: try { michael@0: cursor.continue(); michael@0: ok(false, "continue twice should throw"); michael@0: } michael@0: catch (e) { michael@0: ok(e instanceof DOMException, "got a database exception"); michael@0: is(e.name, "InvalidStateError", "correct error"); michael@0: is(e.code, DOMException.INVALID_STATE_ERR, "correct code"); michael@0: } michael@0: michael@0: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "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, keys.length, "Saw all added items"); michael@0: michael@0: keyIndex = 4; michael@0: michael@0: let range = IDBKeyRange.bound(2000, "q"); michael@0: request = objectStore.openCursor(range); 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, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: cursor.continue(); michael@0: michael@0: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "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, 8, "Saw all the expected keys"); 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: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: if (keyIndex) { michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: cursor.continue("b"); michael@0: } michael@0: michael@0: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: keyIndex += keyIndex ? 1: 6; michael@0: } michael@0: else { michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, keys.length, "Saw all the expected keys"); 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: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: if (keyIndex) { michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: cursor.continue(10); michael@0: } michael@0: michael@0: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: keyIndex += keyIndex ? 1: 3; michael@0: } michael@0: else { michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, keys.length, "Saw all the expected keys"); 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: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: if (keyIndex) { michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: cursor.continue("c"); michael@0: } michael@0: michael@0: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: keyIndex += keyIndex ? 1 : 7; michael@0: } michael@0: else { michael@0: ok(cursor === null, "The request result should be null."); michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, keys.length, "Saw all the expected keys"); michael@0: michael@0: keyIndex = 0; michael@0: michael@0: request = objectStore.openCursor(); michael@0: request.onerror = errorHandler; michael@0: let storedCursor = null; michael@0: request.onsuccess = function (event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: storedCursor = cursor; michael@0: michael@0: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: if (keyIndex == 4) { michael@0: request = cursor.update("bar"); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function(event) { michael@0: keyIndex++; michael@0: cursor.continue(); michael@0: }; michael@0: } michael@0: else { michael@0: keyIndex++; michael@0: cursor.continue(); michael@0: } michael@0: } michael@0: else { michael@0: ok(cursor === null, "The request result should be null."); michael@0: ok(storedCursor.value === undefined, "The cursor's value should be undefined."); michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, keys.length, "Saw all the expected keys"); michael@0: michael@0: request = objectStore.get(sortedKeys[4]); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: is(event.target.result, "bar", "Update succeeded"); michael@0: michael@0: request = objectStore.put("foo", sortedKeys[4]); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: keyIndex = 0; michael@0: michael@0: let gotRemoveEvent = false; michael@0: let retval = false; michael@0: michael@0: request = objectStore.openCursor(null, "next"); michael@0: request.onerror = errorHandler; michael@0: storedCursor = null; michael@0: request.onsuccess = function (event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: storedCursor = cursor; michael@0: michael@0: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: if (keyIndex == 4) { michael@0: request = cursor.delete(); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = function(event) { michael@0: ok(event.target.result === undefined, "Should be undefined"); michael@0: is(keyIndex, 5, "Got result of remove before next continue"); michael@0: gotRemoveEvent = true; michael@0: }; michael@0: } michael@0: michael@0: keyIndex++; michael@0: cursor.continue(); michael@0: } michael@0: else { michael@0: ok(cursor === null, "The request result should be null."); michael@0: ok(storedCursor.value === undefined, "The cursor's value should be undefined."); michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, keys.length, "Saw all the expected keys"); michael@0: is(gotRemoveEvent, true, "Saw the remove event"); michael@0: michael@0: request = objectStore.get(sortedKeys[4]); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: is(event.target.result, undefined, "Entry was deleted"); michael@0: michael@0: request = objectStore.add("foo", sortedKeys[4]); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = grabEventAndContinueHandler; michael@0: event = yield undefined; michael@0: michael@0: keyIndex = sortedKeys.length - 1; michael@0: michael@0: request = objectStore.openCursor(null, "prev"); michael@0: request.onerror = errorHandler; michael@0: storedCursor = null; michael@0: request.onsuccess = function (event) { michael@0: let cursor = event.target.result; michael@0: if (cursor) { michael@0: storedCursor = cursor; michael@0: michael@0: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: cursor.continue(); michael@0: michael@0: is(cursor.key, sortedKeys[keyIndex], "Correct key"); michael@0: is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); michael@0: is(cursor.value, "foo", "Correct value"); michael@0: michael@0: keyIndex--; michael@0: } michael@0: else { michael@0: ok(cursor === null, "The request result should be null."); michael@0: ok(storedCursor.value === undefined, "The cursor's value should be undefined."); michael@0: testGenerator.next(); michael@0: } michael@0: } michael@0: yield undefined; michael@0: michael@0: is(keyIndex, -1, "Saw all added items"); michael@0: michael@0: finishTest(); michael@0: yield undefined; michael@0: }