1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test IndexedDB's objectStore.openCursor + the cursor it produces in depth.");
8 // In order of how it should be sorted by IndexedDB.
20 indexedDBTest(prepareDatabase);
21 function prepareDatabase()
23 db = event.target.result;
24 event.target.transaction.onabort = unexpectedAbortCallback;
26 evalAndLog("objectStore = db.createObjectStore('someObjectStore')");
29 debug("Verify that specifying an invalid direction raises an exception:");
30 evalAndExpectExceptionClass("objectStore.openCursor(0, 'invalid-direction')", "TypeError");
39 request = evalAndLog("objectStore.add('', testData[nextToAdd])");
40 request.onsuccess = ++self.nextToAdd < testData.length ? addData : scheduleTests;
41 request.onerror = unexpectedErrorCallback;
44 function scheduleTests()
46 debug("Scheduling tests...");
47 self.scheduledTests = [];
48 for (var i = 0; i < testData.length; ++i) {
49 /* left bound, is open, right bound, is open, ascending */
50 scheduledTests.unshift([i, true, null, null, true]);
51 scheduledTests.unshift([i, false, null, null, true]);
52 scheduledTests.unshift([null, null, i, true, true]);
53 scheduledTests.unshift([null, null, i, false, true]);
54 scheduledTests.unshift([i, true, null, null, false]);
55 scheduledTests.unshift([i, false, null, null, false]);
56 scheduledTests.unshift([null, null, i, true, false]);
57 scheduledTests.unshift([null, null, i, false, false]);
58 for (var j = 6; j < testData.length; ++j) {
59 scheduledTests.unshift([i, true, j, true, true]);
60 scheduledTests.unshift([i, true, j, false, true]);
61 scheduledTests.unshift([i, false, j, true, true]);
62 scheduledTests.unshift([i, false, j, false, true]);
63 scheduledTests.unshift([i, true, j, true, false]);
64 scheduledTests.unshift([i, true, j, false, false]);
65 scheduledTests.unshift([i, false, j, true, false]);
66 scheduledTests.unshift([i, false, j, false, false]);
70 debug("Running tests...");
74 function runNextTest()
76 if (!scheduledTests.length) {
81 var test = scheduledTests.pop();
83 self.lowerIsOpen = test[1];
85 self.upperIsOpen = test[3];
86 self.ascending = test[4];
93 str += "bound is " + lower + "; ";
99 str += "bound is " + upper + "; ";
102 str += "sorted ascending.";
104 str += "sorted descending.";
110 if (lower !== null) {
112 self.expectedIndex = lower;
114 self.expectedIndex = lower+1;
116 self.expectedIndex = 0;
118 if (upper !== null) {
120 self.expectedIndex = upper;
122 self.expectedIndex = upper-1;
124 self.expectedIndex = testData.length-1;
128 if (testData[lower] === testData[upper] && (lowerIsOpen || upperIsOpen)) {
129 debug("Skipping illegal key range.");
135 if (lower !== null && upper !== null)
136 keyRange = IDBKeyRange.bound(testData[lower], testData[upper], lowerIsOpen, upperIsOpen);
137 else if (lower !== null)
138 keyRange = IDBKeyRange.lowerBound(testData[lower], lowerIsOpen);
140 keyRange = IDBKeyRange.upperBound(testData[upper], upperIsOpen);
142 var request = objectStore.openCursor(keyRange, ascending ? 'next' : 'prev');
143 request.onsuccess = cursorIteration;
144 request.onerror = unexpectedErrorCallback;
147 function testWithinBounds()
149 if (expectedIndex < 0 || testData.length <= expectedIndex)
150 self.expectedIndex = null;
151 if (lower !== null && expectedIndex < lower)
152 self.expectedIndex = null;
153 if (upper !== null && upper < expectedIndex)
154 self.expectedIndex = null;
155 if (lower !== null && lowerIsOpen && expectedIndex <= lower)
156 self.expectedIndex = null;
157 if (upper !== null && upperIsOpen && upper <= expectedIndex)
158 self.expectedIndex = null;
161 function cursorIteration()
163 if (expectedIndex === null) {
164 shouldBeNull("event.target.result");
168 if (event.target.result === null) {
169 testFailed("event.target.result should not be null.");
174 shouldBe("event.target.result.key", "testData[" + expectedIndex + "]");
175 self.expectedIndex = ascending ? expectedIndex+1 : expectedIndex-1;
178 event.target.result.continue();
181 self.nullKeyRangeStep = 0;
182 function testNullKeyRange()
185 self.lowerIsOpen = false;
186 self.upper = testData.length-1;
187 self.upperIsOpen = false;
189 str = "Next test: null key path ";
190 if (self.nullKeyRangeStep == 0) {
191 str += "sorted ascending.";
192 self.ascending = true;
193 self.expectedIndex = lower;
194 self.nullKeyRangeStep = 1;
195 } else if (self.nullKeyRangeStep == 1) {
196 str += "sorted descending.";
197 self.ascending = false;
198 self.expectedIndex = upper;
199 self.nullKeyRangeStep = 2;
208 var request = objectStore.openCursor(null, ascending ? 'next' : 'prev');
209 request.onsuccess = cursorIteration;
210 request.onerror = unexpectedErrorCallback;