summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbindex-rename-errors.html
blob: 2339cf10221d8dff73d887f3a694b5c891ae37fe (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
<!doctype html>
<meta charset="utf8">
<title>IndexedDB: index renaming error handling</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="resources/support-promises.js"></script>
<script>
'use strict';

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_author');
        assert_throws_dom(
            'InvalidStateError', () => index.name = 'renamed_by_author');
    })).then(database => database.close());
}, 'IndexedDB deleted index rename throws');

promise_test(testCase => {
    return createDatabase(testCase, (database, transaction) => {
        createBooksStore(testCase, database);
    }).then(database => {
        const transaction = database.transaction('books', 'readonly', {durability: 'relaxed'});
        const store = transaction.objectStore('books');
        const index = store.index('by_author');

        assert_throws_dom(
            'InvalidStateError', () => index.name = 'renamed_by_author');
        database.close();
    });
}, 'IndexedDB index rename throws in a readonly transaction');

promise_test(testCase => {
    return createDatabase(testCase, (database, transaction) => {
        createBooksStore(testCase, database);
    }).then(database => {
        const transaction = database.transaction('books', 'readwrite', {durability: 'relaxed'});
        const store = transaction.objectStore('books');
        const index = store.index('by_author');

        assert_throws_dom(
            'InvalidStateError', () => index.name = 'renamed_by_author');
        database.close();
    });
}, 'IndexedDB index rename throws in a readwrite transaction');

promise_test(testCase => {
    let authorIndex = null;
    return createDatabase(testCase, (database, transaction) => {
        const store = createBooksStore(testCase, database);
        authorIndex = store.index('by_author');
    }).then(database => {
        assert_throws_dom(
            'TransactionInactiveError',
            () => authorIndex.name = 'renamed_by_author');
        database.close();
    });
}, 'IndexedDB index rename throws in an inactive transaction');

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');

        assert_throws_dom('ConstraintError', () => index.name = 'by_title');
        assert_array_equals(
            store.indexNames, ['by_author', 'by_title'],
            'An index rename that throws an exception should not change the ' +
            "index's IDBObjectStore.indexNames");
    })).then(database => {
        const transaction = database.transaction('books', 'readonly', {durability: 'relaxed'});
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, ['by_author', 'by_title'],
            'Committing a transaction with a failed store rename attempt ' +
            "should not change the index's IDBObjectStore.indexNames");
        const index = store.index('by_author');
        return checkAuthorIndexContents(
            testCase, index,
            'Committing a transaction with a failed rename attempt should ' +
            "not change the index's contents").then(() => database.close());
    });
}, 'IndexedDB index rename to the name of another index throws');

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');
        const exception = { name: 'Custom stringifying error' };
        assert_throws_exactly(
            exception,
            () => {
              index.name = {
                toString: () => { throw exception; }
              };
            }, 'IDBObjectStore rename should re-raise toString() exception');
        assert_array_equals(
            store.indexNames, ['by_author', 'by_title'],
            'An index rename that throws an exception should not change the ' +
            "index's IDBObjectStore.indexNames");
    })).then(database => {
        const transaction = database.transaction('books', 'readonly', {durability: 'relaxed'});
        const store = transaction.objectStore('books');
        assert_array_equals(
            store.indexNames, ['by_author', 'by_title'],
            'Committing a transaction with a failed store rename attempt ' +
            "should not change the index's IDBObjectStore.indexNames");
        const index = store.index('by_author');
        return checkAuthorIndexContents(
            testCase, index,
            'Committing a transaction with a failed rename attempt should ' +
            "not change the index's contents").then(() => database.close());
    });
}, 'IndexedDB index rename handles exceptions when stringifying names');

</script>