1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test the basics of IndexedDB's IDBIndex.");
8 indexedDBTest(prepareDatabase);
9 function prepareDatabase(evt)
12 db = event.target.result;
13 event.target.transaction.onabort = unexpectedAbortCallback;
14 self.store = evalAndLog("db.createObjectStore('storeName', null)");
15 self.indexObject = evalAndLog("store.createIndex('indexName', 'x')");
16 self.indexObject2 = evalAndLog("store.createIndex('indexName2', 'y', {unique: false})");
17 self.indexObject3 = evalAndLog("store.createIndex('zIndex', 'z', {unique: true})");
18 shouldNotThrow("store.createIndex('index4', 'path', undefined)");
19 shouldBeFalse("indexObject2.unique");
20 shouldBeTrue("indexObject3.unique");
21 evalAndExpectExceptionClass("store.createIndex('failureIndex', 'zzz', true)", "TypeError");
22 evalAndExpectExceptionClass("store.createIndex('failureIndex', 'zzz', 'string')", "TypeError");
28 shouldBeTrue("'name' in indexObject");
29 shouldBeEqualToString("indexObject.name", "indexName");
30 shouldBeTrue("'objectStore' in indexObject");
31 shouldBeEqualToString("indexObject.objectStore.name", "storeName");
32 shouldBeTrue("'keyPath' in indexObject");
33 shouldBeEqualToString("indexObject.keyPath", "x");
34 shouldBeTrue("'unique' in indexObject");
35 shouldBeTrue("'multiEntry' in indexObject");
36 shouldBeFalse("indexObject.unique");
37 shouldBeFalse("indexObject.multiEntry");
38 shouldBeTrue("'openKeyCursor' in indexObject");
39 shouldBeEqualToString("typeof indexObject.openKeyCursor", "function");
40 shouldBeTrue("'openCursor' in indexObject");
41 shouldBeEqualToString("typeof indexObject.openCursor", "function");
42 shouldBeTrue("'getKey' in indexObject");
43 shouldBeEqualToString("typeof indexObject.getKey", "function");
44 shouldBeTrue("'get' in indexObject");
45 shouldBeEqualToString("typeof indexObject.get", "function");
46 shouldBeTrue("'count' in indexObject");
47 shouldBeEqualToString("typeof indexObject.count", "function");
49 request = evalAndLog("store.add({x: 'value', y: 'zzz', z: 2.72}, 'key')");
50 request.onsuccess = addData2;
51 request.onerror = unexpectedErrorCallback;
54 function addData2(evt)
57 request = evalAndLog("event.target.source.add({x: 'value2', y: 'zzz2', z: 2.71, foobar: 12}, 'key2')");
58 request.onsuccess = addData3;
59 request.onerror = unexpectedErrorCallback;
60 self.indexObject4 = evalAndLog("store.createIndex('indexWhileAddIsInFlight', 'x')");
61 self.indexObject5 = evalAndLog("store.createIndex('indexWithWeirdKeyPath', 'foobar')");
64 function addData3(evt)
67 debug("Add data which doesn't have a key in the z index.");
68 request = evalAndLog("event.target.source.add({x: 'value3', y: '456'}, 'key3')");
69 request.onsuccess = addData4;
70 request.onerror = unexpectedErrorCallback;
73 function addData4(evt)
76 debug("Add data which has invalid key for y index, no key for the z index.");
77 request = evalAndLog("event.target.source.add({x: 'value4', y: null}, 'key4')");
78 request.onsuccess = getData;
79 request.onerror = unexpectedErrorCallback;
85 request = evalAndLog("indexObject.getKey('value')");
86 request.onsuccess = getObjectData;
87 request.onerror = unexpectedErrorCallback;
90 function getObjectData(evt)
93 shouldBeEqualToString("event.target.result", "key");
95 request = evalAndLog("indexObject2.getKey('zzz')");
96 request.onsuccess = getObjectData2;
97 request.onerror = unexpectedErrorCallback;
100 function getObjectData2(evt)
103 shouldBeEqualToString("event.target.result", "key");
105 request = evalAndLog("indexObject3.get(2.71)");
106 request.onsuccess = getObjectData3;
107 request.onerror = unexpectedErrorCallback;
110 function getObjectData3(evt)
113 shouldBeEqualToString("event.target.result.x", "value2");
115 request = evalAndLog("indexObject.get('value')");
116 request.onsuccess = getDataFail;
117 request.onerror = unexpectedErrorCallback;
120 function getDataFail(evt)
123 shouldBeEqualToString("event.target.result.x", "value");
124 shouldBeEqualToString("event.target.result.y", "zzz");
126 request = evalAndLog("indexObject.getKey('does not exist')");
127 request.onsuccess = getObjectDataFail;
128 request.onerror = unexpectedSuccessCallback;
131 function getObjectDataFail(evt)
134 shouldBe("event.target.result", "undefined");
136 request = evalAndLog("indexObject.get('does not exist')");
137 request.onsuccess = getObjectData4;
138 request.onerror = unexpectedSuccessCallback;
141 function getObjectData4(evt)
144 shouldBe("event.target.result", "undefined");
146 request = evalAndLog("indexObject4.getKey('value2')");
147 request.onsuccess = openKeyCursor;
148 request.onerror = unexpectedErrorCallback;
151 function openKeyCursor(evt)
154 shouldBeEqualToString("event.target.result", "key2");
157 debug("Verify that specifying an invalid direction raises an exception:");
158 evalAndExpectExceptionClass("indexObject.openKeyCursor(0, 'invalid-direction')", "TypeError");
161 self.request = evalAndLog("indexObject.openKeyCursor()");
162 request.onsuccess = cursor1Continue;
163 request.onerror = unexpectedErrorCallback;
166 function cursor1Continue(evt)
169 shouldBe("event.target.source", "indexObject");
170 shouldBeNonNull("event.target.result");
171 shouldBeEqualToString("event.target.result.key", "value");
172 shouldBeEqualToString("event.target.result.primaryKey", "key");
174 // We re-use the last request object.
175 evalAndLog("event.target.result.continue()");
176 self.request.onsuccess = cursor1Continue2;
179 function cursor1Continue2(evt)
182 shouldBeNonNull("event.target.result");
183 shouldBeEqualToString("event.target.result.key", "value2");
184 shouldBeEqualToString("event.target.result.primaryKey", "key2");
186 // We re-use the last request object.
187 evalAndLog("event.target.result.continue()");
188 self.request.onsuccess = cursor1Continue3;
191 function cursor1Continue3(evt)
194 shouldBeNonNull("event.target.result");
195 shouldBeEqualToString("event.target.result.key", "value3");
196 shouldBeEqualToString("event.target.result.primaryKey", "key3");
198 // We re-use the last request object.
199 evalAndLog("event.target.result.continue()");
200 self.request.onsuccess = cursor1Continue4;
203 function cursor1Continue4(evt)
206 shouldBeNonNull("event.target.result");
207 shouldBeEqualToString("event.target.result.key", "value4");
208 shouldBeEqualToString("event.target.result.primaryKey", "key4");
210 // We re-use the last request object.
211 evalAndLog("event.target.result.continue()");
212 self.request.onsuccess = openObjectCursor;
215 function openObjectCursor(evt)
218 shouldBeNull("event.target.result");
221 debug("Verify that specifying an invalid direction raises an exception:");
222 evalAndExpectExceptionClass("indexObject.openCursor(0, 'invalid-direction')", "TypeError");
225 self.request = evalAndLog("indexObject.openCursor()");
226 request.onsuccess = cursor2Continue;
227 request.onerror = unexpectedErrorCallback;
230 function cursor2Continue(evt)
233 shouldBe("event.target.source", "indexObject");
234 shouldBeNonNull("event.target.result");
235 shouldBeEqualToString("event.target.result.key", "value");
236 shouldBeEqualToString("event.target.result.value.x", "value");
237 shouldBeEqualToString("event.target.result.value.y", "zzz");
239 // We re-use the last request object.
240 evalAndLog("event.target.result.continue()");
241 self.request.onsuccess = cursor2Continue2;
244 function cursor2Continue2(evt)
247 shouldBeNonNull("event.target.result");
248 shouldBeEqualToString("event.target.result.key", "value2");
249 shouldBeEqualToString("event.target.result.value.x", "value2");
250 shouldBeEqualToString("event.target.result.value.y", "zzz2");
252 // We re-use the last request object.
253 evalAndLog("event.target.result.continue()");
254 self.request.onsuccess = cursor2Continue3;
257 function cursor2Continue3(evt)
260 shouldBeNonNull("event.target.result");
261 shouldBeEqualToString("event.target.result.key", "value3");
262 shouldBeEqualToString("event.target.result.value.x", "value3");
263 shouldBeEqualToString("event.target.result.value.y", "456");
265 // We re-use the last request object.
266 evalAndLog("event.target.result.continue()");
267 self.request.onsuccess = cursor2Continue4;
270 function cursor2Continue4(evt)
273 shouldBeNonNull("event.target.result");
274 shouldBeEqualToString("event.target.result.key", "value4");
275 shouldBeEqualToString("event.target.result.value.x", "value4");
276 shouldBe("event.target.result.value.y", "null");
278 // We re-use the last request object.
279 evalAndLog("event.target.result.continue()");
280 self.request.onsuccess = last;
286 shouldBeNull("event.target.result");
288 evalAndLog("request = indexObject.count()");
289 request.onerror = unexpectedErrorCallback;
290 request.onsuccess = index1Count;
293 function index1Count(evt)
296 shouldBe("event.target.result", "4");
298 evalAndLog("request = indexObject2.count()");
299 request.onerror = unexpectedErrorCallback;
300 request.onsuccess = index2Count;
303 function index2Count(evt)
306 shouldBe("event.target.result", "3");
308 evalAndLog("request = indexObject3.count()");
309 request.onerror = unexpectedErrorCallback;
310 request.onsuccess = index3Count;
313 function index3Count(evt)
316 shouldBe("event.target.result", "2");
318 debug("Passing an invalid key into indexObject.get({}).");
319 evalAndExpectException("indexObject.get({})", "0", "'DataError'");
321 debug("Passing an invalid key into indexObject.getKey({}).");
322 evalAndExpectException("indexObject.getKey({})", "0", "'DataError'");