11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<title>window.event tests</title> 31cb0ef41Sopenharmony_ci<link rel="author" title="Mike Taylor" href="mailto:miketaylr@gmail.com"> 41cb0ef41Sopenharmony_ci<script src="/resources/testharness.js"></script> 51cb0ef41Sopenharmony_ci<script src="/resources/testharnessreport.js"></script> 61cb0ef41Sopenharmony_ci<div id=log></div> 71cb0ef41Sopenharmony_ci<script> 81cb0ef41Sopenharmony_cisetup({allow_uncaught_exception: true}); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_citest(t => { 111cb0ef41Sopenharmony_ci assert_own_property(window, "event"); 121cb0ef41Sopenharmony_ci assert_equals(window.event, undefined); 131cb0ef41Sopenharmony_ci}, "event exists on window, which is initially set to undefined"); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciasync_test(t => { 161cb0ef41Sopenharmony_ci let target = document.createElement("div"); 171cb0ef41Sopenharmony_ci assert_equals(window.event, undefined, "undefined before dispatch"); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci let clickEvent = new Event("click"); 201cb0ef41Sopenharmony_ci target.addEventListener("click", t.step_func_done(e => { 211cb0ef41Sopenharmony_ci assert_equals(window.event, clickEvent, "window.event set to current event during dispatch"); 221cb0ef41Sopenharmony_ci })); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci target.dispatchEvent(clickEvent); 251cb0ef41Sopenharmony_ci assert_equals(window.event, undefined, "undefined after dispatch"); 261cb0ef41Sopenharmony_ci}, "window.event is only defined during dispatch"); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciasync_test(t => { 291cb0ef41Sopenharmony_ci let parent = document.createElement("div"); 301cb0ef41Sopenharmony_ci let root = parent.attachShadow({mode: "closed"}); 311cb0ef41Sopenharmony_ci let span = document.createElement("span"); 321cb0ef41Sopenharmony_ci root.appendChild(span); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci span.addEventListener("test", t.step_func(e => { 351cb0ef41Sopenharmony_ci assert_equals(window.event, undefined); 361cb0ef41Sopenharmony_ci assert_not_equals(window.event, e); 371cb0ef41Sopenharmony_ci })); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci parent.addEventListener("test", t.step_func_done(e => { 401cb0ef41Sopenharmony_ci assert_equals(window.event, e); 411cb0ef41Sopenharmony_ci assert_not_equals(window.event, undefined); 421cb0ef41Sopenharmony_ci })); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci parent.dispatchEvent(new Event("test", {composed: true})); 451cb0ef41Sopenharmony_ci}, "window.event is undefined if the target is in a shadow tree (event dispatched outside shadow tree)"); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciasync_test(t => { 481cb0ef41Sopenharmony_ci let parent = document.createElement("div"); 491cb0ef41Sopenharmony_ci let root = parent.attachShadow({mode: "closed"}); 501cb0ef41Sopenharmony_ci let span = document.createElement("span"); 511cb0ef41Sopenharmony_ci root.appendChild(span); 521cb0ef41Sopenharmony_ci let shadowNode = root.firstElementChild; 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci shadowNode.addEventListener("test", t.step_func((e) => { 551cb0ef41Sopenharmony_ci assert_not_equals(window.event, e); 561cb0ef41Sopenharmony_ci assert_equals(window.event, undefined); 571cb0ef41Sopenharmony_ci })); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci parent.addEventListener("test", t.step_func_done(e => { 601cb0ef41Sopenharmony_ci assert_equals(window.event, e); 611cb0ef41Sopenharmony_ci assert_not_equals(window.event, undefined); 621cb0ef41Sopenharmony_ci })); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci shadowNode.dispatchEvent(new Event("test", {composed: true, bubbles: true})); 651cb0ef41Sopenharmony_ci}, "window.event is undefined if the target is in a shadow tree (event dispatched inside shadow tree)"); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ciasync_test(t => { 681cb0ef41Sopenharmony_ci let parent = document.createElement("div"); 691cb0ef41Sopenharmony_ci let root = parent.attachShadow({mode: "open"}); 701cb0ef41Sopenharmony_ci document.body.append(parent) 711cb0ef41Sopenharmony_ci let span = document.createElement("span"); 721cb0ef41Sopenharmony_ci root.append(span); 731cb0ef41Sopenharmony_ci let shadowNode = root.firstElementChild; 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci shadowNode.addEventListener("error", t.step_func(e => { 761cb0ef41Sopenharmony_ci assert_not_equals(window.event, e); 771cb0ef41Sopenharmony_ci assert_equals(window.event, undefined); 781cb0ef41Sopenharmony_ci })); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci let windowOnErrorCalled = false; 811cb0ef41Sopenharmony_ci window.onerror = t.step_func_done(() => { 821cb0ef41Sopenharmony_ci windowOnErrorCalled = true; 831cb0ef41Sopenharmony_ci assert_equals(typeof window.event, "object"); 841cb0ef41Sopenharmony_ci assert_equals(window.event.type, "error"); 851cb0ef41Sopenharmony_ci }); 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci shadowNode.dispatchEvent(new ErrorEvent("error", {composed: true, bubbles: true})); 881cb0ef41Sopenharmony_ci assert_true(windowOnErrorCalled); 891cb0ef41Sopenharmony_ci}, "window.event is undefined inside window.onerror if the target is in a shadow tree (ErrorEvent dispatched inside shadow tree)"); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ciasync_test(t => { 921cb0ef41Sopenharmony_ci let target1 = document.createElement("div"); 931cb0ef41Sopenharmony_ci let target2 = document.createElement("div"); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci target2.addEventListener("dude", t.step_func(() => { 961cb0ef41Sopenharmony_ci assert_equals(window.event.type, "dude"); 971cb0ef41Sopenharmony_ci })); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci target1.addEventListener("cool", t.step_func_done(() => { 1001cb0ef41Sopenharmony_ci assert_equals(window.event.type, "cool", "got expected event from global event during dispatch"); 1011cb0ef41Sopenharmony_ci target2.dispatchEvent(new Event("dude")); 1021cb0ef41Sopenharmony_ci assert_equals(window.event.type, "cool", "got expected event from global event after handling a different event handler callback"); 1031cb0ef41Sopenharmony_ci })); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci target1.dispatchEvent(new Event("cool")); 1061cb0ef41Sopenharmony_ci}, "window.event is set to the current event during dispatch"); 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ciasync_test(t => { 1091cb0ef41Sopenharmony_ci let target = document.createElement("div"); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci target.addEventListener("click", t.step_func_done(e => { 1121cb0ef41Sopenharmony_ci assert_equals(e, window.event); 1131cb0ef41Sopenharmony_ci })); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci target.dispatchEvent(new Event("click")); 1161cb0ef41Sopenharmony_ci}, "window.event is set to the current event, which is the event passed to dispatch"); 1171cb0ef41Sopenharmony_ci</script> 118