tor-browser

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

idbobjectstore_add.any.js (11296B)


      1 // META: global=window,worker
      2 // META: title=IDBObjectStore.add()
      3 // META: script=resources/support.js
      4 // @author Microsoft <https://www.microsoft.com>
      5 // @author Intel <http://www.intel.com>
      6 'use strict';
      7 
      8 
      9 async_test(t => {
     10    let db;
     11    const record = { key: 1, property: "data" };
     12 
     13    const open_rq = createdb(t);
     14    open_rq.onupgradeneeded = function(e) {
     15      db = e.target.result;
     16      const objStore = db.createObjectStore("store", { keyPath: "key" });
     17 
     18      objStore.add(record);
     19    };
     20 
     21    open_rq.onsuccess = function(e) {
     22      const rq = db.transaction("store", "readonly")
     23        .objectStore("store")
     24        .get(record.key);
     25 
     26      rq.onsuccess = t.step_func(function(e) {
     27        assert_equals(e.target.result.property, record.property);
     28        assert_equals(e.target.result.key, record.key);
     29        t.done();
     30      });
     31    };
     32 }, 'add() with an inline key');
     33 
     34 async_test(t => {
     35    let db;
     36    const key = 1;
     37    const record = { property: "data" };
     38 
     39    const open_rq = createdb(t);
     40    open_rq.onupgradeneeded = function(e) {
     41      db = e.target.result;
     42      const objStore = db.createObjectStore("store");
     43 
     44      objStore.add(record, key);
     45    };
     46 
     47    open_rq.onsuccess = function(e) {
     48      const rq = db.transaction("store", "readonly")
     49        .objectStore("store")
     50        .get(key);
     51 
     52      rq.onsuccess = t.step_func(function(e) {
     53        assert_equals(e.target.result.property, record.property);
     54 
     55        t.done();
     56      });
     57    };
     58 }, 'add() with an out-of-line key');
     59 
     60 async_test(t => {
     61    const record = { key: 1, property: "data" };
     62 
     63    const open_rq = createdb(t);
     64    open_rq.onupgradeneeded = function(e) {
     65      let db = e.target.result;
     66      const objStore = db.createObjectStore("store", { keyPath: "key" });
     67      objStore.add(record);
     68 
     69      const rq = objStore.add(record);
     70      rq.onsuccess = fail(t, "success on adding duplicate record");
     71 
     72      rq.onerror = t.step_func(function(e) {
     73        assert_equals(e.target.error.name, "ConstraintError");
     74        assert_equals(rq.error.name, "ConstraintError");
     75        assert_equals(e.type, "error");
     76 
     77        e.preventDefault();
     78        e.stopPropagation();
     79      });
     80    };
     81 
     82    // Defer done, giving rq.onsuccess a chance to run
     83    open_rq.onsuccess = function(e) {
     84      t.done();
     85    };
     86 }, 'add() record with same key already exists');
     87 
     88 async_test(t => {
     89    const record = { key: 1, property: "data" };
     90 
     91    const open_rq = createdb(t);
     92    open_rq.onupgradeneeded = function(e) {
     93      let db = e.target.result;
     94      const objStore = db.createObjectStore("store", { autoIncrement: true });
     95      objStore.createIndex("i1", "property", { unique: true });
     96      objStore.add(record);
     97 
     98      const rq = objStore.add(record);
     99      rq.onsuccess = fail(t, "success on adding duplicate indexed record");
    100 
    101      rq.onerror = t.step_func(function(e) {
    102        assert_equals(rq.error.name, "ConstraintError");
    103        assert_equals(e.target.error.name, "ConstraintError");
    104        assert_equals(e.type, "error");
    105 
    106        e.preventDefault();
    107        e.stopPropagation();
    108      });
    109    };
    110 
    111    // Defer done, giving a spurious rq.onsuccess a chance to run
    112    open_rq.onsuccess = function(e) {
    113      t.done();
    114    };
    115 }, 'add() where an index has unique:true specified');
    116 
    117 async_test(t => {
    118    let db;
    119    const record = { test: { obj: { key: 1 } }, property: "data" };
    120 
    121    const open_rq = createdb(t);
    122    open_rq.onupgradeneeded = function(e) {
    123      db = e.target.result;
    124      const objStore = db.createObjectStore("store",
    125        { keyPath: "test.obj.key" });
    126      objStore.add(record);
    127    };
    128 
    129    open_rq.onsuccess = function(e) {
    130      const rq = db.transaction("store", "readonly")
    131        .objectStore("store")
    132        .get(record.test.obj.key);
    133 
    134      rq.onsuccess = t.step_func(function(e) {
    135        assert_equals(e.target.result.property, record.property);
    136 
    137        t.done();
    138      });
    139    };
    140 }, 'add() object store\'s key path is an object attribute');
    141 
    142 async_test(t => {
    143    let db;
    144    const record = { property: "data" };
    145    const expected_keys = [1, 2, 3, 4];
    146 
    147    const open_rq = createdb(t);
    148    open_rq.onupgradeneeded = function(e) {
    149      db = e.target.result;
    150      const objStore = db.createObjectStore("store", { keyPath: "key",
    151       autoIncrement: true });
    152 
    153      objStore.add(record);
    154      objStore.add(record);
    155      objStore.add(record);
    156      objStore.add(record);
    157    };
    158 
    159    open_rq.onsuccess = function(e) {
    160      const actual_keys = [];
    161      const rq = db.transaction("store", "readonly")
    162        .objectStore("store")
    163        .openCursor();
    164 
    165      rq.onsuccess = t.step_func(function(e) {
    166        const cursor = e.target.result;
    167 
    168        if (cursor) {
    169          actual_keys.push(cursor.value.key);
    170          cursor.continue();
    171        } else {
    172          assert_array_equals(actual_keys, expected_keys);
    173          t.done();
    174        }
    175      });
    176    };
    177 }, 'add() autoIncrement and inline keys');
    178 
    179 async_test(t => {
    180    let db;
    181    const record = { property: "data" };
    182    const expected_keys = [1, 2, 3, 4];
    183 
    184    const open_rq = createdb(t);
    185    open_rq.onupgradeneeded = function(e) {
    186      db = e.target.result;
    187      const objStore = db.createObjectStore("store", { autoIncrement: true });
    188 
    189      objStore.add(record);
    190      objStore.add(record);
    191      objStore.add(record);
    192      objStore.add(record);
    193    };
    194 
    195    open_rq.onsuccess = function(e) {
    196      const actual_keys = [];
    197      const rq = db.transaction("store", "readonly")
    198        .objectStore("store")
    199        .openCursor();
    200 
    201      rq.onsuccess = t.step_func(function(e) {
    202        const cursor = e.target.result;
    203 
    204        if (cursor) {
    205          actual_keys.push(cursor.key);
    206          cursor.continue();
    207        } else {
    208          assert_array_equals(actual_keys, expected_keys);
    209          t.done();
    210        }
    211      });
    212    };
    213 }, 'add() autoIncrement and out-of-line keys');
    214 
    215 async_test(t => {
    216    let db;
    217    const record = { property: "data" };
    218    const expected_keys = [1, 2, 3, 4];
    219 
    220    const open_rq = createdb(t);
    221    open_rq.onupgradeneeded = function(e) {
    222      db = e.target.result;
    223      const objStore = db.createObjectStore("store", { keyPath: "test.obj.key",
    224        autoIncrement: true });
    225 
    226      objStore.add(record);
    227      objStore.add(record);
    228      objStore.add(record);
    229      objStore.add(record);
    230    };
    231 
    232    open_rq.onsuccess = function(e) {
    233      const actual_keys = [];
    234      const rq = db.transaction("store", "readonly")
    235        .objectStore("store")
    236        .openCursor();
    237 
    238      rq.onsuccess = t.step_func(function(e) {
    239        const cursor = e.target.result;
    240 
    241        if (cursor) {
    242          actual_keys.push(cursor.value.test.obj.key);
    243          cursor.continue();
    244        } else {
    245          assert_array_equals(actual_keys, expected_keys);
    246          t.done();
    247        }
    248      });
    249    };
    250 }, 'Object store has autoIncrement:true and the key path is an object \
    251 attribute');
    252 
    253 async_test(t => {
    254    const record = { key: 1, property: "data" };
    255 
    256    const open_rq = createdb(t);
    257    open_rq.onupgradeneeded = function(e) {
    258      let rq;
    259      const db = e.target.result;
    260      const objStore = db.createObjectStore("store", { keyPath: "key" });
    261 
    262      assert_throws_dom("DataError", function() {
    263        rq = objStore.add(record, 1);
    264      });
    265 
    266      assert_equals(rq, undefined);
    267      t.done();
    268    };
    269  }, 'Attempt to \'add()\' a record that does not meet the constraints of an \
    270  object store\'s inline key requirements');
    271 
    272 async_test(t => {
    273    const record = { property: "data" };
    274 
    275    const open_rq = createdb(t);
    276    open_rq.onupgradeneeded = function(e) {
    277      let db = e.target.result;
    278      let rq;
    279      const objStore = db.createObjectStore("store");
    280 
    281      assert_throws_dom("DataError", function() {
    282        rq = objStore.add(record);
    283      });
    284 
    285      assert_equals(rq, undefined);
    286      t.done();
    287    };
    288 }, 'Attempt to call \'add()\' without a key parameter when the object store \
    289 uses out-of-line keys');
    290 
    291 async_test(t => {
    292    const record = { key: { value: 1 }, property: "data" };
    293 
    294    const open_rq = createdb(t);
    295    open_rq.onupgradeneeded = function(e) {
    296      let db = e.target.result;
    297 
    298      let rq;
    299      const objStore = db.createObjectStore("store", { keyPath: "key" });
    300 
    301      assert_throws_dom("DataError", function() {
    302        rq = objStore.add(record);
    303      });
    304 
    305      assert_equals(rq, undefined);
    306      t.done();
    307    };
    308 }, 'Attempt to \'add()\' a record where the record\'s key does not meet the \
    309 constraints of a valid key');
    310 
    311 async_test(t => {
    312    const record = { property: "data" };
    313 
    314    const open_rq = createdb(t);
    315    open_rq.onupgradeneeded = function(e) {
    316      let db = e.target.result;
    317 
    318      let rq;
    319      const objStore = db.createObjectStore("store", { keyPath: "key" });
    320 
    321      assert_throws_dom("DataError", function() {
    322        rq = objStore.add(record);
    323      });
    324 
    325      assert_equals(rq, undefined);
    326      t.done();
    327    };
    328 }, 'Attempt to \'add()\' a record where the record\'s in-line key is not \
    329 defined');
    330 
    331 async_test(t => {
    332    const record = { property: "data" };
    333 
    334    const open_rq = createdb(t);
    335    open_rq.onupgradeneeded = function(e) {
    336      let db = e.target.result;
    337 
    338      let rq;
    339      const objStore = db.createObjectStore("store");
    340 
    341      assert_throws_dom("DataError", function() {
    342        rq = objStore.add(record, { value: 1 });
    343      });
    344 
    345      assert_equals(rq, undefined);
    346      t.done();
    347    };
    348 }, 'Attempt to \'add()\' a record where the out of line key provided does not \
    349 meet the constraints of a valid key');
    350 
    351 async_test(t => {
    352    const record = { key: 1, indexedProperty: { property: "data" } };
    353 
    354    const open_rq = createdb(t);
    355    open_rq.onupgradeneeded = function(e) {
    356      let db = e.target.result;
    357 
    358      let rq;
    359      const objStore = db.createObjectStore("store", { keyPath: "key" });
    360 
    361      objStore.createIndex("index", "indexedProperty");
    362 
    363      rq = objStore.add(record);
    364 
    365      assert_true(rq instanceof IDBRequest);
    366      rq.onsuccess = function() {
    367          t.done();
    368      }
    369    };
    370 }, 'add() a record where a value being indexed does not meet the constraints \
    371 of a valid key');
    372 
    373 async_test(t => {
    374    let db;
    375 
    376    const open_rq = createdb(t);
    377    open_rq.onupgradeneeded = function (event) {
    378        db = event.target.result;
    379        db.createObjectStore("store", {keyPath: "pKey"});
    380    }
    381 
    382    open_rq.onsuccess = function (event) {
    383        const txn = db.transaction("store", "readonly");
    384        const ostore = txn.objectStore("store");
    385        t.step(function() {
    386            assert_throws_dom("ReadOnlyError", function() {
    387                ostore.add({pKey: "primaryKey_0"});
    388            });
    389        });
    390        t.done();
    391    }
    392 }, 'If the transaction this IDBObjectStore belongs to has its mode set to \
    393 readonly, throw ReadOnlyError');
    394 
    395 async_test(t => {
    396    const open_rq = createdb(t);
    397    open_rq.onupgradeneeded = function (event) {
    398        let db = event.target.result;
    399        const ostore = db.createObjectStore("store", {keyPath: "pKey"});
    400        db.deleteObjectStore("store");
    401        assert_throws_dom("InvalidStateError", function() {
    402            ostore.add({pKey: "primaryKey_0"});
    403        });
    404        t.done();
    405    };
    406 }, 'If the object store has been deleted, the implementation must throw a \
    407 DOMException of type InvalidStateError');