|
1 /** |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
4 */ |
|
5 |
|
6 let testGenerator = testSteps(); |
|
7 |
|
8 function testSteps() { |
|
9 const dbName = this.window ? |
|
10 window.location.pathname : |
|
11 "test_objectStore_openKeyCursor"; |
|
12 const dbVersion = 1; |
|
13 const objectStoreName = "foo"; |
|
14 const keyCount = 100; |
|
15 |
|
16 let request = indexedDB.open(dbName, dbVersion); |
|
17 request.onerror = errorHandler; |
|
18 request.onupgradeneeded = grabEventAndContinueHandler; |
|
19 request.onsuccess = unexpectedSuccessHandler; |
|
20 |
|
21 let event = yield undefined; |
|
22 |
|
23 info("Creating database"); |
|
24 |
|
25 let db = event.target.result; |
|
26 let objectStore = db.createObjectStore(objectStoreName); |
|
27 for (let i = 0; i < keyCount; i++) { |
|
28 objectStore.add(true, i); |
|
29 } |
|
30 |
|
31 request.onupgradeneeded = unexpectedSuccessHandler; |
|
32 request.onsuccess = grabEventAndContinueHandler; |
|
33 |
|
34 event = yield undefined; |
|
35 |
|
36 db = event.target.result; |
|
37 objectStore = db.transaction(objectStoreName, "readwrite") |
|
38 .objectStore(objectStoreName); |
|
39 |
|
40 info("Getting all keys"); |
|
41 objectStore.getAllKeys().onsuccess = grabEventAndContinueHandler; |
|
42 event = yield undefined; |
|
43 |
|
44 const allKeys = event.target.result; |
|
45 |
|
46 ok(Array.isArray(allKeys), "Got an array result"); |
|
47 is(allKeys.length, keyCount, "Got correct array length"); |
|
48 |
|
49 info("Opening normal key cursor"); |
|
50 |
|
51 let seenKeys = []; |
|
52 objectStore.openKeyCursor().onsuccess = event => { |
|
53 let cursor = event.target.result; |
|
54 if (!cursor) { |
|
55 continueToNextStepSync(); |
|
56 return; |
|
57 } |
|
58 |
|
59 is(cursor.source, objectStore, "Correct source"); |
|
60 is(cursor.direction, "next", "Correct direction"); |
|
61 |
|
62 let exception = null; |
|
63 try { |
|
64 cursor.update(10); |
|
65 } catch(e) { |
|
66 exception = e; |
|
67 } |
|
68 ok(!!exception, "update() throws for key cursor"); |
|
69 |
|
70 exception = null; |
|
71 try { |
|
72 cursor.delete(); |
|
73 } catch(e) { |
|
74 exception = e; |
|
75 } |
|
76 ok(!!exception, "delete() throws for key cursor"); |
|
77 |
|
78 is(cursor.key, cursor.primaryKey, "key and primaryKey match"); |
|
79 ok(!("value" in cursor), "No 'value' property on key cursor"); |
|
80 |
|
81 seenKeys.push(cursor.key); |
|
82 cursor.continue(); |
|
83 }; |
|
84 yield undefined; |
|
85 |
|
86 is(seenKeys.length, allKeys.length, "Saw the right number of keys"); |
|
87 |
|
88 let match = true; |
|
89 for (let i = 0; i < seenKeys.length; i++) { |
|
90 if (seenKeys[i] !== allKeys[i]) { |
|
91 match = false; |
|
92 break; |
|
93 } |
|
94 } |
|
95 ok(match, "All keys matched"); |
|
96 |
|
97 info("Opening key cursor with keyRange"); |
|
98 |
|
99 let keyRange = IDBKeyRange.bound(10, 20, false, true); |
|
100 |
|
101 seenKeys = []; |
|
102 objectStore.openKeyCursor(keyRange).onsuccess = event => { |
|
103 let cursor = event.target.result; |
|
104 if (!cursor) { |
|
105 continueToNextStepSync(); |
|
106 return; |
|
107 } |
|
108 |
|
109 is(cursor.source, objectStore, "Correct source"); |
|
110 is(cursor.direction, "next", "Correct direction"); |
|
111 |
|
112 let exception = null; |
|
113 try { |
|
114 cursor.update(10); |
|
115 } catch(e) { |
|
116 exception = e; |
|
117 } |
|
118 ok(!!exception, "update() throws for key cursor"); |
|
119 |
|
120 exception = null; |
|
121 try { |
|
122 cursor.delete(); |
|
123 } catch(e) { |
|
124 exception = e; |
|
125 } |
|
126 ok(!!exception, "delete() throws for key cursor"); |
|
127 |
|
128 is(cursor.key, cursor.primaryKey, "key and primaryKey match"); |
|
129 ok(!("value" in cursor), "No 'value' property on key cursor"); |
|
130 |
|
131 seenKeys.push(cursor.key); |
|
132 cursor.continue(); |
|
133 }; |
|
134 yield undefined; |
|
135 |
|
136 is(seenKeys.length, 10, "Saw the right number of keys"); |
|
137 |
|
138 match = true; |
|
139 for (let i = 0; i < seenKeys.length; i++) { |
|
140 if (seenKeys[i] !== allKeys[i + 10]) { |
|
141 match = false; |
|
142 break; |
|
143 } |
|
144 } |
|
145 ok(match, "All keys matched"); |
|
146 |
|
147 info("Opening key cursor with unmatched keyRange"); |
|
148 |
|
149 keyRange = IDBKeyRange.bound(10000, 200000); |
|
150 |
|
151 seenKeys = []; |
|
152 objectStore.openKeyCursor(keyRange).onsuccess = event => { |
|
153 let cursor = event.target.result; |
|
154 if (!cursor) { |
|
155 continueToNextStepSync(); |
|
156 return; |
|
157 } |
|
158 |
|
159 ok(false, "Shouldn't have any keys here"); |
|
160 cursor.continue(); |
|
161 }; |
|
162 yield undefined; |
|
163 |
|
164 is(seenKeys.length, 0, "Saw the right number of keys"); |
|
165 |
|
166 info("Opening reverse key cursor"); |
|
167 |
|
168 seenKeys = []; |
|
169 objectStore.openKeyCursor(null, "prev").onsuccess = event => { |
|
170 let cursor = event.target.result; |
|
171 if (!cursor) { |
|
172 continueToNextStepSync(); |
|
173 return; |
|
174 } |
|
175 |
|
176 is(cursor.source, objectStore, "Correct source"); |
|
177 is(cursor.direction, "prev", "Correct direction"); |
|
178 |
|
179 let exception = null; |
|
180 try { |
|
181 cursor.update(10); |
|
182 } catch(e) { |
|
183 exception = e; |
|
184 } |
|
185 ok(!!exception, "update() throws for key cursor"); |
|
186 |
|
187 exception = null; |
|
188 try { |
|
189 cursor.delete(); |
|
190 } catch(e) { |
|
191 exception = e; |
|
192 } |
|
193 ok(!!exception, "delete() throws for key cursor"); |
|
194 |
|
195 is(cursor.key, cursor.primaryKey, "key and primaryKey match"); |
|
196 ok(!("value" in cursor), "No 'value' property on key cursor"); |
|
197 |
|
198 seenKeys.push(cursor.key); |
|
199 cursor.continue(); |
|
200 }; |
|
201 yield undefined; |
|
202 |
|
203 is(seenKeys.length, allKeys.length, "Saw the right number of keys"); |
|
204 |
|
205 seenKeys.reverse(); |
|
206 |
|
207 match = true; |
|
208 for (let i = 0; i < seenKeys.length; i++) { |
|
209 if (seenKeys[i] !== allKeys[i]) { |
|
210 match = false; |
|
211 break; |
|
212 } |
|
213 } |
|
214 ok(match, "All keys matched"); |
|
215 |
|
216 info("Opening reverse key cursor with key range"); |
|
217 |
|
218 keyRange = IDBKeyRange.bound(10, 20, false, true); |
|
219 |
|
220 seenKeys = []; |
|
221 objectStore.openKeyCursor(keyRange, "prev").onsuccess = event => { |
|
222 let cursor = event.target.result; |
|
223 if (!cursor) { |
|
224 continueToNextStepSync(); |
|
225 return; |
|
226 } |
|
227 |
|
228 is(cursor.source, objectStore, "Correct source"); |
|
229 is(cursor.direction, "prev", "Correct direction"); |
|
230 |
|
231 let exception = null; |
|
232 try { |
|
233 cursor.update(10); |
|
234 } catch(e) { |
|
235 exception = e; |
|
236 } |
|
237 ok(!!exception, "update() throws for key cursor"); |
|
238 |
|
239 exception = null; |
|
240 try { |
|
241 cursor.delete(); |
|
242 } catch(e) { |
|
243 exception = e; |
|
244 } |
|
245 ok(!!exception, "delete() throws for key cursor"); |
|
246 |
|
247 is(cursor.key, cursor.primaryKey, "key and primaryKey match"); |
|
248 ok(!("value" in cursor), "No 'value' property on key cursor"); |
|
249 |
|
250 seenKeys.push(cursor.key); |
|
251 cursor.continue(); |
|
252 }; |
|
253 yield undefined; |
|
254 |
|
255 is(seenKeys.length, 10, "Saw the right number of keys"); |
|
256 |
|
257 seenKeys.reverse(); |
|
258 |
|
259 match = true; |
|
260 for (let i = 0; i < 10; i++) { |
|
261 if (seenKeys[i] !== allKeys[i + 10]) { |
|
262 match = false; |
|
263 break; |
|
264 } |
|
265 } |
|
266 ok(match, "All keys matched"); |
|
267 |
|
268 info("Opening reverse key cursor with unmatched key range"); |
|
269 |
|
270 keyRange = IDBKeyRange.bound(10000, 200000); |
|
271 |
|
272 seenKeys = []; |
|
273 objectStore.openKeyCursor(keyRange, "prev").onsuccess = event => { |
|
274 let cursor = event.target.result; |
|
275 if (!cursor) { |
|
276 continueToNextStepSync(); |
|
277 return; |
|
278 } |
|
279 |
|
280 ok(false, "Shouldn't have any keys here"); |
|
281 cursor.continue(); |
|
282 }; |
|
283 yield undefined; |
|
284 |
|
285 is(seenKeys.length, 0, "Saw the right number of keys"); |
|
286 |
|
287 info("Opening key cursor with advance"); |
|
288 |
|
289 seenKeys = []; |
|
290 objectStore.openKeyCursor().onsuccess = event => { |
|
291 let cursor = event.target.result; |
|
292 if (!cursor) { |
|
293 continueToNextStepSync(); |
|
294 return; |
|
295 } |
|
296 |
|
297 is(cursor.source, objectStore, "Correct source"); |
|
298 is(cursor.direction, "next", "Correct direction"); |
|
299 |
|
300 let exception = null; |
|
301 try { |
|
302 cursor.update(10); |
|
303 } catch(e) { |
|
304 exception = e; |
|
305 } |
|
306 ok(!!exception, "update() throws for key cursor"); |
|
307 |
|
308 exception = null; |
|
309 try { |
|
310 cursor.delete(); |
|
311 } catch(e) { |
|
312 exception = e; |
|
313 } |
|
314 ok(!!exception, "delete() throws for key cursor"); |
|
315 |
|
316 is(cursor.key, cursor.primaryKey, "key and primaryKey match"); |
|
317 ok(!("value" in cursor), "No 'value' property on key cursor"); |
|
318 |
|
319 seenKeys.push(cursor.key); |
|
320 if (seenKeys.length == 1) { |
|
321 cursor.advance(10); |
|
322 } else { |
|
323 cursor.continue(); |
|
324 } |
|
325 }; |
|
326 yield undefined; |
|
327 |
|
328 is(seenKeys.length, allKeys.length - 9, "Saw the right number of keys"); |
|
329 |
|
330 let match = true; |
|
331 for (let i = 0, j = 0; i < seenKeys.length; i++) { |
|
332 if (seenKeys[i] !== allKeys[i + j]) { |
|
333 match = false; |
|
334 break; |
|
335 } |
|
336 if (i == 0) { |
|
337 j = 9; |
|
338 } |
|
339 } |
|
340 ok(match, "All keys matched"); |
|
341 |
|
342 info("Opening key cursor with continue-to-key"); |
|
343 |
|
344 seenKeys = []; |
|
345 objectStore.openKeyCursor().onsuccess = event => { |
|
346 let cursor = event.target.result; |
|
347 if (!cursor) { |
|
348 continueToNextStepSync(); |
|
349 return; |
|
350 } |
|
351 |
|
352 is(cursor.source, objectStore, "Correct source"); |
|
353 is(cursor.direction, "next", "Correct direction"); |
|
354 |
|
355 let exception = null; |
|
356 try { |
|
357 cursor.update(10); |
|
358 } catch(e) { |
|
359 exception = e; |
|
360 } |
|
361 ok(!!exception, "update() throws for key cursor"); |
|
362 |
|
363 exception = null; |
|
364 try { |
|
365 cursor.delete(); |
|
366 } catch(e) { |
|
367 exception = e; |
|
368 } |
|
369 ok(!!exception, "delete() throws for key cursor"); |
|
370 |
|
371 is(cursor.key, cursor.primaryKey, "key and primaryKey match"); |
|
372 ok(!("value" in cursor), "No 'value' property on key cursor"); |
|
373 |
|
374 seenKeys.push(cursor.key); |
|
375 |
|
376 if (seenKeys.length == 1) { |
|
377 cursor.continue(10); |
|
378 } else { |
|
379 cursor.continue(); |
|
380 } |
|
381 }; |
|
382 yield undefined; |
|
383 |
|
384 is(seenKeys.length, allKeys.length - 9, "Saw the right number of keys"); |
|
385 |
|
386 let match = true; |
|
387 for (let i = 0, j = 0; i < seenKeys.length; i++) { |
|
388 if (seenKeys[i] !== allKeys[i + j]) { |
|
389 match = false; |
|
390 break; |
|
391 } |
|
392 if (i == 0) { |
|
393 j = 9; |
|
394 } |
|
395 } |
|
396 ok(match, "All keys matched"); |
|
397 |
|
398 finishTest(); |
|
399 yield undefined; |
|
400 } |