1<html> 2<head> 3<link rel="stylesheet" href="../../fast/js/resources/js-test-style.css"> 4<script src="../../fast/js/resources/js-test-pre.js"></script> 5<script src="../../fast/js/resources/js-test-post-function.js"></script> 6<script src="resources/shared.js"></script> 7</head> 8<body> 9<p id="description"></p> 10<div id="console"></div> 11<script> 12 13description("Test IndexedDB's webkitIDBIndex.openCursor + the cursor it produces in depth."); 14if (window.layoutTestController) 15 layoutTestController.waitUntilDone(); 16 17// In order of how it should be sorted by IndexedDB. 18window.testData = [ 19 1, 20 1, 21 3.14159, 22 3.14159, 23 10, 24 // FIXME: Dates. 25 "A big string", 26 "the BIGEST string", 27 "the BIGEST string" 28]; 29 30function openDatabase() 31{ 32 result = evalAndLog("webkitIndexedDB.open('index-cursor')"); 33 result.onsuccess = setVersion; 34 result.onerror = unexpectedErrorCallback; 35} 36 37function setVersion() 38{ 39 window.db = evalAndLog("db = event.target.result"); 40 41 result = evalAndLog("db.setVersion('new version')"); 42 result.onsuccess = deleteExisting; 43 result.onerror = unexpectedErrorCallback; 44} 45 46function deleteExisting() 47{ 48 window.trans = evalAndLog("trans = event.target.result"); 49 shouldBeTrue("trans !== null"); 50 trans.onabort = unexpectedAbortCallback; 51 52 deleteAllObjectStores(db); 53 54 window.objectStore = evalAndLog("db.createObjectStore('someObjectStore')"); 55 window.indexObject = evalAndLog("objectStore.createIndex('someIndex', 'x')"); 56 window.nextToAdd = 0; 57 addData(); 58} 59 60function addData() 61{ 62 result = evalAndLog("objectStore.add({'x': testData[nextToAdd]}, nextToAdd)"); 63 result.onsuccess = ++window.nextToAdd < testData.length ? addData : scheduleTests; 64 result.onerror = unexpectedErrorCallback; 65} 66 67function scheduleTests() 68{ 69 debug("Scheduling tests..."); 70 window.scheduledTests = []; 71 for (var i = 0; i < testData.length; ++i) { 72 /* left bound, is open, right bound, is open, ascending */ 73 scheduledTests.unshift([i, true, null, null, true]); 74 scheduledTests.unshift([i, false, null, null, true]); 75 scheduledTests.unshift([null, null, i, true, true]); 76 scheduledTests.unshift([null, null, i, false, true]); 77 scheduledTests.unshift([i, true, null, null, false]); 78 scheduledTests.unshift([i, false, null, null, false]); 79 scheduledTests.unshift([null, null, i, true, false]); 80 scheduledTests.unshift([null, null, i, false, false]); 81 for (var j = 6; j < testData.length; ++j) { 82 scheduledTests.unshift([i, true, j, true, true]); 83 scheduledTests.unshift([i, true, j, false, true]); 84 scheduledTests.unshift([i, false, j, true, true]); 85 scheduledTests.unshift([i, false, j, false, true]); 86 scheduledTests.unshift([i, true, j, true, false]); 87 scheduledTests.unshift([i, true, j, false, false]); 88 scheduledTests.unshift([i, false, j, true, false]); 89 scheduledTests.unshift([i, false, j, false, false]); 90 } 91 } 92 93 debug("Running tests..."); 94 runNextTest(); 95} 96 97function runNextTest() 98{ 99 if (!scheduledTests.length) { 100 testNullKeyRange(); 101 return; 102 } 103 104 var test = scheduledTests.pop(); 105 window.lower = test[0]; 106 window.lowerIsOpen = test[1]; 107 window.upper = test[2]; 108 window.upperIsOpen = test[3]; 109 window.ascending = test[4]; 110 111 str = "Next test: "; 112 if (lower !== null) { 113 str += "lower "; 114 if (lowerIsOpen) 115 str += "open "; 116 str += "bound is " + lower + "; "; 117 } 118 if (upper !== null) { 119 str += "upper "; 120 if (upperIsOpen) 121 str += "open "; 122 str += "bound is " + upper + "; "; 123 } 124 if (ascending) 125 str += "sorted ascending."; 126 else 127 str += "sorted descending."; 128 129 debug(""); 130 debug(str); 131 132 // Work around for duplicate values. 133 if (upper !== null) { 134 if (upperIsOpen) { 135 while (upper > 0 && testData[upper] === testData[upper - 1]) 136 --window.upper; 137 } else { 138 while (upper < testData.length - 1 && testData[upper] === testData[upper + 1]) 139 ++window.upper; 140 } 141 } 142 if (lower !== null) { 143 if (lowerIsOpen) { 144 while (lower < testData.length - 1 && testData[lower] === testData[lower + 1]) 145 ++window.lower; 146 } else { 147 while (lower > 0 && testData[lower] === testData[lower - 1]) 148 --window.lower; 149 } 150 } 151 152 if (ascending) { 153 if (lower !== null) { 154 if (!lowerIsOpen) 155 window.expectedIndex = lower; 156 else 157 window.expectedIndex = lower + 1; 158 } else 159 window.expectedIndex = 0; 160 } else { 161 if (upper !== null) { 162 if (!upperIsOpen) 163 window.expectedIndex = upper; 164 else 165 window.expectedIndex = upper - 1; 166 } else 167 window.expectedIndex = testData.length - 1; 168 } 169 testWithinBounds(); 170 171 var keyRange; 172 if (lower !== null && upper !== null) 173 keyRange = webkitIDBKeyRange.bound(testData[lower], testData[upper], lowerIsOpen, upperIsOpen); 174 else if (lower !== null) 175 keyRange = webkitIDBKeyRange.lowerBound(testData[lower], lowerIsOpen); 176 else 177 keyRange = webkitIDBKeyRange.upperBound(testData[upper], upperIsOpen); 178 179 var request = indexObject.openKeyCursor(keyRange, ascending ? webkitIDBCursor.NEXT : webkitIDBCursor.PREV); 180 request.onsuccess = cursorIteration; 181 request.onerror = unexpectedErrorCallback; 182} 183 184function testWithinBounds() 185{ 186 if (expectedIndex < 0 || testData.length <= expectedIndex) 187 window.expectedIndex = null; 188 if (lower !== null && expectedIndex < lower) 189 window.expectedIndex = null; 190 if (upper !== null && upper < expectedIndex) 191 window.expectedIndex = null; 192 if (lower !== null && lowerIsOpen && expectedIndex <= lower) 193 window.expectedIndex = null; 194 if (upper !== null && upperIsOpen && upper <= expectedIndex) 195 window.expectedIndex = null; 196} 197 198function cursorIteration() 199{ 200 if (expectedIndex === null) { 201 shouldBeNull("event.target.result"); 202 runNextTest(); 203 return; 204 } 205 if (event.target.result === null) { 206 testFailed("event.target.result should not be null."); 207 runNextTest(); 208 return; 209 } 210 211 shouldBe("event.target.result.primaryKey", "expectedIndex"); 212 shouldBe("event.target.result.key", "testData[" + expectedIndex + "]"); 213 window.expectedIndex = ascending ? expectedIndex + 1 : expectedIndex - 1; 214 testWithinBounds(); 215 216 event.target.result.continue(); 217} 218 219window.nullKeyRangeStep = 0; 220function testNullKeyRange() 221{ 222 window.lower = 0; 223 window.lowerIsOpen = false; 224 window.upper = testData.length-1; 225 window.upperIsOpen = false; 226 227 str = "Next test: null key path "; 228 if (window.nullKeyRangeStep == 0) { 229 str += "sorted ascending."; 230 window.ascending = true; 231 window.expectedIndex = lower; 232 window.nullKeyRangeStep = 1; 233 } else if (window.nullKeyRangeStep == 1) { 234 str += "sorted descending."; 235 window.ascending = false; 236 window.expectedIndex = upper; 237 window.nullKeyRangeStep = 2; 238 } else { 239 done(); 240 return; 241 } 242 243 debug(""); 244 debug(str); 245 246 var request = indexObject.openKeyCursor(null, ascending ? webkitIDBCursor.NEXT : webkitIDBCursor.PREV); 247 request.onsuccess = cursorIteration; 248 request.onerror = unexpectedErrorCallback; 249} 250 251openDatabase(); // The first step. 252var successfullyParsed = true; 253 254</script> 255</body> 256</html> 257