1<!DOCTYPE html> 2<meta charset=urf-8> 3<title>EventTarget#dispatchEvent(): redispatching a native event</title> 4<script src="/resources/testharness.js"></script> 5<script src="/resources/testharnessreport.js"></script> 6<script src="/resources/testdriver.js"></script> 7<script src="/resources/testdriver-actions.js"></script> 8<script src="/resources/testdriver-vendor.js"></script> 9<button>click me!</button> 10<div id=log></div> 11<script> 12var test_contentLoaded_redispatching = async_test("Redispatching DOMContentLoaded event after being dispatched"); 13var test_mouseup_redispatching = async_test("Redispatching mouseup event whose default action dispatches a click event"); 14var test_redispatching_of_dispatching_event = async_test("Redispatching event which is being dispatched"); 15 16var buttonElement = document.querySelector("button"); 17var contentLoadedEvent; 18 19var waitForLoad = new Promise(resolve => { 20 window.addEventListener("load", () => { requestAnimationFrame(resolve); }, {capture: false, once: true}); 21}); 22 23document.addEventListener("DOMContentLoaded", event => { 24 contentLoadedEvent = event; 25 test_redispatching_of_dispatching_event.step(() => { 26 assert_throws_dom("InvalidStateError", () => { 27 document.dispatchEvent(contentLoadedEvent); 28 }, "Trusted DOMContentLoaded event"); 29 }); 30}, {capture: true, once: true}); 31 32window.addEventListener("load", loadEvent => { 33 let untrustedContentLoadedEvent; 34 buttonElement.addEventListener("DOMContentLoaded", event => { 35 untrustedContentLoadedEvent = event; 36 test_contentLoaded_redispatching.step(() => { 37 assert_false(untrustedContentLoadedEvent.isTrusted, "Redispatched DOMContentLoaded event shouldn't be trusted"); 38 }); 39 test_redispatching_of_dispatching_event.step(() => { 40 assert_throws_dom("InvalidStateError", () => { 41 document.dispatchEvent(untrustedContentLoadedEvent); 42 }, "Untrusted DOMContentLoaded event"); 43 }); 44 }); 45 46 test_contentLoaded_redispatching.step(() => { 47 assert_true(contentLoadedEvent.isTrusted, "Received DOMContentLoaded event should be trusted before redispatching"); 48 buttonElement.dispatchEvent(contentLoadedEvent); 49 assert_false(contentLoadedEvent.isTrusted, "Received DOMContentLoaded event shouldn't be trusted after redispatching"); 50 assert_not_equals(untrustedContentLoadedEvent, undefined, "Untrusted DOMContentLoaded event should've been fired"); 51 test_contentLoaded_redispatching.done(); 52 }); 53}, {capture: true, once: true}); 54 55async function testMouseUpAndClickEvent() { 56 let mouseupEvent; 57 buttonElement.addEventListener("mouseup", event => { 58 mouseupEvent = event; 59 test_mouseup_redispatching.step(() => { 60 assert_true(mouseupEvent.isTrusted, "First mouseup event should be trusted"); 61 }); 62 test_redispatching_of_dispatching_event.step(() => { 63 assert_throws_dom("InvalidStateError", () => { 64 buttonElement.dispatchEvent(mouseupEvent); 65 }, "Trusted mouseup event"); 66 }); 67 }, {once: true}); 68 69 let clickEvent; 70 buttonElement.addEventListener("click", event => { 71 clickEvent = event; 72 test_mouseup_redispatching.step(() => { 73 assert_true(clickEvent.isTrusted, "First click event should be trusted"); 74 }); 75 test_redispatching_of_dispatching_event.step(() => { 76 assert_throws_dom("InvalidStateError", function() { 77 buttonElement.dispatchEvent(event); 78 }, "Trusted click event"); 79 }); 80 buttonElement.addEventListener("mouseup", event => { 81 test_mouseup_redispatching.step(() => { 82 assert_false(event.isTrusted, "Redispatched mouseup event shouldn't be trusted"); 83 }); 84 test_redispatching_of_dispatching_event.step(() => { 85 assert_throws_dom("InvalidStateError", function() { 86 buttonElement.dispatchEvent(event); 87 }, "Untrusted mouseup event"); 88 }); 89 }, {once: true}); 90 function onClick() { 91 test_mouseup_redispatching.step(() => { 92 assert_true(false, "click event shouldn't be fired for dispatched mouseup event"); 93 }); 94 } 95 test_mouseup_redispatching.step(() => { 96 assert_true(mouseupEvent.isTrusted, "Received mouseup event should be trusted before redispatching from click event listener"); 97 buttonElement.addEventListener("click", onClick); 98 buttonElement.dispatchEvent(mouseupEvent); 99 buttonElement.removeEventListener("click", onClick); 100 assert_false(mouseupEvent.isTrusted, "Received mouseup event shouldn't be trusted after redispatching"); 101 assert_true(clickEvent.isTrusted, "First click event should still be trusted even after redispatching mouseup event"); 102 }); 103 }, {once: true}); 104 105 await waitForLoad; 106 let bounds = buttonElement.getBoundingClientRect(); 107 test(() => { assert_true(true); }, `Synthesizing click on button...`); 108 new test_driver.click(buttonElement) 109 .then(() => { 110 test_mouseup_redispatching.step(() => { 111 assert_not_equals(clickEvent, undefined, "mouseup and click events should've been fired"); 112 }); 113 test_mouseup_redispatching.done(); 114 test_redispatching_of_dispatching_event.done(); 115 }, (reason) => { 116 test_mouseup_redispatching.step(() => { 117 assert_true(false, `Failed to send mouse click due to ${reason}`); 118 }); 119 test_mouseup_redispatching.done(); 120 test_redispatching_of_dispatching_event.done(); 121 }); 122} 123testMouseUpAndClickEvent(); 124</script> 125