<p>This test verifies that garbage collection successfully cleans up after the destruction
of a frame containing a JavaScript interpreter. If the test passes, you'll see a PASS message below.</p>
<hr>
<pre id="log"></pre>

<!-- The test: -->
<iframe id="iframe" src="data:text/html,<script>;</script>" style="display:none"></iframe>
<script>
function log(s)
{
    document.getElementById("log").appendChild(document.createTextNode(s));
}

var wiggleRoom = 20;

window.onload = function main()
{
    if (!window.testRunner || !window.GCController) {
        log("FAIL: This test uses the testRunner and the GCController, so it can only run in DumpRenderTree.\n");
        return;
    }
    
    testRunner.dumpAsText();
    testRunner.waitUntilDone();

    // Start with no garbage, so GC doesn't run automatically during the test.
    GCController.collect();
    
    // Create a little garbage, so we can detect failure in the case where the 
    // test doesn't produce any garbage because it fails to destroy the frame.
    var garbage = [ { }, { }, { }, { }, { }, { }, { }, { }, { }, { } ];
    garbage = null;

    // Destroy a frame that has a JavaScript interpreter.
    var iframe = document.getElementById("iframe");
    iframe.parentNode.removeChild(iframe);

    // Finish the test on a time-out, to allow the Frame keepAlive timer to fire
    // and release the frame before we generate results.
    var continueTest = function continueTest()
    {
        // Measure how many garbage JS objects are still around.
        var jsObjectCountStart = GCController.getJSObjectCount();
        GCController.collect();
        var jsObjectCountDelta = jsObjectCountStart - GCController.getJSObjectCount();
        var garbageCount = jsObjectCountDelta > 0 ? jsObjectCountDelta : 0;
        
        // Fail if a lot of garbage was left behind. This can mean one of many things:
        // the frame was not destroyed; the frame destruction did not trigger GC; or
        // GC was not effective.
        if (garbageCount > wiggleRoom) {
            log("FAIL: " + garbageCount + " garbage JS objects left uncollected.\n");
        } else {
            log("PASS: " + wiggleRoom + " garbage JS objects or fewer left uncollected after destroying a frame.\n");
        }

        testRunner.notifyDone();
    }
    setTimeout(continueTest, 10);
}
</script>
