dom/indexedDB/test/unit/test_indexes_bad_values.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /**
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/
     4  */
     6 var testGenerator = testSteps();
     8 function testSteps()
     9 {
    10   const name = this.window ? window.location.pathname : "Splendid Test";
    12   const objectStoreName = "People";
    14   const objectStoreData = [
    15     { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } },
    16     { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } },
    17     { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } },
    18     { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } },
    19     { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } },
    20     { key: "237-23-7737", value: { name: "Pat", height: 65 } },
    21     { key: "237-23-7738", value: { name: "Mel", height: 66, weight: {} } }
    22   ];
    24   const badObjectStoreData = [
    25     { key: "237-23-7739", value: { name: "Rob", height: 65 } },
    26     { key: "237-23-7740", value: { name: "Jen", height: 66, weight: {} } }
    27   ];
    29   const indexData = [
    30     { name: "weight", keyPath: "weight", options: { unique: false } }
    31   ];
    33   const objectStoreDataWeightSort = [
    34     { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } },
    35     { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } },
    36     { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } },
    37     { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } },
    38     { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }
    39   ];
    41   let request = indexedDB.open(name, 1);
    42   request.onerror = errorHandler;
    43   request.onupgradeneeded = grabEventAndContinueHandler;
    44   request.onsuccess = grabEventAndContinueHandler;
    45   let event = yield undefined;
    47   let db = event.target.result;
    49   let objectStore = db.createObjectStore(objectStoreName, { } );
    51   let addedData = 0;
    52   for (let i in objectStoreData) {
    53     request = objectStore.add(objectStoreData[i].value,
    54                               objectStoreData[i].key);
    55     request.onerror = errorHandler;
    56     request.onsuccess = function(event) {
    57       if (++addedData == objectStoreData.length) {
    58         testGenerator.send(event);
    59       }
    60     }
    61   }
    62   event = yield undefined;
    64   for (let i in indexData) {
    65     objectStore.createIndex(indexData[i].name, indexData[i].keyPath,
    66                             indexData[i].options);
    67   }
    69   addedData = 0;
    70   for (let i in badObjectStoreData) {
    71     request = objectStore.add(badObjectStoreData[i].value,
    72                               badObjectStoreData[i].key);
    73     request.onerror = errorHandler;
    74     request.onsuccess = function(event) {
    75       if (++addedData == badObjectStoreData.length) {
    76         executeSoon(function() { testGenerator.next() });
    77       }
    78     }
    79   }
    80   yield undefined;
    81   yield undefined;
    83   objectStore = db.transaction(objectStoreName)
    84                   .objectStore(objectStoreName);
    86   let keyIndex = 0;
    88   request = objectStore.index("weight").openKeyCursor();
    89   request.onerror = errorHandler;
    90   request.onsuccess = function (event) {
    91     let cursor = event.target.result;
    92     if (cursor) {
    93       is(cursor.key, objectStoreDataWeightSort[keyIndex].value.weight,
    94          "Correct key");
    95       is(cursor.primaryKey, objectStoreDataWeightSort[keyIndex].key,
    96          "Correct value");
    97       keyIndex++;
    99       cursor.continue();
   100     }
   101     else {
   102       testGenerator.next();
   103     }
   104   }
   105   yield undefined;
   107   is(keyIndex, objectStoreDataWeightSort.length, "Saw all weights");
   109   keyIndex = 0;
   111   request = objectStore.openCursor();
   112   request.onerror = errorHandler;
   113   request.onsuccess = function (event) {
   114     let cursor = event.target.result;
   115     if (cursor) {
   116       keyIndex++;
   117       cursor.continue();
   118     }
   119     else {
   120       testGenerator.next();
   121     }
   122   }
   123   yield undefined;
   125   is(keyIndex, objectStoreData.length + badObjectStoreData.length,
   126      "Saw all people");
   128   finishTest();
   129   yield undefined;
   130 }

mercurial