2011-02-04 Jeremy Orlow <jorlow@chromium.org>
[WebKit-https.git] / LayoutTests / storage / indexeddb / cursor-index-delete.html
1 <html>
2 <head>
3 <link rel="stylesheet" href="../../fast/js/resources/js-test-style.css">
4 <script src="../../fast/js/resources/js-test-pre.js"></script>
5 <script src="../../fast/js/resources/js-test-post-function.js"></script>
6 <script src="resources/shared.js"></script>
7 </head>
8 <body>
9 <p id="description"></p>
10 <div id="console"></div>
11 <script>
12
13 description("Test IndexedDB's openCursor.");
14 if (window.layoutTestController)
15     layoutTestController.waitUntilDone();
16
17 test();
18
19 function test()
20 {
21     result = evalAndLog("webkitIndexedDB.open('name')");
22     verifyResult(result);
23     result.onsuccess = openSuccess;
24     result.onerror = unexpectedErrorCallback;
25 }
26
27 function openSuccess()
28 {
29     verifySuccessEvent(event);
30     var db = evalAndLog("db = event.result");
31
32     result = evalAndLog("db.setVersion('new version')");
33     verifyResult(result);
34     result.onsuccess = setVersionSuccess;
35     result.onerror = unexpectedErrorCallback;
36 }
37
38 function setVersionSuccess()
39 {
40     debug("setVersionSuccess():");
41     verifySuccessEvent(event);
42     window.trans = evalAndLog("trans = event.result");
43     shouldBeTrue("trans !== null");
44     trans.onabort = unexpectedAbortCallback;
45     trans.oncomplete = openCursor;
46
47     deleteAllObjectStores(db, createAndPopulateObjectStoreAndIndex);
48 }
49
50 function createAndPopulateObjectStoreAndIndex() 
51 {
52     objectStore = evalAndLog("objectStore = db.createObjectStore('test')");
53     evalAndLog("objectStore.createIndex('testIndex', 'x')");
54
55     evalAndLog("objectStore.add({x: 1}, 'myKey1')");
56     evalAndLog("objectStore.add({x: 2}, 'myKey2')");
57     evalAndLog("objectStore.add({x: 3}, 'myKey3')");
58     evalAndLog("objectStore.add({x: 4}, 'myKey4')");
59 }
60
61 function openCursor()
62 {
63     debug("openCursor1");
64     evalAndLog("trans = db.transaction(['test'], webkitIDBTransaction.READ_WRITE)");
65     keyRange = webkitIDBKeyRange.lowerBound(1);
66     result = evalAndLog("trans.objectStore('test').index('testIndex').openCursor(keyRange)");
67     verifyResult(result);
68     result.onsuccess = cursorSuccess;
69     result.onerror = unexpectedErrorCallback;
70     counter = 1;
71 }
72
73 function cursorSuccess()
74 {
75     if (event.result == null) {
76         shouldBe("counter", "5");
77         result = evalAndLog("trans.objectStore('test').index('testIndex').openCursor(keyRange)");
78         verifyResult(result);
79         result.onsuccess = cursorEmpty;
80         result.onerror = unexpectedErrorCallback;
81         return;
82     }
83     var deleteResult = evalAndLog("event.result.delete()");
84     deleteResult.onerror = unexpectedErrorCallback;
85     shouldBe("event.result.key", "counter++");
86     evalAndLog("event.result.continue()");
87 }
88
89 function cursorEmpty()
90 {
91     verifySuccessEvent(event);
92     shouldBeNull("event.result");
93     trans.oncomplete = addObject;
94 }
95
96 function addObject()
97 {
98     evalAndLog("trans = db.transaction(['test'], webkitIDBTransaction.READ_WRITE)");
99     objectStore = evalAndLog("objectStore = trans.objectStore('test')");
100     result = evalAndLog("objectStore.add({x: 1}, 'myKey1')");
101     verifyResult(result);
102     result.onsuccess = openCursor2;
103     result.onerror = unexpectedErrorCallback;
104 }
105
106 function openCursor2()
107 {
108     debug("openCursor2");
109     evalAndLog("index = event.source.index('testIndex')");
110     result = evalAndLog("index.openCursor(keyRange)");
111     verifyResult(result);
112     result.onsuccess = deleteObject;
113     result.onerror = unexpectedErrorCallback;
114 }
115
116 function deleteObject()
117 {
118     verifySuccessEvent(event);
119     shouldBeNonNull(event.result);
120     evalAndLog("event.result.delete()");
121     result = evalAndLog("index.get(1)");
122     result.onsuccess = unexpectedSuccessCallback;
123     result.onerror = verifyObjectDeleted;
124 }
125
126 function verifyObjectDeleted()
127 {
128     verifyErrorEvent(event);
129     evalAndLog("event.preventDefault()");
130     done();
131 }
132
133 var successfullyParsed = true;
134
135 </script>
136 </body>
137 </html>