<html>
<head></head>
<body>

<p>Test bug 13141 : XMLHttpRequest should set readyState to 0 after abort() </p>
<p>Updated for bug 16989: Add send() flag checks in XmlHttpRequest </p>
<p>Should see "PASS" two times:</p>
<div id="ans"></div>

<script type="text/javascript">
function log(message) {
    document.getElementById("ans").appendChild(document.createTextNode(message));
    document.getElementById("ans").appendChild(document.createElement("br"));
}

// Used to detect if a readystate event is dispatched
function catchReadystateEventAbort() {
    log("FAILED (readystate event send)");
}

function testAbortDoNotDispatchEvent(num)
{
    var xhr;

    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (ex) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    xhr.onreadystatechange = function () {
        if (this.readyState == num) {
            ++finishedTests;
            this.onreadystatechange = catchReadystateEventAbort;
            this.abort();
            // Following https://github.com/whatwg/xhr/issues/54, abort() should
            // set readyState to UNSENT only when it (readyState) is DONE.
            var pass;
            if (num == XMLHttpRequest.DONE)
                pass = this.readyState == XMLHttpRequest.UNSENT;
            else
                pass = this.readyState == num;

            if (pass)
                log("PASS");
            else
                log("FAILED");
        }

        if (finishedTests == abortToDo.length && window.testRunner)
            testRunner.notifyDone();
    }

    xhr.open("GET", "resources/1251.html", true);

    // We want to be able to test in the OPENED state with the send() flag to false
    // so do not call send in that case
    if (num != 1)
        xhr.send(null);
}

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

var finishedTests = 0;

var abortToDo = ['1', '4'];

for (i = 0; i < abortToDo.length; i++)
    testAbortDoNotDispatchEvent(abortToDo[i]);

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