Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 1 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /html/semantics/the-button-element/interest-for/interestfor-event-timing.tentative.html - WPT Dashboard Interop Dashboard
<!DOCTYPE html>
<meta charset="utf-8" />
<link rel="author" href="mailto:masonf@chromium.org">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/invoker-utils.js"></script>
<button interestfor=popover id=invoker>Button</button>
<div popover id=popover>Target</div>
<button id=unrelated>Unrelated</div>
<style>
[interestfor] {
interest-delay: 0s;
}
</style>
<script>
promise_test(async (t) => {
const invoker = document.getElementById('invoker');
const popover = document.getElementById('popover');
const unrelated = document.getElementById('unrelated');
let interestFired = 0;
let loseInterestFired = 0;
let listenerErrors = [];
function assert_in_listener(cond, expectation, msg) {
if (cond != expectation) {
listenerErrors.push(msg);
}
}
popover.addEventListener('interest', (e) => {
++interestFired;
assert_in_listener(invoker.matches(':has-interest'),false,':has-interest should not yet match in the event handler');
assert_in_listener(popover.matches(':target-of-interest'),false,':target-of-interest should not yet match in the event handler');
assert_in_listener(popover.matches(':popover-open'),false,'popover shouldn\'t be open yet');
if (interestFired === 1) {
e.preventDefault();
}
});
popover.addEventListener('loseinterest', (e) => {
++loseInterestFired;
assert_in_listener(invoker.matches(':has-interest'),true,':has-interest should match in the event handler');
assert_in_listener(popover.matches(':target-of-interest'),true,':target-of-interest should match in the event handler');
assert_in_listener(popover.matches(':popover-open'),true,'popover should still be open');
if (loseInterestFired === 1) {
e.preventDefault();
}
});
// Hover once, and cancel the event:
await hoverOver(invoker);
assert_equals(interestFired, 1, 'The `interest` event should have fired once (and event canceled)');
await hoverOver(unrelated);
assert_equals(interestFired, 1, 'No further interest events');
assert_equals(loseInterestFired, 0, 'No loseinterest events yet (the `interest` event was canceled, so we can\'t "lose" interest we never gained)');
// Hover again, and don't cancel the event:
await hoverOver(invoker);
assert_equals(interestFired, 2, 'The `interest` event should have fired twice (event not canceled the second time)');
assert_equals(loseInterestFired, 0, 'Still no loseinterest events');
// De-hover, and cancel the event:
await hoverOver(unrelated);
assert_equals(interestFired, 2, 'No further interest events');
assert_equals(loseInterestFired, 1, 'The `loseinterest` event should have fired once (and event canceled)');
// De-hover again, and don't cancel the event:
await hoverOver(invoker); // Have to re-hover to get another interest event
assert_equals(interestFired, 2, 'No additional `interest` event because we cancelled loseinterest, so we still had interest');
await hoverOver(unrelated);
assert_equals(interestFired, 2, 'No further interest events');
assert_equals(loseInterestFired, 2, 'The `loseinterest` event should have fired once (event not canceled the second time)');
assert_false(popover.matches(':popover-open'),'popover should be closed at the end');
// Make sure none of the conditions within the event listeners failed.
assert_equals(listenerErrors.length,0,listenerErrors.join(', '));
},'Event and pseudo-class timing for interest and loseinterest');
</script>