11cb0ef41Sopenharmony_ci"use strict"; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_citest(() => { 41cb0ef41Sopenharmony_ci const target = new EventTarget(); 51cb0ef41Sopenharmony_ci const event = new Event("foo", { bubbles: true, cancelable: false }); 61cb0ef41Sopenharmony_ci let callCount = 0; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci function listener(e) { 91cb0ef41Sopenharmony_ci assert_equals(e, event); 101cb0ef41Sopenharmony_ci ++callCount; 111cb0ef41Sopenharmony_ci } 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci target.addEventListener("foo", listener); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci target.dispatchEvent(event); 161cb0ef41Sopenharmony_ci assert_equals(callCount, 1); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci target.dispatchEvent(event); 191cb0ef41Sopenharmony_ci assert_equals(callCount, 2); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci target.removeEventListener("foo", listener); 221cb0ef41Sopenharmony_ci target.dispatchEvent(event); 231cb0ef41Sopenharmony_ci assert_equals(callCount, 2); 241cb0ef41Sopenharmony_ci}, "A constructed EventTarget can be used as expected"); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_citest(() => { 271cb0ef41Sopenharmony_ci class NicerEventTarget extends EventTarget { 281cb0ef41Sopenharmony_ci on(...args) { 291cb0ef41Sopenharmony_ci this.addEventListener(...args); 301cb0ef41Sopenharmony_ci } 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci off(...args) { 331cb0ef41Sopenharmony_ci this.removeEventListener(...args); 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci dispatch(type, detail) { 371cb0ef41Sopenharmony_ci this.dispatchEvent(new CustomEvent(type, { detail })); 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci const target = new NicerEventTarget(); 421cb0ef41Sopenharmony_ci const event = new Event("foo", { bubbles: true, cancelable: false }); 431cb0ef41Sopenharmony_ci const detail = "some data"; 441cb0ef41Sopenharmony_ci let callCount = 0; 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci function listener(e) { 471cb0ef41Sopenharmony_ci assert_equals(e.detail, detail); 481cb0ef41Sopenharmony_ci ++callCount; 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci target.on("foo", listener); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci target.dispatch("foo", detail); 541cb0ef41Sopenharmony_ci assert_equals(callCount, 1); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci target.dispatch("foo", detail); 571cb0ef41Sopenharmony_ci assert_equals(callCount, 2); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci target.off("foo", listener); 601cb0ef41Sopenharmony_ci target.dispatch("foo", detail); 611cb0ef41Sopenharmony_ci assert_equals(callCount, 2); 621cb0ef41Sopenharmony_ci}, "EventTarget can be subclassed"); 63