summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_index_object_cursors.js
blob: 992fe9e0e3e3d71fea0420d56a2fdce03a8e5323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/* exported testGenerator */
var testGenerator = testSteps();

function* testSteps() {
  const objectStoreData = [
    { name: "", options: { keyPath: "id", autoIncrement: true } },
    { name: null, options: { keyPath: "ss" } },
    { name: undefined, options: {} },
    { name: "4", options: { autoIncrement: true } },
  ];

  const indexData = [
    { name: "", keyPath: "name", options: { unique: true } },
    { name: null, keyPath: "height", options: {} },
  ];

  const data = [
    { ss: "237-23-7732", name: "Ann", height: 60 },
    { ss: "237-23-7733", name: "Bob", height: 65 },
  ];

  let request = indexedDB.open(
    this.window ? window.location.pathname : "Splendid Test",
    1
  );
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  let event = yield undefined;

  let db = event.target.result;
  db.onerror = errorHandler;

  event.target.onsuccess = continueToNextStep;

  for (let objectStoreIndex in objectStoreData) {
    const objectStoreInfo = objectStoreData[objectStoreIndex];
    let objectStore = db.createObjectStore(
      objectStoreInfo.name,
      objectStoreInfo.options
    );
    for (let indexIndex in indexData) {
      const indexInfo = indexData[indexIndex];
      objectStore.createIndex(
        indexInfo.name,
        indexInfo.keyPath,
        indexInfo.options
      );
    }
  }
  yield undefined;

  ok(true, "Initial setup");

  for (let objectStoreIndex in objectStoreData) {
    const info = objectStoreData[objectStoreIndex];

    for (let indexIndex in indexData) {
      const objectStoreName = objectStoreData[objectStoreIndex].name;
      const indexName = indexData[indexIndex].name;

      let objectStore = db
        .transaction(objectStoreName, "readwrite")
        .objectStore(objectStoreName);
      ok(true, "Got objectStore " + objectStoreName);

      for (let dataIndex in data) {
        const obj = data[dataIndex];
        let key;
        if (!info.options.keyPath && !info.options.autoIncrement) {
          key = obj.ss;
        }
        objectStore.add(obj, key);
      }

      let index = objectStore.index(indexName);
      ok(true, "Got index " + indexName);

      let keyIndex = 0;

      index.openCursor().onsuccess = function (event) {
        let cursor = event.target.result;
        if (!cursor) {
          continueToNextStep();
          return;
        }

        is(
          cursor.key,
          data[keyIndex][indexData[indexIndex].keyPath],
          "Good key"
        );
        is(cursor.value.ss, data[keyIndex].ss, "Correct ss");
        is(cursor.value.name, data[keyIndex].name, "Correct name");
        is(cursor.value.height, data[keyIndex].height, "Correct height");

        if (!keyIndex) {
          let obj = cursor.value;
          obj.updated = true;

          cursor.update(obj).onsuccess = function () {
            ok(true, "Object updated");
            cursor.continue();
            keyIndex++;
          };
          return;
        }

        cursor.delete().onsuccess = function () {
          ok(true, "Object deleted");
          cursor.continue();
          keyIndex++;
        };
      };
      yield undefined;

      is(keyIndex, 2, "Saw all the items");

      keyIndex = 0;

      db
        .transaction(objectStoreName)
        .objectStore(objectStoreName)
        .openCursor().onsuccess = function (event) {
        let cursor = event.target.result;
        if (!cursor) {
          continueToNextStep();
          return;
        }

        is(cursor.value.ss, data[keyIndex].ss, "Correct ss");
        is(cursor.value.name, data[keyIndex].name, "Correct name");
        is(cursor.value.height, data[keyIndex].height, "Correct height");
        is(cursor.value.updated, true, "Correct updated flag");

        cursor.continue();
        keyIndex++;
      };
      yield undefined;

      is(keyIndex, 1, "Saw all the items");

      db
        .transaction(objectStoreName, "readwrite")
        .objectStore(objectStoreName)
        .clear().onsuccess = continueToNextStep;
      yield undefined;

      objectStore = index = null; // Bug 943409 workaround.
    }
  }

  finishTest();
}