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