1 <html> 2 <head> 3 <script> 4 5 function log(message) 6 { 7 document.getElementById("console").innerHTML += message + "<br>"; 8 } 9 10 // signal to layoutTestController when this reaches zero. 11 var testCount = 4; 12 // we first retrieve and store the number of rows already in our test database. 13 // our goal is to keep the number unchanged through the tests. 14 var initialRowCount = 0; 15 var database; 16 var successCallbackCalled; 17 18 function finishTest() 19 { 20 if (--testCount) 21 return; 22 23 log("All Tests are complete."); 24 25 if (window.layoutTestController) 26 layoutTestController.notifyDone(); 27 } 28 29 function successCallback() 30 { 31 successCallbackCalled = true; 32 } 33 34 function verifySuccess(msg) 35 { 36 database.transaction(function(tx) 37 { 38 tx.executeSql("SELECT count(*) AS count FROM ErrorCallbackTest", [], function(tx, rs) 39 { 40 log(msg + " : " + (rs.rows.item(0).count == initialRowCount && !successCallbackCalled ? "SUCCESS" : "FAILURE")); 41 finishTest(); 42 }); 43 }); 44 } 45 46 function failMidWay(errorCallback) 47 { 48 successCallbackCalled = false; 49 database.transaction(function(tx) 50 { 51 tx.executeSql("INSERT INTO ErrorCallbackTest(someValue) VALUES(?);", [ 1 ]); 52 tx.executeSql("MUTTER SOMETHING ILLEGIBLE"); 53 }, errorCallback, successCallback); 54 } 55 56 function statementCallbackThrowsException(errorCallback) 57 { 58 successCallbackCalled = false; 59 database.transaction(function(tx) 60 { 61 tx.executeSql("INSERT INTO ErrorCallbackTest(someValue) VALUES(?);", [ 1 ], function() 62 { 63 throw {}; 64 }); 65 }); 66 } 67 68 function runTest() 69 { 70 if (window.layoutTestController) { 71 layoutTestController.clearAllDatabases(); 72 layoutTestController.dumpAsText(); 73 layoutTestController.waitUntilDone(); 74 } 75 76 database = openDatabase("ErrorCallbackDatabase", "1.0", "Test for error callback", 1); 77 database.transaction(function(tx) 78 { 79 tx.executeSql("CREATE TABLE IF NOT EXISTS ErrorCallbackTest (someValue)", []); 80 tx.executeSql("SELECT count(*) AS count FROM ErrorCallbackTest", [], function(tx, rs) 81 { 82 initialRowCount = rs.rows.item(0).count; 83 }); 84 }); 85 86 failMidWay(function() { return true; }); 87 verifySuccess("Testing transaction failing mid-way and error callback returning true"); 88 failMidWay(function() { return false; }); 89 verifySuccess("Testing transaction failing mid-way and error callback return false"); 90 statementCallbackThrowsException(function() { return true; }); 91 verifySuccess("Testing statement callback throwing exception and error callback returning true"); 92 statementCallbackThrowsException(function() { return false; }); 93 verifySuccess("Testing statement callback throwing exception and error callback returning false"); 94 } 95 96 </script> 97 </head> 98 99 <body onload="runTest()"> 100 This test confirms that <code>SQLTransactionErrorCallback</code> is invoked correctly and regardless of its output, the transaction is always rolled back on failure. 101 <pre id="console"> 102 </pre> 103 </body> 104 105 </html> 106