tor-browser

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

idbindex_get.any.js (5157B)


      1 // META: global=window,worker
      2 // META: title=IDBIndex.get()
      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  let index;
     12  const record = { key: 1, indexedProperty: "data" };
     13 
     14  const open_rq = createdb(t);
     15  open_rq.onupgradeneeded = function(e) {
     16    db = e.target.result;
     17    const objStore = db.createObjectStore("store", { keyPath: "key" });
     18    index = objStore.createIndex("index", "indexedProperty");
     19 
     20    objStore.add(record);
     21  };
     22 
     23  open_rq.onsuccess = function(e) {
     24    const rq = db.transaction("store", "readonly")
     25      .objectStore("store")
     26      .index("index")
     27      .get(record.indexedProperty);
     28 
     29    rq.onsuccess = t.step_func(function(e) {
     30      assert_equals(e.target.result.key, record.key);
     31      t.done();
     32    });
     33  };
     34 }, 'get() returns the record');
     35 
     36 async_test(t => {
     37  let db;
     38  const records = [
     39    { key: 1, indexedProperty: "data" },
     40    { key: 2, indexedProperty: "data" },
     41    { key: 3, indexedProperty: "data" }
     42  ];
     43 
     44  const open_rq = createdb(t);
     45  open_rq.onupgradeneeded = function(e) {
     46    db = e.target.result;
     47    const objStore = db.createObjectStore("test", { keyPath: "key" });
     48    objStore.createIndex("index", "indexedProperty");
     49 
     50    for (let i = 0; i < records.length; i++)
     51      objStore.add(records[i]);
     52  };
     53 
     54  open_rq.onsuccess = function(e) {
     55    const rq = db.transaction("test", "readonly")
     56      .objectStore("test")
     57      .index("index")
     58      .get("data");
     59 
     60    rq.onsuccess = t.step_func(function(e) {
     61      assert_equals(e.target.result.key, records[0].key);
     62      t.done();
     63    });
     64  };
     65 }, 'get() returns the record where the index contains duplicate values');
     66 
     67 async_test(t => {
     68  let db;
     69 
     70  const open_rq = createdb(t);
     71  open_rq.onupgradeneeded = function(e) {
     72    db = e.target.result;
     73    const rq = db.createObjectStore("test", { keyPath: "key" })
     74      .createIndex("index", "indexedProperty")
     75      .get(1);
     76 
     77    rq.onsuccess = t.step_func(function(e) {
     78      assert_equals(e.target.result, undefined);
     79      t.done();
     80    });
     81  };
     82 }, 'get() attempts to retrieve a record that does not exist');
     83 
     84 async_test(t => {
     85  let db;
     86 
     87  const open_rq = createdb(t);
     88  open_rq.onupgradeneeded = function(e) {
     89    db = e.target.result;
     90    const store = db.createObjectStore("store", { keyPath: "key" });
     91    store.createIndex("index", "indexedProperty");
     92 
     93    for (let i = 0; i < 10; i++) {
     94      store.add({ key: i, indexedProperty: "data" + i });
     95    }
     96  };
     97 
     98  open_rq.onsuccess = function(e) {
     99    const rq = db.transaction("store", "readonly")
    100      .objectStore("store")
    101      .index("index")
    102      .get(IDBKeyRange.bound('data4', 'data7'));
    103 
    104    rq.onsuccess = t.step_func(function(e) {
    105      assert_equals(e.target.result.key, 4);
    106      assert_equals(e.target.result.indexedProperty, 'data4');
    107 
    108      step_timeout(function () { t.done(); }, 4);
    109    });
    110  };
    111 }, 'get() returns the record with the first key in the range');
    112 
    113 async_test(t => {
    114  let db;
    115  const open_rq = createdb(t);
    116 
    117  open_rq.onupgradeneeded = function(e) {
    118    db = e.target.result;
    119 
    120    const index = db.createObjectStore("test", { keyPath: "key" })
    121      .createIndex("index", "indexedProperty");
    122 
    123    assert_throws_dom("DataError", function () {
    124      index.get(NaN);
    125    });
    126    t.done();
    127  };
    128 }, 'get() throws DataError when using invalid key');
    129 
    130 async_test(t => {
    131  let db;
    132  const open_rq = createdb(t);
    133 
    134  open_rq.onupgradeneeded = function(e) {
    135    db = e.target.result;
    136    const store = db.createObjectStore("store", { keyPath: "key" });
    137    const index = store.createIndex("index", "indexedProperty");
    138 
    139    store.add({ key: 1, indexedProperty: "data" });
    140    store.deleteIndex("index");
    141 
    142    assert_throws_dom("InvalidStateError", function () {
    143      index.get("data");
    144    });
    145    t.done();
    146  };
    147 }, 'get() throws InvalidStateError when the index is deleted');
    148 
    149 async_test(t => {
    150  let db;
    151  const open_rq = createdb(t);
    152 
    153  open_rq.onupgradeneeded = function(e) {
    154    db = e.target.result;
    155    const store = db.createObjectStore("store", { keyPath: "key" });
    156    const index = store.createIndex("index", "indexedProperty");
    157    store.add({ key: 1, indexedProperty: "data" });
    158  };
    159 
    160  open_rq.onsuccess = function(e) {
    161    db = e.target.result;
    162    const tx = db.transaction('store', 'readonly');
    163    const index = tx.objectStore('store').index('index');
    164    tx.abort();
    165 
    166    assert_throws_dom("TransactionInactiveError", function () {
    167      index.get("data");
    168    });
    169    t.done();
    170  };
    171 }, 'get() throws TransactionInactiveError on aborted transaction');
    172 
    173 async_test(t => {
    174  let db;
    175  const open_rq = createdb(t);
    176 
    177  open_rq.onupgradeneeded = function(e) {
    178    db = e.target.result;
    179    const store = db.createObjectStore("store", { keyPath: "key" });
    180    const index = store.createIndex("index", "indexedProperty");
    181    store.add({ key: 1, indexedProperty: "data" });
    182 
    183    e.target.transaction.abort();
    184 
    185    assert_throws_dom("InvalidStateError", function () {
    186      index.get("data");
    187    });
    188    t.done();
    189  };
    190 }, 'get() throws InvalidStateError on index deleted by aborted upgrade');