1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/indexedDB/test/unit/test_objectStore_openKeyCursor.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,400 @@ 1.4 +/** 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + */ 1.8 + 1.9 +let testGenerator = testSteps(); 1.10 + 1.11 +function testSteps() { 1.12 + const dbName = this.window ? 1.13 + window.location.pathname : 1.14 + "test_objectStore_openKeyCursor"; 1.15 + const dbVersion = 1; 1.16 + const objectStoreName = "foo"; 1.17 + const keyCount = 100; 1.18 + 1.19 + let request = indexedDB.open(dbName, dbVersion); 1.20 + request.onerror = errorHandler; 1.21 + request.onupgradeneeded = grabEventAndContinueHandler; 1.22 + request.onsuccess = unexpectedSuccessHandler; 1.23 + 1.24 + let event = yield undefined; 1.25 + 1.26 + info("Creating database"); 1.27 + 1.28 + let db = event.target.result; 1.29 + let objectStore = db.createObjectStore(objectStoreName); 1.30 + for (let i = 0; i < keyCount; i++) { 1.31 + objectStore.add(true, i); 1.32 + } 1.33 + 1.34 + request.onupgradeneeded = unexpectedSuccessHandler; 1.35 + request.onsuccess = grabEventAndContinueHandler; 1.36 + 1.37 + event = yield undefined; 1.38 + 1.39 + db = event.target.result; 1.40 + objectStore = db.transaction(objectStoreName, "readwrite") 1.41 + .objectStore(objectStoreName); 1.42 + 1.43 + info("Getting all keys"); 1.44 + objectStore.getAllKeys().onsuccess = grabEventAndContinueHandler; 1.45 + event = yield undefined; 1.46 + 1.47 + const allKeys = event.target.result; 1.48 + 1.49 + ok(Array.isArray(allKeys), "Got an array result"); 1.50 + is(allKeys.length, keyCount, "Got correct array length"); 1.51 + 1.52 + info("Opening normal key cursor"); 1.53 + 1.54 + let seenKeys = []; 1.55 + objectStore.openKeyCursor().onsuccess = event => { 1.56 + let cursor = event.target.result; 1.57 + if (!cursor) { 1.58 + continueToNextStepSync(); 1.59 + return; 1.60 + } 1.61 + 1.62 + is(cursor.source, objectStore, "Correct source"); 1.63 + is(cursor.direction, "next", "Correct direction"); 1.64 + 1.65 + let exception = null; 1.66 + try { 1.67 + cursor.update(10); 1.68 + } catch(e) { 1.69 + exception = e; 1.70 + } 1.71 + ok(!!exception, "update() throws for key cursor"); 1.72 + 1.73 + exception = null; 1.74 + try { 1.75 + cursor.delete(); 1.76 + } catch(e) { 1.77 + exception = e; 1.78 + } 1.79 + ok(!!exception, "delete() throws for key cursor"); 1.80 + 1.81 + is(cursor.key, cursor.primaryKey, "key and primaryKey match"); 1.82 + ok(!("value" in cursor), "No 'value' property on key cursor"); 1.83 + 1.84 + seenKeys.push(cursor.key); 1.85 + cursor.continue(); 1.86 + }; 1.87 + yield undefined; 1.88 + 1.89 + is(seenKeys.length, allKeys.length, "Saw the right number of keys"); 1.90 + 1.91 + let match = true; 1.92 + for (let i = 0; i < seenKeys.length; i++) { 1.93 + if (seenKeys[i] !== allKeys[i]) { 1.94 + match = false; 1.95 + break; 1.96 + } 1.97 + } 1.98 + ok(match, "All keys matched"); 1.99 + 1.100 + info("Opening key cursor with keyRange"); 1.101 + 1.102 + let keyRange = IDBKeyRange.bound(10, 20, false, true); 1.103 + 1.104 + seenKeys = []; 1.105 + objectStore.openKeyCursor(keyRange).onsuccess = event => { 1.106 + let cursor = event.target.result; 1.107 + if (!cursor) { 1.108 + continueToNextStepSync(); 1.109 + return; 1.110 + } 1.111 + 1.112 + is(cursor.source, objectStore, "Correct source"); 1.113 + is(cursor.direction, "next", "Correct direction"); 1.114 + 1.115 + let exception = null; 1.116 + try { 1.117 + cursor.update(10); 1.118 + } catch(e) { 1.119 + exception = e; 1.120 + } 1.121 + ok(!!exception, "update() throws for key cursor"); 1.122 + 1.123 + exception = null; 1.124 + try { 1.125 + cursor.delete(); 1.126 + } catch(e) { 1.127 + exception = e; 1.128 + } 1.129 + ok(!!exception, "delete() throws for key cursor"); 1.130 + 1.131 + is(cursor.key, cursor.primaryKey, "key and primaryKey match"); 1.132 + ok(!("value" in cursor), "No 'value' property on key cursor"); 1.133 + 1.134 + seenKeys.push(cursor.key); 1.135 + cursor.continue(); 1.136 + }; 1.137 + yield undefined; 1.138 + 1.139 + is(seenKeys.length, 10, "Saw the right number of keys"); 1.140 + 1.141 + match = true; 1.142 + for (let i = 0; i < seenKeys.length; i++) { 1.143 + if (seenKeys[i] !== allKeys[i + 10]) { 1.144 + match = false; 1.145 + break; 1.146 + } 1.147 + } 1.148 + ok(match, "All keys matched"); 1.149 + 1.150 + info("Opening key cursor with unmatched keyRange"); 1.151 + 1.152 + keyRange = IDBKeyRange.bound(10000, 200000); 1.153 + 1.154 + seenKeys = []; 1.155 + objectStore.openKeyCursor(keyRange).onsuccess = event => { 1.156 + let cursor = event.target.result; 1.157 + if (!cursor) { 1.158 + continueToNextStepSync(); 1.159 + return; 1.160 + } 1.161 + 1.162 + ok(false, "Shouldn't have any keys here"); 1.163 + cursor.continue(); 1.164 + }; 1.165 + yield undefined; 1.166 + 1.167 + is(seenKeys.length, 0, "Saw the right number of keys"); 1.168 + 1.169 + info("Opening reverse key cursor"); 1.170 + 1.171 + seenKeys = []; 1.172 + objectStore.openKeyCursor(null, "prev").onsuccess = event => { 1.173 + let cursor = event.target.result; 1.174 + if (!cursor) { 1.175 + continueToNextStepSync(); 1.176 + return; 1.177 + } 1.178 + 1.179 + is(cursor.source, objectStore, "Correct source"); 1.180 + is(cursor.direction, "prev", "Correct direction"); 1.181 + 1.182 + let exception = null; 1.183 + try { 1.184 + cursor.update(10); 1.185 + } catch(e) { 1.186 + exception = e; 1.187 + } 1.188 + ok(!!exception, "update() throws for key cursor"); 1.189 + 1.190 + exception = null; 1.191 + try { 1.192 + cursor.delete(); 1.193 + } catch(e) { 1.194 + exception = e; 1.195 + } 1.196 + ok(!!exception, "delete() throws for key cursor"); 1.197 + 1.198 + is(cursor.key, cursor.primaryKey, "key and primaryKey match"); 1.199 + ok(!("value" in cursor), "No 'value' property on key cursor"); 1.200 + 1.201 + seenKeys.push(cursor.key); 1.202 + cursor.continue(); 1.203 + }; 1.204 + yield undefined; 1.205 + 1.206 + is(seenKeys.length, allKeys.length, "Saw the right number of keys"); 1.207 + 1.208 + seenKeys.reverse(); 1.209 + 1.210 + match = true; 1.211 + for (let i = 0; i < seenKeys.length; i++) { 1.212 + if (seenKeys[i] !== allKeys[i]) { 1.213 + match = false; 1.214 + break; 1.215 + } 1.216 + } 1.217 + ok(match, "All keys matched"); 1.218 + 1.219 + info("Opening reverse key cursor with key range"); 1.220 + 1.221 + keyRange = IDBKeyRange.bound(10, 20, false, true); 1.222 + 1.223 + seenKeys = []; 1.224 + objectStore.openKeyCursor(keyRange, "prev").onsuccess = event => { 1.225 + let cursor = event.target.result; 1.226 + if (!cursor) { 1.227 + continueToNextStepSync(); 1.228 + return; 1.229 + } 1.230 + 1.231 + is(cursor.source, objectStore, "Correct source"); 1.232 + is(cursor.direction, "prev", "Correct direction"); 1.233 + 1.234 + let exception = null; 1.235 + try { 1.236 + cursor.update(10); 1.237 + } catch(e) { 1.238 + exception = e; 1.239 + } 1.240 + ok(!!exception, "update() throws for key cursor"); 1.241 + 1.242 + exception = null; 1.243 + try { 1.244 + cursor.delete(); 1.245 + } catch(e) { 1.246 + exception = e; 1.247 + } 1.248 + ok(!!exception, "delete() throws for key cursor"); 1.249 + 1.250 + is(cursor.key, cursor.primaryKey, "key and primaryKey match"); 1.251 + ok(!("value" in cursor), "No 'value' property on key cursor"); 1.252 + 1.253 + seenKeys.push(cursor.key); 1.254 + cursor.continue(); 1.255 + }; 1.256 + yield undefined; 1.257 + 1.258 + is(seenKeys.length, 10, "Saw the right number of keys"); 1.259 + 1.260 + seenKeys.reverse(); 1.261 + 1.262 + match = true; 1.263 + for (let i = 0; i < 10; i++) { 1.264 + if (seenKeys[i] !== allKeys[i + 10]) { 1.265 + match = false; 1.266 + break; 1.267 + } 1.268 + } 1.269 + ok(match, "All keys matched"); 1.270 + 1.271 + info("Opening reverse key cursor with unmatched key range"); 1.272 + 1.273 + keyRange = IDBKeyRange.bound(10000, 200000); 1.274 + 1.275 + seenKeys = []; 1.276 + objectStore.openKeyCursor(keyRange, "prev").onsuccess = event => { 1.277 + let cursor = event.target.result; 1.278 + if (!cursor) { 1.279 + continueToNextStepSync(); 1.280 + return; 1.281 + } 1.282 + 1.283 + ok(false, "Shouldn't have any keys here"); 1.284 + cursor.continue(); 1.285 + }; 1.286 + yield undefined; 1.287 + 1.288 + is(seenKeys.length, 0, "Saw the right number of keys"); 1.289 + 1.290 + info("Opening key cursor with advance"); 1.291 + 1.292 + seenKeys = []; 1.293 + objectStore.openKeyCursor().onsuccess = event => { 1.294 + let cursor = event.target.result; 1.295 + if (!cursor) { 1.296 + continueToNextStepSync(); 1.297 + return; 1.298 + } 1.299 + 1.300 + is(cursor.source, objectStore, "Correct source"); 1.301 + is(cursor.direction, "next", "Correct direction"); 1.302 + 1.303 + let exception = null; 1.304 + try { 1.305 + cursor.update(10); 1.306 + } catch(e) { 1.307 + exception = e; 1.308 + } 1.309 + ok(!!exception, "update() throws for key cursor"); 1.310 + 1.311 + exception = null; 1.312 + try { 1.313 + cursor.delete(); 1.314 + } catch(e) { 1.315 + exception = e; 1.316 + } 1.317 + ok(!!exception, "delete() throws for key cursor"); 1.318 + 1.319 + is(cursor.key, cursor.primaryKey, "key and primaryKey match"); 1.320 + ok(!("value" in cursor), "No 'value' property on key cursor"); 1.321 + 1.322 + seenKeys.push(cursor.key); 1.323 + if (seenKeys.length == 1) { 1.324 + cursor.advance(10); 1.325 + } else { 1.326 + cursor.continue(); 1.327 + } 1.328 + }; 1.329 + yield undefined; 1.330 + 1.331 + is(seenKeys.length, allKeys.length - 9, "Saw the right number of keys"); 1.332 + 1.333 + let match = true; 1.334 + for (let i = 0, j = 0; i < seenKeys.length; i++) { 1.335 + if (seenKeys[i] !== allKeys[i + j]) { 1.336 + match = false; 1.337 + break; 1.338 + } 1.339 + if (i == 0) { 1.340 + j = 9; 1.341 + } 1.342 + } 1.343 + ok(match, "All keys matched"); 1.344 + 1.345 + info("Opening key cursor with continue-to-key"); 1.346 + 1.347 + seenKeys = []; 1.348 + objectStore.openKeyCursor().onsuccess = event => { 1.349 + let cursor = event.target.result; 1.350 + if (!cursor) { 1.351 + continueToNextStepSync(); 1.352 + return; 1.353 + } 1.354 + 1.355 + is(cursor.source, objectStore, "Correct source"); 1.356 + is(cursor.direction, "next", "Correct direction"); 1.357 + 1.358 + let exception = null; 1.359 + try { 1.360 + cursor.update(10); 1.361 + } catch(e) { 1.362 + exception = e; 1.363 + } 1.364 + ok(!!exception, "update() throws for key cursor"); 1.365 + 1.366 + exception = null; 1.367 + try { 1.368 + cursor.delete(); 1.369 + } catch(e) { 1.370 + exception = e; 1.371 + } 1.372 + ok(!!exception, "delete() throws for key cursor"); 1.373 + 1.374 + is(cursor.key, cursor.primaryKey, "key and primaryKey match"); 1.375 + ok(!("value" in cursor), "No 'value' property on key cursor"); 1.376 + 1.377 + seenKeys.push(cursor.key); 1.378 + 1.379 + if (seenKeys.length == 1) { 1.380 + cursor.continue(10); 1.381 + } else { 1.382 + cursor.continue(); 1.383 + } 1.384 + }; 1.385 + yield undefined; 1.386 + 1.387 + is(seenKeys.length, allKeys.length - 9, "Saw the right number of keys"); 1.388 + 1.389 + let match = true; 1.390 + for (let i = 0, j = 0; i < seenKeys.length; i++) { 1.391 + if (seenKeys[i] !== allKeys[i + j]) { 1.392 + match = false; 1.393 + break; 1.394 + } 1.395 + if (i == 0) { 1.396 + j = 9; 1.397 + } 1.398 + } 1.399 + ok(match, "All keys matched"); 1.400 + 1.401 + finishTest(); 1.402 + yield undefined; 1.403 +}