tor-browser

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

idbcursor_advance_index.any.js (7560B)


      1 // META: global=window,worker
      2 // META: title=IDBCursor.advance()
      3 // META: script=resources/support.js
      4 // @author Microsoft <https://www.microsoft.com>
      5 // @author Odin Hørthe Omdal <mailto:odinho@opera.com>
      6 // @author Intel <http://www.intel.com>
      7 
      8 'use strict';
      9 
     10 function createObjectStoreWithIndexAndPopulate(db, records) {
     11  let objStore = db.createObjectStore("test", { keyPath: "pKey" });
     12  objStore.createIndex("index", "iKey");
     13  for (let i = 0; i < records.length; i++) {
     14    objStore.add(records[i]);
     15  }
     16  return objStore;
     17 }
     18 
     19 function setOnUpgradeNeeded(dbObj, records) {
     20  return function (event) {
     21    dbObj.db = event.target.result;
     22    createObjectStoreWithIndexAndPopulate(dbObj.db, records);
     23  };
     24 }
     25 
     26 async_test(t => {
     27  let dbObj = {};
     28  let count = 0;
     29  const records = [{ pKey: "primaryKey_0", iKey: "indexKey_0" },
     30  { pKey: "primaryKey_1", iKey: "indexKey_1" },
     31  { pKey: "primaryKey_2", iKey: "indexKey_2" },
     32  { pKey: "primaryKey_3", iKey: "indexKey_3" }
     33  ];
     34 
     35  let open_rq = createdb(t);
     36  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
     37 
     38  open_rq.onsuccess = function (e) {
     39    let cursor_rq = dbObj.db.transaction("test", "readonly")
     40      .objectStore("test")
     41      .index("index")
     42      .openCursor();
     43 
     44    cursor_rq.onsuccess = t.step_func(function (e) {
     45      let cursor = e.target.result;
     46      assert_true(cursor instanceof IDBCursor);
     47 
     48      switch (count) {
     49        case 0:
     50          count += 3;
     51          cursor.advance(3);
     52          break;
     53        case 3:
     54          let record = cursor.value;
     55          assert_equals(record.pKey, records[count].pKey, "record.pKey");
     56          assert_equals(record.iKey, records[count].iKey, "record.iKey");
     57          t.done();
     58          break;
     59        default:
     60          assert_unreached("unexpected count");
     61          break;
     62      }
     63    });
     64  };
     65 }, "index - iterate cursor number of times specified by count");
     66 
     67 async_test(t => {
     68  let dbObj = {};
     69  const records = [
     70    { pKey: "primaryKey_0", iKey: "indexKey_0" },
     71    { pKey: "primaryKey_1", iKey: "indexKey_1" }
     72  ];
     73 
     74  let open_rq = createdb(t);
     75  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
     76 
     77  open_rq.onsuccess = function (e) {
     78    let cursor_rq = dbObj.db.transaction("test", "readonly")
     79      .objectStore("test")
     80      .index("index")
     81      .openCursor();
     82 
     83    cursor_rq.onsuccess = t.step_func(function (e) {
     84      let cursor = e.target.result;
     85 
     86      assert_true(cursor != null, "cursor exist");
     87      assert_throws_js(TypeError,
     88        function () { cursor.advance(-1); });
     89 
     90      t.done();
     91    });
     92  };
     93 }, "attempt to pass a count parameter that is not a number");
     94 
     95 async_test(t => {
     96  let dbObj = {};
     97  const records = [
     98    { pKey: "primaryKey_0", iKey: "indexKey_0" },
     99    { pKey: "primaryKey_1", iKey: "indexKey_1" }
    100  ];
    101 
    102  let open_rq = createdb(t);
    103  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
    104 
    105  open_rq.onsuccess = function (e) {
    106    let cursor_rq = dbObj.db.transaction("test", "readonly")
    107      .objectStore("test")
    108      .index("index")
    109      .openCursor(undefined, "next");
    110 
    111    cursor_rq.onsuccess = t.step_func(function (e) {
    112      let cursor = e.target.result;
    113 
    114      assert_true(cursor != null, "cursor exist");
    115      assert_throws_js(TypeError,
    116        function () { cursor.advance(-1); });
    117 
    118      t.done();
    119    });
    120  };
    121 }, "index - attempt to advance backwards");
    122 
    123 async_test(t => {
    124  let dbObj = {};
    125  let count = 0;
    126  const records = [
    127    { pKey: "primaryKey_0", iKey: "indexKey_0" },
    128    { pKey: "primaryKey_1", iKey: "indexKey_1" },
    129    { pKey: "primaryKey_1-2", iKey: "indexKey_1" }
    130  ];
    131  const expected = [
    132    { pKey: "primaryKey_0", iKey: "indexKey_0" },
    133    { pKey: "primaryKey_1-2", iKey: "indexKey_1" }
    134  ];
    135 
    136  let open_rq = createdb(t);
    137  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
    138 
    139  open_rq.onsuccess = function (e) {
    140    let cursor_rq = dbObj.db.transaction("test", "readonly")
    141      .objectStore("test")
    142      .index("index")
    143      .openCursor();
    144 
    145    cursor_rq.onsuccess = t.step_func(function (e) {
    146      let cursor = e.target.result;
    147      if (!cursor) {
    148        assert_equals(count, expected.length, "cursor run count")
    149        t.done()
    150      }
    151 
    152      let record = cursor.value;
    153      assert_equals(record.pKey, expected[count].pKey, "primary key");
    154      assert_equals(record.iKey, expected[count].iKey, "index key");
    155 
    156      cursor.advance(2);
    157      count++;
    158    });
    159  };
    160 }, "index - iterate to the next record");
    161 
    162 async_test(t => {
    163  let db;
    164  const records = [
    165    { pKey: "primaryKey_0", iKey: "indexKey_0" },
    166    { pKey: "primaryKey_1", iKey: "indexKey_1" }
    167  ];
    168 
    169  let open_rq = createdb(t);
    170  open_rq.onupgradeneeded = function (event) {
    171    db = event.target.result;
    172    let objStore = createObjectStoreWithIndexAndPopulate(db, records);
    173    let rq = objStore.index("index").openCursor();
    174    rq.onsuccess = t.step_func(function (event) {
    175      let cursor = event.target.result;
    176      assert_true(cursor instanceof IDBCursor);
    177      assert_throws_js(TypeError,
    178        function () { cursor.advance(0); });
    179 
    180      t.done();
    181    });
    182  }
    183 }, "Calling advance() with count argument 0 should throw TypeError.");
    184 
    185 async_test(t => {
    186  let db;
    187  const records = [
    188    { pKey: "primaryKey_0", iKey: "indexKey_0" },
    189    { pKey: "primaryKey_1", iKey: "indexKey_1" }
    190  ];
    191 
    192  let open_rq = createdb(t);
    193  open_rq.onupgradeneeded = function (event) {
    194    db = event.target.result;
    195    let objStore = createObjectStoreWithIndexAndPopulate(db, records);
    196    let rq = objStore.index("index").openCursor();
    197    rq.onsuccess = t.step_func(function (event) {
    198      let cursor = event.target.result;
    199      assert_true(cursor instanceof IDBCursor);
    200 
    201      event.target.transaction.abort();
    202      assert_throws_dom("TransactionInactiveError",
    203        function () { cursor.advance(1); });
    204 
    205      t.done();
    206    });
    207  }
    208 }, "Calling advance() should throws an exception TransactionInactiveError when the transaction is not active.");
    209 
    210 async_test(t => {
    211  let db;
    212  const records = [
    213    { pKey: "primaryKey_0", iKey: "indexKey_0" },
    214    { pKey: "primaryKey_1", iKey: "indexKey_1" }
    215  ];
    216 
    217  let open_rq = createdb(t);
    218  open_rq.onupgradeneeded = function (event) {
    219    db = event.target.result;
    220    let objStore = createObjectStoreWithIndexAndPopulate(db, records);
    221    let rq = objStore.index("index").openCursor();
    222    rq.onsuccess = t.step_func(function (event) {
    223      let cursor = event.target.result;
    224      assert_true(cursor instanceof IDBCursor);
    225 
    226      cursor.advance(1);
    227      assert_throws_dom("InvalidStateError",
    228        function () { cursor.advance(1); });
    229 
    230      t.done();
    231    });
    232  }
    233 }, "Calling advance() should throw DOMException when the cursor is currently being iterated.");
    234 
    235 async_test(t => {
    236  let db;
    237  const records = [
    238    { pKey: "primaryKey_0", iKey: "indexKey_0" },
    239    { pKey: "primaryKey_1", iKey: "indexKey_1" }
    240  ];
    241 
    242  let open_rq = createdb(t);
    243  open_rq.onupgradeneeded = function (event) {
    244    db = event.target.result;
    245    let objStore = createObjectStoreWithIndexAndPopulate(db, records);
    246    let rq = objStore.index("index").openCursor();
    247    rq.onsuccess = t.step_func(function (event) {
    248      let cursor = event.target.result;
    249      assert_true(cursor instanceof IDBCursor, "cursor exist");
    250 
    251      db.deleteObjectStore("test");
    252      assert_throws_dom("InvalidStateError",
    253        function () { cursor.advance(1); });
    254 
    255      t.done();
    256    });
    257  }
    258 }, "If the cursor's source or effective object store has been deleted, the implementation MUST throw a DOMException of type InvalidStateError");