1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test IndexedDB's IDBIndex.openCursor + the cursor it produces in depth.");
8 // In order of how it should be sorted by IndexedDB.
21 indexedDBTest(prepareDatabase);
22 function prepareDatabase()
24 db = event.target.result;
25 event.target.transaction.onabort = unexpectedAbortCallback;
27 self.objectStore = evalAndLog("db.createObjectStore('someObjectStore')");
28 self.indexObject = evalAndLog("objectStore.createIndex('someIndex', 'x')");
35 result = evalAndLog("objectStore.add({'x': testData[nextToAdd]}, nextToAdd)");
36 result.onsuccess = ++self.nextToAdd < testData.length ? addData : scheduleTests;
37 result.onerror = unexpectedErrorCallback;
40 function scheduleTests()
42 debug("Scheduling tests...");
43 self.scheduledTests = [];
44 for (var i = 0; i < testData.length; ++i) {
45 /* left bound, is open, right bound, is open, ascending */
46 scheduledTests.unshift([i, true, null, null, true]);
47 scheduledTests.unshift([i, false, null, null, true]);
48 scheduledTests.unshift([null, null, i, true, true]);
49 scheduledTests.unshift([null, null, i, false, true]);
50 scheduledTests.unshift([i, true, null, null, false]);
51 scheduledTests.unshift([i, false, null, null, false]);
52 scheduledTests.unshift([null, null, i, true, false]);
53 scheduledTests.unshift([null, null, i, false, false]);
54 for (var j = 6; j < testData.length; ++j) {
55 scheduledTests.unshift([i, true, j, true, true]);
56 scheduledTests.unshift([i, true, j, false, true]);
57 scheduledTests.unshift([i, false, j, true, true]);
58 scheduledTests.unshift([i, false, j, false, true]);
59 scheduledTests.unshift([i, true, j, true, false]);
60 scheduledTests.unshift([i, true, j, false, false]);
61 scheduledTests.unshift([i, false, j, true, false]);
62 scheduledTests.unshift([i, false, j, false, false]);
66 debug("Running tests...");
70 function runNextTest()
72 if (!scheduledTests.length) {
77 var test = scheduledTests.pop();
79 self.lowerIsOpen = test[1];
81 self.upperIsOpen = test[3];
82 self.ascending = test[4];
89 str += "bound is " + lower + "; ";
95 str += "bound is " + upper + "; ";
98 str += "sorted ascending.";
100 str += "sorted descending.";
105 // Work around for duplicate values.
106 if (upper !== null) {
108 while (upper > 0 && testData[upper] === testData[upper - 1])
111 while (upper < testData.length - 1 && testData[upper] === testData[upper + 1])
115 if (lower !== null) {
117 while (lower < testData.length - 1 && testData[lower] === testData[lower + 1])
120 while (lower > 0 && testData[lower] === testData[lower - 1])
126 if (lower !== null) {
128 self.expectedIndex = lower;
130 self.expectedIndex = lower + 1;
132 self.expectedIndex = 0;
134 if (upper !== null) {
136 self.expectedIndex = upper;
138 self.expectedIndex = upper - 1;
140 self.expectedIndex = testData.length - 1;
144 if (testData[lower] === testData[upper] && (lowerIsOpen || upperIsOpen)) {
145 debug("Skipping illegal key range.");
151 if (lower !== null && upper !== null)
152 keyRange = IDBKeyRange.bound(testData[lower], testData[upper], lowerIsOpen, upperIsOpen);
153 else if (lower !== null)
154 keyRange = IDBKeyRange.lowerBound(testData[lower], lowerIsOpen);
156 keyRange = IDBKeyRange.upperBound(testData[upper], upperIsOpen);
158 var request = indexObject.openKeyCursor(keyRange, ascending ? 'next' : 'prev');
159 request.onsuccess = cursorIteration;
160 request.onerror = unexpectedErrorCallback;
163 function testWithinBounds()
165 if (expectedIndex < 0 || testData.length <= expectedIndex)
166 self.expectedIndex = null;
167 if (lower !== null && expectedIndex < lower)
168 self.expectedIndex = null;
169 if (upper !== null && upper < expectedIndex)
170 self.expectedIndex = null;
171 if (lower !== null && lowerIsOpen && expectedIndex <= lower)
172 self.expectedIndex = null;
173 if (upper !== null && upperIsOpen && upper <= expectedIndex)
174 self.expectedIndex = null;
177 function cursorIteration()
179 if (expectedIndex === null) {
180 shouldBeNull("event.target.result");
184 if (event.target.result === null) {
185 testFailed("event.target.result should not be null.");
191 shouldBe("event.target.result.primaryKey", "expectedIndex", quiet);
192 shouldBe("event.target.result.key", "testData[" + expectedIndex + "]", quiet);
193 self.expectedIndex = ascending ? expectedIndex + 1 : expectedIndex - 1;
196 event.target.result.continue();
199 self.nullKeyRangeStep = 0;
200 function testNullKeyRange()
203 self.lowerIsOpen = false;
204 self.upper = testData.length-1;
205 self.upperIsOpen = false;
207 str = "Next test: null key path ";
208 if (self.nullKeyRangeStep == 0) {
209 str += "sorted ascending.";
210 self.ascending = true;
211 self.expectedIndex = lower;
212 self.nullKeyRangeStep = 1;
213 } else if (self.nullKeyRangeStep == 1) {
214 str += "sorted descending.";
215 self.ascending = false;
216 self.expectedIndex = upper;
217 self.nullKeyRangeStep = 2;
226 var request = indexObject.openKeyCursor(null, ascending ? 'next' : 'prev');
227 request.onsuccess = cursorIteration;
228 request.onerror = unexpectedErrorCallback;