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 transaction basics.");
     14 if (window.layoutTestController)
     15     layoutTestController.waitUntilDone();
     16 
     17 function test()
     18 {
     19     shouldBeTrue("'webkitIndexedDB' in window");
     20     shouldBeFalse("webkitIndexedDB == null");
     21 
     22     request = evalAndLog("webkitIndexedDB.open('transaction-basics')");
     23     request.onsuccess = openSuccess;
     24     request.onerror = unexpectedErrorCallback;
     25 }
     26 
     27 function openSuccess()
     28 {
     29     debug("openSuccess():");
     30     window.db = evalAndLog("db = event.target.result");
     31     request = evalAndLog("request = db.setVersion('version 1')");
     32     request.onsuccess = cleanDatabase;
     33     request.onerror = unexpectedErrorCallback;
     34 }
     35 
     36 function cleanDatabase()
     37 {
     38     deleteAllObjectStores(db, checkMetadataEmpty);
     39     commitAndContinue(testSetVersionAbort1);
     40 }
     41 
     42 function testSetVersionAbort1()
     43 {
     44     checkMetadataEmpty();
     45     request = evalAndLog("request = startSetVersion('version fail')");
     46     request.onsuccess = addRemoveIDBObjects;
     47 }
     48 
     49 function addRemoveIDBObjects()
     50 {
     51     debug("addRemoveIDBObjects():");
     52     var trans = evalAndLog("trans = event.target.result");
     53     shouldBeTrue("trans !== null");
     54     trans.addEventListener('abort', testSetVersionAbort2, true);
     55     trans.oncomplete = unexpectedCompleteCallback;
     56 
     57     var store = evalAndLog("store = db.createObjectStore('storeFail', null)");
     58     var index = evalAndLog("index = store.createIndex('indexFail', 'x', false)");
     59 
     60     evalAndLog("db.deleteObjectStore('storeFail')");
     61     evalAndLog("store.deleteIndex('indexFail')");
     62 
     63     trans.abort();
     64 }
     65 
     66 function testSetVersionAbort2()
     67 {
     68     checkMetadataEmpty();
     69     request = evalAndLog("request = startSetVersion('version fail')");
     70     request.onsuccess = addRemoveAddIDBObjects;
     71 }
     72 
     73 function addRemoveAddIDBObjects()
     74 {
     75     debug("addRemoveAddIDBObjects():");
     76     var trans = evalAndLog("trans = event.target.result");
     77     shouldBeTrue("trans !== null");
     78     trans.addEventListener('abort', testSetVersionAbort3, false);
     79     trans.oncomplete = unexpectedCompleteCallback;
     80 
     81     var store = evalAndLog("store = db.createObjectStore('storeFail', null)");
     82     var index = evalAndLog("index = store.createIndex('indexFail', 'x', false)");
     83 
     84     evalAndLog("db.deleteObjectStore('storeFail')");
     85     evalAndLog("store.deleteIndex('indexFail')");
     86 
     87     var store = evalAndLog("store = db.createObjectStore('storeFail', null)");
     88     var index = evalAndLog("index = store.createIndex('indexFail', 'x', false)");
     89 
     90     trans.abort();
     91 }
     92 
     93 function testSetVersionAbort3()
     94 {
     95     shouldBeFalse("event.cancelable");
     96     checkMetadataEmpty();
     97     request = evalAndLog("request = startSetVersion('version fail')");
     98     request.onsuccess = addIDBObjects;
     99 }
    100 
    101 function addIDBObjects()
    102 {
    103     shouldBeFalse("event.cancelable");
    104     debug("addIDBObjects():");
    105     var trans = evalAndLog("trans = event.target.result");
    106     shouldBeTrue("trans !== null");
    107     trans.onabort = testSetVersionAbort4;
    108     trans.oncomplete = unexpectedCompleteCallback;
    109 
    110     var store = evalAndLog("store = db.createObjectStore('storeFail', null)");
    111     var index = evalAndLog("index = store.createIndex('indexFail', 'x', false)");
    112 
    113     trans.abort();
    114 }
    115 
    116 function testSetVersionAbort4()
    117 {
    118     checkMetadataEmpty();
    119     request = evalAndLog("request = startSetVersion('version fail')");
    120     request.onsuccess = addIDBObjectsAndCommit;
    121 }
    122 
    123 function addIDBObjects()
    124 {
    125     debug("addIDBObjects():");
    126     var trans = evalAndLog("trans = event.target.result");
    127     shouldBeTrue("trans !== null");
    128     trans.onabort = unexpectedAbortCallback;
    129 
    130     var store = evalAndLog("store = db.createObjectStore('storeFail', null)");
    131     var index = evalAndLog("index = store.createIndex('indexFail', 'x', false)");
    132 
    133     commitAndContinue(testSetVersionAbort5);
    134 }
    135 
    136 function commitAndContinue(continueFunction)
    137 {
    138     window.setTimeout(continueFunction, 0);
    139 }
    140 
    141 function testSetVersionAbort5()
    142 {
    143     checkMetadataExistingObjectStore();
    144     request = evalAndLog("request = startSetVersion('version fail')");
    145     request.onsuccess = removeIDBObjects;
    146 }
    147 
    148 function removeIDBObjects()
    149 {
    150     debug("removeIDBObjects():");
    151     var trans = evalAndLog("trans = event.target.result");
    152     shouldBeTrue("trans !== null");
    153     trans.onabort = testSetVersionAbort6;
    154     trans.oncomplete = unexpectedCompleteCallback;
    155 
    156     var store = evalAndLog("store = trans.objectStore('storeFail')");
    157     evalAndLog("store.deleteIndex('indexFail')");
    158     evalAndLog("db.deleteObjectStore('storeFail')");
    159 
    160     trans.abort();
    161 }
    162 
    163 function testSetVersionAbort6()
    164 {
    165     checkMetadataExistingObjectStore();
    166     setNewVersion();
    167 }
    168 
    169 function startSetVersion(versionName)
    170 {
    171     request = db.setVersion(versionName);
    172     request.onerror = unexpectedErrorCallback;
    173     return request;
    174 }
    175 
    176 function checkMetadataEmpty()
    177 {
    178     shouldBe("window.db.objectStoreNames", "[]");
    179     shouldBe("window.db.objectStoreNames.length", "0");
    180     shouldBe("window.db.objectStoreNames.contains('storeFail')", "false");
    181 }
    182 
    183 function checkMetadataExistingObjectStore()
    184 {
    185     shouldBe("db.objectStoreNames", "['storeFail']");
    186     shouldBe("db.objectStoreNames.length", "1");
    187     shouldBe("db.objectStoreNames.contains('storeFail')", "true");
    188 }
    189 
    190 function setNewVersion()
    191 {
    192     request = evalAndLog("db.setVersion('new version')");
    193     request.onsuccess = setVersionSuccess;
    194     request.onerror = unexpectedErrorCallback;
    195 }
    196 
    197 function setVersionSuccess()
    198 {
    199     debug("setVersionSuccess():");
    200     window.trans = evalAndLog("trans = event.target.result");
    201     shouldBeTrue("trans !== null");
    202     trans.onabort = unexpectedAbortCallback;
    203     trans.addEventListener('complete', completeCallback, false);
    204     window.completeEventFired = false;
    205 
    206     deleteAllObjectStores(db);
    207 
    208     evalAndLog("db.createObjectStore('storeName', null)");
    209     transaction = evalAndLog("db.transaction({mode: webkitIDBTransaction.READ_WRITE})");
    210     transaction.onabort = abortCallback;
    211     var store = evalAndLog("store = transaction.objectStore('storeName')");
    212     shouldBeEqualToString("store.name", "storeName");
    213 }
    214 
    215 function completeCallback()
    216 {
    217     shouldBeFalse("event.cancelable");
    218     testPassed("complete event fired");
    219     window.completeEventFired = true;
    220 }
    221 
    222 function abortCallback()
    223 {
    224     testPassed("complete event fired");
    225     checkForCompleteEvent();
    226 }
    227 
    228 function checkForCompleteEvent()
    229 {
    230     if (completeEventFired) {
    231         testPassed("oncomplete event had fired");
    232         done();
    233     } else
    234         setTimeout(checkForCompleteEvent, 1);
    235 }
    236 
    237 var successfullyParsed = true;
    238 
    239 test();
    240 
    241 </script>
    242 </body>
    243 </html>
    244