summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/transaction-abort-generator-revert.html
blob: bbe0338c3acdc8791e308d8411c986048096f36c (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
<!doctype html>
<meta charset="utf8">
<title>IndexedDB: aborting transactions reverts an object store's key generator state</title>
<link rel="help" href="https://w3c.github.io/IndexedDB/#abort-transaction">
<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(() => {
        return new Promise((resolve, reject) => {
            const request = indexedDB.open(databaseName(testCase), 2);
            request.onupgradeneeded = testCase.step_func(event => {
                const database = event.target.result;
                const transaction = event.target.transaction;
                const store = transaction.objectStore('books');
                const request2 = store.put(
                    { title: 'Bedrock Nights II', author: 'Barney' });
                request2.onerror = testCase.unreached_func(
                    'IDBObjectStore.put() should not receive an error request');
                request2.onsuccess = testCase.step_func(event => {
                    assert_equals(
                        event.target.result, 345679,
                        "The key generator's current number should be set by " +
                        'the last put operation in the database creation ' +
                        'transaction');

                    request.onerror = event => {
                        event.preventDefault();
                        resolve(event);
                    };
                    request.onsuccess = () => reject(new Error(
                        'indexedDB.open should not succeed after the ' +
                        'versionchange transaction is aborted'));

                    transaction.abort();
                });
            });
            request.onerror = event => reject(event.target.error);
            request.onsuccess = () => reject(new Error(
                'indexedDB.open should not succeed without creating a ' +
                'versionchange transaction'));
        });
    }).then(() => {
        return openDatabase(testCase, 1);
    }).then(database => {
        const transaction = database.transaction(['books'], 'readwrite');
        const store = transaction.objectStore('books');

        return checkStoreGenerator(
            testCase, store, 345679,
            "The key generator's current number should be reverted after the " +
            'transaction modifying it is aborted').then(() => database.close());
    });
}, 'The current number of a key generator is reverted when a versionchange ' +
    'transaction aborts');

promise_test(testCase => {
    return createDatabase(testCase, (database, transaction) => {
        createBooksStore(testCase, database);
    }).then(database => {
        return new Promise((resolve, reject) => {
            const transaction = database.transaction(['books'], 'readwrite');
            const store = transaction.objectStore('books');
            const request = store.put(
                { title: 'Bedrock Nights II', author: 'Barney' });
            request.onerror = testCase.unreached_func(
                'IDBObjectStore.put() should not receive an error request');
            request.onsuccess = testCase.step_func(event => {
                assert_equals(
                    event.target.result, 345679,
                    "The key generator's current number should be set by the " +
                    'last put operation in the database creation transaction');

                transaction.onabort = event => {
                    event.preventDefault();
                    resolve(event);
                }
                transaction.abort();
            });
            transaction.onabort = () => reject(new Error(
                'The aborted readwrite transaction should not receive an ' +
                'abort event before IDBTransaction.abort() is called'));
            transaction.oncomplete = () => reject(new Error(
                'The aborted readwrite transaction should not receive a ' +
                'completed event'));
            transaction.onerror = () => reject(new Error(
                'The aborted readwrite transaction should not receive an ' +
                'error event'));
        }).then(() => database);
    }).then(database => {
        const transaction = database.transaction(['books'], 'readwrite');
        const store = transaction.objectStore('books');

        return checkStoreGenerator(
            testCase, store, 345679,
            "The key generator's current number should be reverted after the " +
            'transaction modifying it is aborted').then(() => database.close());
    });
}, 'The current number of a key generator is reverted when a readwrite ' +
    'transaction aborts');

</script>