<html>
<head>
<script>

function log(message)
{
    document.getElementById("console").innerHTML += message + "<br>";
}

function finishTest()
{
    log("Test Complete");
    if (window.testRunner)
        testRunner.notifyDone();
}

var txCallbackCount = 0;
var NUMBER_OF_TRANSACTIONS = 10;
var database;

function runTransaction(expectedToFail, statementErrorCallback)
{
    database.transaction(function(tx) {
        tx.executeSql("CREATE TABLE IF NOT EXISTS TestTable (RandomData TEXT)");
        tx.executeSql("INSERT INTO TestTable VALUES (?)", ['test']);
        tx.executeSql("THIS STATEMENT WILL FAIL", [],
            function(tx, data) {
                log("FAIL - this statement should have failed");
                finishTest();
            }, statementErrorCallback);
        tx.executeSql("INSERT INTO TestTable VALUES (?)", ['test1'],
            function(error) {
                if (expectedToFail)
                    log("FAIL - This statement should not have been executed");
            }, function() {
                if (expectedToFail)
                    log("FAIL - This statement should not have been executed");
            });
    }, function(error) {
        if (expectedToFail)
            log("PASS - the transaction error callback was invoked.");
        else
            log("FAIL - the transaction error callback should not have been invoked.");
        if (++txCallbackCount == NUMBER_OF_TRANSACTIONS)
            finishTest();
    }, function() {
        if (expectedToFail)
            log("FAIL - the transaction success callback should not have been invoked.");
        else
            log("PASS - the transaction success callback was invoked.");
        if (++txCallbackCount == NUMBER_OF_TRANSACTIONS)
            finishTest();
    });
}

function runTest()
{
    if (window.testRunner) {
        testRunner.clearAllDatabases();
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }

    database = openDatabase("StatementErrorCallbackTest", "1.0", "statement error callback test", 1024);

    runTransaction(true, function(error) { return true; });
    runTransaction(true, function(error) { throw "Exception in statement error callback"; return false; });
    runTransaction(true, function(error) { return "some string"; });
    runTransaction(true, function(error) { return 1234; });
    runTransaction(true, function(error) { return {a: 2, b: "abc"}; });
    runTransaction(true, function(error) { return "false"; });
    runTransaction(false, function(error) {});
    runTransaction(false, function(error) { return false; });
    runTransaction(false, function(error) { return 0; });
    runTransaction(false, function(error) { return null; });
}

</script>
</head>

<body onload="runTest()">
This test confirms that a transaction is immediately rolled back if and only if a statement's error callback throws an exception, returns true, or doesn't return any value.
<pre id="console">
</pre>
</body>

</html>
