|
1 /** |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
4 */ |
|
5 |
|
6 var testGenerator = testSteps(); |
|
7 |
|
8 function testSteps() |
|
9 { |
|
10 const name = this.window ? window.location.pathname : "Splendid Test"; |
|
11 const keys = [1, -1, 0, 10, 2000, "q", "z", "two", "b", "a"]; |
|
12 const sortedKeys = [-1, 0, 1, 10, 2000, "a", "b", "q", "two", "z"]; |
|
13 |
|
14 is(keys.length, sortedKeys.length, "Good key setup"); |
|
15 |
|
16 let request = indexedDB.open(name, 1); |
|
17 request.onerror = errorHandler; |
|
18 request.onupgradeneeded = grabEventAndContinueHandler; |
|
19 let event = yield undefined; |
|
20 |
|
21 let db = event.target.result; |
|
22 |
|
23 let objectStore = db.createObjectStore("autoIncrement", |
|
24 { autoIncrement: true }); |
|
25 |
|
26 request = objectStore.openCursor(); |
|
27 request.onerror = errorHandler; |
|
28 request.onsuccess = function (event) { |
|
29 ok(!event.target.result, "No results"); |
|
30 testGenerator.next(); |
|
31 } |
|
32 yield undefined; |
|
33 |
|
34 objectStore = db.createObjectStore("autoIncrementKeyPath", |
|
35 { keyPath: "foo", |
|
36 autoIncrement: true }); |
|
37 |
|
38 request = objectStore.openCursor(); |
|
39 request.onerror = errorHandler; |
|
40 request.onsuccess = function (event) { |
|
41 ok(!event.target.result, "No results"); |
|
42 testGenerator.next(); |
|
43 } |
|
44 yield undefined; |
|
45 |
|
46 objectStore = db.createObjectStore("keyPath", { keyPath: "foo" }); |
|
47 |
|
48 request = objectStore.openCursor(); |
|
49 request.onerror = errorHandler; |
|
50 request.onsuccess = function (event) { |
|
51 ok(!event.target.result, "No results"); |
|
52 testGenerator.next(); |
|
53 } |
|
54 yield undefined; |
|
55 |
|
56 objectStore = db.createObjectStore("foo"); |
|
57 |
|
58 request = objectStore.openCursor(); |
|
59 request.onerror = errorHandler; |
|
60 request.onsuccess = function (event) { |
|
61 ok(!event.target.result, "No results"); |
|
62 testGenerator.next(); |
|
63 } |
|
64 yield undefined; |
|
65 |
|
66 let keyIndex = 0; |
|
67 |
|
68 for (let i in keys) { |
|
69 request = objectStore.add("foo", keys[i]); |
|
70 request.onerror = errorHandler; |
|
71 request.onsuccess = function(event) { |
|
72 if (++keyIndex == keys.length) { |
|
73 testGenerator.next(); |
|
74 } |
|
75 }; |
|
76 } |
|
77 yield undefined; |
|
78 |
|
79 keyIndex = 0; |
|
80 |
|
81 request = objectStore.openCursor(); |
|
82 request.onerror = errorHandler; |
|
83 request.onsuccess = function (event) { |
|
84 let cursor = event.target.result; |
|
85 if (cursor) { |
|
86 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
87 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
88 is(cursor.value, "foo", "Correct value"); |
|
89 |
|
90 cursor.continue(); |
|
91 |
|
92 try { |
|
93 cursor.continue(); |
|
94 ok(false, "continue twice should throw"); |
|
95 } |
|
96 catch (e) { |
|
97 ok(e instanceof DOMException, "got a database exception"); |
|
98 is(e.name, "InvalidStateError", "correct error"); |
|
99 is(e.code, DOMException.INVALID_STATE_ERR, "correct code"); |
|
100 } |
|
101 |
|
102 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
103 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
104 is(cursor.value, "foo", "Correct value"); |
|
105 |
|
106 keyIndex++; |
|
107 } |
|
108 else { |
|
109 testGenerator.next(); |
|
110 } |
|
111 } |
|
112 yield undefined; |
|
113 |
|
114 is(keyIndex, keys.length, "Saw all added items"); |
|
115 |
|
116 keyIndex = 4; |
|
117 |
|
118 let range = IDBKeyRange.bound(2000, "q"); |
|
119 request = objectStore.openCursor(range); |
|
120 request.onerror = errorHandler; |
|
121 request.onsuccess = function (event) { |
|
122 let cursor = event.target.result; |
|
123 if (cursor) { |
|
124 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
125 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
126 is(cursor.value, "foo", "Correct value"); |
|
127 |
|
128 cursor.continue(); |
|
129 |
|
130 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
131 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
132 is(cursor.value, "foo", "Correct value"); |
|
133 |
|
134 keyIndex++; |
|
135 } |
|
136 else { |
|
137 testGenerator.next(); |
|
138 } |
|
139 } |
|
140 yield undefined; |
|
141 |
|
142 is(keyIndex, 8, "Saw all the expected keys"); |
|
143 |
|
144 keyIndex = 0; |
|
145 |
|
146 request = objectStore.openCursor(); |
|
147 request.onerror = errorHandler; |
|
148 request.onsuccess = function (event) { |
|
149 let cursor = event.target.result; |
|
150 if (cursor) { |
|
151 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
152 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
153 is(cursor.value, "foo", "Correct value"); |
|
154 |
|
155 if (keyIndex) { |
|
156 cursor.continue(); |
|
157 } |
|
158 else { |
|
159 cursor.continue("b"); |
|
160 } |
|
161 |
|
162 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
163 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
164 is(cursor.value, "foo", "Correct value"); |
|
165 |
|
166 keyIndex += keyIndex ? 1: 6; |
|
167 } |
|
168 else { |
|
169 testGenerator.next(); |
|
170 } |
|
171 } |
|
172 yield undefined; |
|
173 |
|
174 is(keyIndex, keys.length, "Saw all the expected keys"); |
|
175 |
|
176 keyIndex = 0; |
|
177 |
|
178 request = objectStore.openCursor(); |
|
179 request.onerror = errorHandler; |
|
180 request.onsuccess = function (event) { |
|
181 let cursor = event.target.result; |
|
182 if (cursor) { |
|
183 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
184 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
185 is(cursor.value, "foo", "Correct value"); |
|
186 |
|
187 if (keyIndex) { |
|
188 cursor.continue(); |
|
189 } |
|
190 else { |
|
191 cursor.continue(10); |
|
192 } |
|
193 |
|
194 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
195 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
196 is(cursor.value, "foo", "Correct value"); |
|
197 |
|
198 keyIndex += keyIndex ? 1: 3; |
|
199 } |
|
200 else { |
|
201 testGenerator.next(); |
|
202 } |
|
203 } |
|
204 yield undefined; |
|
205 |
|
206 is(keyIndex, keys.length, "Saw all the expected keys"); |
|
207 |
|
208 keyIndex = 0; |
|
209 |
|
210 request = objectStore.openCursor(); |
|
211 request.onerror = errorHandler; |
|
212 request.onsuccess = function (event) { |
|
213 let cursor = event.target.result; |
|
214 if (cursor) { |
|
215 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
216 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
217 is(cursor.value, "foo", "Correct value"); |
|
218 |
|
219 if (keyIndex) { |
|
220 cursor.continue(); |
|
221 } |
|
222 else { |
|
223 cursor.continue("c"); |
|
224 } |
|
225 |
|
226 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
227 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
228 is(cursor.value, "foo", "Correct value"); |
|
229 |
|
230 keyIndex += keyIndex ? 1 : 7; |
|
231 } |
|
232 else { |
|
233 ok(cursor === null, "The request result should be null."); |
|
234 testGenerator.next(); |
|
235 } |
|
236 } |
|
237 yield undefined; |
|
238 |
|
239 is(keyIndex, keys.length, "Saw all the expected keys"); |
|
240 |
|
241 keyIndex = 0; |
|
242 |
|
243 request = objectStore.openCursor(); |
|
244 request.onerror = errorHandler; |
|
245 let storedCursor = null; |
|
246 request.onsuccess = function (event) { |
|
247 let cursor = event.target.result; |
|
248 if (cursor) { |
|
249 storedCursor = cursor; |
|
250 |
|
251 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
252 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
253 is(cursor.value, "foo", "Correct value"); |
|
254 |
|
255 if (keyIndex == 4) { |
|
256 request = cursor.update("bar"); |
|
257 request.onerror = errorHandler; |
|
258 request.onsuccess = function(event) { |
|
259 keyIndex++; |
|
260 cursor.continue(); |
|
261 }; |
|
262 } |
|
263 else { |
|
264 keyIndex++; |
|
265 cursor.continue(); |
|
266 } |
|
267 } |
|
268 else { |
|
269 ok(cursor === null, "The request result should be null."); |
|
270 ok(storedCursor.value === undefined, "The cursor's value should be undefined."); |
|
271 testGenerator.next(); |
|
272 } |
|
273 } |
|
274 yield undefined; |
|
275 |
|
276 is(keyIndex, keys.length, "Saw all the expected keys"); |
|
277 |
|
278 request = objectStore.get(sortedKeys[4]); |
|
279 request.onerror = errorHandler; |
|
280 request.onsuccess = grabEventAndContinueHandler; |
|
281 event = yield undefined; |
|
282 |
|
283 is(event.target.result, "bar", "Update succeeded"); |
|
284 |
|
285 request = objectStore.put("foo", sortedKeys[4]); |
|
286 request.onerror = errorHandler; |
|
287 request.onsuccess = grabEventAndContinueHandler; |
|
288 event = yield undefined; |
|
289 |
|
290 keyIndex = 0; |
|
291 |
|
292 let gotRemoveEvent = false; |
|
293 let retval = false; |
|
294 |
|
295 request = objectStore.openCursor(null, "next"); |
|
296 request.onerror = errorHandler; |
|
297 storedCursor = null; |
|
298 request.onsuccess = function (event) { |
|
299 let cursor = event.target.result; |
|
300 if (cursor) { |
|
301 storedCursor = cursor; |
|
302 |
|
303 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
304 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
305 is(cursor.value, "foo", "Correct value"); |
|
306 |
|
307 if (keyIndex == 4) { |
|
308 request = cursor.delete(); |
|
309 request.onerror = errorHandler; |
|
310 request.onsuccess = function(event) { |
|
311 ok(event.target.result === undefined, "Should be undefined"); |
|
312 is(keyIndex, 5, "Got result of remove before next continue"); |
|
313 gotRemoveEvent = true; |
|
314 }; |
|
315 } |
|
316 |
|
317 keyIndex++; |
|
318 cursor.continue(); |
|
319 } |
|
320 else { |
|
321 ok(cursor === null, "The request result should be null."); |
|
322 ok(storedCursor.value === undefined, "The cursor's value should be undefined."); |
|
323 testGenerator.next(); |
|
324 } |
|
325 } |
|
326 yield undefined; |
|
327 |
|
328 is(keyIndex, keys.length, "Saw all the expected keys"); |
|
329 is(gotRemoveEvent, true, "Saw the remove event"); |
|
330 |
|
331 request = objectStore.get(sortedKeys[4]); |
|
332 request.onerror = errorHandler; |
|
333 request.onsuccess = grabEventAndContinueHandler; |
|
334 event = yield undefined; |
|
335 |
|
336 is(event.target.result, undefined, "Entry was deleted"); |
|
337 |
|
338 request = objectStore.add("foo", sortedKeys[4]); |
|
339 request.onerror = errorHandler; |
|
340 request.onsuccess = grabEventAndContinueHandler; |
|
341 event = yield undefined; |
|
342 |
|
343 keyIndex = sortedKeys.length - 1; |
|
344 |
|
345 request = objectStore.openCursor(null, "prev"); |
|
346 request.onerror = errorHandler; |
|
347 storedCursor = null; |
|
348 request.onsuccess = function (event) { |
|
349 let cursor = event.target.result; |
|
350 if (cursor) { |
|
351 storedCursor = cursor; |
|
352 |
|
353 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
354 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
355 is(cursor.value, "foo", "Correct value"); |
|
356 |
|
357 cursor.continue(); |
|
358 |
|
359 is(cursor.key, sortedKeys[keyIndex], "Correct key"); |
|
360 is(cursor.primaryKey, sortedKeys[keyIndex], "Correct primary key"); |
|
361 is(cursor.value, "foo", "Correct value"); |
|
362 |
|
363 keyIndex--; |
|
364 } |
|
365 else { |
|
366 ok(cursor === null, "The request result should be null."); |
|
367 ok(storedCursor.value === undefined, "The cursor's value should be undefined."); |
|
368 testGenerator.next(); |
|
369 } |
|
370 } |
|
371 yield undefined; |
|
372 |
|
373 is(keyIndex, -1, "Saw all added items"); |
|
374 |
|
375 finishTest(); |
|
376 yield undefined; |
|
377 } |