summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_rename_index.js
blob: fb4af954b3d5fe5653adac036d086f66457c38a7 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

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

function* testSteps() {
  const name = this.window ? window.location.pathname : "Splendid Test";
  const storeName = "test store";
  const indexName_ToBeDeleted = "test index to be deleted";
  const indexName_v0 = "test index v0";
  const indexName_v1 = "test index v1";
  const indexName_v2 = "test index v2";
  const indexName_v3 = indexName_ToBeDeleted;
  const indexName_v4 = "test index v4";

  info("Rename in v1.");
  let request = indexedDB.open(name, 1);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = unexpectedSuccessHandler;
  let event = yield undefined;

  let db = event.target.result;
  let txn = event.target.transaction;

  is(db.objectStoreNames.length, 0, "Correct objectStoreNames list");

  let objectStore = db.createObjectStore(storeName, { keyPath: "foo" });
  is(db.objectStoreNames.length, 1, "Correct objectStoreNames list");
  is(
    db.objectStoreNames.item(0),
    objectStore.name,
    "Correct object store name"
  );

  // create index to be deleted later in v3.
  objectStore.createIndex(indexName_ToBeDeleted, "foo");
  ok(objectStore.index(indexName_ToBeDeleted), "Index created.");

  // create target index to be renamed.
  let index = objectStore.createIndex(indexName_v0, "bar");
  ok(objectStore.index(indexName_v0), "Index created.");
  is(index.name, indexName_v0, "Correct index name");
  index.name = indexName_v1;
  is(index.name, indexName_v1, "Renamed index successfully");

  txn.oncomplete = continueToNextStepSync;
  yield undefined;
  request.onsuccess = continueToNextStep;
  yield undefined;
  db.close();

  info("Verify renaming done in v1 and run renaming in v2.");
  request = indexedDB.open(name, 2);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = unexpectedSuccessHandler;
  event = yield undefined;

  db = event.target.result;
  txn = event.target.transaction;

  objectStore = txn.objectStore(storeName);

  // indexName_v0 created in v1 shall not be available.
  try {
    index = objectStore.index(indexName_v0);
    ok(false, "NotFoundError shall be thrown.");
  } catch (e) {
    ok(e instanceof DOMException, "got a database exception");
    is(e.name, "NotFoundError", "correct error");
  }

  // rename to "v2".
  index = objectStore.index(indexName_v1);
  is(index.name, indexName_v1, "Correct index name");
  index.name = indexName_v2;
  is(index.name, indexName_v2, "Renamed index successfully");

  txn.oncomplete = continueToNextStepSync;
  yield undefined;
  request.onsuccess = continueToNextStep;
  yield undefined;
  db.close();

  info("Verify renaming done in v2.");
  request = indexedDB.open(name, 2);
  request.onerror = errorHandler;
  request.onupgradeneeded = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  db = event.target.result;
  txn = db.transaction(storeName);

  objectStore = txn.objectStore(storeName);
  index = objectStore.index(indexName_v2);
  is(index.name, indexName_v2, "Correct index name");

  db.close();

  info("Rename in v3.");
  request = indexedDB.open(name, 3);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = unexpectedSuccessHandler;
  event = yield undefined;

  db = event.target.result;
  txn = event.target.transaction;

  objectStore = txn.objectStore(storeName);
  ok(objectStore.index(indexName_ToBeDeleted), "index is valid.");
  objectStore.deleteIndex(indexName_ToBeDeleted);
  try {
    objectStore.index(indexName_ToBeDeleted);
    ok(false, "NotFoundError shall be thrown if the index name is deleted.");
  } catch (e) {
    ok(e instanceof DOMException, "got a database exception");
    is(e.name, "NotFoundError", "correct error");
  }

  info("Rename with the name of the deleted index.");
  index = objectStore.index(indexName_v2);
  index.name = indexName_v3;
  is(index.name, indexName_v3, "Renamed index successfully");

  txn.oncomplete = continueToNextStepSync;
  yield undefined;
  request.onsuccess = continueToNextStep;
  yield undefined;
  db.close();

  info("Verify renaming done in v3.");
  request = indexedDB.open(name, 3);
  request.onerror = errorHandler;
  request.onupgradeneeded = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  db = event.target.result;
  txn = db.transaction(storeName);

  objectStore = txn.objectStore(storeName);
  index = objectStore.index(indexName_v3);
  is(index.name, indexName_v3, "Correct index name");

  db.close();

  info("Abort the version change transaction while renaming index.");
  request = indexedDB.open(name, 4);
  request.onerror = errorHandler;
  request.onupgradeneeded = grabEventAndContinueHandler;
  request.onsuccess = unexpectedSuccessHandler;
  event = yield undefined;

  db = event.target.result;
  txn = event.target.transaction;

  objectStore = txn.objectStore(storeName);
  index = objectStore.index(indexName_v3);
  index.name = indexName_v4;
  is(index.name, indexName_v4, "Renamed successfully");
  let putRequest = objectStore.put({ foo: "fooValue", bar: "barValue" });
  putRequest.onsuccess = continueToNextStepSync;
  yield undefined;

  // Aborting the transaction.
  request.onerror = expectedErrorHandler("AbortError");
  txn.abort();
  yield undefined;

  // Verify if the name of the index handle is reverted.
  is(index.name, indexName_v3, "The name is reverted after aborted.");

  info("Verify if the objectstore name is unchanged.");
  request = indexedDB.open(name, 3);
  request.onerror = errorHandler;
  request.onupgradeneeded = errorHandler;
  request.onsuccess = grabEventAndContinueHandler;
  event = yield undefined;

  db = event.target.result;
  txn = db.transaction(storeName);

  objectStore = txn.objectStore(storeName);
  index = objectStore.index(indexName_v3);
  is(index.name, indexName_v3, "Correct index name");

  db.close();

  finishTest();
}