summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbindex-rename.html
blob: 370b83e5351d38504c500ccc364555f7de778490 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE html>
<meta name="timeout" content="long">
<title>IndexedDB: index renaming support</title>
<link rel="help"
      href="https://w3c.github.io/IndexedDB/#dom-idbindex-name">
<link rel="author" href="pwnall@chromium.org" title="Victor Costan">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support-promises.js"></script>
<script>

promise_test(testCase => {
    let authorIndex = null, authorIndex2 = null;
    let renamedAuthorIndex = null, renamedAuthorIndex2 = null;
    return createDatabase(testCase, (database, transaction) => {
        const store = createBooksStore(testCase, database);
        authorIndex = store.index('by_author');
    }).then(database => {
        const transaction = database.transaction('books', 'readonly');
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, ['by_author', 'by_title'],
            'Test setup should have created two indexes');
        authorIndex2 = store.index('by_author');
        return checkAuthorIndexContents(
            testCase, authorIndex2,
            'The index should have the expected contents before any renaming').
            then(() => database.close());
    }).then(() => migrateDatabase(testCase, 2, (database, transaction) => {
        const store = transaction.objectStore('books');
        renamedAuthorIndex = store.index('by_author');
        renamedAuthorIndex.name = 'renamed_by_author';

        assert_equals(
            renamedAuthorIndex.name, 'renamed_by_author',
            'IDBIndex name should change immediately after a rename');
        assert_array_equals(
            store.indexNames, ['by_title', 'renamed_by_author'],
            'IDBObjectStore.indexNames should immediately reflect the rename');
        assert_equals(
            store.index('renamed_by_author'), renamedAuthorIndex,
            'IDBObjectStore.index should return the renamed index store when ' +
            'queried using the new name immediately after the rename');
        assert_throws(
            'NotFoundError', () => store.index('by_author'),
            'IDBObjectStore.index should throw when queried using the ' +
            "renamed index's old name immediately after the rename");
    })).then(database => {
        const transaction = database.transaction('books', 'readonly');
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, ['by_title', 'renamed_by_author'],
            'IDBObjectStore.indexNames should still reflect the rename after ' +
            'the versionchange transaction commits');
        renamedAuthorIndex2 = store.index('renamed_by_author');
        return checkAuthorIndexContents(
            testCase, renamedAuthorIndex2,
            'Renaming an index should not change its contents').then(
            () => database.close());
    }).then(() => {
        assert_equals(
            authorIndex.name, 'by_author',
            'IDBIndex obtained before the rename transaction should not ' +
            'reflect the rename');
        assert_equals(
            authorIndex2.name, 'by_author',
            'IDBIndex obtained before the rename transaction should not ' +
            'reflect the rename');
        assert_equals(
            renamedAuthorIndex.name, 'renamed_by_author',
            'IDBIndex used in the rename transaction should keep reflecting ' +
            'the new name after the transaction is committed');
        assert_equals(
            renamedAuthorIndex2.name, 'renamed_by_author',
            'IDBIndex obtained after the rename transaction should reflect ' +
            'the new name');
    });
}, 'IndexedDB index rename in new transaction');

promise_test(testCase => {
    let renamedAuthorIndex = null, renamedAuthorIndex2 = null;
    return createDatabase(testCase, (database, transaction) => {
        const store = createBooksStore(testCase, database);
        renamedAuthorIndex = store.index('by_author');
        renamedAuthorIndex.name = 'renamed_by_author';

        assert_equals(
            renamedAuthorIndex.name, 'renamed_by_author',
            'IDBIndex name should change immediately after a rename');
        assert_array_equals(
            store.indexNames, ['by_title', 'renamed_by_author'],
            'IDBObjectStore.indexNames should immediately reflect the rename');
        assert_equals(
            store.index('renamed_by_author'), renamedAuthorIndex,
            'IDBObjectStore.index should return the renamed index store when ' +
            'queried using the new name immediately after the rename');
        assert_throws(
            'NotFoundError', () => store.index('by_author'),
            'IDBObjectStore.index should throw when queried using the ' +
            "renamed index's old name immediately after the rename");
    }).then(database => {
        const transaction = database.transaction('books', 'readonly');
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, ['by_title', 'renamed_by_author'],
            'IDBObjectStore.indexNames should still reflect the rename after ' +
            'the versionchange transaction commits');
        renamedAuthorIndex2 = store.index('renamed_by_author');
        return checkAuthorIndexContents(
            testCase, renamedAuthorIndex2,
            'Renaming an index should not change its contents').then(
            () => database.close());
    }).then(() => {
        assert_equals(
            renamedAuthorIndex.name, 'renamed_by_author',
            'IDBIndex used in the rename transaction should keep reflecting ' +
            'the new name after the transaction is committed');
        assert_equals(
            renamedAuthorIndex2.name, 'renamed_by_author',
            'IDBIndex obtained after the rename transaction should reflect ' +
            'the new name');
    });
}, 'IndexedDB index rename in the transaction where it is created');

promise_test(testCase => {
    return createDatabase(testCase, (database, transaction) => {
        createBooksStore(testCase, database);
    }).then(database => {
        database.close();
    }).then(() => migrateDatabase(testCase, 2, (database, transaction) => {
        const store = transaction.objectStore('books');
        const index = store.index('by_author');
        index.name = 'by_author';
        assert_array_equals(
            store.indexNames, ['by_author', 'by_title'],
            'Renaming an index to the same name should not change the ' +
            "index's IDBObjectStore.indexNames");
    })).then(database => {
        const transaction = database.transaction('books', 'readonly');
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, ['by_author', 'by_title'],
            'Committing a transaction that renames a store to the same name ' +
            "should not change the index's IDBObjectStore.indexNames");
        const index = store.index('by_author');
        return checkAuthorIndexContents(
            testCase, index,
            'Committing a transaction that renames an index to the same name ' +
            "should not change the index's contents").then(
            () => database.close());
    });
}, 'IndexedDB index rename to the same name succeeds');

promise_test(testCase => {
    return createDatabase(testCase, (database, transaction) => {
        createBooksStore(testCase, database);
    }).then(database => {
        database.close();
    }).then(() => migrateDatabase(testCase, 2, (database, transaction) => {
        const store = transaction.objectStore('books');
        const index = store.index('by_author');
        store.deleteIndex('by_title');
        index.name = 'by_title';
        assert_array_equals(
            store.indexNames, ['by_title'],
            'IDBObjectStore.indexNames should immediately reflect the rename');
    })).then(database => {
        const transaction = database.transaction('books', 'readonly');
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, ['by_title'],
            'IDBObjectStore.indexNames should still reflect the rename after ' +
            'the versionchange transaction commits');
        const index = store.index('by_title');
        return checkAuthorIndexContents(
            testCase, index,
            'Renaming an index should not change its contents').then(
            () => database.close());
    });
}, 'IndexedDB index rename to the name of a deleted index succeeds');

promise_test(testCase => {
    return createDatabase(testCase, (database, transaction) => {
        createBooksStore(testCase, database);
    }).then(database => {
        database.close();
    }).then(() => migrateDatabase(testCase, 2, (database, transaction) => {
        const store = transaction.objectStore('books');
        store.index('by_author').name = 'tmp';
        store.index('by_title').name = 'by_author';
        store.index('tmp').name = 'by_title';
        assert_array_equals(
            store.indexNames, ['by_author', 'by_title'],
            'IDBObjectStore.indexNames should reflect the swap immediately ' +
            'after the renames');
        return checkTitleIndexContents(
            testCase, store.index('by_author'),
            'Renaming an index should not change its contents');
    })).then(database => {
        const transaction = database.transaction('books', 'readonly');
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, ['by_author', 'by_title'],
            'IDBObjectStore.indexNames should still reflect the swap after ' +
            'the versionchange transaction commits');
        const index = store.index('by_title');
        return checkAuthorIndexContents(
            testCase, index,
            'Renaming an index should not change its contents').then(
            () => database.close());
    });
}, 'IndexedDB index swapping via renames succeeds');

promise_test(testCase => {
    return createDatabase(testCase, (database, transaction) => {
        createBooksStore(testCase, database);
    }).then(database => {
        database.close();
    }).then(() => migrateDatabase(testCase, 2, (database, transaction) => {
        const store = transaction.objectStore('books');
        const index = store.index('by_author');

        index.name = 42;
        assert_equals(index.name, '42',
            'IDBIndex name should change immediately after a rename to a ' +
            'number');
        assert_array_equals(
            store.indexNames, ['42', 'by_title'],
            'IDBObjectStore.indexNames should immediately reflect the ' +
            'stringifying rename');

        index.name = true;
        assert_equals(index.name, 'true',
            'IDBIndex name should change immediately after a rename to a ' +
            'boolean');

        index.name = {};
        assert_equals(index.name, '[object Object]',
            'IDBIndex name should change immediately after a rename to an ' +
            'object');

        index.name = () => null;
        assert_equals(index.name, '() => null',
            'IDBIndex name should change immediately after a rename to a ' +
            'function');

        index.name = undefined;
        assert_equals(index.name, 'undefined',
            'IDBIndex name should change immediately after a rename to ' +
            'undefined');
    })).then(database => {
        const transaction = database.transaction('books', 'readonly');
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, ['by_title', 'undefined'],
            'IDBObjectStore.indexNames should reflect the last rename ' +
            'after the versionchange transaction commits');
        const index = store.index('undefined');
        return checkAuthorIndexContents(
            testCase, index,
            'Renaming an index should not change its contents').then(
            () => database.close());
    });
}, 'IndexedDB index rename stringifies non-string names');

for (let escapedName of ['', '\\u0000', '\\uDC00\\uD800']) ((escapedName) => {
  const name = JSON.parse('"' + escapedName + '"');
  promise_test(testCase => {
    return createDatabase(testCase, (database, transaction) => {
        createBooksStore(testCase, database);
    }).then(database => {
        database.close();
    }).then(() => migrateDatabase(testCase, 2, (database, transaction) => {
        const store = transaction.objectStore('books');
        const index = store.index('by_author');

        index.name = name;
        assert_equals(index.name, name,
            'IDBIndex name should change immediately after the rename');
        assert_array_equals(
            store.indexNames, [name, 'by_title'].sort(),
            'IDBObjectStore.indexNames should immediately reflect the rename');
    })).then(database => {
        const transaction = database.transaction('books', 'readonly');
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, [name, 'by_title'].sort(),
            'IDBObjectStore.indexNames should reflect the rename ' +
            'after the versionchange transaction commits');
        const index = store.index(name);
        return checkAuthorIndexContents(
            testCase, index,
            'Renaming an index should not change its contents').then(
            () => database.close());
    });
  }, 'IndexedDB index can be renamed to "' + escapedName + '"');
})(escapedName);

</script>