dom/indexedDB/test/unit/test_index_object_cursors.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/unit/test_index_object_cursors.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,147 @@
     1.4 +/**
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +
     1.9 +var testGenerator = testSteps();
    1.10 +
    1.11 +function testSteps()
    1.12 +{
    1.13 +  const objectStoreData = [
    1.14 +    { name: "", options: { keyPath: "id", autoIncrement: true } },
    1.15 +    { name: null, options: { keyPath: "ss" } },
    1.16 +    { name: undefined, options: { } },
    1.17 +    { name: "4", options: { autoIncrement: true } },
    1.18 +  ];
    1.19 +
    1.20 +  const indexData = [
    1.21 +    { name: "", keyPath: "name", options: { unique: true } },
    1.22 +    { name: null, keyPath: "height", options: { } }
    1.23 +  ];
    1.24 +
    1.25 +  const data = [
    1.26 +    { ss: "237-23-7732", name: "Ann", height: 60 },
    1.27 +    { ss: "237-23-7733", name: "Bob", height: 65 }
    1.28 +  ];
    1.29 +
    1.30 +  let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1);
    1.31 +  request.onerror = errorHandler;
    1.32 +  request.onupgradeneeded = grabEventAndContinueHandler;
    1.33 +  let event = yield undefined;
    1.34 +
    1.35 +  let db = event.target.result;
    1.36 +  db.onerror = errorHandler;
    1.37 +
    1.38 +  event.target.onsuccess = continueToNextStep;
    1.39 +
    1.40 +  for (let objectStoreIndex in objectStoreData) {
    1.41 +    const objectStoreInfo = objectStoreData[objectStoreIndex];
    1.42 +    let objectStore = db.createObjectStore(objectStoreInfo.name,
    1.43 +                                           objectStoreInfo.options);
    1.44 +    for (let indexIndex in indexData) {
    1.45 +      const indexInfo = indexData[indexIndex];
    1.46 +      let index = objectStore.createIndex(indexInfo.name,
    1.47 +                                          indexInfo.keyPath,
    1.48 +                                          indexInfo.options);
    1.49 +    }
    1.50 +  }
    1.51 +  yield undefined;
    1.52 +
    1.53 +  ok(true, "Initial setup");
    1.54 +
    1.55 +  for (let objectStoreIndex in objectStoreData) {
    1.56 +    const info = objectStoreData[objectStoreIndex];
    1.57 +
    1.58 +    for (let indexIndex in indexData) {
    1.59 +      const objectStoreName = objectStoreData[objectStoreIndex].name;
    1.60 +      const indexName = indexData[indexIndex].name;
    1.61 +
    1.62 +      let objectStore =
    1.63 +        db.transaction(objectStoreName, "readwrite")
    1.64 +          .objectStore(objectStoreName);
    1.65 +      ok(true, "Got objectStore " + objectStoreName);
    1.66 +
    1.67 +      for (let dataIndex in data) {
    1.68 +        const obj = data[dataIndex];
    1.69 +        let key;
    1.70 +        if (!info.options.keyPath && !info.options.autoIncrement) {
    1.71 +          key = obj.ss;
    1.72 +        }
    1.73 +        objectStore.add(obj, key);
    1.74 +      }
    1.75 +
    1.76 +      let index = objectStore.index(indexName);
    1.77 +      ok(true, "Got index " + indexName);
    1.78 +
    1.79 +      let keyIndex = 0;
    1.80 +
    1.81 +      index.openCursor().onsuccess = function(event) {
    1.82 +        let cursor = event.target.result;
    1.83 +        if (!cursor) {
    1.84 +          continueToNextStep();
    1.85 +          return;
    1.86 +        }
    1.87 +
    1.88 +        is(cursor.key, data[keyIndex][indexData[indexIndex].keyPath],
    1.89 +           "Good key");
    1.90 +        is(cursor.value.ss, data[keyIndex].ss, "Correct ss");
    1.91 +        is(cursor.value.name, data[keyIndex].name, "Correct name");
    1.92 +        is(cursor.value.height, data[keyIndex].height, "Correct height");
    1.93 +
    1.94 +        if (!keyIndex) {
    1.95 +          let obj = cursor.value;
    1.96 +          obj.updated = true;
    1.97 +
    1.98 +          cursor.update(obj).onsuccess = function(event) {
    1.99 +            ok(true, "Object updated");
   1.100 +            cursor.continue();
   1.101 +            keyIndex++
   1.102 +          }
   1.103 +          return;
   1.104 +        }
   1.105 +
   1.106 +        cursor.delete().onsuccess = function(event) {
   1.107 +          ok(true, "Object deleted");
   1.108 +          cursor.continue();
   1.109 +          keyIndex++
   1.110 +        }
   1.111 +      };
   1.112 +      yield undefined;
   1.113 +
   1.114 +      is(keyIndex, 2, "Saw all the items");
   1.115 +
   1.116 +      keyIndex = 0;
   1.117 +
   1.118 +      db.transaction(objectStoreName).objectStore(objectStoreName)
   1.119 +                                     .openCursor()
   1.120 +                                     .onsuccess = function(event) {
   1.121 +        let cursor = event.target.result;
   1.122 +        if (!cursor) {
   1.123 +          continueToNextStep();
   1.124 +          return;
   1.125 +        }
   1.126 +
   1.127 +        is(cursor.value.ss, data[keyIndex].ss, "Correct ss");
   1.128 +        is(cursor.value.name, data[keyIndex].name, "Correct name");
   1.129 +        is(cursor.value.height, data[keyIndex].height, "Correct height");
   1.130 +        is(cursor.value.updated, true, "Correct updated flag");
   1.131 +
   1.132 +        cursor.continue();
   1.133 +        keyIndex++;
   1.134 +      };
   1.135 +      yield undefined;
   1.136 +
   1.137 +      is(keyIndex, 1, "Saw all the items");
   1.138 +
   1.139 +      db.transaction(objectStoreName, "readwrite")
   1.140 +        .objectStore(objectStoreName).clear()
   1.141 +        .onsuccess = continueToNextStep;
   1.142 +      yield undefined;
   1.143 +
   1.144 +      objectStore = index = null; // Bug 943409 workaround.
   1.145 +    }
   1.146 +  }
   1.147 +
   1.148 +  finishTest();
   1.149 +  yield undefined;
   1.150 +}

mercurial