11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<meta charset=urf-8> 31cb0ef41Sopenharmony_ci<title>EventTarget#dispatchEvent(): redispatching a native event</title> 41cb0ef41Sopenharmony_ci<script src="/resources/testharness.js"></script> 51cb0ef41Sopenharmony_ci<script src="/resources/testharnessreport.js"></script> 61cb0ef41Sopenharmony_ci<script src="/resources/testdriver.js"></script> 71cb0ef41Sopenharmony_ci<script src="/resources/testdriver-actions.js"></script> 81cb0ef41Sopenharmony_ci<script src="/resources/testdriver-vendor.js"></script> 91cb0ef41Sopenharmony_ci<button>click me!</button> 101cb0ef41Sopenharmony_ci<div id=log></div> 111cb0ef41Sopenharmony_ci<script> 121cb0ef41Sopenharmony_civar test_contentLoaded_redispatching = async_test("Redispatching DOMContentLoaded event after being dispatched"); 131cb0ef41Sopenharmony_civar test_mouseup_redispatching = async_test("Redispatching mouseup event whose default action dispatches a click event"); 141cb0ef41Sopenharmony_civar test_redispatching_of_dispatching_event = async_test("Redispatching event which is being dispatched"); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_civar buttonElement = document.querySelector("button"); 171cb0ef41Sopenharmony_civar contentLoadedEvent; 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_civar waitForLoad = new Promise(resolve => { 201cb0ef41Sopenharmony_ci window.addEventListener("load", () => { requestAnimationFrame(resolve); }, {capture: false, once: true}); 211cb0ef41Sopenharmony_ci}); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_cidocument.addEventListener("DOMContentLoaded", event => { 241cb0ef41Sopenharmony_ci contentLoadedEvent = event; 251cb0ef41Sopenharmony_ci test_redispatching_of_dispatching_event.step(() => { 261cb0ef41Sopenharmony_ci assert_throws_dom("InvalidStateError", () => { 271cb0ef41Sopenharmony_ci document.dispatchEvent(contentLoadedEvent); 281cb0ef41Sopenharmony_ci }, "Trusted DOMContentLoaded event"); 291cb0ef41Sopenharmony_ci }); 301cb0ef41Sopenharmony_ci}, {capture: true, once: true}); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciwindow.addEventListener("load", loadEvent => { 331cb0ef41Sopenharmony_ci let untrustedContentLoadedEvent; 341cb0ef41Sopenharmony_ci buttonElement.addEventListener("DOMContentLoaded", event => { 351cb0ef41Sopenharmony_ci untrustedContentLoadedEvent = event; 361cb0ef41Sopenharmony_ci test_contentLoaded_redispatching.step(() => { 371cb0ef41Sopenharmony_ci assert_false(untrustedContentLoadedEvent.isTrusted, "Redispatched DOMContentLoaded event shouldn't be trusted"); 381cb0ef41Sopenharmony_ci }); 391cb0ef41Sopenharmony_ci test_redispatching_of_dispatching_event.step(() => { 401cb0ef41Sopenharmony_ci assert_throws_dom("InvalidStateError", () => { 411cb0ef41Sopenharmony_ci document.dispatchEvent(untrustedContentLoadedEvent); 421cb0ef41Sopenharmony_ci }, "Untrusted DOMContentLoaded event"); 431cb0ef41Sopenharmony_ci }); 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci test_contentLoaded_redispatching.step(() => { 471cb0ef41Sopenharmony_ci assert_true(contentLoadedEvent.isTrusted, "Received DOMContentLoaded event should be trusted before redispatching"); 481cb0ef41Sopenharmony_ci buttonElement.dispatchEvent(contentLoadedEvent); 491cb0ef41Sopenharmony_ci assert_false(contentLoadedEvent.isTrusted, "Received DOMContentLoaded event shouldn't be trusted after redispatching"); 501cb0ef41Sopenharmony_ci assert_not_equals(untrustedContentLoadedEvent, undefined, "Untrusted DOMContentLoaded event should've been fired"); 511cb0ef41Sopenharmony_ci test_contentLoaded_redispatching.done(); 521cb0ef41Sopenharmony_ci }); 531cb0ef41Sopenharmony_ci}, {capture: true, once: true}); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciasync function testMouseUpAndClickEvent() { 561cb0ef41Sopenharmony_ci let mouseupEvent; 571cb0ef41Sopenharmony_ci buttonElement.addEventListener("mouseup", event => { 581cb0ef41Sopenharmony_ci mouseupEvent = event; 591cb0ef41Sopenharmony_ci test_mouseup_redispatching.step(() => { 601cb0ef41Sopenharmony_ci assert_true(mouseupEvent.isTrusted, "First mouseup event should be trusted"); 611cb0ef41Sopenharmony_ci }); 621cb0ef41Sopenharmony_ci test_redispatching_of_dispatching_event.step(() => { 631cb0ef41Sopenharmony_ci assert_throws_dom("InvalidStateError", () => { 641cb0ef41Sopenharmony_ci buttonElement.dispatchEvent(mouseupEvent); 651cb0ef41Sopenharmony_ci }, "Trusted mouseup event"); 661cb0ef41Sopenharmony_ci }); 671cb0ef41Sopenharmony_ci }, {once: true}); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci let clickEvent; 701cb0ef41Sopenharmony_ci buttonElement.addEventListener("click", event => { 711cb0ef41Sopenharmony_ci clickEvent = event; 721cb0ef41Sopenharmony_ci test_mouseup_redispatching.step(() => { 731cb0ef41Sopenharmony_ci assert_true(clickEvent.isTrusted, "First click event should be trusted"); 741cb0ef41Sopenharmony_ci }); 751cb0ef41Sopenharmony_ci test_redispatching_of_dispatching_event.step(() => { 761cb0ef41Sopenharmony_ci assert_throws_dom("InvalidStateError", function() { 771cb0ef41Sopenharmony_ci buttonElement.dispatchEvent(event); 781cb0ef41Sopenharmony_ci }, "Trusted click event"); 791cb0ef41Sopenharmony_ci }); 801cb0ef41Sopenharmony_ci buttonElement.addEventListener("mouseup", event => { 811cb0ef41Sopenharmony_ci test_mouseup_redispatching.step(() => { 821cb0ef41Sopenharmony_ci assert_false(event.isTrusted, "Redispatched mouseup event shouldn't be trusted"); 831cb0ef41Sopenharmony_ci }); 841cb0ef41Sopenharmony_ci test_redispatching_of_dispatching_event.step(() => { 851cb0ef41Sopenharmony_ci assert_throws_dom("InvalidStateError", function() { 861cb0ef41Sopenharmony_ci buttonElement.dispatchEvent(event); 871cb0ef41Sopenharmony_ci }, "Untrusted mouseup event"); 881cb0ef41Sopenharmony_ci }); 891cb0ef41Sopenharmony_ci }, {once: true}); 901cb0ef41Sopenharmony_ci function onClick() { 911cb0ef41Sopenharmony_ci test_mouseup_redispatching.step(() => { 921cb0ef41Sopenharmony_ci assert_true(false, "click event shouldn't be fired for dispatched mouseup event"); 931cb0ef41Sopenharmony_ci }); 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci test_mouseup_redispatching.step(() => { 961cb0ef41Sopenharmony_ci assert_true(mouseupEvent.isTrusted, "Received mouseup event should be trusted before redispatching from click event listener"); 971cb0ef41Sopenharmony_ci buttonElement.addEventListener("click", onClick); 981cb0ef41Sopenharmony_ci buttonElement.dispatchEvent(mouseupEvent); 991cb0ef41Sopenharmony_ci buttonElement.removeEventListener("click", onClick); 1001cb0ef41Sopenharmony_ci assert_false(mouseupEvent.isTrusted, "Received mouseup event shouldn't be trusted after redispatching"); 1011cb0ef41Sopenharmony_ci assert_true(clickEvent.isTrusted, "First click event should still be trusted even after redispatching mouseup event"); 1021cb0ef41Sopenharmony_ci }); 1031cb0ef41Sopenharmony_ci }, {once: true}); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci await waitForLoad; 1061cb0ef41Sopenharmony_ci let bounds = buttonElement.getBoundingClientRect(); 1071cb0ef41Sopenharmony_ci test(() => { assert_true(true); }, `Synthesizing click on button...`); 1081cb0ef41Sopenharmony_ci new test_driver.click(buttonElement) 1091cb0ef41Sopenharmony_ci .then(() => { 1101cb0ef41Sopenharmony_ci test_mouseup_redispatching.step(() => { 1111cb0ef41Sopenharmony_ci assert_not_equals(clickEvent, undefined, "mouseup and click events should've been fired"); 1121cb0ef41Sopenharmony_ci }); 1131cb0ef41Sopenharmony_ci test_mouseup_redispatching.done(); 1141cb0ef41Sopenharmony_ci test_redispatching_of_dispatching_event.done(); 1151cb0ef41Sopenharmony_ci }, (reason) => { 1161cb0ef41Sopenharmony_ci test_mouseup_redispatching.step(() => { 1171cb0ef41Sopenharmony_ci assert_true(false, `Failed to send mouse click due to ${reason}`); 1181cb0ef41Sopenharmony_ci }); 1191cb0ef41Sopenharmony_ci test_mouseup_redispatching.done(); 1201cb0ef41Sopenharmony_ci test_redispatching_of_dispatching_event.done(); 1211cb0ef41Sopenharmony_ci }); 1221cb0ef41Sopenharmony_ci} 1231cb0ef41Sopenharmony_citestMouseUpAndClickEvent(); 1241cb0ef41Sopenharmony_ci</script> 125