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 cursor update.");
     14 if (window.layoutTestController)
     15     layoutTestController.waitUntilDone();
     16 
     17 test();
     18 
     19 function test()
     20 {
     21     request = evalAndLog("webkitIndexedDB.open('cursor-update')");
     22     request.onsuccess = openSuccess;
     23     request.onerror = unexpectedErrorCallback;
     24 }
     25 
     26 function openSuccess()
     27 {
     28     var db = evalAndLog("db = event.target.result");
     29 
     30     request = evalAndLog("db.setVersion('new version')");
     31     request.onsuccess = setVersionSuccess;
     32     request.onerror = unexpectedErrorCallback;
     33 }
     34 
     35 function setVersionSuccess()
     36 {
     37     debug("setVersionSuccess():");
     38     window.trans = evalAndLog("trans = event.target.result");
     39     shouldBeTrue("trans !== null");
     40     trans.onabort = unexpectedAbortCallback;
     41     trans.oncomplete = openBasicCursor;
     42 
     43     deleteAllObjectStores(db);
     44 
     45     var objectStore = evalAndLog("objectStore = db.createObjectStore('basicStore')");
     46     evalAndLog("objectStore.add('myValue1', 'myKey1').onerror = unexpectedErrorCallback");
     47     evalAndLog("objectStore.add('myValue2', 'myKey2').onerror = unexpectedErrorCallback");
     48     evalAndLog("objectStore.add('myValue3', 'myKey3').onerror = unexpectedErrorCallback");
     49     evalAndLog("objectStore.add('myValue4', 'myKey4').onerror = unexpectedErrorCallback");
     50 
     51     var objectStore = evalAndLog("objectStore = db.createObjectStore('autoIncrementStore', {autoIncrement: true})");
     52     evalAndLog("objectStore.add('foo1').onerror = unexpectedErrorCallback");
     53     evalAndLog("objectStore.add('foo2').onerror = unexpectedErrorCallback");
     54     evalAndLog("objectStore.add('foo3').onerror = unexpectedErrorCallback");
     55     evalAndLog("objectStore.add('foo4').onerror = unexpectedErrorCallback");
     56 
     57     var objectStore = evalAndLog("objectStore = db.createObjectStore('keyPathStore', {keyPath: 'id'})");
     58     evalAndLog("objectStore.createIndex('numberIndex', 'number')");
     59     evalAndLog("objectStore.add({number: 1, id: 1}).onerror = unexpectedErrorCallback");
     60     evalAndLog("objectStore.add({number: 2, id: 2}).onerror = unexpectedErrorCallback");
     61     evalAndLog("objectStore.add({number: 3, id: 3}).onerror = unexpectedErrorCallback");
     62     evalAndLog("objectStore.add({number: 4, id: 4}).onerror = unexpectedErrorCallback");
     63 }
     64 
     65 function openBasicCursor()
     66 {
     67     debug("openBasicCursor()");
     68     evalAndLog("trans = db.transaction([], webkitIDBTransaction.READ_WRITE)");
     69     trans.onabort = unexpectedAbortCallback;
     70     trans.oncomplete = transactionComplete;
     71 
     72     keyRange = webkitIDBKeyRange.lowerBound("myKey1");
     73     window.objectStore = evalAndLog("trans.objectStore('basicStore')");
     74     request = evalAndLog("objectStore.openCursor(keyRange)");
     75     request.onsuccess = basicUpdateCursor;
     76     request.onerror = unexpectedErrorCallback;
     77     counter = 1;
     78 }
     79 
     80 function basicUpdateCursor()
     81 {
     82     debug("basicUpdateCursor()");
     83     shouldBe("event.target.source", "objectStore");
     84     if (event.target.result == null) {
     85         shouldBe("counter", "5");
     86         counter = 1;
     87 
     88         request = evalAndLog("trans.objectStore('basicStore').openCursor(keyRange)");
     89         request.onsuccess = basicCheckCursor;
     90         request.onerror = unexpectedErrorCallback;
     91         return;
     92     }
     93 
     94     request = evalAndLog("event.target.result.update('myUpdatedValue' + counter++)");
     95     request.onsuccess = function() { evalAndLog("event.target.source.continue()"); }
     96     request.onerror = unexpectedErrorCallback;
     97 }
     98 
     99 function basicCheckCursor()
    100 {
    101     debug("basicCheckCursor()");
    102     if (event.target.result == null) {
    103         shouldBe("counter", "5");
    104         counter = 1;
    105 
    106         keyRange = webkitIDBKeyRange.lowerBound(1);
    107         request = evalAndLog("trans.objectStore('autoIncrementStore').openCursor(keyRange)");
    108         request.onsuccess = autoIncrementUpdateCursor;
    109         request.onerror = unexpectedErrorCallback;
    110         return;
    111     }
    112 
    113     shouldBeEqualToString("event.target.result.key", "myKey" + counter);
    114     shouldBeEqualToString("event.target.result.value", "myUpdatedValue" + counter++);
    115     evalAndLog("event.target.result.continue()");
    116 }
    117 
    118 function autoIncrementUpdateCursor()
    119 {
    120     debug("autoIncrementUpdateCursor()");
    121     if (event.target.result == null) {
    122         shouldBe("counter", "5");
    123         counter = 1;
    124 
    125         request = evalAndLog("trans.objectStore('autoIncrementStore').openCursor(keyRange)");
    126         request.onsuccess = autoIncrementCheckCursor;
    127         request.onerror = unexpectedErrorCallback;
    128         return;
    129     }
    130 
    131     request = evalAndLog("event.target.result.update('myUpdatedFoo' + counter++)");
    132     request.onsuccess = function() { evalAndLog("event.target.source.continue()"); }
    133     request.onerror = unexpectedErrorCallback;
    134 }
    135 
    136 function autoIncrementCheckCursor()
    137 {
    138     debug("autoIncrementCheckCursor()");
    139     if (event.target.result == null) {
    140         shouldBe("counter", "5");
    141         counter = 1;
    142 
    143         keyRange = webkitIDBKeyRange.lowerBound(1);
    144         request = evalAndLog("trans.objectStore('keyPathStore').openCursor(keyRange)");
    145         request.onsuccess = keyPathUpdateCursor;
    146         request.onerror = unexpectedErrorCallback;
    147         return;
    148     }
    149 
    150     shouldBe("event.target.result.key", "counter");
    151     shouldBeEqualToString("event.target.result.value", "myUpdatedFoo" + counter++);
    152     evalAndLog("event.target.result.continue()");
    153 }
    154 
    155 function keyPathUpdateCursor()
    156 {
    157     debug("keyPathUpdateCursor()");
    158     if (event.target.result == null) {
    159         shouldBe("counter", "5");
    160         counter = 1;
    161 
    162         request = evalAndLog("trans.objectStore('keyPathStore').openCursor(keyRange)");
    163         request.onsuccess = keyPathCheckCursor;
    164         request.onerror = unexpectedErrorCallback;
    165         return;
    166     }
    167 
    168     request = evalAndLog("event.target.result.update({id: 100 + counter, number: 100 + counter})");
    169     request.onsuccess = unexpectedSuccessCallback;
    170     request.onerror = function() {
    171         shouldBe("event.target.errorCode", "webkitIDBDatabaseException.DATA_ERR");
    172 
    173         evalAndLog("event.preventDefault()");
    174 
    175         request = evalAndLog("event.target.source.update({id: counter, number: 100 + counter++})");
    176         request.onsuccess = function() { evalAndLog("event.target.source.continue()") };
    177         request.onerror = unexpectedErrorCallback;
    178     }
    179 }
    180 
    181 function keyPathCheckCursor()
    182 {
    183     debug("keyPathCheckCursor()");
    184     if (event.target.result == null) {
    185         shouldBe("counter", "5");
    186         counter = 1;
    187 
    188         keyRange = webkitIDBKeyRange.lowerBound(101);
    189         request = evalAndLog("trans.objectStore('keyPathStore').index('numberIndex').openKeyCursor(keyRange)");
    190         request.onsuccess = keyCursor;
    191         request.onerror = unexpectedErrorCallback;
    192         return;
    193     }
    194 
    195     shouldBe("event.target.result.key", "counter");
    196     shouldBe("event.target.result.value.id", "counter");
    197     shouldBe("event.target.result.value.number", (counter + 100).toString());
    198     counter++;
    199     evalAndLog("event.target.result.continue()");
    200 }
    201 
    202 function keyCursor()
    203 {
    204     debug("keyCursor()");
    205     if (event.target.result == null) {
    206         shouldBe("counter", "5");
    207         return;
    208     }
    209 
    210     shouldBe("event.target.result.key", "counter + 100");
    211     shouldBe("event.target.result.primaryKey", "counter");
    212 
    213     try {
    214         debug("event.target.result.update({id: counter, number: counter + 200})");
    215         event.target.result.update({id: counter, number: counter + 200});
    216         testFailed("Expected exception.");
    217     } catch (e) {
    218         code = e.code;
    219         shouldBe("code", "webkitIDBDatabaseException.NOT_ALLOWED_ERR");
    220     }
    221 
    222     counter++;
    223     event.target.result.continue();
    224 }
    225 
    226 function transactionComplete()
    227 {
    228     debug("transactionComplete()");
    229     done();
    230 }
    231 
    232 var successfullyParsed = true;
    233 
    234 </script>
    235 </body>
    236 </html>
    237