<!DOCTYPE html>
<html>
<body>
<script src="../../resources/js-test-pre.js"></script>
<script>
description("Tests calling initEvent() on an event while it is being dispatched and after.");

function eventHandler(ev) {
    event = ev;
    debug("");
    debug("* In event handler");
    shouldBeEqualToString("event.type", "foo");
    shouldBeTrue("event.bubbles");
    shouldBeFalse("event.cancelable");
    shouldBe("event.target", "document.body");
    shouldBe("event.currentTarget", "document.body");
    shouldBe("event.eventPhase", "Event.AT_TARGET");

    debug("");
    // It should not be possible to re-initialize an event being dispatched.
    shouldNotThrow('event.initEvent("bar", false, true)');
    shouldBeEqualToString("event.type", "foo");
    shouldBeTrue("event.bubbles");
    shouldBeFalse("event.cancelable");
    shouldBe("event.target", "document.body");
    shouldBe("event.currentTarget", "document.body");
    shouldBe("event.eventPhase", "Event.AT_TARGET");
}

var testEvent = document.createEvent("Event");
shouldNotThrow('testEvent.initEvent("foo", true, false)');
shouldBeEqualToString("testEvent.type", "foo");
shouldBeTrue("testEvent.bubbles");
shouldBeFalse("testEvent.cancelable");
shouldBeNull("testEvent.target");
shouldBeNull("testEvent.currentTarget");
shouldBe("testEvent.eventPhase", "Event.NONE");

document.body.addEventListener("foo", eventHandler);
shouldNotThrow('document.body.dispatchEvent(testEvent)');

debug("");
debug("* After event has been dispatched");
shouldBeEqualToString("testEvent.type", "foo");
shouldBeTrue("testEvent.bubbles");
shouldBeFalse("testEvent.cancelable");
shouldBe("testEvent.target", "document.body");
shouldBeNull("testEvent.currentTarget");
shouldBe("testEvent.eventPhase", "Event.NONE");

// It should now be possible to re-initialize the event.
debug("");
shouldNotThrow('testEvent.initEvent("bar", false, true)');
shouldBeEqualToString("testEvent.type", "bar");
shouldBeFalse("testEvent.bubbles");
shouldBeTrue("testEvent.cancelable");
shouldBeNull("testEvent.target");
shouldBeNull("testEvent.currentTarget");
shouldBe("testEvent.eventPhase", "Event.NONE");
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
