tor-browser

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

test_index_getAllObjects.js (7304B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 var testGenerator = testSteps();
      7 
      8 function* testSteps() {
      9  const name = this.window ? window.location.pathname : "Splendid Test";
     10  const objectStoreName = "People";
     11 
     12  const objectStoreData = [
     13    { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } },
     14    { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } },
     15    { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } },
     16    { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } },
     17    { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } },
     18    { key: "237-23-7737", value: { name: "Pat", height: 65 } },
     19  ];
     20 
     21  const indexData = [
     22    { name: "name", keyPath: "name", options: { unique: true } },
     23    { name: "height", keyPath: "height", options: { unique: false } },
     24    { name: "weight", keyPath: "weight", options: { unique: false } },
     25  ];
     26 
     27  const objectStoreDataHeightSort = [
     28    { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } },
     29    { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } },
     30    { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } },
     31    { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } },
     32    { key: "237-23-7737", value: { name: "Pat", height: 65 } },
     33    { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } },
     34  ];
     35 
     36  let request = indexedDB.open(name, 1);
     37  request.onerror = errorHandler;
     38  request.onupgradeneeded = grabEventAndContinueHandler;
     39  request.onsuccess = grabEventAndContinueHandler;
     40  let event = yield undefined;
     41 
     42  let db = event.target.result;
     43 
     44  let objectStore = db.createObjectStore(objectStoreName, {});
     45 
     46  // First, add all our data to the object store.
     47  let addedData = 0;
     48  for (let i in objectStoreData) {
     49    request = objectStore.add(objectStoreData[i].value, objectStoreData[i].key);
     50    request.onerror = errorHandler;
     51    request.onsuccess = function (event) {
     52      if (++addedData == objectStoreData.length) {
     53        testGenerator.next(event);
     54      }
     55    };
     56  }
     57  event = yield undefined;
     58 
     59  // Now create the indexes.
     60  for (let i in indexData) {
     61    objectStore.createIndex(
     62      indexData[i].name,
     63      indexData[i].keyPath,
     64      indexData[i].options
     65    );
     66  }
     67 
     68  is(objectStore.indexNames.length, indexData.length, "Good index count");
     69  yield undefined;
     70 
     71  objectStore = db.transaction(objectStoreName).objectStore(objectStoreName);
     72 
     73  request = objectStore.index("height").mozGetAll(65);
     74  request.onerror = errorHandler;
     75  request.onsuccess = grabEventAndContinueHandler;
     76  event = yield undefined;
     77 
     78  is(event.target.result instanceof Array, true, "Got an array object");
     79  is(event.target.result.length, 2, "Correct length");
     80 
     81  for (let i in event.target.result) {
     82    let result = event.target.result[i];
     83    let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value;
     84 
     85    is(result.name, testObj.name, "Correct name");
     86    is(result.height, testObj.height, "Correct height");
     87 
     88    if (testObj.hasOwnProperty("weight")) {
     89      is(result.weight, testObj.weight, "Correct weight");
     90    }
     91  }
     92 
     93  request = objectStore.index("height").mozGetAll(65, 0);
     94  request.onerror = errorHandler;
     95  request.onsuccess = grabEventAndContinueHandler;
     96  event = yield undefined;
     97 
     98  is(event.target.result instanceof Array, true, "Got an array object");
     99  is(event.target.result.length, 2, "Correct length");
    100 
    101  for (let i in event.target.result) {
    102    let result = event.target.result[i];
    103    let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value;
    104 
    105    is(result.name, testObj.name, "Correct name");
    106    is(result.height, testObj.height, "Correct height");
    107 
    108    if (testObj.hasOwnProperty("weight")) {
    109      is(result.weight, testObj.weight, "Correct weight");
    110    }
    111  }
    112 
    113  request = objectStore.index("height").mozGetAll(65, null);
    114  request.onerror = errorHandler;
    115  request.onsuccess = grabEventAndContinueHandler;
    116  event = yield undefined;
    117 
    118  is(event.target.result instanceof Array, true, "Got an array object");
    119  is(event.target.result.length, 2, "Correct length");
    120 
    121  for (let i in event.target.result) {
    122    let result = event.target.result[i];
    123    let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value;
    124 
    125    is(result.name, testObj.name, "Correct name");
    126    is(result.height, testObj.height, "Correct height");
    127 
    128    if (testObj.hasOwnProperty("weight")) {
    129      is(result.weight, testObj.weight, "Correct weight");
    130    }
    131  }
    132 
    133  request = objectStore.index("height").mozGetAll(65, undefined);
    134  request.onerror = errorHandler;
    135  request.onsuccess = grabEventAndContinueHandler;
    136  event = yield undefined;
    137 
    138  is(event.target.result instanceof Array, true, "Got an array object");
    139  is(event.target.result.length, 2, "Correct length");
    140 
    141  for (let i in event.target.result) {
    142    let result = event.target.result[i];
    143    let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value;
    144 
    145    is(result.name, testObj.name, "Correct name");
    146    is(result.height, testObj.height, "Correct height");
    147 
    148    if (testObj.hasOwnProperty("weight")) {
    149      is(result.weight, testObj.weight, "Correct weight");
    150    }
    151  }
    152 
    153  request = objectStore.index("height").mozGetAll();
    154  request.onerror = errorHandler;
    155  request.onsuccess = grabEventAndContinueHandler;
    156  event = yield undefined;
    157 
    158  is(event.target.result instanceof Array, true, "Got an array object");
    159  is(
    160    event.target.result.length,
    161    objectStoreDataHeightSort.length,
    162    "Correct length"
    163  );
    164 
    165  for (let i in event.target.result) {
    166    let result = event.target.result[i];
    167    let testObj = objectStoreDataHeightSort[i].value;
    168 
    169    is(result.name, testObj.name, "Correct name");
    170    is(result.height, testObj.height, "Correct height");
    171 
    172    if (testObj.hasOwnProperty("weight")) {
    173      is(result.weight, testObj.weight, "Correct weight");
    174    }
    175  }
    176 
    177  request = objectStore.index("height").mozGetAll(null, 4);
    178  request.onerror = errorHandler;
    179  request.onsuccess = grabEventAndContinueHandler;
    180  event = yield undefined;
    181 
    182  is(event.target.result instanceof Array, true, "Got an array object");
    183  is(event.target.result.length, 4, "Correct length");
    184 
    185  for (let i in event.target.result) {
    186    let result = event.target.result[i];
    187    let testObj = objectStoreDataHeightSort[i].value;
    188 
    189    is(result.name, testObj.name, "Correct name");
    190    is(result.height, testObj.height, "Correct height");
    191 
    192    if (testObj.hasOwnProperty("weight")) {
    193      is(result.weight, testObj.weight, "Correct weight");
    194    }
    195  }
    196 
    197  request = objectStore.index("height").mozGetAll(65, 1);
    198  request.onerror = errorHandler;
    199  request.onsuccess = grabEventAndContinueHandler;
    200  event = yield undefined;
    201 
    202  is(event.target.result instanceof Array, true, "Got an array object");
    203  is(event.target.result.length, 1, "Correct length");
    204 
    205  for (let i in event.target.result) {
    206    let result = event.target.result[i];
    207    let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value;
    208 
    209    is(result.name, testObj.name, "Correct name");
    210    is(result.height, testObj.height, "Correct height");
    211 
    212    if (testObj.hasOwnProperty("weight")) {
    213      is(result.weight, testObj.weight, "Correct weight");
    214    }
    215  }
    216 
    217  finishTest();
    218 }