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