dom/indexedDB/test/unit/test_index_update_delete.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

     1 /**
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     6 var testGenerator = testSteps();
     8 function testSteps()
     9 {
    10   let name = this.window ? window.location.pathname : "Splendid Test";
    11   let request = indexedDB.open(name, 1);
    12   request.onerror = errorHandler;
    13   request.onupgradeneeded = grabEventAndContinueHandler;
    14   request.onsuccess = grabEventAndContinueHandler;
    16   let event = yield undefined;
    18   let db = event.target.result;
    19   db.onerror = errorHandler;
    21   for each (let autoIncrement in [false, true]) {
    22     let objectStore =
    23       db.createObjectStore(autoIncrement, { keyPath: "id",
    24                                             autoIncrement: autoIncrement });
    26     for (let i = 0; i < 10; i++) {
    27       objectStore.add({ id: i, index: i });
    28     }
    30     for each (let unique in [false, true]) {
    31       objectStore.createIndex(unique, "index", { unique: unique });
    32     }
    34     for (let i = 10; i < 20; i++) {
    35       objectStore.add({ id: i, index: i });
    36     }
    37   }
    39   event = yield undefined;
    40   is(event.type, "success", "expect a success event");
    42   for each (let autoIncrement in [false, true]) {
    43     let objectStore = db.transaction(autoIncrement)
    44                         .objectStore(autoIncrement);
    46     objectStore.count().onsuccess = grabEventAndContinueHandler;
    47     let event = yield undefined;
    49     is(event.target.result, 20, "Correct number of entries in objectStore");
    51     let objectStoreCount = event.target.result;
    52     let indexCount = event.target.result;
    54     for each (let unique in [false, true]) {
    55       let index = db.transaction(autoIncrement, "readwrite")
    56                     .objectStore(autoIncrement)
    57                     .index(unique);
    59       index.count().onsuccess = grabEventAndContinueHandler;
    60       let event = yield undefined;
    62       is(event.target.result, indexCount,
    63          "Correct number of entries in index");
    65       let modifiedEntry = unique ? 5 : 10;
    66       let keyRange = IDBKeyRange.only(modifiedEntry);
    68       let sawEntry = false;
    69       index.openCursor(keyRange).onsuccess = function(event) {
    70         let cursor = event.target.result;
    71         if (cursor) {
    72           sawEntry = true;
    73           is(cursor.key, modifiedEntry, "Correct key");
    75           cursor.value.index = unique ? 30 : 35;
    76           cursor.update(cursor.value).onsuccess = function(event) {
    77             cursor.continue();
    78           }
    79         }
    80         else {
    81           continueToNextStep();
    82         }
    83       }
    84       yield undefined;
    86       is(sawEntry, true, "Saw entry for key value " + modifiedEntry);
    88       // Recount index. Shouldn't change.
    89       index = db.transaction(autoIncrement, "readwrite")
    90                 .objectStore(autoIncrement)
    91                 .index(unique);
    93       index.count().onsuccess = grabEventAndContinueHandler;
    94       event = yield undefined;
    96       is(event.target.result, indexCount,
    97          "Correct number of entries in index");
    99       modifiedEntry = unique ? 30 : 35;
   100       keyRange = IDBKeyRange.only(modifiedEntry);
   102       sawEntry = false;
   103       index.openCursor(keyRange).onsuccess = function(event) {
   104         let cursor = event.target.result;
   105         if (cursor) {
   106           sawEntry = true;
   107           is(cursor.key, modifiedEntry, "Correct key");
   109           delete cursor.value.index;
   110           cursor.update(cursor.value).onsuccess = function(event) {
   111             indexCount--;
   112             cursor.continue();
   113           }
   114         }
   115         else {
   116           continueToNextStep();
   117         }
   118       }
   119       yield undefined;
   121       is(sawEntry, true, "Saw entry for key value " + modifiedEntry);
   123       // Recount objectStore. Should be unchanged.
   124       objectStore = db.transaction(autoIncrement, "readwrite")
   125                       .objectStore(autoIncrement);
   127       objectStore.count().onsuccess = grabEventAndContinueHandler;
   128       event = yield undefined;
   130       is(event.target.result, objectStoreCount,
   131          "Correct number of entries in objectStore");
   133       // Recount index. Should be one item less.
   134       index = objectStore.index(unique);
   136       index.count().onsuccess = grabEventAndContinueHandler;
   137       event = yield undefined;
   139       is(event.target.result, indexCount,
   140          "Correct number of entries in index");
   142       modifiedEntry = objectStoreCount - 1;
   144       objectStore.delete(modifiedEntry).onsuccess =
   145         grabEventAndContinueHandler;
   146       event = yield undefined;
   148       objectStoreCount--;
   149       indexCount--;
   151       objectStore.count().onsuccess = grabEventAndContinueHandler;
   152       event = yield undefined;
   154       is(event.target.result, objectStoreCount,
   155          "Correct number of entries in objectStore");
   157       index.count().onsuccess = grabEventAndContinueHandler;
   158       event = yield undefined;
   160       is(event.target.result, indexCount,
   161          "Correct number of entries in index");
   163       index = event = null; // Bug 943409 workaround.
   164     }
   165     objectStore = event = null; // Bug 943409 workaround.
   166   }
   168   finishTest();
   169   event = db = request = null; // Bug 943409 workaround.
   170   yield undefined;
   171 }

mercurial