tor-browser

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

idbcursor_continue_objectstore.any.js (7927B)


      1 // META: global=window,worker
      2 // META: title=IDBCursor.continue() - object store
      3 // META: script=resources/support.js
      4 
      5 'use strict';
      6 
      7 function createObjectStoreAndPopulate(db, records) {
      8  let objStore = db.createObjectStore("test", { keyPath: "pKey" });
      9 
     10  for (let i = 0; i < records.length; i++) {
     11    objStore.add(records[i]);
     12  }
     13  return objStore;
     14 }
     15 
     16 function setOnUpgradeNeeded(dbObj, records) {
     17  return function (event) {
     18    dbObj.db = event.target.result;
     19    createObjectStoreAndPopulate(dbObj.db, records);
     20  };
     21 }
     22 
     23 function setOnUpgradeNeededWithCleanup(t, dbObj, records) {
     24  return function (e) {
     25    dbObj.db = e.target.result;
     26    t.add_cleanup(function () {
     27      dbObj.db.close();
     28      indexedDB.deleteDatabase(dbObj.db.name);
     29    });
     30    createObjectStoreAndPopulate(dbObj.db, records);
     31  };
     32 }
     33 
     34 async_test(t => {
     35  let dbObj = {};
     36  let count = 0;
     37 
     38  const records = [
     39    { pKey: "primaryKey_0" },
     40    { pKey: "primaryKey_1" }
     41  ];
     42 
     43  let open_rq = createdb(t);
     44  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
     45 
     46  open_rq.onsuccess = function (e) {
     47    let store = dbObj.db.transaction("test", "readonly")
     48      .objectStore("test");
     49 
     50    let cursor_rq = store.openCursor();
     51    cursor_rq.onsuccess = t.step_func(function (e) {
     52      let cursor = e.target.result;
     53      if (!cursor) {
     54        assert_equals(count, records.length, "cursor run count");
     55        t.done();
     56      }
     57 
     58      let record = cursor.value;
     59      assert_equals(record.pKey, records[count].pKey, "primary key");
     60      assert_equals(record.iKey, records[count].iKey, "index key");
     61 
     62      cursor.continue();
     63      count++;
     64    });
     65  }
     66 }, "Iterate to the next record");
     67 
     68 async_test(t => {
     69  let dbObj = {};
     70 
     71  const records = [
     72    { pKey: "primaryKey_0" },
     73    { pKey: "primaryKey_1" }
     74  ];
     75 
     76  let open_rq = createdb(t);
     77  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
     78 
     79  open_rq.onsuccess = function (e) {
     80    let cursor_rq = dbObj.db.transaction("test", "readonly")
     81      .objectStore("test").openCursor();
     82 
     83    cursor_rq.onsuccess = t.step_func(function (e) {
     84      let cursor = e.target.result;
     85 
     86      assert_true(cursor instanceof IDBCursor, "cursor exists");
     87      assert_throws_dom("DataError",
     88        function () { cursor.continue(-1); });
     89 
     90      t.done();
     91    });
     92  }
     93 
     94 }, "Attempt to pass a key parameter is not a valid key");
     95 
     96 async_test(t => {
     97  let dbObj = {};
     98 
     99  const records = [
    100    { pKey: "primaryKey_0" },
    101    { pKey: "primaryKey_1" }
    102  ];
    103 
    104  let open_rq = createdb(t);
    105  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
    106 
    107  open_rq.onsuccess = function (e) {
    108    let cursor_rq = dbObj.db.transaction("test", "readonly")
    109      .objectStore("test")
    110      .openCursor(undefined, "next");
    111 
    112    cursor_rq.onsuccess = t.step_func(function (e) {
    113      let cursor = e.target.result;
    114 
    115      assert_true(cursor instanceof IDBCursor, "cursor exist");
    116      assert_throws_dom("DataError",
    117        function () { cursor.continue(records[0].pKey); });
    118 
    119      t.done();
    120    });
    121  }
    122 }, "Attempt to iterate to the previous record when the direction is set for the next record");
    123 
    124 async_test(t => {
    125  let dbObj = {};
    126 
    127  const records = [
    128    { pKey: "primaryKey_0" },
    129    { pKey: "primaryKey_1" },
    130    { pKey: "primaryKey_2" }
    131  ];
    132 
    133  let open_rq = createdb(t);
    134  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
    135 
    136  open_rq.onsuccess = function (e) {
    137    let count = 0,
    138      cursor_rq = dbObj.db.transaction("test", "readonly")
    139        .objectStore("test")
    140        .openCursor(null, "prev");
    141 
    142    cursor_rq.onsuccess = t.step_func(function (e) {
    143      let cursor = e.target.result;
    144 
    145      assert_true(cursor != null, "cursor exist");
    146 
    147      switch (count) {
    148        case 0:
    149          assert_equals(cursor.value.pKey, records[2].pKey, "first cursor pkey");
    150          cursor.continue(records[1].pKey);
    151          break;
    152 
    153        case 1:
    154          assert_equals(cursor.value.pKey, records[1].pKey, "second cursor pkey");
    155          assert_throws_dom("DataError",
    156            function () { cursor.continue(records[2].pKey); });
    157          t.done();
    158          break;
    159 
    160        default:
    161          assert_unreached("Unexpected count value: " + count);
    162      }
    163 
    164      count++;
    165    });
    166  }
    167 }, "Attempt to iterate to the next record when the direction is set for the next record");
    168 
    169 async_test(t => {
    170  let dbObj = {};
    171 
    172  const records = [
    173    { pKey: "primaryKey_0" },
    174    { pKey: "primaryKey_1" }
    175  ];
    176 
    177  let open_rq = createdb(t);
    178  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
    179 
    180  open_rq.onsuccess = function (e) {
    181    let cursor_rq = dbObj.db.transaction("test", "readonly")
    182      .objectStore("test")
    183      .openCursor();
    184 
    185    cursor_rq.onsuccess = t.step_func(function (e) {
    186      let cursor = e.target.result;
    187      assert_true(cursor instanceof IDBCursor, "cursor exists");
    188 
    189      e.target.transaction.abort();
    190      assert_throws_dom("TransactionInactiveError",
    191        function () { cursor.continue(); });
    192 
    193      t.done();
    194    });
    195  }
    196 
    197 }, "Calling continue() should throws an exception TransactionInactiveError when the transaction is not active.");
    198 
    199 async_test(t => {
    200  let db;
    201  const records = [
    202    { pKey: "primaryKey_0" },
    203    { pKey: "primaryKey_1" }
    204  ];
    205 
    206  let open_rq = createdb(t);
    207  open_rq.onupgradeneeded = function (e) {
    208    db = e.target.result;
    209    let objStore = createObjectStoreAndPopulate(db, records);
    210 
    211    let cursor_rq = objStore.openCursor();
    212 
    213    cursor_rq.onsuccess = t.step_func(function (e) {
    214      let cursor = e.target.result;
    215      assert_true(cursor instanceof IDBCursor, "cursor exists");
    216 
    217      db.deleteObjectStore("test");
    218      assert_throws_dom("InvalidStateError",
    219        function () { cursor.continue(); });
    220 
    221      t.done();
    222    });
    223  }
    224 }, "If the cursor's source or effective object store has been deleted, the implementation MUST throw a DOMException of type InvalidStateError");
    225 
    226 async_test(t => {
    227  let dbObj = {};
    228  let count = 0;
    229  const records = [
    230    { pKey: "primaryKey_0" },
    231    { pKey: "primaryKey_1" },
    232    { pKey: "primaryKey_2" }
    233  ];
    234 
    235  const expected_records = [
    236    { pKey: "primaryKey_0" },
    237    { pKey: "primaryKey_2" }
    238  ];
    239 
    240  let open_rq = createdb(t);
    241  open_rq.onupgradeneeded = setOnUpgradeNeededWithCleanup(t, dbObj, records);
    242 
    243  open_rq.onsuccess = function (e) {
    244    let cursor_rq = dbObj.db.transaction("test", "readwrite")
    245      .objectStore("test")
    246      .openCursor();
    247 
    248    cursor_rq.onsuccess = t.step_func(function (e) {
    249      let cursor = e.target.result;
    250      if (!cursor) {
    251        assert_equals(count, 2, "cursor run count");
    252        t.done();
    253      }
    254 
    255      let record = cursor.value;
    256      if (record.pKey == "primaryKey_0") {
    257        e.target.source.delete("primaryKey_1");
    258      }
    259      assert_equals(record.pKey, expected_records[count].pKey, "primary key");
    260 
    261      cursor.continue();
    262      count++;
    263    });
    264  }
    265 }, "Delete next element, and iterate to it");
    266 
    267 async_test(t => {
    268  let dbObj = {};
    269  let count = 0;
    270  const records = [
    271    { pKey: "primaryKey_0" },
    272    { pKey: "primaryKey_2" }
    273  ];
    274 
    275  const expected_records = [
    276    { pKey: "primaryKey_0" },
    277    { pKey: "primaryKey_1" },
    278    { pKey: "primaryKey_2" }
    279  ];
    280 
    281  let open_rq = createdb(t);
    282  open_rq.onupgradeneeded = setOnUpgradeNeededWithCleanup(t, dbObj, records);
    283 
    284  open_rq.onsuccess = function (e) {
    285    let cursor_rq = dbObj.db.transaction("test", "readwrite")
    286      .objectStore("test")
    287      .openCursor();
    288 
    289    cursor_rq.onsuccess = t.step_func(function (e) {
    290      let cursor = e.target.result;
    291      if (!cursor) {
    292        assert_equals(count, 3, "cursor run count");
    293        t.done();
    294      }
    295 
    296      let record = cursor.value;
    297      if (record.pKey == "primaryKey_0") {
    298        e.target.source.add({ pKey: "primaryKey_1" });
    299      }
    300      assert_equals(record.pKey, expected_records[count].pKey, "primary key");
    301 
    302      cursor.continue();
    303      count++;
    304    });
    305  }
    306 }, "Add next element, and iterate to it");