<body>
<p>Test that adding an event listener for an event with a unique name works correctly (this used to corrupt event listener map after GC).</p>
<pre id=log></pre>

<script>
function gc()
{
    if (window.GCController)
        return GCController.collect();

    for (var i = 0; i < 10000; i++) { // > force garbage collection (FF requires about 9K allocations before a collect)
        var s = new String("abc");
    }
}

function log(message)
{
    document.getElementById("log").innerHTML += message + "<br>";
}

if (window.testRunner)
    testRunner.dumpAsText();

var listenerCallCount = 0;
var expectedListenerCallCount = 0;
function listener()
{
    ++listenerCallCount;
}

function addListener(targetName, name)
{
    var target;
    try {
        target = eval(targetName);
        eval(target).addEventListener(name, listener, true);
        ++expectedListenerCallCount;
    } catch (ex) {
        log((target ? target : targetName) + " is not supported, or cannot be used to register arbitrary event handlers.");
    }
}

function dispatchEvent(target, name)
{
    try {
        var evt = document.createEvent("MessageEvent");
        evt.initMessageEvent(name);
        eval(target).dispatchEvent(evt);
    } catch (ex) {
    }
}

var xhr = new XMLHttpRequest;
addListener("xhr", "foo1");
addListener("xhr.upload", "foo2");
addListener("window.applicationCache", "foo3");
var channel;
try {
    channel = new MessageChannel;
    channel.port1.addEventListener("foo4", listener, true);
    ++expectedListenerCallCount;
} catch (ex) {
    log("MessageChannel is not supported, or cannot be used to register arbitrary event handlers.");
}
// window.dispatchEvent is currently unimplemented.
// window.addEventListener("foo5", listener, true);
// ++expectedListenerCallCount;
document.body.addEventListener("foo6", listener, true);
++expectedListenerCallCount;

gc();

dispatchEvent("xhr", "foo1");
dispatchEvent("xhr.upload", "foo2");
dispatchEvent("window.applicationCache", "foo3");
dispatchEvent("channel.port1", "foo4");
dispatchEvent("window", "foo5");
dispatchEvent("document.body", "foo6");

gc();

log((listenerCallCount == expectedListenerCallCount) ? "PASS" : "FAIL");
</script>
</body>
