dom/indexedDB/test/unit/test_lowDiskSpace.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 "use strict";
michael@0 6
michael@0 7 var self = this;
michael@0 8
michael@0 9 var testGenerator = testSteps();
michael@0 10
michael@0 11 function testSteps()
michael@0 12 {
michael@0 13 const dbName = self.window ? window.location.pathname : "test_lowDiskSpace";
michael@0 14 const dbVersion = 1;
michael@0 15
michael@0 16 const objectStoreName = "foo";
michael@0 17 const objectStoreOptions = { keyPath: "foo" };
michael@0 18
michael@0 19 const indexName = "bar";
michael@0 20 const indexOptions = { unique: true };
michael@0 21
michael@0 22 const dbData = [
michael@0 23 { foo: 0, bar: 0 },
michael@0 24 { foo: 1, bar: 10 },
michael@0 25 { foo: 2, bar: 20 },
michael@0 26 { foo: 3, bar: 30 },
michael@0 27 { foo: 4, bar: 40 },
michael@0 28 { foo: 5, bar: 50 },
michael@0 29 { foo: 6, bar: 60 },
michael@0 30 { foo: 7, bar: 70 },
michael@0 31 { foo: 8, bar: 80 },
michael@0 32 { foo: 9, bar: 90 }
michael@0 33 ];
michael@0 34
michael@0 35 let lowDiskMode = false;
michael@0 36 function setLowDiskMode(val) {
michael@0 37 let data = val ? "full" : "free";
michael@0 38
michael@0 39 if (val == lowDiskMode) {
michael@0 40 info("Low disk mode is: " + data);
michael@0 41 }
michael@0 42 else {
michael@0 43 info("Changing low disk mode to: " + data);
michael@0 44 SpecialPowers.notifyObserversInParentProcess(null, "disk-space-watcher",
michael@0 45 data);
michael@0 46 lowDiskMode = val;
michael@0 47 }
michael@0 48 }
michael@0 49
michael@0 50 { // Make sure opening works from the beginning.
michael@0 51 info("Test 1");
michael@0 52
michael@0 53 setLowDiskMode(false);
michael@0 54
michael@0 55 let request = indexedDB.open(dbName, dbVersion);
michael@0 56 request.onerror = errorHandler;
michael@0 57 request.onsuccess = grabEventAndContinueHandler;
michael@0 58 let event = yield undefined;
michael@0 59
michael@0 60 is(event.type, "success", "Opened database without setting low disk mode");
michael@0 61
michael@0 62 let db = event.target.result;
michael@0 63 db.close();
michael@0 64 }
michael@0 65
michael@0 66 { // Make sure delete works in low disk mode.
michael@0 67 info("Test 2");
michael@0 68
michael@0 69 setLowDiskMode(true);
michael@0 70
michael@0 71 let request = indexedDB.deleteDatabase(dbName);
michael@0 72 request.onerror = errorHandler;
michael@0 73 request.onsuccess = grabEventAndContinueHandler;
michael@0 74 let event = yield undefined;
michael@0 75
michael@0 76 is(event.type, "success", "Deleted database after setting low disk mode");
michael@0 77 }
michael@0 78
michael@0 79 { // Make sure creating a db in low disk mode fails.
michael@0 80 info("Test 3");
michael@0 81
michael@0 82 setLowDiskMode(true);
michael@0 83
michael@0 84 let request = indexedDB.open(dbName, dbVersion);
michael@0 85 request.onerror = expectedErrorHandler("QuotaExceededError");
michael@0 86 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 87 request.onsuccess = unexpectedSuccessHandler;
michael@0 88 let event = yield undefined;
michael@0 89
michael@0 90 is(event.type, "error", "Didn't create new database in low disk mode");
michael@0 91 }
michael@0 92
michael@0 93 { // Make sure opening an already-existing db in low disk mode succeeds.
michael@0 94 info("Test 4");
michael@0 95
michael@0 96 setLowDiskMode(false);
michael@0 97
michael@0 98 let request = indexedDB.open(dbName, dbVersion);
michael@0 99 request.onerror = errorHandler;
michael@0 100 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 101 request.onsuccess = unexpectedSuccessHandler;
michael@0 102 let event = yield undefined;
michael@0 103
michael@0 104 is(event.type, "upgradeneeded", "Upgrading database");
michael@0 105
michael@0 106 let db = event.target.result;
michael@0 107 db.onerror = errorHandler;
michael@0 108
michael@0 109 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 110 request.onsuccess = grabEventAndContinueHandler;
michael@0 111 event = yield undefined;
michael@0 112
michael@0 113 is(event.type, "success", "Created database");
michael@0 114 ok(event.target.result === db, "Got the same database");
michael@0 115
michael@0 116 db.close();
michael@0 117
michael@0 118 setLowDiskMode(true);
michael@0 119
michael@0 120 request = indexedDB.open(dbName);
michael@0 121 request.onerror = errorHandler;
michael@0 122 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 123 request.onsuccess = grabEventAndContinueHandler;
michael@0 124 event = yield undefined;
michael@0 125
michael@0 126 is(event.type, "success", "Opened existing database in low disk mode");
michael@0 127
michael@0 128 db = event.target.result;
michael@0 129 db.close();
michael@0 130 }
michael@0 131
michael@0 132 { // Make sure upgrading an already-existing db in low disk mode succeeds.
michael@0 133 info("Test 5");
michael@0 134
michael@0 135 setLowDiskMode(true);
michael@0 136
michael@0 137 let request = indexedDB.open(dbName, dbVersion + 1);
michael@0 138 request.onerror = errorHandler;
michael@0 139 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 140 request.onsuccess = unexpectedSuccessHandler;
michael@0 141
michael@0 142 let event = yield undefined;
michael@0 143
michael@0 144 is(event.type, "upgradeneeded", "Upgrading database");
michael@0 145
michael@0 146 let db = event.target.result;
michael@0 147 db.onerror = errorHandler;
michael@0 148
michael@0 149 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 150 request.onsuccess = grabEventAndContinueHandler;
michael@0 151 event = yield undefined;
michael@0 152
michael@0 153 is(event.type, "success", "Created database");
michael@0 154 ok(event.target.result === db, "Got the same database");
michael@0 155
michael@0 156 db.close();
michael@0 157 }
michael@0 158
michael@0 159 { // Make sure creating objectStores in low disk mode fails.
michael@0 160 info("Test 6");
michael@0 161
michael@0 162 setLowDiskMode(true);
michael@0 163
michael@0 164 let request = indexedDB.open(dbName, dbVersion + 2);
michael@0 165 request.onerror = expectedErrorHandler("QuotaExceededError");
michael@0 166 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 167 request.onsuccess = unexpectedSuccessHandler;
michael@0 168
michael@0 169 let event = yield undefined;
michael@0 170
michael@0 171 is(event.type, "upgradeneeded", "Upgrading database");
michael@0 172
michael@0 173 let db = event.target.result;
michael@0 174 db.onerror = errorHandler;
michael@0 175
michael@0 176 let objectStore = db.createObjectStore(objectStoreName, objectStoreOptions);
michael@0 177
michael@0 178 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 179 event = yield undefined;
michael@0 180
michael@0 181 is(event.type, "error", "Failed database upgrade");
michael@0 182 }
michael@0 183
michael@0 184 { // Make sure creating indexes in low disk mode fails.
michael@0 185 info("Test 7");
michael@0 186
michael@0 187 setLowDiskMode(false);
michael@0 188
michael@0 189 let request = indexedDB.open(dbName, dbVersion + 2);
michael@0 190 request.onerror = errorHandler;
michael@0 191 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 192 request.onsuccess = unexpectedSuccessHandler;
michael@0 193
michael@0 194 let event = yield undefined;
michael@0 195
michael@0 196 is(event.type, "upgradeneeded", "Upgrading database");
michael@0 197
michael@0 198 let db = event.target.result;
michael@0 199 db.onerror = errorHandler;
michael@0 200
michael@0 201 let objectStore = db.createObjectStore(objectStoreName, objectStoreOptions);
michael@0 202
michael@0 203 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 204 request.onsuccess = grabEventAndContinueHandler;
michael@0 205 event = yield undefined;
michael@0 206
michael@0 207 is(event.type, "success", "Upgraded database");
michael@0 208 ok(event.target.result === db, "Got the same database");
michael@0 209
michael@0 210 db.close();
michael@0 211
michael@0 212 setLowDiskMode(true);
michael@0 213
michael@0 214 request = indexedDB.open(dbName, dbVersion + 3);
michael@0 215 request.onerror = expectedErrorHandler("QuotaExceededError");
michael@0 216 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 217 request.onsuccess = unexpectedSuccessHandler;
michael@0 218 event = yield undefined;
michael@0 219
michael@0 220 is(event.type, "upgradeneeded", "Upgrading database");
michael@0 221
michael@0 222 db = event.target.result;
michael@0 223 db.onerror = errorHandler;
michael@0 224
michael@0 225 objectStore = event.target.transaction.objectStore(objectStoreName);
michael@0 226 let index = objectStore.createIndex(indexName, indexName, indexOptions);
michael@0 227
michael@0 228 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 229 event = yield undefined;
michael@0 230
michael@0 231 is(event.type, "error", "Failed database upgrade");
michael@0 232 }
michael@0 233
michael@0 234 { // Make sure deleting indexes in low disk mode succeeds.
michael@0 235 info("Test 8");
michael@0 236
michael@0 237 setLowDiskMode(false);
michael@0 238
michael@0 239 let request = indexedDB.open(dbName, dbVersion + 3);
michael@0 240 request.onerror = errorHandler;
michael@0 241 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 242 request.onsuccess = unexpectedSuccessHandler;
michael@0 243
michael@0 244 let event = yield undefined;
michael@0 245
michael@0 246 is(event.type, "upgradeneeded", "Upgrading database");
michael@0 247
michael@0 248 let db = event.target.result;
michael@0 249 db.onerror = errorHandler;
michael@0 250
michael@0 251 let objectStore = event.target.transaction.objectStore(objectStoreName);
michael@0 252 let index = objectStore.createIndex(indexName, indexName, indexOptions);
michael@0 253
michael@0 254 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 255 request.onsuccess = grabEventAndContinueHandler;
michael@0 256 event = yield undefined;
michael@0 257
michael@0 258 is(event.type, "success", "Upgraded database");
michael@0 259 ok(event.target.result === db, "Got the same database");
michael@0 260
michael@0 261 db.close();
michael@0 262
michael@0 263 setLowDiskMode(true);
michael@0 264
michael@0 265 request = indexedDB.open(dbName, dbVersion + 4);
michael@0 266 request.onerror = errorHandler;
michael@0 267 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 268 request.onsuccess = unexpectedSuccessHandler;
michael@0 269 event = yield undefined;
michael@0 270
michael@0 271 is(event.type, "upgradeneeded", "Upgrading database");
michael@0 272
michael@0 273 db = event.target.result;
michael@0 274 db.onerror = errorHandler;
michael@0 275
michael@0 276 objectStore = event.target.transaction.objectStore(objectStoreName);
michael@0 277 objectStore.deleteIndex(indexName);
michael@0 278
michael@0 279 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 280 request.onsuccess = grabEventAndContinueHandler;
michael@0 281 event = yield undefined;
michael@0 282
michael@0 283 is(event.type, "success", "Upgraded database");
michael@0 284 ok(event.target.result === db, "Got the same database");
michael@0 285
michael@0 286 db.close();
michael@0 287 }
michael@0 288
michael@0 289 { // Make sure deleting objectStores in low disk mode succeeds.
michael@0 290 info("Test 9");
michael@0 291
michael@0 292 setLowDiskMode(true);
michael@0 293
michael@0 294 let request = indexedDB.open(dbName, dbVersion + 5);
michael@0 295 request.onerror = errorHandler;
michael@0 296 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 297 request.onsuccess = unexpectedSuccessHandler;
michael@0 298
michael@0 299 let event = yield undefined;
michael@0 300
michael@0 301 is(event.type, "upgradeneeded", "Upgrading database");
michael@0 302
michael@0 303 let db = event.target.result;
michael@0 304 db.onerror = errorHandler;
michael@0 305
michael@0 306 db.deleteObjectStore(objectStoreName);
michael@0 307
michael@0 308 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 309 request.onsuccess = grabEventAndContinueHandler;
michael@0 310 event = yield undefined;
michael@0 311
michael@0 312 is(event.type, "success", "Upgraded database");
michael@0 313 ok(event.target.result === db, "Got the same database");
michael@0 314
michael@0 315 db.close();
michael@0 316
michael@0 317 // Reset everything.
michael@0 318 indexedDB.deleteDatabase(dbName);
michael@0 319 }
michael@0 320
michael@0 321
michael@0 322 { // Add data that the rest of the tests will use.
michael@0 323 info("Adding test data");
michael@0 324
michael@0 325 setLowDiskMode(false);
michael@0 326
michael@0 327 let request = indexedDB.open(dbName, dbVersion);
michael@0 328 request.onerror = errorHandler;
michael@0 329 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 330 request.onsuccess = unexpectedSuccessHandler;
michael@0 331 let event = yield undefined;
michael@0 332
michael@0 333 is(event.type, "upgradeneeded", "Upgrading database");
michael@0 334
michael@0 335 let db = event.target.result;
michael@0 336 db.onerror = errorHandler;
michael@0 337
michael@0 338 let objectStore = db.createObjectStore(objectStoreName, objectStoreOptions);
michael@0 339 let index = objectStore.createIndex(indexName, indexName, indexOptions);
michael@0 340
michael@0 341 for each (let data in dbData) {
michael@0 342 objectStore.add(data);
michael@0 343 }
michael@0 344
michael@0 345 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 346 request.onsuccess = grabEventAndContinueHandler;
michael@0 347 event = yield undefined;
michael@0 348
michael@0 349 is(event.type, "success", "Upgraded database");
michael@0 350 ok(event.target.result === db, "Got the same database");
michael@0 351
michael@0 352 db.close();
michael@0 353 }
michael@0 354
michael@0 355 { // Make sure read operations in readonly transactions succeed in low disk
michael@0 356 // mode.
michael@0 357 info("Test 10");
michael@0 358
michael@0 359 setLowDiskMode(true);
michael@0 360
michael@0 361 let request = indexedDB.open(dbName, dbVersion);
michael@0 362 request.onerror = errorHandler;
michael@0 363 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 364 request.onsuccess = grabEventAndContinueHandler;
michael@0 365 let event = yield undefined;
michael@0 366
michael@0 367 let db = event.target.result;
michael@0 368 db.onerror = errorHandler;
michael@0 369
michael@0 370 let transaction = db.transaction(objectStoreName);
michael@0 371 let objectStore = transaction.objectStore(objectStoreName);
michael@0 372 let index = objectStore.index(indexName);
michael@0 373
michael@0 374 let data = dbData[0];
michael@0 375
michael@0 376 let requestCounter = new RequestCounter();
michael@0 377
michael@0 378 objectStore.get(data.foo).onsuccess = requestCounter.handler();
michael@0 379 objectStore.mozGetAll().onsuccess = requestCounter.handler();
michael@0 380 objectStore.count().onsuccess = requestCounter.handler();
michael@0 381 index.get(data.bar).onsuccess = requestCounter.handler();
michael@0 382 index.mozGetAll().onsuccess = requestCounter.handler();
michael@0 383 index.getKey(data.bar).onsuccess = requestCounter.handler();
michael@0 384 index.mozGetAllKeys().onsuccess = requestCounter.handler();
michael@0 385 index.count().onsuccess = requestCounter.handler();
michael@0 386
michael@0 387 let objectStoreDataCount = 0;
michael@0 388
michael@0 389 request = objectStore.openCursor();
michael@0 390 request.onsuccess = function(event) {
michael@0 391 let cursor = event.target.result;
michael@0 392 if (cursor) {
michael@0 393 objectStoreDataCount++;
michael@0 394 objectStoreDataCount % 2 ? cursor.continue() : cursor.advance(1);
michael@0 395 }
michael@0 396 else {
michael@0 397 is(objectStoreDataCount, dbData.length, "Saw all data");
michael@0 398 requestCounter.decr();
michael@0 399 }
michael@0 400 };
michael@0 401 requestCounter.incr();
michael@0 402
michael@0 403 let indexDataCount = 0;
michael@0 404
michael@0 405 request = index.openCursor();
michael@0 406 request.onsuccess = function(event) {
michael@0 407 let cursor = event.target.result;
michael@0 408 if (cursor) {
michael@0 409 indexDataCount++;
michael@0 410 indexDataCount % 2 ? cursor.continue() : cursor.advance(1);
michael@0 411 }
michael@0 412 else {
michael@0 413 is(indexDataCount, dbData.length, "Saw all data");
michael@0 414 requestCounter.decr();
michael@0 415 }
michael@0 416 };
michael@0 417 requestCounter.incr();
michael@0 418
michael@0 419 let indexKeyDataCount = 0;
michael@0 420
michael@0 421 request = index.openCursor();
michael@0 422 request.onsuccess = function(event) {
michael@0 423 let cursor = event.target.result;
michael@0 424 if (cursor) {
michael@0 425 indexKeyDataCount++;
michael@0 426 indexKeyDataCount % 2 ? cursor.continue() : cursor.advance(1);
michael@0 427 }
michael@0 428 else {
michael@0 429 is(indexKeyDataCount, dbData.length, "Saw all data");
michael@0 430 requestCounter.decr();
michael@0 431 }
michael@0 432 };
michael@0 433 requestCounter.incr();
michael@0 434
michael@0 435 // Wait for all requests.
michael@0 436 yield undefined;
michael@0 437
michael@0 438 transaction.oncomplete = grabEventAndContinueHandler;
michael@0 439 event = yield undefined;
michael@0 440
michael@0 441 is(event.type, "complete", "Transaction succeeded");
michael@0 442
michael@0 443 db.close();
michael@0 444 }
michael@0 445
michael@0 446 { // Make sure read operations in readwrite transactions succeed in low disk
michael@0 447 // mode.
michael@0 448 info("Test 11");
michael@0 449
michael@0 450 setLowDiskMode(true);
michael@0 451
michael@0 452 let request = indexedDB.open(dbName, dbVersion);
michael@0 453 request.onerror = errorHandler;
michael@0 454 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 455 request.onsuccess = grabEventAndContinueHandler;
michael@0 456 let event = yield undefined;
michael@0 457
michael@0 458 let db = event.target.result;
michael@0 459 db.onerror = errorHandler;
michael@0 460
michael@0 461 let transaction = db.transaction(objectStoreName, "readwrite");
michael@0 462 let objectStore = transaction.objectStore(objectStoreName);
michael@0 463 let index = objectStore.index(indexName);
michael@0 464
michael@0 465 let data = dbData[0];
michael@0 466
michael@0 467 let requestCounter = new RequestCounter();
michael@0 468
michael@0 469 objectStore.get(data.foo).onsuccess = requestCounter.handler();
michael@0 470 objectStore.mozGetAll().onsuccess = requestCounter.handler();
michael@0 471 objectStore.count().onsuccess = requestCounter.handler();
michael@0 472 index.get(data.bar).onsuccess = requestCounter.handler();
michael@0 473 index.mozGetAll().onsuccess = requestCounter.handler();
michael@0 474 index.getKey(data.bar).onsuccess = requestCounter.handler();
michael@0 475 index.mozGetAllKeys().onsuccess = requestCounter.handler();
michael@0 476 index.count().onsuccess = requestCounter.handler();
michael@0 477
michael@0 478 let objectStoreDataCount = 0;
michael@0 479
michael@0 480 request = objectStore.openCursor();
michael@0 481 request.onsuccess = function(event) {
michael@0 482 let cursor = event.target.result;
michael@0 483 if (cursor) {
michael@0 484 objectStoreDataCount++;
michael@0 485 objectStoreDataCount % 2 ? cursor.continue() : cursor.advance(1);
michael@0 486 }
michael@0 487 else {
michael@0 488 is(objectStoreDataCount, dbData.length, "Saw all data");
michael@0 489 requestCounter.decr();
michael@0 490 }
michael@0 491 };
michael@0 492 requestCounter.incr();
michael@0 493
michael@0 494 let indexDataCount = 0;
michael@0 495
michael@0 496 request = index.openCursor();
michael@0 497 request.onsuccess = function(event) {
michael@0 498 let cursor = event.target.result;
michael@0 499 if (cursor) {
michael@0 500 indexDataCount++;
michael@0 501 indexDataCount % 2 ? cursor.continue() : cursor.advance(1);
michael@0 502 }
michael@0 503 else {
michael@0 504 is(indexDataCount, dbData.length, "Saw all data");
michael@0 505 requestCounter.decr();
michael@0 506 }
michael@0 507 };
michael@0 508 requestCounter.incr();
michael@0 509
michael@0 510 let indexKeyDataCount = 0;
michael@0 511
michael@0 512 request = index.openCursor();
michael@0 513 request.onsuccess = function(event) {
michael@0 514 let cursor = event.target.result;
michael@0 515 if (cursor) {
michael@0 516 indexKeyDataCount++;
michael@0 517 indexKeyDataCount % 2 ? cursor.continue() : cursor.advance(1);
michael@0 518 }
michael@0 519 else {
michael@0 520 is(indexKeyDataCount, dbData.length, "Saw all data");
michael@0 521 requestCounter.decr();
michael@0 522 }
michael@0 523 };
michael@0 524 requestCounter.incr();
michael@0 525
michael@0 526 // Wait for all requests.
michael@0 527 yield undefined;
michael@0 528
michael@0 529 transaction.oncomplete = grabEventAndContinueHandler;
michael@0 530 event = yield undefined;
michael@0 531
michael@0 532 is(event.type, "complete", "Transaction succeeded");
michael@0 533
michael@0 534 db.close();
michael@0 535 }
michael@0 536
michael@0 537 { // Make sure write operations in readwrite transactions fail in low disk
michael@0 538 // mode.
michael@0 539 info("Test 12");
michael@0 540
michael@0 541 setLowDiskMode(true);
michael@0 542
michael@0 543 let request = indexedDB.open(dbName, dbVersion);
michael@0 544 request.onerror = errorHandler;
michael@0 545 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 546 request.onsuccess = grabEventAndContinueHandler;
michael@0 547 let event = yield undefined;
michael@0 548
michael@0 549 let db = event.target.result;
michael@0 550 db.onerror = errorHandler;
michael@0 551
michael@0 552 let transaction = db.transaction(objectStoreName, "readwrite");
michael@0 553 let objectStore = transaction.objectStore(objectStoreName);
michael@0 554 let index = objectStore.index(indexName);
michael@0 555
michael@0 556 let data = dbData[0];
michael@0 557 let newData = { foo: 999, bar: 999 };
michael@0 558
michael@0 559 let requestCounter = new RequestCounter();
michael@0 560
michael@0 561 objectStore.add(newData).onerror = requestCounter.errorHandler();
michael@0 562 objectStore.put(newData).onerror = requestCounter.errorHandler();
michael@0 563
michael@0 564 objectStore.get(data.foo).onsuccess = requestCounter.handler();
michael@0 565 objectStore.mozGetAll().onsuccess = requestCounter.handler();
michael@0 566 objectStore.count().onsuccess = requestCounter.handler();
michael@0 567 index.get(data.bar).onsuccess = requestCounter.handler();
michael@0 568 index.mozGetAll().onsuccess = requestCounter.handler();
michael@0 569 index.getKey(data.bar).onsuccess = requestCounter.handler();
michael@0 570 index.mozGetAllKeys().onsuccess = requestCounter.handler();
michael@0 571 index.count().onsuccess = requestCounter.handler();
michael@0 572
michael@0 573 let objectStoreDataCount = 0;
michael@0 574
michael@0 575 request = objectStore.openCursor();
michael@0 576 request.onsuccess = function(event) {
michael@0 577 let cursor = event.target.result;
michael@0 578 if (cursor) {
michael@0 579 objectStoreDataCount++;
michael@0 580 cursor.update(cursor.value).onerror = requestCounter.errorHandler();
michael@0 581 objectStoreDataCount % 2 ? cursor.continue() : cursor.advance(1);
michael@0 582 }
michael@0 583 else {
michael@0 584 is(objectStoreDataCount, dbData.length, "Saw all data");
michael@0 585 requestCounter.decr();
michael@0 586 }
michael@0 587 };
michael@0 588 requestCounter.incr();
michael@0 589
michael@0 590 let indexDataCount = 0;
michael@0 591
michael@0 592 request = index.openCursor();
michael@0 593 request.onsuccess = function(event) {
michael@0 594 let cursor = event.target.result;
michael@0 595 if (cursor) {
michael@0 596 indexDataCount++;
michael@0 597 cursor.update(cursor.value).onerror = requestCounter.errorHandler();
michael@0 598 indexDataCount % 2 ? cursor.continue() : cursor.advance(1);
michael@0 599 }
michael@0 600 else {
michael@0 601 is(indexDataCount, dbData.length, "Saw all data");
michael@0 602 requestCounter.decr();
michael@0 603 }
michael@0 604 };
michael@0 605 requestCounter.incr();
michael@0 606
michael@0 607 let indexKeyDataCount = 0;
michael@0 608
michael@0 609 request = index.openCursor();
michael@0 610 request.onsuccess = function(event) {
michael@0 611 let cursor = event.target.result;
michael@0 612 if (cursor) {
michael@0 613 indexKeyDataCount++;
michael@0 614 cursor.update(cursor.value).onerror = requestCounter.errorHandler();
michael@0 615 indexKeyDataCount % 2 ? cursor.continue() : cursor.advance(1);
michael@0 616 }
michael@0 617 else {
michael@0 618 is(indexKeyDataCount, dbData.length, "Saw all data");
michael@0 619 requestCounter.decr();
michael@0 620 }
michael@0 621 };
michael@0 622 requestCounter.incr();
michael@0 623
michael@0 624 // Wait for all requests.
michael@0 625 yield undefined;
michael@0 626
michael@0 627 transaction.oncomplete = grabEventAndContinueHandler;
michael@0 628 event = yield undefined;
michael@0 629
michael@0 630 is(event.type, "complete", "Transaction succeeded");
michael@0 631
michael@0 632 db.close();
michael@0 633 }
michael@0 634
michael@0 635 { // Make sure deleting operations in readwrite transactions succeed in low
michael@0 636 // disk mode.
michael@0 637 info("Test 13");
michael@0 638
michael@0 639 setLowDiskMode(true);
michael@0 640
michael@0 641 let request = indexedDB.open(dbName, dbVersion);
michael@0 642 request.onerror = errorHandler;
michael@0 643 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 644 request.onsuccess = grabEventAndContinueHandler;
michael@0 645 let event = yield undefined;
michael@0 646
michael@0 647 let db = event.target.result;
michael@0 648 db.onerror = errorHandler;
michael@0 649
michael@0 650 let transaction = db.transaction(objectStoreName, "readwrite");
michael@0 651 let objectStore = transaction.objectStore(objectStoreName);
michael@0 652 let index = objectStore.index(indexName);
michael@0 653
michael@0 654 let dataIndex = 0;
michael@0 655 let data = dbData[dataIndex++];
michael@0 656
michael@0 657 let requestCounter = new RequestCounter();
michael@0 658
michael@0 659 objectStore.delete(data.foo).onsuccess = requestCounter.handler();
michael@0 660
michael@0 661 objectStore.openCursor().onsuccess = function(event) {
michael@0 662 let cursor = event.target.result;
michael@0 663 if (cursor) {
michael@0 664 cursor.delete().onsuccess = requestCounter.handler();
michael@0 665 }
michael@0 666 requestCounter.decr();
michael@0 667 };
michael@0 668 requestCounter.incr();
michael@0 669
michael@0 670 index.openCursor(null, "prev").onsuccess = function(event) {
michael@0 671 let cursor = event.target.result;
michael@0 672 if (cursor) {
michael@0 673 cursor.delete().onsuccess = requestCounter.handler();
michael@0 674 }
michael@0 675 requestCounter.decr();
michael@0 676 };
michael@0 677 requestCounter.incr();
michael@0 678
michael@0 679 yield undefined;
michael@0 680
michael@0 681 objectStore.count().onsuccess = grabEventAndContinueHandler;
michael@0 682 event = yield undefined;
michael@0 683
michael@0 684 is(event.target.result, dbData.length - 3, "Actually deleted something");
michael@0 685
michael@0 686 objectStore.clear();
michael@0 687 objectStore.count().onsuccess = grabEventAndContinueHandler;
michael@0 688 event = yield undefined;
michael@0 689
michael@0 690 is(event.target.result, 0, "Actually cleared");
michael@0 691
michael@0 692 transaction.oncomplete = grabEventAndContinueHandler;
michael@0 693 event = yield undefined;
michael@0 694
michael@0 695 is(event.type, "complete", "Transaction succeeded");
michael@0 696
michael@0 697 db.close();
michael@0 698 }
michael@0 699
michael@0 700 finishTest();
michael@0 701 yield undefined;
michael@0 702 }
michael@0 703
michael@0 704 function RequestCounter(expectedType) {
michael@0 705 this._counter = 0;
michael@0 706 }
michael@0 707 RequestCounter.prototype = {
michael@0 708 incr: function() {
michael@0 709 this._counter++;
michael@0 710 },
michael@0 711
michael@0 712 decr: function() {
michael@0 713 if (!--this._counter) {
michael@0 714 continueToNextStepSync();
michael@0 715 }
michael@0 716 },
michael@0 717
michael@0 718 handler: function(type, preventDefault) {
michael@0 719 this.incr();
michael@0 720 return function(event) {
michael@0 721 is(event.type, type || "success", "Correct type");
michael@0 722 this.decr();
michael@0 723 }.bind(this);
michael@0 724 },
michael@0 725
michael@0 726 errorHandler: function(eventType, errorName) {
michael@0 727 this.incr();
michael@0 728 return function(event) {
michael@0 729 is(event.type, eventType || "error", "Correct type");
michael@0 730 is(event.target.error.name, errorName || "QuotaExceededError",
michael@0 731 "Correct error name");
michael@0 732 event.preventDefault();
michael@0 733 event.stopPropagation();
michael@0 734 this.decr();
michael@0 735 }.bind(this);
michael@0 736 }
michael@0 737 };

mercurial