dom/indexedDB/test/unit/test_cursors.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

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

mercurial