<script>
// The test works by first navigating iframe to a page that allocates a lot of JS objects
// and then creates a long timeout. Then iframe is navigated to about:blank and the number of JS
// objects is verified - there should be a significant drop in their number.

var objectCountBefore;
function start() {
    if (window.testRunner && window.GCController) {
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
        window.GCController.collect();
        objectCountBefore = window.GCController.getJSObjectCount();
        var iframeElement = document.getElementById("iframe");
        iframeElement.setAttribute("onload", "loadBlank()");
        iframeElement.src = "resources/long_timeout.html";
    }
}

var objectCountAfterLoad;
function loadBlank() {
    window.GCController.collect();
    objectCountAfterLoad = window.GCController.getJSObjectCount();
    var iframeElement = document.getElementById("iframe");
    iframeElement.setAttribute("onload", "verify()");
    iframeElement.src = "about:blank";
}

function verify()
{
  var allocatedObjects = 1000; // Number of allocated objects in "resources/long_timeout.html".
  var threshold = allocatedObjects / 2;

    window.GCController.collect();
    var objectCountAfter = window.GCController.getJSObjectCount();
    // Some number of objects is still not released (possibly related to execution of this script).
    // Use a threshold which is less then the numebr of allocated objects in "resources/long_timeout.html" to account for this.
    var success = (objectCountAfter - objectCountBefore) < threshold;
    document.getElementById("result").innerText = (success ? "PASS" : "FAIL" +
                                                                      ", before: " +  objectCountBefore +
                                                                      ", after load: " + objectCountAfterLoad +
                                                                      ", after: " + objectCountAfter);
    testRunner.notifyDone();
}
</script>
<body onload="start()">
<p>This test verifies that an active timeout that didn't yet fire does not prevent a Document from releasing (bug https://bugs.webkit.org/show_bug.cgi?id=22710). Test can only run in DumpRenderTree since it needs GCController. Test is successful if it prints 'PASS'.</p>
<div id="result"></div>
<iframe id="iframe"></iframe>
</body>
