<html manifest="resources/multi-fallback.manifest">
<body>
<p>Test application cache fallback entries with multiple candidates.</p>
<p>Should say SUCCESS:</p>
<div id=result></div>

<script>
if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

var hadError = false;

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

function setNetworkEnabled(state)
{
    try {
        var req = new XMLHttpRequest;
        req.open("GET", "/resources/network-simulator.py?test=appcache-multi-fallback&command=" + (state ? "connect" : "disconnect"), false);
        req.send("");
    } catch (ex) {
        log("Cannot access network simulator URL, ex = " + ex);
        hadError = true;
    }
}

function load(url)
{
    try {
        var req = new XMLHttpRequest();
        req.open("GET", url, false);
        req.send("");
        return req.responseText;
    } catch (ex) {
        log("FAIL: Cannot load " + url + ", ex = " + ex);
        hadError = true;
        return ""; // This value should not be expected as the responseText for a url presented to this function.
    }
}

var nonexistentURL = "resources/does-not-exist";

function test()
{
    applicationCache.onnoupdate = function() { log("FAIL: received unexpected noupdate event") }
    applicationCache.oncached = function() { log("FAIL: received unexpected cached event") }

    setNetworkEnabled(true);

    if (load(nonexistentURL) != "Hello, World!") {
        log("FAIL: The longest fallback namespace wasn't used for a 404 response");
        hadError = true;
    }

    log(hadError ? "FAILURE" : "SUCCESS");
    if (window.testRunner)
        testRunner.notifyDone();
}

applicationCache.onnoupdate = function() { test() }
applicationCache.oncached = function() { test() }

applicationCache.onupdateready = function() { log("FAIL: received unexpected updateready event") }
applicationCache.onerror = function() { log("FAIL: received unexpected error event") }

</script>
</body>
</html>
