chromium/third_party/blink/web_tests/http/tests/xmlhttprequest/onloadend-event-after-load.html

<!doctype html>
<html>
<head>
<title>Test case for bug 40952</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
</head>
<body>
<p> Test case for <a href="https://bugs.webkit.org/show_bug.cgi?id=40952"> bug 40952</a>: Onloadend event is not supported in XMLHttpRequest</p>
<p> Verify that a loadend ProgressEvent is dispatched after the load ProgressEvent when an async request completes normally.</p>
<div id="log"></div>
<script type="text/javascript">

var xhr;
var payload = "data";
var expectedLength = payload.length;
var results = "";
var expected = " loadstart readyState=DONE load loadend";

function verifyProgressEvent(context, e, expected)
{
    assert_true(e.lengthComputable);
    assert_equals(e.loaded, expected, "Expected 'loaded' value for '" + context + "' event.");
    assert_equals(e.total, expected, "Expected 'total' value for '" + context + "' event.");
}

function logProgressEvent(e) {
    results += " " + e.type;
}

function logUnexpectedProgressEvent(e) {
    results += " [unexpected ProgressEvent: " + e.type + "]";
    completeTest();
}

function completeTest()
{
    assert_equals(results, expected, "Expected load event sequence");
    testOnloadEndEvent.done();
}

function loadendHandler(e)
{
    logProgressEvent(e);
    assert_true(e instanceof ProgressEvent);
    verifyProgressEvent("onloadend", e, expectedLength);
    completeTest();
}

var testOnloadEndEvent = async_test("Check that 'loadend' events are delivered and have expected values.");
testOnloadEndEvent.step(function () {
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = testOnloadEndEvent.step_func(function(e) {
        if (xhr.readyState == xhr.DONE)
            results += " readyState=DONE";
    });
    xhr.onloadstart = testOnloadEndEvent.step_func(logProgressEvent);
    xhr.onabort = testOnloadEndEvent.step_func(logUnexpectedProgressEvent);
    xhr.onerror = testOnloadEndEvent.step_func(logUnexpectedProgressEvent);
    xhr.onload = testOnloadEndEvent.step_func(logProgressEvent);
    xhr.onloadend = testOnloadEndEvent.step_func(loadendHandler);
    xhr.open("POST", "resources/post-echo.php", true);
    xhr.send(payload);
});
</script>
</body>
</html>