tor-browser

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

test_autoIncrement.js (15851B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 /* exported testGenerator, disableWorkerTest */
      7 var disableWorkerTest = "Need to implement a gc() function for worker tests";
      8 
      9 if (!this.window) {
     10  this.runTest = function () {
     11    todo(false, "Test disabled in xpcshell test suite for now");
     12    finishTest();
     13  };
     14 }
     15 
     16 var testGenerator = testSteps();
     17 
     18 function genCheck(key, value, test, options) {
     19  return function (event) {
     20    is(
     21      JSON.stringify(event.target.result),
     22      JSON.stringify(key),
     23      "correct returned key in " + test
     24    );
     25    if (options && options.store) {
     26      is(event.target.source, options.store, "correct store in " + test);
     27    }
     28    if (options && options.trans) {
     29      is(
     30        event.target.transaction,
     31        options.trans,
     32        "correct transaction in " + test
     33      );
     34    }
     35 
     36    event.target.source.get(key).onsuccess = function (event) {
     37      is(
     38        JSON.stringify(event.target.result),
     39        JSON.stringify(value),
     40        "correct stored value in " + test
     41      );
     42      continueToNextStepSync();
     43    };
     44  };
     45 }
     46 
     47 function* testSteps() {
     48  const dbname = this.window ? window.location.pathname : "Splendid Test";
     49  const RW = "readwrite";
     50  let c1 = 1;
     51  let c2 = 1;
     52 
     53  let openRequest = indexedDB.open(dbname, 1);
     54  openRequest.onerror = errorHandler;
     55  openRequest.onupgradeneeded = grabEventAndContinueHandler;
     56  openRequest.onsuccess = unexpectedSuccessHandler;
     57  let event = yield undefined;
     58  let db = event.target.result;
     59  let trans = event.target.transaction;
     60 
     61  // Create test stores
     62  let store1 = db.createObjectStore("store1", { autoIncrement: true });
     63  let store2 = db.createObjectStore("store2", {
     64    autoIncrement: true,
     65    keyPath: "id",
     66  });
     67  let store3 = db.createObjectStore("store3", { autoIncrement: false });
     68  is(store1.autoIncrement, true, "store1 .autoIncrement");
     69  is(store2.autoIncrement, true, "store2 .autoIncrement");
     70  is(store3.autoIncrement, false, "store3 .autoIncrement");
     71 
     72  store1.createIndex("unique1", "unique", { unique: true });
     73  store2.createIndex("unique1", "unique", { unique: true });
     74 
     75  // Test simple inserts
     76  let test = " for test simple insert";
     77  store1.add({ foo: "value1" }).onsuccess = genCheck(
     78    c1++,
     79    { foo: "value1" },
     80    "first" + test
     81  );
     82  store1.add({ foo: "value2" }).onsuccess = genCheck(
     83    c1++,
     84    { foo: "value2" },
     85    "second" + test
     86  );
     87 
     88  yield undefined;
     89  yield undefined;
     90 
     91  store2.put({ bar: "value1" }).onsuccess = genCheck(
     92    c2,
     93    { bar: "value1", id: c2 },
     94    "first in store2" + test,
     95    { store: store2 }
     96  );
     97  c2++;
     98  store1.put({ foo: "value3" }).onsuccess = genCheck(
     99    c1++,
    100    { foo: "value3" },
    101    "third" + test,
    102    { store: store1 }
    103  );
    104 
    105  yield undefined;
    106  yield undefined;
    107 
    108  store2.get(IDBKeyRange.lowerBound(c2)).onsuccess =
    109    grabEventAndContinueHandler;
    110  event = yield undefined;
    111  is(event.target.result, undefined, "no such value" + test);
    112 
    113  // Close version_change transaction
    114  openRequest.onsuccess = grabEventAndContinueHandler;
    115  event = yield undefined;
    116 
    117  is(event.target, openRequest, "succeeded to open" + test);
    118  is(event.type, "success", "succeeded to open" + test);
    119 
    120  // Test inserting explicit keys
    121  test = " for test explicit keys";
    122  trans = db.transaction("store1", RW);
    123  trans.objectStore("store1").add({ explicit: 1 }, 100).onsuccess = genCheck(
    124    100,
    125    { explicit: 1 },
    126    "first" + test
    127  );
    128  c1 = 101;
    129  trans = db.transaction("store1", RW);
    130  trans.objectStore("store1").add({ explicit: 2 }).onsuccess = genCheck(
    131    c1++,
    132    { explicit: 2 },
    133    "second" + test
    134  );
    135  yield undefined;
    136  yield undefined;
    137 
    138  trans = db.transaction("store1", RW);
    139  trans.objectStore("store1").add({ explicit: 3 }, 200).onsuccess = genCheck(
    140    200,
    141    { explicit: 3 },
    142    "third" + test
    143  );
    144  c1 = 201;
    145  trans.objectStore("store1").add({ explicit: 4 }).onsuccess = genCheck(
    146    c1++,
    147    { explicit: 4 },
    148    "fourth" + test
    149  );
    150  yield undefined;
    151  yield undefined;
    152 
    153  trans = db.transaction("store1", RW);
    154  trans.objectStore("store1").add({ explicit: 5 }, 150).onsuccess = genCheck(
    155    150,
    156    { explicit: 5 },
    157    "fifth" + test
    158  );
    159  yield undefined;
    160  trans.objectStore("store1").add({ explicit: 6 }).onsuccess = genCheck(
    161    c1++,
    162    { explicit: 6 },
    163    "sixth" + test
    164  );
    165  yield undefined;
    166 
    167  trans = db.transaction("store1", RW);
    168  trans.objectStore("store1").add({ explicit: 7 }, "key").onsuccess = genCheck(
    169    "key",
    170    { explicit: 7 },
    171    "seventh" + test
    172  );
    173  yield undefined;
    174  trans.objectStore("store1").add({ explicit: 8 }).onsuccess = genCheck(
    175    c1++,
    176    { explicit: 8 },
    177    "eighth" + test
    178  );
    179  yield undefined;
    180 
    181  trans = db.transaction("store1", RW);
    182  trans.objectStore("store1").add({ explicit: 7 }, [100000]).onsuccess =
    183    genCheck([100000], { explicit: 7 }, "seventh" + test);
    184  yield undefined;
    185  trans.objectStore("store1").add({ explicit: 8 }).onsuccess = genCheck(
    186    c1++,
    187    { explicit: 8 },
    188    "eighth" + test
    189  );
    190  yield undefined;
    191 
    192  trans = db.transaction("store1", RW);
    193  trans.objectStore("store1").add({ explicit: 9 }, -100000).onsuccess =
    194    genCheck(-100000, { explicit: 9 }, "ninth" + test);
    195  yield undefined;
    196  trans.objectStore("store1").add({ explicit: 10 }).onsuccess = genCheck(
    197    c1++,
    198    { explicit: 10 },
    199    "tenth" + test
    200  );
    201  yield undefined;
    202 
    203  trans = db.transaction("store2", RW);
    204  trans.objectStore("store2").add({ explicit2: 1, id: 300 }).onsuccess =
    205    genCheck(300, { explicit2: 1, id: 300 }, "first store2" + test);
    206  c2 = 301;
    207  trans = db.transaction("store2", RW);
    208  trans.objectStore("store2").add({ explicit2: 2 }).onsuccess = genCheck(
    209    c2,
    210    { explicit2: 2, id: c2 },
    211    "second store2" + test
    212  );
    213  c2++;
    214  yield undefined;
    215  yield undefined;
    216 
    217  trans = db.transaction("store2", RW);
    218  trans.objectStore("store2").add({ explicit2: 3, id: 400 }).onsuccess =
    219    genCheck(400, { explicit2: 3, id: 400 }, "third store2" + test);
    220  c2 = 401;
    221  trans.objectStore("store2").add({ explicit2: 4 }).onsuccess = genCheck(
    222    c2,
    223    { explicit2: 4, id: c2 },
    224    "fourth store2" + test
    225  );
    226  c2++;
    227  yield undefined;
    228  yield undefined;
    229 
    230  trans = db.transaction("store2", RW);
    231  trans.objectStore("store2").add({ explicit: 5, id: 150 }).onsuccess =
    232    genCheck(150, { explicit: 5, id: 150 }, "fifth store2" + test);
    233  yield undefined;
    234  trans.objectStore("store2").add({ explicit: 6 }).onsuccess = genCheck(
    235    c2,
    236    { explicit: 6, id: c2 },
    237    "sixth store2" + test
    238  );
    239  c2++;
    240  yield undefined;
    241 
    242  trans = db.transaction("store2", RW);
    243  trans.objectStore("store2").add({ explicit: 7, id: "key" }).onsuccess =
    244    genCheck("key", { explicit: 7, id: "key" }, "seventh store2" + test);
    245  yield undefined;
    246  trans.objectStore("store2").add({ explicit: 8 }).onsuccess = genCheck(
    247    c2,
    248    { explicit: 8, id: c2 },
    249    "eighth store2" + test
    250  );
    251  c2++;
    252  yield undefined;
    253 
    254  trans = db.transaction("store2", RW);
    255  trans.objectStore("store2").add({ explicit: 7, id: [100000] }).onsuccess =
    256    genCheck([100000], { explicit: 7, id: [100000] }, "seventh store2" + test);
    257  yield undefined;
    258  trans.objectStore("store2").add({ explicit: 8 }).onsuccess = genCheck(
    259    c2,
    260    { explicit: 8, id: c2 },
    261    "eighth store2" + test
    262  );
    263  c2++;
    264  yield undefined;
    265 
    266  trans = db.transaction("store2", RW);
    267  trans.objectStore("store2").add({ explicit: 9, id: -100000 }).onsuccess =
    268    genCheck(-100000, { explicit: 9, id: -100000 }, "ninth store2" + test);
    269  yield undefined;
    270  trans.objectStore("store2").add({ explicit: 10 }).onsuccess = genCheck(
    271    c2,
    272    { explicit: 10, id: c2 },
    273    "tenth store2" + test
    274  );
    275  c2++;
    276  yield undefined;
    277 
    278  // Test separate transactions doesn't generate overlapping numbers
    279  test = " for test non-overlapping counts";
    280  trans = db.transaction("store1", RW);
    281  let trans2 = db.transaction("store1", RW);
    282  trans2.objectStore("store1").put({ over: 2 }).onsuccess = genCheck(
    283    c1 + 1,
    284    { over: 2 },
    285    "first" + test,
    286    { trans: trans2 }
    287  );
    288  trans.objectStore("store1").put({ over: 1 }).onsuccess = genCheck(
    289    c1,
    290    { over: 1 },
    291    "second" + test,
    292    { trans }
    293  );
    294  c1 += 2;
    295  yield undefined;
    296  yield undefined;
    297 
    298  trans = db.transaction("store2", RW);
    299  trans2 = db.transaction("store2", RW);
    300  trans2.objectStore("store2").put({ over: 2 }).onsuccess = genCheck(
    301    c2 + 1,
    302    { over: 2, id: c2 + 1 },
    303    "third" + test,
    304    { trans: trans2 }
    305  );
    306  trans.objectStore("store2").put({ over: 1 }).onsuccess = genCheck(
    307    c2,
    308    { over: 1, id: c2 },
    309    "fourth" + test,
    310    { trans }
    311  );
    312  c2 += 2;
    313  yield undefined;
    314  yield undefined;
    315 
    316  // Test that error inserts doesn't increase generator
    317  test = " for test error inserts";
    318  trans = db.transaction(["store1", "store2"], RW);
    319  trans.objectStore("store1").add({ unique: 1 }, -1);
    320  trans.objectStore("store2").add({ unique: 1, id: "unique" });
    321 
    322  trans
    323    .objectStore("store1")
    324    .add({ error: 1, unique: 1 })
    325    .addEventListener("error", new ExpectError("ConstraintError", true));
    326  trans.objectStore("store1").add({ error: 2 }).onsuccess = genCheck(
    327    c1++,
    328    { error: 2 },
    329    "first" + test
    330  );
    331  yield undefined;
    332  yield undefined;
    333 
    334  trans
    335    .objectStore("store2")
    336    .add({ error: 3, unique: 1 })
    337    .addEventListener("error", new ExpectError("ConstraintError", true));
    338  trans.objectStore("store2").add({ error: 4 }).onsuccess = genCheck(
    339    c2,
    340    { error: 4, id: c2 },
    341    "second" + test
    342  );
    343  c2++;
    344  yield undefined;
    345  yield undefined;
    346 
    347  trans
    348    .objectStore("store1")
    349    .add({ error: 5, unique: 1 }, 100000)
    350    .addEventListener("error", new ExpectError("ConstraintError", true));
    351  trans.objectStore("store1").add({ error: 6 }).onsuccess = genCheck(
    352    c1++,
    353    { error: 6 },
    354    "third" + test
    355  );
    356  yield undefined;
    357  yield undefined;
    358 
    359  trans
    360    .objectStore("store2")
    361    .add({ error: 7, unique: 1, id: 100000 })
    362    .addEventListener("error", new ExpectError("ConstraintError", true));
    363  trans.objectStore("store2").add({ error: 8 }).onsuccess = genCheck(
    364    c2,
    365    { error: 8, id: c2 },
    366    "fourth" + test
    367  );
    368  c2++;
    369  yield undefined;
    370  yield undefined;
    371 
    372  // Test that aborts doesn't increase generator
    373  test = " for test aborted transaction";
    374  trans = db.transaction(["store1", "store2"], RW);
    375  trans.objectStore("store1").add({ abort: 1 }).onsuccess = genCheck(
    376    c1,
    377    { abort: 1 },
    378    "first" + test
    379  );
    380  trans.objectStore("store2").put({ abort: 2 }).onsuccess = genCheck(
    381    c2,
    382    { abort: 2, id: c2 },
    383    "second" + test
    384  );
    385  yield undefined;
    386  yield undefined;
    387 
    388  trans.objectStore("store1").add({ abort: 3 }, 500).onsuccess = genCheck(
    389    500,
    390    { abort: 3 },
    391    "third" + test
    392  );
    393  trans.objectStore("store2").put({ abort: 4, id: 600 }).onsuccess = genCheck(
    394    600,
    395    { abort: 4, id: 600 },
    396    "fourth" + test
    397  );
    398  yield undefined;
    399  yield undefined;
    400 
    401  trans.objectStore("store1").add({ abort: 5 }).onsuccess = genCheck(
    402    501,
    403    { abort: 5 },
    404    "fifth" + test
    405  );
    406  trans.objectStore("store2").put({ abort: 6 }).onsuccess = genCheck(
    407    601,
    408    { abort: 6, id: 601 },
    409    "sixth" + test
    410  );
    411  yield undefined;
    412  yield undefined;
    413 
    414  trans.abort();
    415  trans.onabort = grabEventAndContinueHandler;
    416  event = yield;
    417  is(event.type, "abort", "transaction aborted");
    418  is(event.target, trans, "correct transaction aborted");
    419 
    420  trans = db.transaction(["store1", "store2"], RW);
    421  trans.objectStore("store1").add({ abort: 1 }).onsuccess = genCheck(
    422    c1++,
    423    { abort: 1 },
    424    "re-first" + test
    425  );
    426  trans.objectStore("store2").put({ abort: 2 }).onsuccess = genCheck(
    427    c2,
    428    { abort: 2, id: c2 },
    429    "re-second" + test
    430  );
    431  c2++;
    432  yield undefined;
    433  yield undefined;
    434 
    435  // Test that delete doesn't decrease generator
    436  test = " for test delete items";
    437  trans = db.transaction(["store1", "store2"], RW);
    438  trans.objectStore("store1").add({ delete: 1 }).onsuccess = genCheck(
    439    c1++,
    440    { delete: 1 },
    441    "first" + test
    442  );
    443  trans.objectStore("store2").put({ delete: 2 }).onsuccess = genCheck(
    444    c2,
    445    { delete: 2, id: c2 },
    446    "second" + test
    447  );
    448  c2++;
    449  yield undefined;
    450  yield undefined;
    451 
    452  trans.objectStore("store1").delete(c1 - 1).onsuccess =
    453    grabEventAndContinueHandler;
    454  trans.objectStore("store2").delete(c2 - 1).onsuccess =
    455    grabEventAndContinueHandler;
    456  yield undefined;
    457  yield undefined;
    458 
    459  trans.objectStore("store1").add({ delete: 3 }).onsuccess = genCheck(
    460    c1++,
    461    { delete: 3 },
    462    "first" + test
    463  );
    464  trans.objectStore("store2").put({ delete: 4 }).onsuccess = genCheck(
    465    c2,
    466    { delete: 4, id: c2 },
    467    "second" + test
    468  );
    469  c2++;
    470  yield undefined;
    471  yield undefined;
    472 
    473  trans.objectStore("store1").delete(c1 - 1).onsuccess =
    474    grabEventAndContinueHandler;
    475  trans.objectStore("store2").delete(c2 - 1).onsuccess =
    476    grabEventAndContinueHandler;
    477  yield undefined;
    478  yield undefined;
    479 
    480  trans = db.transaction(["store1", "store2"], RW);
    481  trans.objectStore("store1").add({ delete: 5 }).onsuccess = genCheck(
    482    c1++,
    483    { delete: 5 },
    484    "first" + test
    485  );
    486  trans.objectStore("store2").put({ delete: 6 }).onsuccess = genCheck(
    487    c2,
    488    { delete: 6, id: c2 },
    489    "second" + test
    490  );
    491  c2++;
    492  yield undefined;
    493  yield undefined;
    494 
    495  // Test that clears doesn't decrease generator
    496  test = " for test clear stores";
    497  trans = db.transaction(["store1", "store2"], RW);
    498  trans.objectStore("store1").add({ clear: 1 }).onsuccess = genCheck(
    499    c1++,
    500    { clear: 1 },
    501    "first" + test
    502  );
    503  trans.objectStore("store2").put({ clear: 2 }).onsuccess = genCheck(
    504    c2,
    505    { clear: 2, id: c2 },
    506    "second" + test
    507  );
    508  c2++;
    509  yield undefined;
    510  yield undefined;
    511 
    512  trans.objectStore("store1").clear().onsuccess = grabEventAndContinueHandler;
    513  trans.objectStore("store2").clear().onsuccess = grabEventAndContinueHandler;
    514  yield undefined;
    515  yield undefined;
    516 
    517  trans.objectStore("store1").add({ clear: 3 }).onsuccess = genCheck(
    518    c1++,
    519    { clear: 3 },
    520    "third" + test
    521  );
    522  trans.objectStore("store2").put({ clear: 4 }).onsuccess = genCheck(
    523    c2,
    524    { clear: 4, id: c2 },
    525    "forth" + test
    526  );
    527  c2++;
    528  yield undefined;
    529  yield undefined;
    530 
    531  trans.objectStore("store1").clear().onsuccess = grabEventAndContinueHandler;
    532  trans.objectStore("store2").clear().onsuccess = grabEventAndContinueHandler;
    533  yield undefined;
    534  yield undefined;
    535 
    536  trans = db.transaction(["store1", "store2"], RW);
    537  trans.objectStore("store1").add({ clear: 5 }).onsuccess = genCheck(
    538    c1++,
    539    { clear: 5 },
    540    "fifth" + test
    541  );
    542  trans.objectStore("store2").put({ clear: 6 }).onsuccess = genCheck(
    543    c2,
    544    { clear: 6, id: c2 },
    545    "sixth" + test
    546  );
    547  c2++;
    548  yield undefined;
    549  yield undefined;
    550 
    551  // Test that close/reopen doesn't decrease generator
    552  test = " for test clear stores";
    553  trans = db.transaction(["store1", "store2"], RW);
    554  trans.objectStore("store1").clear().onsuccess = grabEventAndContinueHandler;
    555  trans.objectStore("store2").clear().onsuccess = grabEventAndContinueHandler;
    556  yield undefined;
    557  yield undefined;
    558  db.close();
    559 
    560  gc();
    561 
    562  openRequest = indexedDB.open(dbname, 2);
    563  openRequest.onerror = errorHandler;
    564  openRequest.onupgradeneeded = grabEventAndContinueHandler;
    565  openRequest.onsuccess = unexpectedSuccessHandler;
    566  event = yield undefined;
    567  db = event.target.result;
    568  trans = event.target.transaction;
    569 
    570  trans.objectStore("store1").add({ reopen: 1 }).onsuccess = genCheck(
    571    c1++,
    572    { reopen: 1 },
    573    "first" + test
    574  );
    575  trans.objectStore("store2").put({ reopen: 2 }).onsuccess = genCheck(
    576    c2,
    577    { reopen: 2, id: c2 },
    578    "second" + test
    579  );
    580  c2++;
    581  yield undefined;
    582  yield undefined;
    583 
    584  openRequest.onsuccess = grabEventAndContinueHandler;
    585  yield undefined;
    586 
    587  finishTest();
    588 }