summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbobjectstore_openKeyCursor.htm
blob: 9dc547c3ff968a962c7fd413e609a49633961bb9 (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
<!DOCTYPE html>
<title>IDBObjectStore.openKeyCursor()</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
function store_test(func, name) {
    async_test(function(t) {
        var del = indexedDB.deleteDatabase(name);
        del.onerror = t.unreached_func("deleteDatabase failed");
        var open = indexedDB.open(name);
        open.onupgradeneeded = t.step_func(function() {
            var db = open.result;
            var store = db.createObjectStore("store");
            for (var i = 0; i < 10; ++i) {
                store.put("value: " + i, i);
            }
        });

        open.onsuccess = t.step_func(function() {
            var db = open.result;
            var tx = db.transaction("store");
            var store = tx.objectStore("store");
            func(t, db, tx, store);
        });
    }, name);
}

store_test(function(t, db, tx, store) {
    var expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    var actual = [];
    var request = store.openKeyCursor();
    request.onsuccess = t.step_func(function() {
        var cursor = request.result;
        if (!cursor)
            return;
        assert_equals(cursor.direction, "next");
        assert_false("value" in cursor);
        assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
        actual.push(cursor.key);
        cursor.continue();
    });

    tx.onabort = t.unreached_func("transaction aborted");
    tx.oncomplete = t.step_func(function() {
        assert_array_equals(expected, actual, "keys should match");
        t.done();
    });

}, "IDBObjectStore.openKeyCursor() - forward iteration");

store_test(function(t, db, tx, store) {
    var expected = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
    var actual = [];
    var request = store.openKeyCursor(null, "prev");
    request.onsuccess = t.step_func(function() {
        var cursor = request.result;
        if (!cursor)
            return;
        assert_equals(cursor.direction, "prev");
        assert_false("value" in cursor);
        assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
        actual.push(cursor.key);
        cursor.continue();
    });

    tx.onabort = t.unreached_func("transaction aborted");
    tx.oncomplete = t.step_func(function() {
        assert_array_equals(expected, actual, "keys should match");
        t.done();
    });

}, "IDBObjectStore.openKeyCursor() - reverse iteration");

store_test(function(t, db, tx, store) {
    var expected = [4, 5, 6];
    var actual = [];
    var request = store.openKeyCursor(IDBKeyRange.bound(4, 6));
    request.onsuccess = t.step_func(function() {
        var cursor = request.result;
        if (!cursor)
            return;
        assert_equals(cursor.direction, "next");
        assert_false("value" in cursor);
        assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
        actual.push(cursor.key);
        cursor.continue();
    });

    tx.onabort = t.unreached_func("transaction aborted");
    tx.oncomplete = t.step_func(function() {
        assert_array_equals(expected, actual, "keys should match");
        t.done();
    });

}, "IDBObjectStore.openKeyCursor() - forward iteration with range");

store_test(function(t, db, tx, store) {
    var expected = [6, 5, 4];
    var actual = [];
    var request = store.openKeyCursor(IDBKeyRange.bound(4, 6), "prev");
    request.onsuccess = t.step_func(function() {
        var cursor = request.result;
        if (!cursor)
            return;
        assert_equals(cursor.direction, "prev");
        assert_false("value" in cursor);
        assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
        actual.push(cursor.key);
        cursor.continue();
    });

    tx.onabort = t.unreached_func("transaction aborted");
    tx.oncomplete = t.step_func(function() {
        assert_array_equals(expected, actual, "keys should match");
        t.done();
    });

}, "IDBObjectStore.openKeyCursor() - reverse iteration with range");

store_test(function(t, db, tx, store) {
    assert_throws("DataError", function() { store.openKeyCursor(NaN); },
        "openKeyCursor should throw on invalid number key");
    assert_throws("DataError", function() { store.openKeyCursor(new Date(NaN)); },
        "openKeyCursor should throw on invalid date key");
    assert_throws("DataError", function() {
        var cycle = [];
        cycle.push(cycle);
        store.openKeyCursor(cycle);
    }, "openKeyCursor should throw on invalid array key");
    assert_throws("DataError", function() { store.openKeyCursor({}); },
        "openKeyCursor should throw on invalid key type");
    setTimeout(t.step_func(function() {
        assert_throws("TransactionInactiveError", function() { store.openKeyCursor(); },
            "openKeyCursor should throw if transaction is inactive");
        t.done();
    }), 0);

}, "IDBObjectStore.openKeyCursor() - invalid inputs");
</script>