tor-browser

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

test_rename_index_errors.js (4147B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 /* exported testGenerator */
      7 var testGenerator = testSteps();
      8 
      9 function* testSteps() {
     10  const name = this.window ? window.location.pathname : "Splendid Test";
     11  const storeName = "test store";
     12  const indexName1 = "test index 1";
     13  const indexName2 = "test index 2";
     14 
     15  info("Setup test indexes.");
     16  let request = indexedDB.open(name, 1);
     17  request.onerror = errorHandler;
     18  request.onupgradeneeded = grabEventAndContinueHandler;
     19  request.onsuccess = unexpectedSuccessHandler;
     20  let event = yield undefined;
     21 
     22  let db = event.target.result;
     23  let txn = event.target.transaction;
     24 
     25  is(db.objectStoreNames.length, 0, "Correct objectStoreNames list");
     26 
     27  let objectStore = db.createObjectStore(storeName, { keyPath: "foo" });
     28  is(db.objectStoreNames.length, 1, "Correct objectStoreNames list");
     29  is(db.objectStoreNames.item(0), objectStore.name, "Correct name");
     30 
     31  let index1 = objectStore.createIndex(indexName1, "bar");
     32  is(objectStore.index(indexName1).name, index1.name, "Correct index name");
     33  is(index1.name, indexName1, "Correct index name");
     34  let index2 = objectStore.createIndex(indexName2, "baz");
     35  is(objectStore.index(indexName2).name, index2.name, "Correct index name");
     36  is(index2.name, indexName2, "Correct index name");
     37 
     38  txn.oncomplete = continueToNextStepSync;
     39  yield undefined;
     40  request.onsuccess = continueToNextStep;
     41  yield undefined;
     42  db.close();
     43 
     44  info("Verify IDB Errors in version 2.");
     45  request = indexedDB.open(name, 2);
     46  request.onerror = errorHandler;
     47  request.onupgradeneeded = grabEventAndContinueHandler;
     48  request.onsuccess = unexpectedSuccessHandler;
     49  event = yield undefined;
     50 
     51  db = event.target.result;
     52  txn = event.target.transaction;
     53 
     54  objectStore = txn.objectStore(storeName);
     55  index1 = objectStore.index(indexName1);
     56  index2 = objectStore.index(indexName2);
     57  is(index1.name, indexName1, "Correct index name");
     58  is(index2.name, indexName2, "Correct index name");
     59 
     60  // Rename with the name already adopted by the other index.
     61  try {
     62    index1.name = indexName2;
     63    ok(
     64      false,
     65      "ConstraintError shall be thrown if the index name already exists."
     66    );
     67  } catch (e) {
     68    ok(e instanceof DOMException, "got a database exception");
     69    is(e.name, "ConstraintError", "correct error");
     70  }
     71 
     72  // Rename with identical name.
     73  try {
     74    index1.name = indexName1;
     75    ok(true, "It shall be fine to set the same name.");
     76  } catch (e) {
     77    ok(false, "Got a database exception: " + e.name);
     78  }
     79 
     80  objectStore.deleteIndex(indexName2);
     81 
     82  // Rename after deleted.
     83  try {
     84    index2.name = indexName2;
     85    ok(false, "InvalidStateError shall be thrown if deleted.");
     86  } catch (e) {
     87    ok(e instanceof DOMException, "got a database exception");
     88    is(e.name, "InvalidStateError", "correct error");
     89  }
     90 
     91  txn.oncomplete = continueToNextStepSync;
     92  yield undefined;
     93  request.onsuccess = continueToNextStep;
     94  yield undefined;
     95  db.close();
     96 
     97  // Rename when the transaction is inactive.
     98  try {
     99    index1.name = indexName1;
    100    ok(
    101      false,
    102      "TransactionInactiveError shall be thrown if the transaction is inactive."
    103    );
    104  } catch (e) {
    105    ok(e instanceof DOMException, "got a database exception");
    106    is(e.name, "TransactionInactiveError", "correct error");
    107  }
    108 
    109  info("Rename when the transaction is not an upgrade one.");
    110  request = indexedDB.open(name, 2);
    111  request.onerror = errorHandler;
    112  request.onupgradeneeded = errorHandler;
    113  request.onsuccess = grabEventAndContinueHandler;
    114  event = yield undefined;
    115 
    116  db = event.target.result;
    117  txn = db.transaction(storeName);
    118  objectStore = txn.objectStore(storeName);
    119  index1 = objectStore.index(indexName1);
    120 
    121  try {
    122    index1.name = indexName1;
    123    ok(
    124      false,
    125      "InvalidStateError shall be thrown if it's not an upgrade transaction."
    126    );
    127  } catch (e) {
    128    ok(e instanceof DOMException, "got a database exception");
    129    is(e.name, "InvalidStateError", "correct error");
    130  }
    131 
    132  txn.oncomplete = continueToNextStepSync;
    133  yield undefined;
    134  db.close();
    135 
    136  finishTest();
    137 }