<html>
<head>
<script src="/js-test-resources/js-test-pre.js"></script>
<script>
function write(s)
{
    document.getElementById('pre').appendChild(document.createTextNode(s));
}

var didCollect = false;
function collect()
{
    didCollect = true;
    gc();
}

function processStateChange(e)
{
    write("PASS: event handler fired" + (didCollect ? " after garbage collection" : "") + ", readyState: " + e.target.readyState + ".\n");
    if (e.target.readyState == 4) {
        if (window.testRunner)
            testRunner.notifyDone();
    }
}

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

    var request = new XMLHttpRequest();
    request.onreadystatechange = processStateChange;
    request.open("GET", "resources/print-headers.cgi", true);
    request.send(null);
    request = null;
}

window.addEventListener("load", test);
window.addEventListener("load", collect);
</script>
</head>

<body>
<p>This test checks whether event handlers for outstanding XMLHttpRequests survive garbage collection.</p>
<p>See https://bugs.webkit.org/show_bug.cgi?id=9113 REGRESSION (14581): 
XMLHttpRequest never calls onreadystatechange with a readystate == 4</p>
<p>If the test passes, you'll see a series of 'PASS' messages below.</p>
<hr>

<pre id='pre'></pre>

</body>
</html>
