summaryrefslogtreecommitdiffstats
path: root/LayoutTests/storage/indexeddb/objectstore-cursor.html
blob: d0d786b9bb11feda715a6707f0fc64d91d395265 (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
<html>
<head>
<link rel="stylesheet" href="../../fast/js/resources/js-test-style.css">
<script src="../../fast/js/resources/js-test-pre.js"></script>
<script src="../../fast/js/resources/js-test-post-function.js"></script>
<script src="resources/shared.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script> 

description("Test IndexedDB's objectStore.openCursor + the cursor it produces in depth.");
if (window.layoutTestController)
    layoutTestController.waitUntilDone();
 
// In order of how it should be sorted by IndexedDB.
window.testData = [
    null,
    2,
    3,
    10,
    // FIXME: Dates.
    "A bigger string",
    "The biggest",
    "a low string"
];
 
function openDatabase()
{
    result = evalAndLog("webkitIndexedDB.open('someDB', 'some description')");
    verifyResult(result);
    result.onsuccess = setVersion;
    result.onerror = unexpectedErrorCallback;
}
 
function setVersion()
{
    verifySuccessEvent(event);
    window.db = evalAndLog("db = event.result");

    result = evalAndLog("db.setVersion('new version')");
    verifyResult(result);
    result.onsuccess = deleteExisting;
    result.onerror = unexpectedErrorCallback;
}

function deleteExisting()
{
    verifySuccessEvent(event);
    window.trans = evalAndLog("trans = event.result");
    shouldBeTrue("trans !== null");
    trans.onabort = unexpectedAbortCallback;

    deleteAllObjectStores(db, startAddingData);
}
 
function startAddingData()
{
    window.objectStore = evalAndLog("db.createObjectStore('someObjectStore')");
    window.nextToAdd = 0;
    addData();
}

function addData()
{
    if (window.nextToAdd > 0)
        verifySuccessEvent(event);

    result = evalAndLog("objectStore.add('', testData[nextToAdd])");
    verifyResult(result);
    result.onsuccess = ++window.nextToAdd < testData.length ? addData : scheduleTests;
}

function scheduleTests()
{
    debug("Scheduling tests...");
    window.scheduledTests = [];
    for (var i = 0; i < testData.length; ++i) {
        /* left bound, is open, right bound, is open, ascending */
        scheduledTests.unshift([i, true, null, null, true]);
        scheduledTests.unshift([i, false, null, null, true]);
        scheduledTests.unshift([null, null, i, true, true]);
        scheduledTests.unshift([null, null, i, false, true]);
        scheduledTests.unshift([i, true, null, null, false]);
        scheduledTests.unshift([i, false, null, null, false]);
        scheduledTests.unshift([null, null, i, true, false]);
        scheduledTests.unshift([null, null, i, false, false]);
        for (var j = 6; j < testData.length; ++j) {
            scheduledTests.unshift([i, true, j, true, true]);
            scheduledTests.unshift([i, true, j, false, true]);
            scheduledTests.unshift([i, false, j, true, true]);
            scheduledTests.unshift([i, false, j, false, true]);
            scheduledTests.unshift([i, true, j, true, false]);
            scheduledTests.unshift([i, true, j, false, false]);
            scheduledTests.unshift([i, false, j, true, false]);
            scheduledTests.unshift([i, false, j, false, false]);
        }
    }
 
    debug("Running tests...");
    runNextTest();
}
 
function runNextTest()
{
    if (!scheduledTests.length) {
        testNullKeyRange();
        return;
    }
 
    var test = scheduledTests.pop();
    window.lower = test[0];
    window.lowerIsOpen = test[1];
    window.upper = test[2];
    window.upperIsOpen = test[3];
    window.ascending = test[4];

    str = "Next test: ";
    if (lower !== null) {
        str += "lower ";
        if (lowerIsOpen)
            str += "open ";
        str += "bound is " + lower + "; ";
    }
    if (upper !== null) {
        str += "upper ";
        if (upperIsOpen)
            str += "open ";
        str += "bound is " + upper + "; ";
    }
    if (ascending)
        str += "sorted ascending.";
    else
        str += "sorted descending.";

    debug("");
    debug(str);
 
    if (ascending) {
        if (lower !== null) {
            if (!lowerIsOpen)
                window.expectedIndex = lower;
            else
                window.expectedIndex = lower+1;
        } else
            window.expectedIndex = 0;
    } else {
        if (upper !== null) {
            if (!upperIsOpen)
                window.expectedIndex = upper;
            else
                window.expectedIndex = upper-1;
        } else
            window.expectedIndex = testData.length-1;
    }
    testWithinBounds();
 
    var keyRange;
    if (lower !== null && upper !== null)
        keyRange = webkitIDBKeyRange.bound(testData[lower], testData[upper], lowerIsOpen, upperIsOpen);
    else if (lower !== null)
        keyRange = webkitIDBKeyRange.leftBound(testData[lower], lowerIsOpen);
    else
        keyRange = webkitIDBKeyRange.rightBound(testData[upper], upperIsOpen);
 
    var request = objectStore.openCursor(keyRange, ascending ? webkitIDBCursor.NEXT : webkitIDBCursor.PREV);
    request.onsuccess = cursorIteration;
    request.onerror = unexpectedErrorCallback;
}

function testWithinBounds()
{
    if (expectedIndex < 0 || testData.length <= expectedIndex)
        window.expectedIndex = null;
    if (lower !== null && expectedIndex < lower)
        window.expectedIndex = null;
    if (upper !== null && upper < expectedIndex)
        window.expectedIndex = null;
    if (lower !== null && lowerIsOpen && expectedIndex <= lower)
        window.expectedIndex = null;
    if (upper !== null && upperIsOpen && upper <= expectedIndex)
        window.expectedIndex = null;
}
 
function cursorIteration()
{
    if (expectedIndex === null) {
        shouldBeNull("event.result");
        runNextTest();
        return;
    }
    if (event.result === null) {
        testFailed("Event.result should not be null.");
        runNextTest();
        return;
    }
 
    shouldBe("event.result.key", "testData[" + expectedIndex + "]");
    window.expectedIndex = ascending ? expectedIndex+1 : expectedIndex-1;
    testWithinBounds();

    event.result.continue();
}
 
window.nullKeyRangeStep = 0;
function testNullKeyRange()
{
    window.lower = 0;
    window.lowerIsOpen = false;
    window.upper = testData.length-1;
    window.upperIsOpen = false;

    str = "Next test: null key path ";
    if (window.nullKeyRangeStep == 0) {
        str += "sorted ascending.";
        window.ascending = true;
        window.expectedIndex = lower;
        window.nullKeyRangeStep = 1;
    } else if (window.nullKeyRangeStep == 1) {
        str += "sorted descending.";
        window.ascending = false;
        window.expectedIndex = upper;
        window.nullKeyRangeStep = 2;
    } else {
        done();
        return;
    }

    debug("");
    debug(str);
 
    var request = objectStore.openCursor(null, ascending ? webkitIDBCursor.NEXT : webkitIDBCursor.PREV);
    request.onsuccess = cursorIteration;
    request.onerror = unexpectedErrorCallback;
}
 
openDatabase(); // The first step.
var successfullyParsed = true;
 
</script>
</body>
</html>