123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- /**
- * Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/
- */
- var testGenerator = testSteps();
- function testSteps()
- {
- const name = this.window ? window.location.pathname : "Splendid Test";
- const keys = [1, -1, 0, 10, 2000, "q", "z", "two", "b", "a"];
- const sortedKeys = [-1, 0, 1, 10, 2000, "a", "b", "q", "two", "z"];
- is(keys.length, sortedKeys.length, "Good key setup");
- let request = indexedDB.open(name, 1);
- request.onerror = errorHandler;
- request.onupgradeneeded = grabEventAndContinueHandler;
- request.onsuccess = grabEventAndContinueHandler;
- let event = yield undefined;
- let db = event.target.result;
- let objectStore = db.createObjectStore("autoIncrement",
- { autoIncrement: true });
- request = objectStore.openCursor();
- request.onerror = errorHandler;
- request.onsuccess = function (event) {
- ok(!event.target.result, "No results");
- testGenerator.next();
- }
- yield undefined;
- objectStore = db.createObjectStore("autoIncrementKeyPath",
- { keyPath: "foo",
- autoIncrement: true });
- request = objectStore.openCursor();
- request.onerror = errorHandler;
- request.onsuccess = function (event) {
- ok(!event.target.result, "No results");
- testGenerator.next();
- }
- yield undefined;
- objectStore = db.createObjectStore("keyPath", { keyPath: "foo" });
- request = objectStore.openCursor();
- request.onerror = errorHandler;
- request.onsuccess = function (event) {
- ok(!event.target.result, "No results");
- testGenerator.next();
- }
- yield undefined;
- objectStore = db.createObjectStore("foo");
- request = objectStore.openCursor();
- request.onerror = errorHandler;
- request.onsuccess = function (event) {
- ok(!event.target.result, "No results");
- testGenerator.next();
- }
- yield undefined;
- let keyIndex = 0;
- for (let i in keys) {
- request = objectStore.add("foo", keys[i]);
- request.onerror = errorHandler;
- request.onsuccess = function(event) {
- if (++keyIndex == keys.length) {
- testGenerator.next();
- }
- };
- }
- yield undefined;
- keyIndex = 0;
- request = objectStore.openCursor();
- request.onerror = errorHandler;
- request.onsuccess = function (event) {
- let cursor = event.target.result;
- if (cursor) {
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- cursor.continue();
- try {
- cursor.continue();
- ok(false, "continue twice should throw");
- }
- catch (e) {
- ok(e instanceof DOMException, "got a database exception");
- is(e.name, "InvalidStateError", "correct error");
- is(e.code, DOMException.INVALID_STATE_ERR, "correct code");
- }
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- keyIndex++;
- }
- else {
- testGenerator.next();
- }
- }
- yield undefined;
- is(keyIndex, keys.length, "Saw all added items");
- keyIndex = 4;
- let range = IDBKeyRange.bound(2000, "q");
- request = objectStore.openCursor(range);
- request.onerror = errorHandler;
- request.onsuccess = function (event) {
- let cursor = event.target.result;
- if (cursor) {
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- cursor.continue();
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- keyIndex++;
- }
- else {
- testGenerator.next();
- }
- }
- yield undefined;
- is(keyIndex, 8, "Saw all the expected keys");
- keyIndex = 0;
- request = objectStore.openCursor();
- request.onerror = errorHandler;
- request.onsuccess = function (event) {
- let cursor = event.target.result;
- if (cursor) {
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- if (keyIndex) {
- cursor.continue();
- }
- else {
- cursor.continue("b");
- }
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- keyIndex += keyIndex ? 1: 6;
- }
- else {
- testGenerator.next();
- }
- }
- yield undefined;
- is(keyIndex, keys.length, "Saw all the expected keys");
- keyIndex = 0;
- request = objectStore.openCursor();
- request.onerror = errorHandler;
- request.onsuccess = function (event) {
- let cursor = event.target.result;
- if (cursor) {
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- if (keyIndex) {
- cursor.continue();
- }
- else {
- cursor.continue(10);
- }
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- keyIndex += keyIndex ? 1: 3;
- }
- else {
- testGenerator.next();
- }
- }
- yield undefined;
- is(keyIndex, keys.length, "Saw all the expected keys");
- keyIndex = 0;
- request = objectStore.openCursor();
- request.onerror = errorHandler;
- request.onsuccess = function (event) {
- let cursor = event.target.result;
- if (cursor) {
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- if (keyIndex) {
- cursor.continue();
- }
- else {
- cursor.continue("c");
- }
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- keyIndex += keyIndex ? 1 : 7;
- }
- else {
- ok(cursor === null, "The request result should be null.");
- testGenerator.next();
- }
- }
- yield undefined;
- is(keyIndex, keys.length, "Saw all the expected keys");
- keyIndex = 0;
- request = objectStore.openCursor();
- request.onerror = errorHandler;
- let storedCursor = null;
- request.onsuccess = function (event) {
- let cursor = event.target.result;
- if (cursor) {
- storedCursor = cursor;
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- if (keyIndex == 4) {
- request = cursor.update("bar");
- request.onerror = errorHandler;
- request.onsuccess = function(event) {
- keyIndex++;
- cursor.continue();
- };
- }
- else {
- keyIndex++;
- cursor.continue();
- }
- }
- else {
- ok(cursor === null, "The request result should be null.");
- ok(storedCursor.value === undefined, "The cursor's value should be undefined.");
- testGenerator.next();
- }
- }
- yield undefined;
- is(keyIndex, keys.length, "Saw all the expected keys");
- request = objectStore.get(sortedKeys[4]);
- request.onerror = errorHandler;
- request.onsuccess = grabEventAndContinueHandler;
- event = yield undefined;
- is(event.target.result, "bar", "Update succeeded");
- request = objectStore.put("foo", sortedKeys[4]);
- request.onerror = errorHandler;
- request.onsuccess = grabEventAndContinueHandler;
- event = yield undefined;
- keyIndex = 0;
- let gotRemoveEvent = false;
- let retval = false;
- request = objectStore.openCursor(null, "next");
- request.onerror = errorHandler;
- storedCursor = null;
- request.onsuccess = function (event) {
- let cursor = event.target.result;
- if (cursor) {
- storedCursor = cursor;
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- if (keyIndex == 4) {
- request = cursor.delete();
- request.onerror = errorHandler;
- request.onsuccess = function(event) {
- ok(event.target.result === undefined, "Should be undefined");
- is(keyIndex, 5, "Got result of remove before next continue");
- gotRemoveEvent = true;
- };
- }
- keyIndex++;
- cursor.continue();
- }
- else {
- ok(cursor === null, "The request result should be null.");
- ok(storedCursor.value === undefined, "The cursor's value should be undefined.");
- testGenerator.next();
- }
- }
- yield undefined;
- is(keyIndex, keys.length, "Saw all the expected keys");
- is(gotRemoveEvent, true, "Saw the remove event");
- request = objectStore.get(sortedKeys[4]);
- request.onerror = errorHandler;
- request.onsuccess = grabEventAndContinueHandler;
- event = yield undefined;
- is(event.target.result, undefined, "Entry was deleted");
- request = objectStore.add("foo", sortedKeys[4]);
- request.onerror = errorHandler;
- request.onsuccess = grabEventAndContinueHandler;
- event = yield undefined;
- keyIndex = sortedKeys.length - 1;
- request = objectStore.openCursor(null, "prev");
- request.onerror = errorHandler;
- storedCursor = null;
- request.onsuccess = function (event) {
- let cursor = event.target.result;
- if (cursor) {
- storedCursor = cursor;
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- cursor.continue();
- is(cursor.key, sortedKeys[keyIndex], "Correct key");
- is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key");
- is(cursor.value, "foo", "Correct value");
- keyIndex--;
- }
- else {
- ok(cursor === null, "The request result should be null.");
- ok(storedCursor.value === undefined, "The cursor's value should be undefined.");
- testGenerator.next();
- }
- }
- yield undefined;
- is(keyIndex, -1, "Saw all added items");
- // Wait for success
- yield undefined;
- db.close();
- finishTest();
- yield undefined;
- }
|