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