2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
6 /* exported testGenerator */
7 var testGenerator = testSteps();
9 function* testSteps() {
10 function checkCursor(cursor, expectedKey) {
11 is(cursor.key, expectedKey, "Correct key");
12 is(cursor.primaryKey, expectedKey, "Correct primary key");
13 is(cursor.value, "foo", "Correct value");
16 const name = this.window ? window.location.pathname : "Splendid Test";
17 const keys = [1, -1, 0, 10, 2000, "q", "z", "two", "b", "a"];
18 const sortedKeys = [-1, 0, 1, 10, 2000, "a", "b", "q", "two", "z"];
20 is(keys.length, sortedKeys.length, "Good key setup");
22 let request = indexedDB.open(name, 1);
23 request.onerror = errorHandler;
24 request.onupgradeneeded = grabEventAndContinueHandler;
25 request.onsuccess = grabEventAndContinueHandler;
26 let event = yield undefined;
28 let db = event.target.result;
30 let objectStore = db.createObjectStore("autoIncrement", {
34 request = objectStore.openCursor();
35 request.onerror = errorHandler;
36 request.onsuccess = function(event) {
37 ok(!event.target.result, "No results");
42 objectStore = db.createObjectStore("autoIncrementKeyPath", {
47 request = objectStore.openCursor();
48 request.onerror = errorHandler;
49 request.onsuccess = function(event) {
50 ok(!event.target.result, "No results");
55 objectStore = db.createObjectStore("keyPath", { keyPath: "foo" });
57 request = objectStore.openCursor();
58 request.onerror = errorHandler;
59 request.onsuccess = function(event) {
60 ok(!event.target.result, "No results");
65 objectStore = db.createObjectStore("foo");
67 request = objectStore.openCursor();
68 request.onerror = errorHandler;
69 request.onsuccess = function(event) {
70 ok(!event.target.result, "No results");
78 request = objectStore.add("foo", keys[i]);
79 request.onerror = errorHandler;
80 request.onsuccess = function(event) {
81 if (++keyIndex == keys.length) {
90 request = objectStore.openCursor();
91 request.onerror = errorHandler;
92 request.onsuccess = function(event) {
93 let cursor = event.target.result;
95 checkCursor(cursor, sortedKeys[keyIndex]);
101 ok(false, "continue twice should throw");
103 ok(e instanceof DOMException, "got a database exception");
104 is(e.name, "InvalidStateError", "correct error");
105 is(e.code, DOMException.INVALID_STATE_ERR, "correct code");
108 checkCursor(cursor, sortedKeys[keyIndex]);
112 testGenerator.next();
117 is(keyIndex, keys.length, "Saw all added items");
121 let range = IDBKeyRange.bound(2000, "q");
122 request = objectStore.openCursor(range);
123 request.onerror = errorHandler;
124 request.onsuccess = function(event) {
125 let cursor = event.target.result;
127 checkCursor(cursor, sortedKeys[keyIndex]);
131 checkCursor(cursor, sortedKeys[keyIndex]);
135 testGenerator.next();
140 is(keyIndex, 8, "Saw all the expected keys");
144 request = objectStore.openCursor();
145 request.onerror = errorHandler;
146 request.onsuccess = function(event) {
147 let cursor = event.target.result;
149 checkCursor(cursor, sortedKeys[keyIndex]);
154 cursor.continue("b");
157 checkCursor(cursor, sortedKeys[keyIndex]);
159 keyIndex += keyIndex ? 1 : 6;
161 testGenerator.next();
166 is(keyIndex, keys.length, "Saw all the expected keys");
170 request = objectStore.openCursor();
171 request.onerror = errorHandler;
172 request.onsuccess = function(event) {
173 let cursor = event.target.result;
175 checkCursor(cursor, sortedKeys[keyIndex]);
183 checkCursor(cursor, sortedKeys[keyIndex]);
185 keyIndex += keyIndex ? 1 : 3;
187 testGenerator.next();
192 is(keyIndex, keys.length, "Saw all the expected keys");
196 request = objectStore.openCursor();
197 request.onerror = errorHandler;
198 request.onsuccess = function(event) {
199 let cursor = event.target.result;
201 checkCursor(cursor, sortedKeys[keyIndex]);
206 cursor.continue("c");
209 checkCursor(cursor, sortedKeys[keyIndex]);
211 keyIndex += keyIndex ? 1 : 7;
213 ok(cursor === null, "The request result should be null.");
214 testGenerator.next();
219 is(keyIndex, keys.length, "Saw all the expected keys");
223 request = objectStore.openCursor();
224 request.onerror = errorHandler;
225 let storedCursor = null;
226 request.onsuccess = function(event) {
227 let cursor = event.target.result;
229 storedCursor = cursor;
231 checkCursor(cursor, sortedKeys[keyIndex]);
234 request = cursor.update("bar");
235 request.onerror = errorHandler;
236 request.onsuccess = function(event) {
245 ok(cursor === null, "The request result should be null.");
247 storedCursor.value === undefined,
248 "The cursor's value should be undefined."
250 testGenerator.next();
255 is(keyIndex, keys.length, "Saw all the expected keys");
257 request = objectStore.get(sortedKeys[4]);
258 request.onerror = errorHandler;
259 request.onsuccess = grabEventAndContinueHandler;
260 event = yield undefined;
262 is(event.target.result, "bar", "Update succeeded");
264 request = objectStore.put("foo", sortedKeys[4]);
265 request.onerror = errorHandler;
266 request.onsuccess = grabEventAndContinueHandler;
267 event = yield undefined;
271 let gotRemoveEvent = false;
273 request = objectStore.openCursor(null, "next");
274 request.onerror = errorHandler;
276 request.onsuccess = function(event) {
277 let cursor = event.target.result;
279 storedCursor = cursor;
281 checkCursor(cursor, sortedKeys[keyIndex]);
284 request = cursor.delete();
285 request.onerror = errorHandler;
286 request.onsuccess = function(event) {
287 ok(event.target.result === undefined, "Should be undefined");
288 is(keyIndex, 5, "Got result of delete before next continue");
289 gotRemoveEvent = true;
296 ok(cursor === null, "The request result should be null.");
298 storedCursor.value === undefined,
299 "The cursor's value should be undefined."
301 testGenerator.next();
306 is(keyIndex, keys.length, "Saw all the expected keys");
307 is(gotRemoveEvent, true, "Saw the remove event");
309 request = objectStore.get(sortedKeys[4]);
310 request.onerror = errorHandler;
311 request.onsuccess = grabEventAndContinueHandler;
312 event = yield undefined;
314 is(event.target.result, undefined, "Entry was deleted");
316 request = objectStore.add("foo", sortedKeys[4]);
317 request.onerror = errorHandler;
318 request.onsuccess = grabEventAndContinueHandler;
319 event = yield undefined;
321 keyIndex = sortedKeys.length - 1;
323 request = objectStore.openCursor(null, "prev");
324 request.onerror = errorHandler;
326 request.onsuccess = function(event) {
327 let cursor = event.target.result;
329 storedCursor = cursor;
331 checkCursor(cursor, sortedKeys[keyIndex]);
335 checkCursor(cursor, sortedKeys[keyIndex]);
339 ok(cursor === null, "The request result should be null.");
341 storedCursor.value === undefined,
342 "The cursor's value should be undefined."
344 testGenerator.next();
349 is(keyIndex, -1, "Saw all added items");