<!DOCTYPE html>
<html>
<body>
<pre id='console'></pre>
<script src="../../resources/js-test-pre.js"></script>

<script>
var fileReader;
var receivedProgressEvent = false;

function log(message)
{
    document.getElementById('console').appendChild(document.createTextNode(message + "\n"));
}

function onLoadStart()
{
    log("Received loadstart event");

    fileReader.removeEventListener("abort", onAbort, false);

    var event = document.createEvent("Events");
    event.initEvent("foo", false, false);
    fileReader.dispatchEvent(event);
}

function onProgress(event)
{
    receivedProgressEvent = true;
    shouldBeFalse("event.bubbles");
    shouldBeFalse("event.cancelable");

    event.preventDefault();
    shouldBeFalse("event.defaultPrevented");
}

function onLoad()
{
    if (receivedProgressEvent)
        log("Received progress event");
    log("Received load event");
}

function onLoadEnd()
{
    log("Received loadend event");
    
    fileReader.removeEventListener("loadstart", onLoadStart, false);
    fileReader.removeEventListener("load", onLoad, false);
    fileReader.removeEventListener("error", onError, false);

    finishJSTest();
}

function onAbort()
{
    log("Received abort event");
}

function onError(event)
{
    log("Received error event: " + event.target.error.code);
}

function onFoo()
{
    log("Received foo event");
}

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

    window.jsTestIsAsync = true;

    log("Test that FileReader's event listener works.");

    var text = "Hello";
    fileReader = new FileReader();

    fileReader.addEventListener("loadstart", onLoadStart, false);
    fileReader.addEventListener("progress", onProgress, false);
    fileReader.addEventListener("load", onLoad, false);
    fileReader.addEventListener("loadend", onLoadEnd, false);
    fileReader.addEventListener("abort", onAbort, false);
    fileReader.addEventListener("error", onError, false);
    fileReader.addEventListener("foo", onFoo, false);

    fileReader.readAsText(new Blob([text]));
}

runTest();

</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
