chromium/third_party/blink/web_tests/fast/events/event-fire-order.html

<p>This page tests event listener fire order for a few objects that had it wrong in the past.</p>
<p>If the test passes, you'll see a series of PASS messages below.</p>

<pre id="console"></pre>

<div id="div"></div>

<script>
if (window.testRunner)
    testRunner.dumpAsText();

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

var result;

function reset()
{
    result = [ ];
}

function f1()
{
    result.push("f1");
}

function f2(event)
{
    result.push("f2");
    event.target.removeEventListener(event.type, f3, false); // Removed: should not fire.
    event.target.addEventListener(event.type, f4, false); // Added after dispatch began: should not fire.
}

function f3()
{
    result.push("f3");
}

function f4()
{
    result.push("f4");
}

function reportResult(name, expected)
{
    var passed = true;
    var end = result.length > expected.length ? result.length : expected.length;
    for (var i = 0; i < end; ++i) {
        if (result[i] != expected[i]) {
            log("FAIL: " + name + " result[" + i + "] should be " + expected[i] + " but instead is " + result[i] + ".");
            passed = false;
        }
    }
    if (passed)
        log("PASS: result should be " + expected + " and is.");
}

var tests = [
    function testDiv()
    {
        reset();
        var x = document.getElementById("div");
        x.addEventListener("click", f1, false);
        x.onclick = f2;
        x.addEventListener("click", f3, false);

        var event = document.createEvent("MouseEvent");
        event.initMouseEvent("click", true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, document);
        x.dispatchEvent(event);

        reportResult(arguments.callee.name, [ "f1", "f2" ]);
    },

    function testXHR()
    {
        reset();
        var x = new XMLHttpRequest;
        x.addEventListener("readystatechange", f1, false);
        x.onreadystatechange = f2;
        x.addEventListener("readystatechange", f3, false);

        x.open("GET", "resources/does-not-exist");

        reportResult(arguments.callee.name, [ "f1", "f2" ]);
    },

    function testXHRUpload()
    {
        reset();
        var x = new XMLHttpRequest;
        x.upload.addEventListener("abort", f1, false);
        x.upload.onabort = f2;
        x.upload.addEventListener("abort", f3, false);

        x.open("POST", "resources/does-not-exist");
        x.send();
        x.abort();

        reportResult(arguments.callee.name, [ "f1", "f2" ]);
    }
];

for (var i = 0; i < tests.length; ++i)
    tests[i]();
</script>