tor-browser

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

idbobjectstore_openKeyCursor.any.js (4459B)


      1 // META: global=window,worker
      2 // META: title=IDBObjectStore.openKeyCursor()
      3 // META: script=resources/support.js
      4 
      5 'use strict';
      6 
      7 function store_test(func, name) {
      8  indexeddb_test(
      9      function(t, db, tx) {
     10        let objectStore = db.createObjectStore('store');
     11        for (let i = 0; i < 10; ++i) {
     12          objectStore.put('value: ' + i, i);
     13        }
     14      },
     15      function(t, db) {
     16        let tx = db.transaction('store', 'readonly');
     17        let objectStore = tx.objectStore('store');
     18        func(t, db, tx, objectStore);
     19      },
     20      name);
     21 }
     22 
     23 store_test(function(t, db, tx, objectStore) {
     24  let expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
     25  let actual = [];
     26  let request = objectStore.openKeyCursor();
     27  request.onsuccess = t.step_func(function() {
     28    let cursor = request.result;
     29    if (!cursor)
     30      return;
     31    assert_equals(cursor.direction, 'next');
     32    assert_false('value' in cursor);
     33    assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
     34    actual.push(cursor.key);
     35    cursor.continue();
     36  });
     37 
     38  tx.onabort = t.unreached_func('transaction aborted');
     39  tx.oncomplete = t.step_func(function() {
     40    assert_array_equals(expected, actual, 'keys should match');
     41    t.done();
     42  });
     43 }, 'IDBObjectStore.openKeyCursor() - forward iteration');
     44 
     45 store_test(function(t, db, tx, objectStore) {
     46  let expected = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
     47  let actual = [];
     48  let request = objectStore.openKeyCursor(null, 'prev');
     49  request.onsuccess = t.step_func(function() {
     50    let cursor = request.result;
     51    if (!cursor)
     52      return;
     53    assert_equals(cursor.direction, 'prev');
     54    assert_false('value' in cursor);
     55    assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
     56    actual.push(cursor.key);
     57    cursor.continue();
     58  });
     59 
     60  tx.onabort = t.unreached_func('transaction aborted');
     61  tx.oncomplete = t.step_func(function() {
     62    assert_array_equals(expected, actual, 'keys should match');
     63    t.done();
     64  });
     65 }, 'IDBObjectStore.openKeyCursor() - reverse iteration');
     66 
     67 store_test(function(t, db, tx, objectStore) {
     68  let expected = [4, 5, 6];
     69  let actual = [];
     70  let request = objectStore.openKeyCursor(IDBKeyRange.bound(4, 6));
     71  request.onsuccess = t.step_func(function() {
     72    let cursor = request.result;
     73    if (!cursor)
     74      return;
     75    assert_equals(cursor.direction, 'next');
     76    assert_false('value' in cursor);
     77    assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
     78    actual.push(cursor.key);
     79    cursor.continue();
     80  });
     81 
     82  tx.onabort = t.unreached_func('transaction aborted');
     83  tx.oncomplete = t.step_func(function() {
     84    assert_array_equals(expected, actual, 'keys should match');
     85    t.done();
     86  });
     87 }, 'IDBObjectStore.openKeyCursor() - forward iteration with range');
     88 
     89 store_test(function(t, db, tx, objectStore) {
     90  let expected = [6, 5, 4];
     91  let actual = [];
     92  let request = objectStore.openKeyCursor(IDBKeyRange.bound(4, 6), 'prev');
     93  request.onsuccess = t.step_func(function() {
     94    let cursor = request.result;
     95    if (!cursor)
     96      return;
     97    assert_equals(cursor.direction, 'prev');
     98    assert_false('value' in cursor);
     99    assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
    100    actual.push(cursor.key);
    101    cursor.continue();
    102  });
    103 
    104  tx.onabort = t.unreached_func('transaction aborted');
    105  tx.oncomplete = t.step_func(function() {
    106    assert_array_equals(expected, actual, 'keys should match');
    107    t.done();
    108  });
    109 }, 'IDBObjectStore.openKeyCursor() - reverse iteration with range');
    110 
    111 store_test(function(t, db, tx, objectStore) {
    112  assert_throws_dom('DataError', function() {
    113    objectStore.openKeyCursor(NaN);
    114  }, 'openKeyCursor should throw on invalid number key');
    115  assert_throws_dom('DataError', function() {
    116    objectStore.openKeyCursor(new Date(NaN));
    117  }, 'openKeyCursor should throw on invalid date key');
    118  assert_throws_dom('DataError', function() {
    119    let cycle = [];
    120    cycle.push(cycle);
    121    objectStore.openKeyCursor(cycle);
    122  }, 'openKeyCursor should throw on invalid array key');
    123  assert_throws_dom('DataError', function() {
    124    objectStore.openKeyCursor({});
    125  }, 'openKeyCursor should throw on invalid key type');
    126  setTimeout(
    127      t.step_func(function() {
    128        assert_throws_dom('TransactionInactiveError', function() {
    129          objectStore.openKeyCursor();
    130        }, 'openKeyCursor should throw if transaction is inactive');
    131        t.done();
    132      }),
    133      0);
    134 }, 'IDBObjectStore.openKeyCursor() - invalid inputs');