<html>
<head>
<title>Test database.changeVersion</title>
<script>
var db1;
var EXPECTED_VERSION_AFTER_HIXIE_TEST = '2';
var EXPECTED_VERSION_AFTER_RELOAD = '3';

function emptyFunction() { }

function changeVersionCallback(tx)
{
    tx.executeSql("DROP table if exists info;", [], emptyFunction, emptyFunction);
    tx.executeSql("CREATE table if not exists info (version INTEGER);", [], emptyFunction, emptyFunction);
    tx.executeSql("INSERT into info values(?);", [EXPECTED_VERSION_AFTER_RELOAD], emptyFunction, emptyFunction);
}

function changeVersionSuccess()
{
    log("Successfully changed version to " + db1.version + ". Reloading.");
    window.location.href = window.location + '?2';
}

function changeVersionError(error)
{
    log("Error: " + error.message);
    finishTest();
}

function finishTest()
{
    if (window.testRunner)
        testRunner.notifyDone();
    log("TEST COMPLETE");
}

function log(message)
{
    document.getElementById("console").innerText += message + "\n";
}

function runTest()
{
    if (window.location.search == "?2") {
        db1 = window.openDatabase("changeversion-test", "", "Test for the database.changeVersion() function", 1024);
        log("Finished tests with version " + db1.version + "; expected version: " + EXPECTED_VERSION_AFTER_RELOAD);

        // Reset the DB version or the next run might fail.
        db1.changeVersion(db1.version, "1");

        finishTest();
    } else
        testPart1();
}

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

    db1 = window.openDatabase("changeversion-test", "1", "Test for the database.changeVersion() function", 1024);
    var db2 = window.openDatabase("changeversion-test", "1", "Test for the database.changeVersion() function", 1024);

    // First run Hixie's test to ensure basic changeVersion functionality works (see bug 28418).
    db1.changeVersion("1", EXPECTED_VERSION_AFTER_HIXIE_TEST, null, function (e) {
        log("FAIL in changeVersion:" + e);
        finishTest();
    }, function () {
        // Make sure the version change has propagated to db2 too.
        // All transactions on db2 should fail.
        if (db2.version != db1.version) {
            log("FAIL: changing db1's version (" + db1.version + ") did not change db2's version (" + db2.version + ") as expected.");
            finishTest();
        }
        db2.transaction(function(tx) {
          tx.executeSql("CREATE TABLE IF NOT EXISTS Test (Foo INT)");
        }, function(error) { }, function() {
            log("FAIL: The DB version changed, all transactions on db2 should fail.");
            finishTest();
        });

        // Make sure any new handle to the same DB sees the new version
        try {
            var db3 = openDatabase("change-version-test", EXPECTED_VERSION_AFTER_HIXIE_TEST, "", 0);
        } catch (e) {
            log("FAIL in openDatabase: " + e);
            finishTest();
        }
        if (db1.version != db3.version) {
            log("FAIL: db.version(" + db1.version + ") does not match db3.version(" + db3.version +")");
            finishTest();
        }

        // Now try a test to ensure the version persists after reloading (see bug 27836)
        db1.changeVersion(EXPECTED_VERSION_AFTER_HIXIE_TEST, EXPECTED_VERSION_AFTER_RELOAD, changeVersionCallback, changeVersionError, changeVersionSuccess);
    });
}
</script>
</head>
<body onload="runTest();">
This test verifies that the JS database.changeVersion() function works as expected.
<pre id="console"></pre>
</body>
</html>
