tor-browser

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

idbindex_tombstones.any.js (2897B)


      1 // META: title=Index Tombstones
      2 // META: script=resources/support-promises.js
      3 'use strict';
      4 
      5 // This test is used to trigger a special case in Chrome with how it deals with
      6 // index creation & modification. This had caused issues before.
      7 // See https://crbug.com/1033996
      8 
      9 async function iterateAndReturnAllCursorResult(testCase, cursorRequest) {
     10  return new Promise((resolve, reject) => {
     11    let results = [];
     12    cursorRequest.onsuccess = testCase.step_func(function(event) {
     13      const cursor = event.target.result;
     14      if (!cursor) {
     15        resolve(results);
     16        return;
     17      }
     18      results.push(cursor.value);
     19      cursor.continue();
     20    });
     21    cursorRequest.onerror = reject;
     22  });
     23 }
     24 
     25 async function createTombstones(testCase, db) {
     26  const txn1 = db.transaction(['objectStore'], 'readwrite');
     27  txn1.objectStore('objectStore').add({key: 'firstItem', indexedOn: 1});
     28  txn1.objectStore('objectStore').add({key: 'secondItem', indexedOn: 2});
     29  txn1.objectStore('objectStore').add({key: 'thirdItem', indexedOn: 3});
     30  const txn2 = db.transaction(['objectStore'], 'readwrite');
     31  txn2.objectStore('objectStore').put({key: 'firstItem', indexedOn: -10});
     32  txn2.objectStore('objectStore').put({key: 'secondItem', indexedOn: 4});
     33  txn2.objectStore('objectStore').put({key: 'thirdItem', indexedOn: 10});
     34  await promiseForTransaction(testCase, txn1);
     35  await promiseForTransaction(testCase, txn2);
     36 }
     37 
     38 async function run_test(testCase, transactionMode, direction) {
     39  const db = await createDatabase(testCase, db => {
     40    db.createObjectStore('objectStore', {keyPath: 'key'})
     41        .createIndex('index', 'indexedOn');
     42  });
     43  await createTombstones(testCase, db);
     44 
     45  const txn = db.transaction(['objectStore'], transactionMode);
     46  const cursor = txn.objectStore('objectStore').index('index').openCursor(
     47      IDBKeyRange.bound(-11, 11), direction);
     48  let results = await iterateAndReturnAllCursorResult(testCase, cursor);
     49  assert_equals(results.length, 3);
     50  // Verify count().
     51  await new Promise((resolve, reject) => {
     52    const countRequest = txn.objectStore('objectStore').index('index').count();
     53    countRequest.onsuccess = testCase.step_func(event => {
     54      assert_equals(event.target.result, 3);
     55      resolve();
     56    });
     57    countRequest.onerror = reject;
     58  });
     59  db.close();
     60 }
     61 
     62 promise_test(async testCase => {
     63  await run_test(testCase, 'readonly', 'next');
     64 }, 'Forward iteration over an index in a readonly transaction');
     65 
     66 promise_test(async testCase => {
     67  await run_test(testCase, 'readonly', 'prev');
     68 }, 'Backward iteration over an index in a readonly transaction');
     69 
     70 promise_test(async testCase => {
     71  await run_test(testCase, 'readwrite', 'next');
     72 }, 'Forward iteration over an index in a readwrite transaction');
     73 
     74 promise_test(async testCase => {
     75  await run_test(testCase, 'readwrite', 'prev');
     76 }, 'Backward iteration over an index in a readwrite transaction');