1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test IndexedDB's cursor value property.");
8 indexedDBTest(prepareDatabase, testCursor);
9 function prepareDatabase()
11 db = event.target.result;
12 evalAndLog("db.createObjectStore('store')");
18 debug("testCursor():");
19 evalAndLog("transaction = db.transaction('store', 'readwrite')");
20 evalAndLog("store = transaction.objectStore('store')");
21 evalAndLog("store.put({a: 1, b: 10}, 'key1')");
22 evalAndLog("store.put({a: 2, b: 20}, 'key2')");
23 evalAndLog("store.put({a: 3, b: 30}, 'key3')");
24 evalAndLog("store.put({a: 4, b: 40}, 'key4')");
25 evalAndLog("store.put({a: 5, b: 50}, 'key5')");
26 evalAndLog("request = store.openCursor()");
27 request.onerror = unexpectedErrorCallback;
29 request.onsuccess = function() {
32 debug("Value at index: " + index);
33 evalAndLog("cursor = request.result");
35 ensureObjectData(1, 10, 'key1');
38 } else if (index == 1) {
39 ensureObjectData(2, 20, 'key2');
42 } else if (index == 3) {
43 ensureObjectData(4, 40, 'key4');
45 testFailed("Bad index: " + index);
49 transaction.oncomplete = ensureModificationsNotPersisted;
52 function ensureObjectData(a, b, key)
57 shouldBe("cursor.key", "expectedKey");
60 debug("Check expected values:");
61 shouldBe("cursor.value.a", "expectedA");
62 shouldBe("cursor.value.b", "expectedB");
63 shouldBe("cursor.value.foo", "undefined");
66 debug("Modify values:");
67 evalAndLog("cursor.value.a = 3");
68 evalAndLog("delete cursor.value.b");
69 evalAndLog("cursor.value.foo = 'bar'");
72 debug("Ensure modifications are retained:");
73 shouldBe("cursor.value.a", "3");
74 shouldBe("cursor.value.b", "undefined");
75 shouldBe("cursor.value.foo", "'bar'");
77 // make sure to test GC before holding a specific ref to the value
79 debug("Check object value survives gc");
81 shouldBe("cursor.value.a", "3");
82 shouldBe("cursor.value.b", "undefined");
83 shouldBe("cursor.value.foo", "'bar'");
86 debug("Check object identity");
87 evalAndLog("localValueRef = cursor.value");
88 shouldBe("localValueRef", "cursor.value");
91 function ensureModificationsNotPersisted()
94 debug("ensureModificationsNotPersisted():");
95 evalAndLog("transaction = db.transaction('store', 'readonly')");
96 evalAndLog("store = transaction.objectStore('store')");
97 evalAndLog("request = store.openCursor()");
98 request.onerror = unexpectedErrorCallback;
99 request.onsuccess = function() {
100 evalAndLog("cursor = request.result");
101 shouldBe("cursor.key", "'key1'");
104 debug("Check expected values:");
105 shouldBe("cursor.value.a", "1");
106 shouldBe("cursor.value.b", "10");
107 shouldBe("cursor.value.foo", "undefined");
109 transaction.oncomplete = finishJSTest;