Html程序  |  106行  |  3 KB

<html>
<head>
<script>

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

// signal to layoutTestController when this reaches zero.
var testCount = 4;
// we first retrieve and store the number of rows already in our test database.
// our goal is to keep the number unchanged through the tests.
var initialRowCount = 0;
var database;
var successCallbackCalled;

function finishTest()
{
    if (--testCount)
        return;

    log("All Tests are complete.");

    if (window.layoutTestController)
        layoutTestController.notifyDone();
}

function successCallback()
{
    successCallbackCalled = true;
}

function verifySuccess(msg)
{
    database.transaction(function(tx)
    {
        tx.executeSql("SELECT count(*) AS count FROM ErrorCallbackTest", [], function(tx, rs)
        {
            log(msg + " : " + (rs.rows.item(0).count == initialRowCount && !successCallbackCalled ? "SUCCESS" : "FAILURE"));
            finishTest();
        });
    });
}

function failMidWay(errorCallback)
{
    successCallbackCalled = false;
    database.transaction(function(tx)
    {
        tx.executeSql("INSERT INTO ErrorCallbackTest(someValue) VALUES(?);", [ 1 ]);
        tx.executeSql("MUTTER SOMETHING ILLEGIBLE");
    }, errorCallback, successCallback);
}

function statementCallbackThrowsException(errorCallback)
{
    successCallbackCalled = false;
    database.transaction(function(tx)
    {
        tx.executeSql("INSERT INTO ErrorCallbackTest(someValue) VALUES(?);", [ 1 ], function()
        {
            throw {};
        });
    });
}

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

    database = openDatabase("ErrorCallbackDatabase", "1.0", "Test for error callback", 1);
    database.transaction(function(tx)
    {
        tx.executeSql("CREATE TABLE IF NOT EXISTS ErrorCallbackTest (someValue)", []);
        tx.executeSql("SELECT count(*) AS count FROM ErrorCallbackTest", [], function(tx, rs)
        {
            initialRowCount = rs.rows.item(0).count;
        });
    });

    failMidWay(function() { return true; });
    verifySuccess("Testing transaction failing mid-way and error callback returning true");
    failMidWay(function() { return false; });
    verifySuccess("Testing transaction failing mid-way and error callback return false");
    statementCallbackThrowsException(function() { return true; });
    verifySuccess("Testing statement callback throwing exception and error callback returning true");
    statementCallbackThrowsException(function() { return false; });
    verifySuccess("Testing statement callback throwing exception and error callback returning false");
}

</script>
</head>

<body onload="runTest()">
This test confirms that <code>SQLTransactionErrorCallback</code> is invoked correctly and regardless of its output, the transaction is always rolled back on failure.
<pre id="console">
</pre>
</body>

</html>