summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_index_update_delete.js
blob: 860087aadf73539fb211671bdeace5a43e536918 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

var testGenerator = testSteps();

function testSteps()
{
  let name = this.window ? window.location.pathname : "Splendid Test";
  let request = indexedDB.open(name, 1);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = grabEventAndContinueHandler;

  let event = yield undefined;

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

  for (let autoIncrement of [false, true]) {
    let objectStore =
      db.createObjectStore(autoIncrement, { keyPath: "id",
                                            autoIncrement: autoIncrement });

    for (let i = 0; i < 10; i++) {
      objectStore.add({ id: i, index: i });
    }

    for (let unique of [false, true]) {
      objectStore.createIndex(unique, "index", { unique: unique });
    }

    for (let i = 10; i < 20; i++) {
      objectStore.add({ id: i, index: i });
    }
  }

  event = yield undefined;
  is(event.type, "success", "expect a success event");

  for (let autoIncrement of [false, true]) {
    let objectStore = db.transaction(autoIncrement)
                        .objectStore(autoIncrement);

    objectStore.count().onsuccess = grabEventAndContinueHandler;
    let event = yield undefined;

    is(event.target.result, 20, "Correct number of entries in objectStore");

    let objectStoreCount = event.target.result;
    let indexCount = event.target.result;

    for (let unique of [false, true]) {
      let index = db.transaction(autoIncrement, "readwrite")
                    .objectStore(autoIncrement)
                    .index(unique);

      index.count().onsuccess = grabEventAndContinueHandler;
      let event = yield undefined;

      is(event.target.result, indexCount,
         "Correct number of entries in index");

      let modifiedEntry = unique ? 5 : 10;
      let keyRange = IDBKeyRange.only(modifiedEntry);

      let sawEntry = false;
      index.openCursor(keyRange).onsuccess = function(event) {
        let cursor = event.target.result;
        if (cursor) {
          sawEntry = true;
          is(cursor.key, modifiedEntry, "Correct key");

          cursor.value.index = unique ? 30 : 35;
          cursor.update(cursor.value).onsuccess = function(event) {
            cursor.continue();
          }
        }
        else {
          continueToNextStep();
        }
      }
      yield undefined;

      is(sawEntry, true, "Saw entry for key value " + modifiedEntry);

      // Recount index. Shouldn't change.
      index = db.transaction(autoIncrement, "readwrite")
                .objectStore(autoIncrement)
                .index(unique);

      index.count().onsuccess = grabEventAndContinueHandler;
      event = yield undefined;

      is(event.target.result, indexCount,
         "Correct number of entries in index");

      modifiedEntry = unique ? 30 : 35;
      keyRange = IDBKeyRange.only(modifiedEntry);

      sawEntry = false;
      index.openCursor(keyRange).onsuccess = function(event) {
        let cursor = event.target.result;
        if (cursor) {
          sawEntry = true;
          is(cursor.key, modifiedEntry, "Correct key");

          delete cursor.value.index;
          cursor.update(cursor.value).onsuccess = function(event) {
            indexCount--;
            cursor.continue();
          }
        }
        else {
          continueToNextStep();
        }
      }
      yield undefined;

      is(sawEntry, true, "Saw entry for key value " + modifiedEntry);

      // Recount objectStore. Should be unchanged.
      objectStore = db.transaction(autoIncrement, "readwrite")
                      .objectStore(autoIncrement);

      objectStore.count().onsuccess = grabEventAndContinueHandler;
      event = yield undefined;

      is(event.target.result, objectStoreCount,
         "Correct number of entries in objectStore");

      // Recount index. Should be one item less.
      index = objectStore.index(unique);

      index.count().onsuccess = grabEventAndContinueHandler;
      event = yield undefined;

      is(event.target.result, indexCount,
         "Correct number of entries in index");

      modifiedEntry = objectStoreCount - 1;

      objectStore.delete(modifiedEntry).onsuccess =
        grabEventAndContinueHandler;
      event = yield undefined;

      objectStoreCount--;
      indexCount--;

      objectStore.count().onsuccess = grabEventAndContinueHandler;
      event = yield undefined;

      is(event.target.result, objectStoreCount,
         "Correct number of entries in objectStore");

      index.count().onsuccess = grabEventAndContinueHandler;
      event = yield undefined;

      is(event.target.result, indexCount,
         "Correct number of entries in index");

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

  finishTest();
  event = db = request = null; // Bug 943409 workaround.
  yield undefined;
}