1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test IndexedDB's openCursor.");
8 indexedDBTest(prepareDatabase);
9 function prepareDatabase()
11 db = event.target.result;
12 self.trans = evalAndLog("trans = event.target.transaction");
13 shouldBeNonNull("trans");
14 trans.onabort = unexpectedAbortCallback;
15 trans.oncomplete = openCursor;
17 objectStore = evalAndLog("objectStore = db.createObjectStore('test')");
18 evalAndLog("objectStore.createIndex('testIndex', 'x')");
20 evalAndLog("objectStore.add({x: 1}, 'myKey1')");
21 evalAndLog("objectStore.add({x: 2}, 'myKey2')");
22 evalAndLog("objectStore.add({x: 3}, 'myKey3')");
23 evalAndLog("objectStore.add({x: 4}, 'myKey4')");
29 evalAndLog("trans = db.transaction(['test'], 'readwrite')");
30 keyRange = IDBKeyRange.lowerBound(1);
31 request = evalAndLog("trans.objectStore('test').index('testIndex').openCursor(keyRange)");
32 request.onsuccess = cursorSuccess;
33 request.onerror = unexpectedErrorCallback;
37 function cursorSuccess()
39 if (event.target.result == null) {
40 shouldBe("counter", "5");
41 request = evalAndLog("trans.objectStore('test').index('testIndex').openCursor(keyRange)");
42 request.onsuccess = cursorEmpty;
43 request.onerror = unexpectedErrorCallback;
46 var deleteRequest = evalAndLog("event.target.result.delete()");
47 deleteRequest.onerror = unexpectedErrorCallback;
48 shouldBe("event.target.result.key", "counter++");
49 evalAndLog("event.target.result.continue()");
52 function cursorEmpty()
54 shouldBeNull("event.target.result");
55 trans.oncomplete = addObject;
60 evalAndLog("trans = db.transaction(['test'], 'readwrite')");
61 objectStore = evalAndLog("objectStore = trans.objectStore('test')");
62 request = evalAndLog("objectStore.add({x: 1}, 'myKey1')");
63 request.onsuccess = openCursor2;
64 request.onerror = unexpectedErrorCallback;
67 function openCursor2()
70 evalAndLog("index = event.target.source.index('testIndex')");
71 request = evalAndLog("index.openCursor(keyRange)");
72 request.onsuccess = deleteObject;
73 request.onerror = unexpectedErrorCallback;
76 function deleteObject()
78 shouldBeNonNull(event.target.result);
79 evalAndLog("event.target.result.delete()");
80 request = evalAndLog("index.get(1)");
81 request.onsuccess = verifyObjectDeleted;
82 request.onerror = unexpectedErrorCallback;
85 function verifyObjectDeleted()
87 shouldBe("event.target.result", "undefined");