-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1913402 [wpt PR 47636] - Add WPT for error events during event di…
…spatch when multiple globals exist, a=testonly Automatic update from web-platform-tests Add WPT for error events during event dispatch when multiple globals exist See also whatwg/dom#1303 Change-Id: Ie54cb8204e41d9e4ba1b972d297a8b43f45585ad Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5784046 Reviewed-by: Mason Freed <[email protected]> Commit-Queue: Jeremy Roman <[email protected]> Cr-Commit-Position: refs/heads/main@{#1342638} -- wpt-commits: 0a811c51619b14f78fec60ba7dd1603795ca6a21 wpt-pr: 47636
- Loading branch information
1 parent
a77cea1
commit 364f8e6
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
testing/web-platform/tests/dom/events/Event-dispatch-throwing-multiple-globals.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<!DOCTYPE html> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<body> | ||
<script> | ||
function createIframe(t, srcdoc = '') { | ||
let iframe = document.createElement('iframe'); | ||
iframe.srcdoc = srcdoc; | ||
t.add_cleanup(() => iframe.remove()); | ||
return new Promise((resolve, reject) => { | ||
iframe.addEventListener('load', () => resolve(iframe.contentWindow)); | ||
document.body.appendChild(iframe); | ||
}); | ||
} | ||
|
||
// Returns a promise which will resolve with the next error event fired at any | ||
// of `windows`, after the invocation of this function. Once one does, this | ||
// function removes its listeners and produces that error event so that it can | ||
// be examined (most notably for which global proxy it was targeted at). | ||
async function nextErrorEvent(windows) { | ||
let listener; | ||
let p = new Promise((resolve, reject) => { | ||
listener = (event) => { resolve(event); event.preventDefault(); }; | ||
}); | ||
for (let w of windows) { | ||
w.addEventListener('error', listener); | ||
} | ||
try { | ||
return await p; | ||
} finally { | ||
for (let w of windows) { | ||
w.removeEventListener('error', listener); | ||
} | ||
} | ||
} | ||
|
||
promise_test(async t => { | ||
let w = await createIframe(t, `<script>function listener() { throw new Error(); }<`+`/script>`); | ||
let w2 = await createIframe(t); | ||
|
||
let target = new w2.EventTarget(); | ||
target.addEventListener('party', w.listener); | ||
let nextErrorPromise = nextErrorEvent([self, w, w2]); | ||
target.dispatchEvent(new Event('party')); | ||
let errorEvent = await nextErrorPromise; | ||
if (errorEvent.error) { | ||
assert_true(errorEvent.error instanceof w.Error, 'error should be an instance created inside the listener function'); | ||
} | ||
assert_equals(errorEvent.target, w, `error event should target listener's global but instead targets ${event.currentTarget === w2 ? 'target\'s global' : 'test harness global'}`); | ||
}, 'exception thrown in event listener function should result in error event on listener\'s global'); | ||
|
||
promise_test(async t => { | ||
let w = await createIframe(t, `<script>listener = {};<`+`/script>`); | ||
let w2 = await createIframe(t, `<script>handleEvent = () => { throw new Error; };<`+`/script>`); | ||
let w3 = await createIframe(t); | ||
w.listener.handleEvent = w2.handleEvent; | ||
|
||
let target = new w3.EventTarget(); | ||
target.addEventListener('party', w.listener); | ||
let nextErrorPromise = nextErrorEvent([self, w, w2, w3]); | ||
target.dispatchEvent(new Event('party')); | ||
let errorEvent = await nextErrorPromise; | ||
if (errorEvent.error) { | ||
assert_true(errorEvent.error instanceof w2.Error, 'error should be an instance created inside the listener function'); | ||
} | ||
assert_equals(errorEvent.target, w, `error event should target listener's global but instead targets ${event.currentTarget === w2 ? 'target\'s global' : event.currentTarget === w3 ? 'function\'s global' : 'test harness global'}`); | ||
}, 'exception thrown in event listener interface object should result in error event on listener\'s global'); | ||
</script> | ||
</body> |