tor-browser

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

idbcursor_update_objectstore.any.js (7656B)


      1 // META: global=window,worker
      2 // META: title=IDBCursor.update() - 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 async_test(t => {
     24  let dbObj = {};
     25  const records = [{ pKey: "primaryKey_0" }, { pKey: "primaryKey_1" }];
     26 
     27  let open_rq = createdb(t);
     28  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
     29  open_rq.onsuccess = CursorUpdateRecord;
     30 
     31  function CursorUpdateRecord(e) {
     32    let txn = dbObj.db.transaction("test", "readwrite"), cursor_rq = txn.objectStore("test")
     33      .openCursor();
     34    cursor_rq.onsuccess = t.step_func(function (e) {
     35      let cursor = e.target.result;
     36 
     37      cursor.value.data = "New information!";
     38      cursor.update(cursor.value);
     39    });
     40 
     41    txn.oncomplete = t.step_func(VerifyRecordWasUpdated);
     42  }
     43 
     44  function VerifyRecordWasUpdated(e) {
     45    let cursor_rq = dbObj.db.transaction("test", "readonly")
     46      .objectStore("test")
     47      .openCursor();
     48 
     49    cursor_rq.onsuccess = t.step_func(function (e) {
     50      let cursor = e.target.result;
     51 
     52      assert_equals(cursor.value.data, "New information!");
     53      t.done();
     54    });
     55  }
     56 
     57 }, "Modify a record in the object store ");
     58 
     59 async_test(t => {
     60  let dbObj = {};
     61  const records = [
     62    { pKey: "primaryKey_0", iKey: "indexKey_0" },
     63    { pKey: "primaryKey_1", iKey: "indexKey_1" }
     64  ];
     65 
     66  let open_rq = createdb(t);
     67  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
     68 
     69  open_rq.onsuccess = function (e) {
     70    let cursor_rq = dbObj.db.transaction("test", "readonly")
     71      .objectStore("test")
     72      .openCursor();
     73 
     74    cursor_rq.onsuccess = t.step_func(function (e) {
     75      let cursor = e.target.result;
     76      assert_throws_dom('ReadOnlyError',
     77        function () { cursor.update(cursor.value); });
     78      t.done();
     79    });
     80  }
     81 
     82 }, "Attempt to modify a record in a read-only transaction");
     83 
     84 async_test(t => {
     85  let db;
     86  const records = [
     87    { pKey: "primaryKey_0", iKey: "indexKey_0" },
     88    { pKey: "primaryKey_1", iKey: "indexKey_1" }
     89  ];
     90 
     91  let open_rq = createdb(t);
     92  open_rq.onupgradeneeded = function (e) {
     93    db = e.target.result;
     94    let objStore = createObjectStoreAndPopulate(db, records);
     95    let cursor_rq = objStore.openCursor();
     96 
     97    cursor_rq.onsuccess = t.step_func(function (e) {
     98      let cursor = e.target.result;
     99      assert_true(cursor instanceof IDBCursor, "cursor exist");
    100      self.cursor = cursor;
    101      self.record = cursor.value;
    102    });
    103 
    104    e.target.transaction.oncomplete = t.step_func(function (e) {
    105      assert_throws_dom('TransactionInactiveError',
    106        function () { self.cursor.update(self.record); })
    107      t.done();
    108    });
    109  }
    110 
    111 }, "Object store - attempt to modify a record in an inactive transaction");
    112 
    113 async_test(t => {
    114  let db;
    115  let open_rq = createdb(t);
    116  open_rq.onupgradeneeded = function (e) {
    117    db = e.target.result;
    118    let objStore = db.createObjectStore("test");
    119 
    120    objStore.add("data", "key");
    121  };
    122 
    123  open_rq.onsuccess = t.step_func(function (e) {
    124    let txn = db.transaction("test", "readwrite");
    125    let cursor_rq = txn.objectStore("test").openCursor();
    126 
    127    cursor_rq.onsuccess = t.step_func(function (e) {
    128      let cursor = e.target.result;
    129 
    130      let updatedValue = Object.assign({}, cursor.value);
    131      updatedValue.pKey = "new data!";
    132      cursor.update(updatedValue).onsuccess = t.step_func(function (e) {
    133        assert_equals(e.target.result, "key");
    134        t.done();
    135      });
    136    });
    137  });
    138 
    139 }, "Index - modify a record in the object store ");
    140 
    141 async_test(t => {
    142 
    143  let db;
    144  const records = [{ pKey: "primaryKey_0" }, { pKey: "primaryKey_1" }];
    145 
    146  let open_rq = createdb(t);
    147  open_rq.onupgradeneeded = function (e) {
    148    db = e.target.result;
    149    let objStore = createObjectStoreAndPopulate(db, records);
    150 
    151    let cursor_rq = objStore.openCursor();
    152 
    153    cursor_rq.onsuccess = t.step_func(function (e) {
    154      let cursor = e.target.result;
    155      assert_true(cursor instanceof IDBCursor, "cursor exists");
    156 
    157      db.deleteObjectStore("test");
    158 
    159      let updatedValue = Object.assign({}, cursor.value);
    160      updatedValue.pKey += "_updated";
    161      assert_throws_dom("InvalidStateError",
    162        function () { cursor.update(updatedValue); });
    163 
    164      t.done();
    165    });
    166  }
    167 
    168 }, "Attempt to modify a record after the cursor's source or effective object store has been deleted. The implementation MUST throw a DOMException of type InvalidStateError");
    169 
    170 async_test(t => {
    171  let dbObj = {};
    172  const records = [
    173    { pKey: "primaryKey_0", iKey: "indexKey_0" },
    174    { pKey: "primaryKey_1", iKey: "indexKey_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", "readwrite")
    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);
    188 
    189      let record = cursor.value;
    190      record.data = self;
    191      assert_throws_dom('DataCloneError',
    192        function () { cursor.update(record); });
    193 
    194      t.done();
    195    });
    196  }
    197 }, "Throw DataCloneError");
    198 
    199 async_test(t => {
    200  let dbObj = {};
    201  const records = [
    202    { pKey: "primaryKey_0", iKey: "indexKey_0" },
    203    { pKey: "primaryKey_1", iKey: "indexKey_1" }
    204  ];
    205 
    206  let open_rq = createdb(t);
    207  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
    208 
    209  open_rq.onsuccess = function (e) {
    210    let cursor_rq = dbObj.db.transaction("test", "readwrite")
    211      .objectStore("test")
    212      .openCursor();
    213 
    214    cursor_rq.onsuccess = t.step_func(function (e) {
    215      let cursor = e.target.result;
    216      assert_true(cursor instanceof IDBCursor);
    217 
    218      assert_throws_js(TypeError, function () { cursor.update(); });
    219      t.done();
    220    });
    221  }
    222 }, "No argument");
    223 
    224 async_test(t => {
    225  let dbObj = {};
    226  const records = [
    227    { pKey: "primaryKey_0", iKey: "indexKey_0" },
    228    { pKey: "primaryKey_1", iKey: "indexKey_1" }
    229  ];
    230 
    231  let open_rq = createdb(t);
    232  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
    233 
    234  open_rq.onsuccess = function (e) {
    235    let cursor_rq = dbObj.db.transaction("test", "readwrite")
    236      .objectStore("test")
    237      .openCursor();
    238 
    239    cursor_rq.onsuccess = t.step_func(function (e) {
    240      let cursor = e.target.result;
    241      assert_true(cursor instanceof IDBCursor);
    242 
    243      assert_throws_dom('DataError', function () { cursor.update(null); });
    244      t.done();
    245    });
    246  }
    247 }, "Throw DataError");
    248 
    249 async_test(t => {
    250  let dbObj = {};
    251  const records = [
    252    { pKey: "primaryKey_0", value: "value_0" },
    253    { pKey: "primaryKey_1", value: "value_1" }
    254  ];
    255 
    256  let open_rq = createdb(t);
    257  open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records);
    258 
    259  open_rq.onsuccess = function (e) {
    260    let cursor_rq = dbObj.db.transaction("test", "readwrite")
    261      .objectStore("test")
    262      .openCursor();
    263 
    264    cursor_rq.onsuccess = t.step_func(function (event) {
    265      let cursor = event.target.result;
    266      assert_true(cursor instanceof IDBCursor, "cursor exists");
    267 
    268      cursor.continue();
    269      assert_throws_dom("InvalidStateError", function () {
    270        cursor.update({ pKey: "primaryKey_0", value: "value_0_updated" });
    271      });
    272 
    273      t.done();
    274    });
    275  }
    276 }, "Throw InvalidStateError when the cursor is being iterated");