tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_index_update_delete.js (4938B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 /* exported testGenerator */
      7 var testGenerator = testSteps();
      8 
      9 function* testSteps() {
     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;
     15 
     16  let event = yield undefined;
     17 
     18  let db = event.target.result;
     19  db.onerror = errorHandler;
     20 
     21  for (let autoIncrement of [false, true]) {
     22    let objectStore = db.createObjectStore(autoIncrement, {
     23      keyPath: "id",
     24      autoIncrement,
     25    });
     26 
     27    for (let i = 0; i < 10; i++) {
     28      objectStore.add({ id: i, index: i });
     29    }
     30 
     31    for (let unique of [false, true]) {
     32      objectStore.createIndex(unique, "index", { unique });
     33    }
     34 
     35    for (let i = 10; i < 20; i++) {
     36      objectStore.add({ id: i, index: i });
     37    }
     38  }
     39 
     40  event = yield undefined;
     41  is(event.type, "success", "expect a success event");
     42 
     43  for (let autoIncrement of [false, true]) {
     44    let objectStore = db.transaction(autoIncrement).objectStore(autoIncrement);
     45 
     46    objectStore.count().onsuccess = grabEventAndContinueHandler;
     47    let event = yield undefined;
     48 
     49    is(event.target.result, 20, "Correct number of entries in objectStore");
     50 
     51    let objectStoreCount = event.target.result;
     52    let indexCount = event.target.result;
     53 
     54    for (let unique of [false, true]) {
     55      let index = db
     56        .transaction(autoIncrement, "readwrite")
     57        .objectStore(autoIncrement)
     58        .index(unique);
     59 
     60      index.count().onsuccess = grabEventAndContinueHandler;
     61      let event = yield undefined;
     62 
     63      is(event.target.result, indexCount, "Correct number of entries in index");
     64 
     65      let modifiedEntry = unique ? 5 : 10;
     66      let keyRange = IDBKeyRange.only(modifiedEntry);
     67 
     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");
     74 
     75          cursor.value.index = unique ? 30 : 35;
     76          cursor.update(cursor.value).onsuccess = function () {
     77            cursor.continue();
     78          };
     79        } else {
     80          continueToNextStep();
     81        }
     82      };
     83      yield undefined;
     84 
     85      is(sawEntry, true, "Saw entry for key value " + modifiedEntry);
     86 
     87      // Recount index. Shouldn't change.
     88      index = db
     89        .transaction(autoIncrement, "readwrite")
     90        .objectStore(autoIncrement)
     91        .index(unique);
     92 
     93      index.count().onsuccess = grabEventAndContinueHandler;
     94      event = yield undefined;
     95 
     96      is(event.target.result, indexCount, "Correct number of entries in index");
     97 
     98      modifiedEntry = unique ? 30 : 35;
     99      keyRange = IDBKeyRange.only(modifiedEntry);
    100 
    101      sawEntry = false;
    102      index.openCursor(keyRange).onsuccess = function (event) {
    103        let cursor = event.target.result;
    104        if (cursor) {
    105          sawEntry = true;
    106          is(cursor.key, modifiedEntry, "Correct key");
    107 
    108          delete cursor.value.index;
    109          cursor.update(cursor.value).onsuccess = function () {
    110            indexCount--;
    111            cursor.continue();
    112          };
    113        } else {
    114          continueToNextStep();
    115        }
    116      };
    117      yield undefined;
    118 
    119      is(sawEntry, true, "Saw entry for key value " + modifiedEntry);
    120 
    121      // Recount objectStore. Should be unchanged.
    122      objectStore = db
    123        .transaction(autoIncrement, "readwrite")
    124        .objectStore(autoIncrement);
    125 
    126      objectStore.count().onsuccess = grabEventAndContinueHandler;
    127      event = yield undefined;
    128 
    129      is(
    130        event.target.result,
    131        objectStoreCount,
    132        "Correct number of entries in objectStore"
    133      );
    134 
    135      // Recount index. Should be one item less.
    136      index = objectStore.index(unique);
    137 
    138      index.count().onsuccess = grabEventAndContinueHandler;
    139      event = yield undefined;
    140 
    141      is(event.target.result, indexCount, "Correct number of entries in index");
    142 
    143      modifiedEntry = objectStoreCount - 1;
    144 
    145      objectStore.delete(modifiedEntry).onsuccess = grabEventAndContinueHandler;
    146      event = yield undefined;
    147 
    148      objectStoreCount--;
    149      indexCount--;
    150 
    151      objectStore.count().onsuccess = grabEventAndContinueHandler;
    152      event = yield undefined;
    153 
    154      is(
    155        event.target.result,
    156        objectStoreCount,
    157        "Correct number of entries in objectStore"
    158      );
    159 
    160      index.count().onsuccess = grabEventAndContinueHandler;
    161      event = yield undefined;
    162 
    163      is(event.target.result, indexCount, "Correct number of entries in index");
    164 
    165      index = event = null; // Bug 943409 workaround.
    166    }
    167    objectStore = event = null; // Bug 943409 workaround.
    168  }
    169 
    170  finishTest();
    171  event = db = request = null; // Bug 943409 workaround.
    172 }