<html>
<head>
<script>
var frame = 1;

function printReferrer()
{
    var req = new XMLHttpRequest();
    req.open("GET", "callbacks-are-called-in-correct-context.cgi", false);
    req.send(null);
    top.frames[0].document.getElementById("console").innerText += "Referrer: " + req.responseText + "\n";
}

function logCallback(callbackType)
{
    top.frames[0].document.getElementById("console").innerText += "PASS: The " + callbackType + " callback was called in the correct context.\n"
    printReferrer();
}

function failingTransactionCallback(tx)
{
    logCallback("transaction");
    tx.executeSql("CREATE TABLE IF NOT EXISTS Test (Foo INT)", [], successStatementCallback, null);
    tx.executeSql("BAD SQL STATEMENT", [], null, errorStatementCallback);
}

function errorStatementCallback(tx, error)
{
    logCallback("statement error");
    return true; // fail the transaction
}

function successStatementCallback(tx, data)
{
    logCallback("statement success");
}

function errorTransactionCallback(error)
{
    logCallback("transaction error");
}

function successTransactionCallback()
{
    logCallback("transaction success");
    frame++;
    switch (frame) {
        case 2:
            runTestInSecondFrame();
            return;
        case 3:
            top.frames[3].runTestInThirdFrame();
            return;
        case 4:
            if (window.testRunner)
                testRunner.notifyDone();
            return;
        default:
            throw "Bad 'frame' value: " + frame;
    }
}

function runTestInSecondFrame()
{
    // test that all callbacks are executed in this context, even though 'db' is defined in another context
    top.frames[1].db.transaction(failingTransactionCallback, errorTransactionCallback);
    top.frames[1].db.transaction(function(tx) { }, null, successTransactionCallback);
}
</script>
</head>
<body>
</body>
</html>
