tor-browser

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

idbcursor-continuePrimaryKey-exceptions.any.js (3673B)


      1 // META: global=window,worker
      2 // META: title=IndexedDB: IDBCursor continuePrimaryKey() exception throwing
      3 // META: script=resources/support.js
      4 
      5 // Spec: https://w3c.github.io/IndexedDB/#dom-idbcursor-continueprimarykey
      6 
      7 'use strict';
      8 
      9 async_test(t => {
     10  const dbname = location + '-' + t.name;
     11  const del = indexedDB.deleteDatabase(dbname);
     12  del.onerror = t.unreached_func('deleteDatabase should succeed');
     13  const open = indexedDB.open(dbname);
     14  open.onerror = t.unreached_func('open should succeed');
     15 
     16  open.onupgradeneeded = t.step_func((e) => {
     17    const db = open.result;
     18    t.add_cleanup((e) => {
     19      db.close();
     20      indexedDB.deleteDatabase(db.name);
     21    });
     22    const store = db.createObjectStore('store');
     23    store.put('a', 1).onerror = t.unreached_func('put should not fail');
     24    const request = store.openCursor();
     25    request.onerror = t.unreached_func('openCursor should not fail');
     26    request.onsuccess = t.step_func((e) => {
     27      const cursor = request.result;
     28      assert_class_string(
     29          cursor, 'IDBCursorWithValue', 'result should be a cursor');
     30 
     31      assert_throws_dom('InvalidAccessError', function() {
     32        cursor.continuePrimaryKey(2, 2);
     33      }, 'continuePrimaryKey() should throw if source is not an index');
     34    });
     35  });
     36 
     37  open.onsuccess = t.step_func((e) => {
     38    const db = open.result;
     39    db.close();
     40    t.done();
     41  });
     42 }, 'IDBCursor continuePrimaryKey() on object store cursor');
     43 
     44 const testcases = [
     45  {
     46    direction: 'nextunique',
     47    expected_key: 1,
     48    expected_primaryKey: 'a',
     49    continue_key: 2,
     50    continue_primaryKey: 'a'
     51  },
     52  {
     53    direction: 'prevunique',
     54    expected_key: 3,
     55    expected_primaryKey: 'a',
     56    continue_key: 2,
     57    continue_primaryKey: 'a'
     58  }
     59 ];
     60 
     61 testcases.forEach(function(testcase) {
     62  async_test(t => {
     63    const dbname = location + '-' + t.name;
     64    const del = indexedDB.deleteDatabase(dbname);
     65    del.onerror = t.unreached_func('deleteDatabase should succeed');
     66    const open = indexedDB.open(dbname);
     67    open.onerror = t.unreached_func('open should succeed');
     68 
     69    open.onupgradeneeded = t.step_func(() => {
     70      const db = open.result;
     71      t.add_cleanup((e) => {
     72        db.close();
     73        indexedDB.deleteDatabase(db.name);
     74      });
     75      const store = db.createObjectStore('store', {keyPath: 'pk'});
     76      const index = store.createIndex('index', 'ik', {multiEntry: true});
     77      store.put({pk: 'a', ik: [1, 2, 3]}).onerror =
     78          t.unreached_func('put should not fail');
     79      store.put({pk: 'b', ik: [1, 2, 3]}).onerror =
     80          t.unreached_func('put should not fail');
     81      const request = index.openKeyCursor(null, testcase.direction);
     82      request.onerror = t.unreached_func('openCursor should not fail');
     83      request.onsuccess = t.step_func((e) => {
     84        const cursor = request.result;
     85        assert_class_string(cursor, 'IDBCursor', 'result should be a cursor');
     86        assert_equals(
     87            cursor.direction, testcase.direction,
     88            'direction should be as specified');
     89        assert_equals(cursor.key, testcase.expected_key, 'key should match');
     90        assert_equals(
     91            cursor.primaryKey, testcase.expected_primaryKey,
     92            'primaryKey should match');
     93 
     94        assert_throws_dom('InvalidAccessError', () => {
     95          cursor.continuePrimaryKey(
     96              testcase.continue_key, testcase.continue_primaryKey);
     97        }, 'continuePrimaryKey() should throw if direction is unique');
     98      });
     99    });
    100 
    101    open.onsuccess = t.step_func(() => {
    102      const db = open.result;
    103      db.close();
    104      t.done();
    105    });
    106  }, 'IDBCursor continuePrimaryKey() on "' + testcase.direction + '" cursor');
    107 });