Skip to content

Truly independent web browser

LibWeb: Import popover autofocus and focus restoration tests

Andreas Klingd2818b30a16e
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus-3.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus-3.txt
new file mode 100644
index 00000000000..87cb5b5cad5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus-3.txt
@@ -0,0 +1,15 @@
+Harness status: OK
+
+Found 9 tests
+
+3 Pass
+6 Fail
+Fail Circular reference tab navigation with popovertarget invocation
+Fail Circular reference tab navigation with command/commandfor invocation
+Fail Circular reference tab navigation with imperative invocation
+Fail Popover focus returns when popover is hidden by invoker with popovertarget invocation
+Fail Popover focus returns when popover is hidden by invoker with commandfor invocation
+Fail Popover focus returns when popover is hidden by invoker with imperative invocation
+Pass Popover focus only returns to invoker when focus is within the popover with popovertarget invocation
+Pass Popover focus only returns to invoker when focus is within the popover with command/commandfor invocation
+Pass Popover focus only returns to invoker when focus is within the popover with imperative invocation
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
new file mode 100644
index 00000000000..29cd31353c2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Fail
+Fail Focusing elements inside a popover with form controls inside, using commandfor
+Fail Focusing elements inside a popover with a details element inside, using commandfor
+Fail Focusing elements inside a popover with a details element inside, and the after button adjacent to invoker, using commandfor
+Fail Focusing elements inside a popover with a custom-element with delegatesfocus inside, using commandfor
+Fail Focusing elements inside a popover with a custom-element with a slotted button inside, using commandfor
+Fail Focusing elements inside a popover with custom-element with a slotted button, followed by a details element, where the after button is adjacent to the invoker, using commandfor
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
new file mode 100644
index 00000000000..d8fb2ad0eea
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Fail
+Fail Focusing elements inside a popover with form controls inside, using commandforelement
+Fail Focusing elements inside a popover with a details element inside, using commandforelement
+Fail Focusing elements inside a popover with a details element inside, and the after button adjacent to invoker, using commandforelement
+Fail Focusing elements inside a popover with a custom-element with delegatesfocus inside, using commandforelement
+Fail Focusing elements inside a popover with a custom-element with a slotted button inside, using commandforelement
+Fail Focusing elements inside a popover with custom-element with a slotted button, followed by a details element, where the after button is adjacent to the invoker, using commandforelement
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
new file mode 100644
index 00000000000..f94bfb39a04
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Fail
+Fail Focusing elements inside a popover with form controls inside, using imperative
+Fail Focusing elements inside a popover with a details element inside, using imperative
+Fail Focusing elements inside a popover with a details element inside, and the after button adjacent to invoker, using imperative
+Fail Focusing elements inside a popover with a custom-element with delegatesfocus inside, using imperative
+Fail Focusing elements inside a popover with a custom-element with a slotted button inside, using imperative
+Fail Focusing elements inside a popover with custom-element with a slotted button, followed by a details element, where the after button is adjacent to the invoker, using imperative
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
new file mode 100644
index 00000000000..a74481ca6c8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Fail
+Fail Focusing elements inside a popover with form controls inside, using popovertargetelement
+Fail Focusing elements inside a popover with a details element inside, using popovertargetelement
+Fail Focusing elements inside a popover with a details element inside, and the after button adjacent to invoker, using popovertargetelement
+Fail Focusing elements inside a popover with a custom-element with delegatesfocus inside, using popovertargetelement
+Fail Focusing elements inside a popover with a custom-element with a slotted button inside, using popovertargetelement
+Fail Focusing elements inside a popover with custom-element with a slotted button, followed by a details element, where the after button is adjacent to the invoker, using popovertargetelement
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus.txt
new file mode 100644
index 00000000000..bdc24529f40
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus.txt
@@ -0,0 +1,36 @@
+Harness status: OK
+
+Found 30 tests
+
+1 Pass
+29 Fail
+Fail Popover focus test: default behavior - popover is not focused
+Fail Popover button click focus test: default behavior - popover is not focused
+Pass Popover corner cases test: default behavior - popover is not focused
+Fail Popover focus test: autofocus popover
+Fail Popover button click focus test: autofocus popover
+Fail Popover corner cases test: autofocus popover
+Fail Popover focus test: autofocus empty popover
+Fail Popover button click focus test: autofocus empty popover
+Fail Popover corner cases test: autofocus empty popover
+Fail Popover focus test: autofocus popover with button
+Fail Popover button click focus test: autofocus popover with button
+Fail Popover corner cases test: autofocus popover with button
+Fail Popover focus test: autofocus child
+Fail Popover button click focus test: autofocus child
+Fail Popover corner cases test: autofocus child
+Fail Popover focus test: autofocus on tabindex=0 element
+Fail Popover button click focus test: autofocus on tabindex=0 element
+Fail Popover corner cases test: autofocus on tabindex=0 element
+Fail Popover focus test: autofocus multiple children
+Fail Popover button click focus test: autofocus multiple children
+Fail Popover corner cases test: autofocus multiple children
+Fail Popover focus test: autofocus popover and multiple autofocus children
+Fail Popover button click focus test: autofocus popover and multiple autofocus children
+Fail Popover corner cases test: autofocus popover and multiple autofocus children
+Fail Popover focus test: Opening dialogs as popovers should use dialog initial focus algorithm.
+Fail Popover button click focus test: Opening dialogs as popovers should use dialog initial focus algorithm.
+Fail Popover corner cases test: Opening dialogs as popovers should use dialog initial focus algorithm.
+Fail Popover focus test: Opening dialogs as popovers which have autofocus should focus the dialog.
+Fail Popover button click focus test: Opening dialogs as popovers which have autofocus should focus the dialog.
+Fail Popover corner cases test: Opening dialogs as popovers which have autofocus should focus the dialog.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-shadowhost-focus.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-shadowhost-focus.txt
new file mode 100644
index 00000000000..57c692bf441
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-shadowhost-focus.txt
@@ -0,0 +1,10 @@
+Harness status: OK
+
+Found 4 tests
+
+2 Pass
+2 Fail
+Fail autofocus=true, delegatesfocus=false
+Fail autofocus=true, delegatesfocus=true
+Pass autofocus=false, delegatesfocus=false
+Pass autofocus=false, delegatesfocus=true
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-focus-3.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-focus-3.html
new file mode 100644
index 00000000000..f1d66e9889a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-focus-3.html
@@ -0,0 +1,219 @@
+<!DOCTYPE html>
+<meta charset="utf-8" />
+<title>Popover focus behaviors</title>
+<meta name="timeout" content="long">
+<link rel="author" title="Luke Warlow" href="mailto:[email protected]">
+<link rel=help href="https://open-ui.org/components/popover.research.explainer">
+<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/popover-utils.js"></script>
+
+<button id=circular0 tabindex="0">Invoker</button>
+<div id=popover4 popover>
+ <button id=circular1 autofocus tabindex="0"></button>
+ <button id=circular2 tabindex="0"></button>
+ <button id=circular3 tabindex="0"></button>
+</div>
+<button id=circular4 tabindex="0">after</button>
+<script>
+async function testCircularReferenceTabNavigation() {
+ circular0.focus();
+ await sendEnter(); // Activate the invoker
+ await verifyFocusOrder([circular0, circular1, circular2, circular3, circular4],'circular reference');
+ popover4.hidePopover();
+}
+promise_test(async t => {
+ circular0.setAttribute('popovertarget', 'popover4');
+ circular1.setAttribute('popovertarget', 'popover4');
+ circular1.setAttribute('popovertargetaction', 'hide');
+ circular2.setAttribute('popovertarget', 'popover4');
+ circular2.setAttribute('popovertargetaction', 'show');
+ circular3.setAttribute('popovertarget', 'popover4');
+ t.add_cleanup(() => {
+ circular0.removeAttribute('popovertarget');
+ circular1.removeAttribute('popovertarget');
+ circular1.removeAttribute('popovertargetaction');
+ circular2.removeAttribute('popovertarget');
+ circular2.removeAttribute('popovertargetaction');
+ circular3.removeAttribute('popovertarget');
+ });
+ await testCircularReferenceTabNavigation();
+}, "Circular reference tab navigation with popovertarget invocation");
+promise_test(async t => {
+ circular0.setAttribute('commandfor', 'popover4');
+ circular1.setAttribute('commandfor', 'popover4');
+ circular2.setAttribute('commandfor', 'popover4');
+ circular3.setAttribute('commandfor', 'popover4');
+ circular0.setAttribute('command', 'toggle-popover');
+ circular1.setAttribute('command', 'hide-popover');
+ circular2.setAttribute('command', 'show-popover');
+ circular3.setAttribute('command', 'toggle-popover');
+ t.add_cleanup(() => {
+ circular0.removeAttribute('commandfor');
+ circular1.removeAttribute('commandfor');
+ circular2.removeAttribute('commandfor');
+ circular3.removeAttribute('commandfor');
+ circular0.removeAttribute('command');
+ circular1.removeAttribute('command');
+ circular2.removeAttribute('command');
+ circular3.removeAttribute('command');
+ });
+ await testCircularReferenceTabNavigation();
+}, "Circular reference tab navigation with command/commandfor invocation");
+promise_test(async t => {
+ const circular0Click = () => {
+ popover4.togglePopover({ source: circular0 });
+ };
+ circular0.addEventListener('click', circular0Click);
+ const circular1Click = () => {
+ popover4.hidePopover();
+ };
+ circular1.addEventListener('click', circular1Click);
+ const circular2Click = () => {
+ popover4.showPopover({ source: circular2 });
+ };
+ circular2.addEventListener('click', circular2Click);
+ const circular3Click = () => {
+ popover4.togglePopover({ source: circular3 });
+ };
+ circular3.addEventListener('click', circular3Click);
+ t.add_cleanup(() => {
+ circular0.removeEventListener('click', circular0Click);
+ circular1.removeEventListener('click', circular1Click);
+ circular2.removeEventListener('click', circular2Click);
+ circular3.removeEventListener('click', circular3Click);
+ });
+ await testCircularReferenceTabNavigation();
+}, "Circular reference tab navigation with imperative invocation");
+</script>
+
+<div id=focus-return1>
+ <button tabindex="0">Show popover</button>
+ <div popover id=focus-return1-p>
+ <button autofocus tabindex="0">Hide popover</button>
+ </div>
+</div>
+<script>
+async function testPopoverFocusReturn1() {
+ const invoker = document.querySelector('#focus-return1>button');
+ const popover = document.querySelector('#focus-return1>[popover]');
+ const hideButton = popover.querySelector('button');
+ invoker.focus(); // Make sure button is focused.
+ assert_equals(document.activeElement,invoker);
+ await sendEnter(); // Activate the invoker
+ assert_true(popover.matches(':popover-open'), 'popover should be invoked by invoker');
+ assert_equals(document.activeElement,hideButton,'Hide button should be focused due to autofocus attribute');
+ await sendEnter(); // Activate the hide invoker
+ assert_false(popover.matches(':popover-open'), 'popover should be hidden by invoker');
+ assert_equals(document.activeElement,invoker,'Focus should be returned to the invoker');
+}
+promise_test(async t => {
+ const invoker = document.querySelector('#focus-return1>button');
+ const popover = document.querySelector('#focus-return1>[popover]');
+ const hideButton = popover.querySelector('button');
+ invoker.setAttribute('popovertarget', 'focus-return1-p');
+ invoker.setAttribute('popovertargetaction', 'show');
+ hideButton.setAttribute('popovertarget', 'focus-return1-p');
+ hideButton.setAttribute('popovertargetaction', 'hide');
+ t.add_cleanup(() => {
+ invoker.removeAttribute('popovertarget');
+ invoker.removeAttribute('popovertargetaction');
+ hideButton.removeAttribute('popovertarget');
+ hideButton.removeAttribute('popovertargetaction');
+ });
+ await testPopoverFocusReturn1();
+}, "Popover focus returns when popover is hidden by invoker with popovertarget invocation");
+promise_test(async t => {
+ const invoker = document.querySelector('#focus-return1>button');
+ const popover = document.querySelector('#focus-return1>[popover]');
+ const hideButton = popover.querySelector('button');
+ invoker.setAttribute('commandfor', 'focus-return1-p');
+ invoker.setAttribute('command', 'show-popover');
+ hideButton.setAttribute('commandfor', 'focus-return1-p');
+ hideButton.setAttribute('command', 'hide-popover');
+ t.add_cleanup(() => {
+ invoker.removeAttribute('commandfor');
+ invoker.removeAttribute('command');
+ hideButton.removeAttribute('commandfor');
+ hideButton.removeAttribute('command');
+ });
+ await testPopoverFocusReturn1();
+}, "Popover focus returns when popover is hidden by invoker with commandfor invocation");
+promise_test(async t => {
+ const invoker = document.querySelector('#focus-return1>button');
+ const popover = document.querySelector('#focus-return1>[popover]');
+ const hideButton = popover.querySelector('button');
+ const invokerClick = () => {
+ popover.showPopover({ source: invoker });
+ };
+ invoker.addEventListener('click', invokerClick);
+ const hideButtonClick = () => {
+ popover.hidePopover();
+ };
+ hideButton.addEventListener('click', hideButtonClick);
+ t.add_cleanup(() => {
+ invoker.removeEventListener('click', invokerClick);
+ hideButton.removeEventListener('click', hideButtonClick);
+ });
+ await testPopoverFocusReturn1();
+}, "Popover focus returns when popover is hidden by invoker with imperative invocation");
+</script>
+
+<div id=focus-return2>
+ <button tabindex="0">Toggle popover</button>
+ <div popover id=focus-return2-p>Popover with <button tabindex="0">focusable element</button></div>
+ <span tabindex=0>Other focusable element</span>
+</div>
+<script>
+async function testPopoverFocusReturn2() {
+ const invoker = document.querySelector('#focus-return2>button');
+ const popover = document.querySelector('#focus-return2>[popover]');
+ const otherElement = document.querySelector('#focus-return2>span');
+ invoker.focus(); // Make sure button is focused.
+ assert_equals(document.activeElement,invoker);
+ invoker.click(); // Activate the invoker
+ assert_true(popover.matches(':popover-open'), 'popover should be invoked by invoker');
+ assert_equals(document.activeElement,invoker,'invoker should still be focused');
+ await sendTab();
+ assert_equals(document.activeElement,popover.querySelector('button'),'next up is the popover');
+ await sendTab();
+ assert_equals(document.activeElement,otherElement,'next focus stop is outside the popover');
+ await sendEscape(); // Close the popover via ESC
+ assert_false(popover.matches(':popover-open'), 'popover should be hidden');
+ assert_equals(document.activeElement,otherElement,'focus does not move because it was not inside the popover');
+}
+promise_test(async t => {
+ const invoker = document.querySelector('#focus-return2>button');
+ invoker.setAttribute('popovertarget', 'focus-return2-p');
+ t.add_cleanup(() => {
+ invoker.removeAttribute('popovertarget');
+ });
+ await testPopoverFocusReturn2();
+}, "Popover focus only returns to invoker when focus is within the popover with popovertarget invocation");
+promise_test(async t => {
+ const invoker = document.querySelector('#focus-return2>button');
+ invoker.setAttribute('command', 'toggle-popover');
+ invoker.setAttribute('commandfor', 'focus-return2-p');
+ t.add_cleanup(() => {
+ invoker.removeAttribute('command');
+ invoker.removeAttribute('commandfor');
+ });
+ await testPopoverFocusReturn2();
+}, "Popover focus only returns to invoker when focus is within the popover with command/commandfor invocation");
+promise_test(async t => {
+ const invoker = document.querySelector('#focus-return2>button');
+ const popover = document.querySelector('#focus-return2>[popover]');
+ const invokerClick = () => {
+ popover.togglePopover({ source: invoker });
+ };
+ invoker.addEventListener('click', invokerClick);
+ t.add_cleanup(() => {
+ invoker.removeEventListener('click', invokerClick);
+ });
+ await testPopoverFocusReturn2();
+}, "Popover focus only returns to invoker when focus is within the popover with imperative invocation");
+</script>
+
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-focus-5.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-focus-5.html
new file mode 100644
index 00000000000..4ded87e6f2b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-focus-5.html
@@ -0,0 +1,279 @@
+<!doctype html>
+<meta charset="utf-8" />
+<title>Popover focus behaviors in slot elements</title>
+<meta name="timeout" content="long" />
+<link rel="author" title="Keith Cirkel" href="mailto:[email protected]" />
+<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/popover-utils.js"></script>
+
+<meta name="variant" content="?commandforelement" />
+<meta name="variant" content="?popovertargetelement" />
+<meta name="variant" content="?commandfor" />
+<meta name="variant" content="?imperative" />
+
+<div data-candidate="form controls inside" data-count="2">
+ <button tabindex="0" command="toggle-popover">Toggle popover</button>
+ <div popover id="popover1">
+ <input tabindex="0" type="text" data-expected="1" />
+ <input tabindex="0" type="text" data-expected="2" />
+ <button tabindex="0" command="hide-popover" data-expected="close">
+ Close popover
+ </button>
+ </div>
+</div>
+<button tabindex="0" data-expected="after">
+ This button is where focus should land after traversing this popover
+</button>
+
+<div data-candidate="a details element inside" data-count="1">
+ <button tabindex="0" command="toggle-popover">Toggle popover</button>
+ <div popover id="popover2">
+ <details>
+ <summary tabindex="0" data-expected="1">A details element</summary>
+ </details>
+ <button tabindex="0" command="hide-popover" data-expected="close">
+ Close popover
+ </button>
+ </div>
+</div>
+<button tabindex="0" data-expected="after">
+ This button is where focus should land after traversing this popover
+</button>
+
+<div
+ data-candidate="a details element inside, and the after button adjacent to invoker"
+ data-count="1"
+>
+ <button tabindex="0" command="toggle-popover">Toggle popover</button>
+ <button tabindex="0" data-expected="after">
+ This button is where focus should land after traversing this popover
+ </button>
+ <div popover id="popover3">
+ <details data-expected="1">A details element</details>
+ <button tabindex="0" command="hide-popover" data-expected="close">
+ Close popover
+ </button>
+ </div>
+</div>
+
+<div
+ data-candidate="a custom-element with delegatesfocus inside"
+ data-count="1"
+>
+ <button tabindex="0" command="toggle-popover">Toggle popover</button>
+ <div popover id="popover4">
+ <my-element data-expected="1">
+ <template shadowrootmode="open" shadowrootdelegatesfocus>
+ <button tabindex="0" data-expected="shadow-1"></button>
+ </template>
+ </my-element>
+ <button tabindex="0" command="hide-popover" data-expected="close">
+ Close popover
+ </button>
+ </div>
+</div>
+<button tabindex="0" data-expected="after">
+ This button is where focus should land after traversing popover
+</button>
+
+<div
+ data-candidate="a custom-element with a slotted button inside"
+ data-count="1"
+>
+ <button tabindex="0" command="toggle-popover">Toggle popover</button>
+ <div popover id="popover5">
+ <my-element>
+ <template shadowrootmode="open">
+ <slot></slot>
+ </template>
+ <button tabindex="0" data-expected="1"></button>
+ </my-element>
+ <button tabindex="0" command="hide-popover" data-expected="close">
+ Close popover
+ </button>
+ </div>
+</div>
+<button tabindex="0" data-expected="after">
+ This button is where focus should land after traversing popover
+</button>
+
+<div
+ data-candidate="custom-element with a slotted button, followed by a details element, where the after button is adjacent to the invoker"
+ data-count="2"
+>
+ <button tabindex="0" command="toggle-popover">Toggle popover</button>
+ <button tabindex="0" data-expected="after">
+ This button is where focus should land after traversing popover
+ </button>
+ <div popover id="popover6">
+ <my-element data-expected="2">
+ <template shadowrootmode="open">
+ <slot></slot>
+ <details>
+ <summary tabindex="0" data-expected="shadow-2">A details element</summary>
+ </details>
+ </template>
+ <button tabindex="0" data-expected="1"></button>
+ </my-element>
+ <button tabindex="0" command="hide-popover" data-expected="close">
+ Close popover
+ </button>
+ </div>
+</div>
+
+<script>
+ // The active element might be an element within the expected focus candidate,e.g. a summary
+ // inside a details, we should traverse the tree to find the nearest `[data-expected]` element
+ // to ensure we're not failing a test for chosing to focus an interior element.
+ function getExpectedValue() {
+ return document.activeElement?.closest('[data-expected]')?.getAttribute("data-expected")
+ }
+
+ async function testCandidate(el, style, signal) {
+ const count = parseInt(el.getAttribute("data-count"));
+ const invoker = el.querySelector("button:first-child");
+ const popover = el.querySelector("[popover]");
+ const popoverClose = el.querySelector('[data-expected="close"]');
+ assert_greater_than_equal(count, 0, "test candidate had an invalid count");
+ assert_not_equals(
+ invoker,
+ null,
+ "could not find invoker in test candidate",
+ );
+ assert_not_equals(
+ popover,
+ null,
+ "could not find popover in test candidate",
+ );
+ assert_not_equals(
+ popoverClose,
+ null,
+ "could not find popover close button in test candidate",
+ );
+ switch (style) {
+ case "popovertarget":
+ invoker.setAttribute("popovertarget", popover.id);
+ popoverClose.setAttribute("popovertarget", popover.id);
+ break;
+ case "popovertargetelement":
+ invoker.popoverTargetElement = popover;
+ popoverClose.popoverTargetElement = popover;
+ break;
+ case "commandfor":
+ invoker.setAttribute("commandfor", popover.id);
+ popoverClose.setAttribute("commandfor", popover.id);
+ break;
+ case "commandforelement":
+ invoker.commandForElement = popover;
+ popoverClose.commandForElement = popover;
+ break;
+ case "imperative":
+ invoker.addEventListener(
+ "click",
+ () => {
+ popover.togglePopover({ source: invoker });
+ },
+ { signal },
+ );
+ popoverClose.addEventListener(
+ "click",
+ () => {
+ popover.hidePopover({ source: invoker });
+ },
+ { signal },
+ );
+ break;
+ default:
+ assert_unreached();
+ }
+ invoker.focus();
+ assert_equals(document.activeElement, invoker);
+ invoker.click();
+ assert_true(
+ popover.matches(":popover-open"),
+ "popover should be invoked by invoker",
+ );
+ assert_equals(
+ document.activeElement,
+ invoker,
+ "invoker should still be focused",
+ );
+ for (let i = 1; i <= count; i += 1) {
+ await sendTab();
+ assert_equals(
+ getExpectedValue(),
+ String(i),
+ `tab press should move to active element ${i}`,
+ );
+ const shadowActive = document.activeElement?.shadowRoot?.activeElement;
+ if (shadowActive) {
+ const expected = `shadow-${i}`;
+ assert_equals(
+ shadowActive.getAttribute("data-expected"),
+ expected,
+ `tab press should move to active element ${expected} in shadow dom`,
+ );
+ }
+ }
+ await sendTab();
+ assert_equals(
+ getExpectedValue(),
+ "close",
+ "tab press should move to close popover button",
+ );
+ await sendShiftTab();
+ assert_equals(
+ getExpectedValue(),
+ String(count),
+ "shift+tab should move back to last active element",
+ );
+ await sendTab();
+ assert_equals(
+ document.activeElement,
+ popoverClose,
+ "tab should move forward to re-land on close popover again",
+ );
+ await sendTab();
+ assert_equals(
+ getExpectedValue(),
+ "after",
+ "tab should move forward to land on the button after the popover contents",
+ );
+ await sendShiftTab();
+ popoverClose.click();
+ assert_equals(
+ document.activeElement,
+ invoker,
+ "popover now closed - focus should have returned to initial invoker",
+ );
+ }
+
+ const style = window.location.search.substring(1) || "popovertarget";
+ for (const candidate of document.querySelectorAll("[data-candidate]")) {
+ promise_test(
+ async (t) => {
+ const controller = new AbortController();
+ t.add_cleanup(() => {
+ controller.abort();
+ for (const el of document.querySelectorAll(
+ ":is([popovertarget],[commandfor],[disabled],[tabindex])",
+ )) {
+ el.removeAttribute("popovertarget");
+ el.removeAttribute("commandfor");
+ el.removeAttribute("disabled");
+ el.removeAttribute("tabindex");
+ }
+ for (const el of document.querySelectorAll(":popover-open")) {
+ el.hidePopover();
+ }
+ });
+ await testCandidate(candidate, style);
+ },
+ `Focusing elements inside a popover with ${candidate.getAttribute("data-candidate")}, using ${style}`,
+ );
+ }
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-focus.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-focus.html
new file mode 100644
index 00000000000..9a710581e60
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-focus.html
@@ -0,0 +1,292 @@
+<!DOCTYPE html>
+<meta charset="utf-8" />
+<title>Popover focus behaviors</title>
+<link rel="author" href="mailto:[email protected]">
+<link rel=help href="https://open-ui.org/components/popover.research.explainer">
+<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/popover-utils.js"></script>
+
+<div popover data-test='default behavior - popover is not focused' data-no-focus>
+ <p>This is a popover</p>
+ <button tabindex="0">first button</button>
+</div>
+
+<div popover data-test='autofocus popover' autofocus tabindex=-1 class=should-be-focused>
+ <p>This is a popover</p>
+</div>
+
+<div popover data-test='autofocus empty popover' autofocus tabindex=-1 class=should-be-focused></div>
+
+<div popover data-test='autofocus popover with button' autofocus tabindex=-1 class=should-be-focused>
+ <p>This is a popover</p>
+ <button tabindex="0">button</button>
+</div>
+
+<div popover data-test='autofocus child'>
+ <p>This is a popover</p>
+ <button autofocus class=should-be-focused tabindex="0">autofocus button</button>
+</div>
+
+<div popover data-test='autofocus on tabindex=0 element'>
+ <p autofocus tabindex=0 class=should-be-focused>This is a popover with autofocus on a tabindex=0 element</p>
+ <button tabindex="0">button</button>
+</div>
+
+<div popover data-test='autofocus multiple children'>
+ <p>This is a popover</p>
+ <button autofocus class=should-be-focused tabindex="0">autofocus button</button>
+ <button autofocus tabindex="0">second autofocus button</button>
+</div>
+
+<div popover autofocus tabindex=-1 data-test='autofocus popover and multiple autofocus children' class=should-be-focused>
+ <p>This is a popover</p>
+ <button autofocus tabindex="0">autofocus button</button>
+ <button autofocus tabindex="0">second autofocus button</button>
+</div>
+
+<dialog popover=auto data-test='Opening dialogs as popovers should use dialog initial focus algorithm.'>
+ <button class=should-be-focused tabindex="0">button</button>
+</dialog>
+
+<dialog popover=auto autofocus class=should-be-focused data-test='Opening dialogs as popovers which have autofocus should focus the dialog.'>
+ <button tabindex="0">button</button>
+</dialog>
+
+<style>
+ [popover] {
+ border: 2px solid black;
+ top:150px;
+ left:150px;
+ }
+ :focus-within { border: 5px dashed red; }
+ :focus { border: 5px solid lime; }
+</style>
+
+<script>
+ function addInvoker(t, popover) {
+ const button = document.createElement('button');
+ button.innerText = 'Click me';
+ const popoverId = 'popover-id';
+ assert_equals(document.querySelectorAll('#' + popoverId).length, 0);
+ document.body.appendChild(button);
+ t.add_cleanup(function() {
+ popover.removeAttribute('id');
+ button.remove();
+ });
+ popover.id = popoverId;
+ button.setAttribute('tabindex', '0');
+ button.setAttribute('popovertarget', popoverId);
+ return button;
+ }
+ function addPriorFocus(t) {
+ const priorFocus = document.createElement('button');
+ priorFocus.setAttribute("tabindex", "0");
+ priorFocus.id = 'priorFocus';
+ document.body.appendChild(priorFocus);
+ t.add_cleanup(() => priorFocus.remove());
+ return priorFocus;
+ }
+ function activateAndVerify(popover) {
+ const testName = popover.getAttribute('data-test');
+ promise_test(async t => {
+ const priorFocus = addPriorFocus(t);
+ let expectedFocusedElement = popover.matches('.should-be-focused') ? popover : popover.querySelector('.should-be-focused');
+ const changesFocus = !popover.hasAttribute('data-no-focus');
+ if (!changesFocus) {
+ expectedFocusedElement = priorFocus;
+ }
+ assert_true(!!expectedFocusedElement);
+ assert_false(popover.matches(':popover-open'));
+
+ // Directly show and hide the popover:
+ priorFocus.focus();
+ assert_equals(document.activeElement, priorFocus);
+ popover.showPopover();
+ assert_equals(document.activeElement, expectedFocusedElement, `${testName} activated by popover.showPopover()`);
+ popover.hidePopover();
+ assert_equals(document.activeElement, priorFocus, 'prior element should get focus on hide, or if focus didn\'t shift on show, focus should stay where it was');
+ assert_false(isElementVisible(popover));
+
+ // Manual popover does not restore focus
+ popover.popover = 'manual';
+ priorFocus.focus();
+ assert_equals(document.activeElement, priorFocus);
+ popover.showPopover();
+ assert_equals(document.activeElement, expectedFocusedElement, `${testName} activated by popover.showPopover()`);
+ popover.hidePopover();
+ if (!popover.hasAttribute('data-no-focus')) {
+ assert_not_equals(document.activeElement, priorFocus, 'prior element should *not* get focus when the popover is manual');
+ }
+ assert_false(isElementVisible(popover));
+ popover.popover = 'auto';
+
+ // Hit Escape:
+ priorFocus.focus();
+ assert_equals(document.activeElement, priorFocus);
+ popover.showPopover();
+ assert_equals(document.activeElement, expectedFocusedElement, `${testName} activated by popover.showPopover()`);
+ await sendEscape();
+ assert_equals(document.activeElement, priorFocus, 'prior element should get focus after Escape');
+ assert_false(isElementVisible(popover));
+
+ // Move focus into the popover, then hit Escape:
+ let containedButton = popover.querySelector('button');
+ if (containedButton) {
+ priorFocus.focus();
+ assert_equals(document.activeElement, priorFocus);
+ popover.showPopover();
+ containedButton.focus();
+ assert_equals(document.activeElement, containedButton);
+ await sendEscape();
+ assert_equals(document.activeElement, priorFocus, 'prior element should get focus after Escape');
+ assert_false(isElementVisible(popover));
+ }
+
+ // Change the popover type:
+ priorFocus.focus();
+ popover.showPopover();
+ assert_equals(document.activeElement, expectedFocusedElement, `${testName} activated by popover.showPopover()`);
+ assert_equals(popover.popover, 'auto', 'All popovers in this test should start as popover=auto');
+ popover.popover = 'manual';
+ assert_false(popover.matches(':popover-open'), 'Changing the popover type should hide the popover');
+ assert_equals(document.activeElement, priorFocus, 'prior element should get focus when the type is changed');
+ assert_false(isElementVisible(popover));
+ popover.popover = 'auto';
+
+ // Remove from the document:
+ priorFocus.focus();
+ popover.showPopover();
+ assert_equals(document.activeElement, expectedFocusedElement, `${testName} activated by popover.showPopover()`);
+ popover.remove();
+ assert_false(isElementVisible(popover), 'Removing the popover should hide it immediately');
+ if (!popover.hasAttribute('data-no-focus')) {
+ assert_not_equals(document.activeElement, priorFocus, 'prior element should *not* get focus when the popover is removed from the document');
+ }
+ document.body.appendChild(popover);
+
+ // Show a modal dialog:
+ priorFocus.focus();
+ popover.showPopover();
+ assert_equals(document.activeElement, expectedFocusedElement, `${testName} activated by popover.showPopover()`);
+ const dialog = document.body.appendChild(document.createElement('dialog'));
+ dialog.showModal();
+ assert_false(popover.matches(':popover-open'), 'Opening a modal dialog should hide the popover');
+ assert_not_equals(document.activeElement, priorFocus, 'prior element should *not* get focus when a modal dialog is shown');
+ assert_false(isElementVisible(popover));
+ dialog.close();
+ dialog.remove();
+
+ // Use an activating element:
+ const button = addInvoker(t, popover);
+ priorFocus.focus();
+ button.click();
+ assert_true(popover.matches(':popover-open'));
+ assert_equals(document.activeElement, expectedFocusedElement, `${testName} activated by button.click()`);
+
+ // Make sure Escape works in the invoker case:
+ await sendEscape();
+ assert_equals(document.activeElement, priorFocus, 'prior element should get focus after Escape (via invoker)');
+ assert_false(isElementVisible(popover));
+
+ // Make sure we can directly focus the (already open) popover:
+ priorFocus.focus();
+ button.click();
+ assert_true(popover.matches(':popover-open'));
+ assert_equals(document.activeElement, expectedFocusedElement, `${testName} activated by button.click()`);
+ popover.focus();
+ assert_equals(document.activeElement, popover.hasAttribute('tabindex') || popover.tagName === 'DIALOG' ? popover : expectedFocusedElement, `${testName} directly focus with popover.focus()`);
+ button.click(); // Button is set to toggle the popover
+ assert_false(popover.matches(':popover-open'));
+ assert_equals(document.activeElement, priorFocus, 'prior element should get focus on button-toggled hide');
+ assert_false(isElementVisible(popover));
+ }, "Popover focus test: " + testName);
+
+ promise_test(async t => {
+ const priorFocus = addPriorFocus(t);
+ assert_false(popover.matches(':popover-open'), 'popover should start out hidden');
+ let button = addInvoker(t, popover);
+ assert_equals(button.getAttribute('popovertarget'), popover.id, 'This test assumes the button uses `popovertarget`.');
+ assert_not_equals(button, priorFocus, 'Stranger things have happened');
+ assert_false(popover.contains(button), 'Start with a non-contained button');
+ priorFocus.focus();
+ assert_equals(document.activeElement, priorFocus);
+ popover.showPopover();
+ assert_true(popover.matches(':popover-open'));
+ await clickOn(button); // This will *not* light dismiss, but will "toggle" the popover.
+ assert_false(popover.matches(':popover-open'));
+ assert_equals(document.activeElement, button, 'focus should move to the button when clicked, and should stay there when the popover closes');
+ assert_false(isElementVisible(popover));
+
+ // Same thing, but the button is contained within the popover
+ button.setAttribute('popovertarget', popover.id);
+ button.setAttribute('popovertargetaction', 'hide');
+ popover.appendChild(button);
+ t.add_cleanup(() => button.remove());
+ priorFocus.focus();
+ popover.showPopover();
+ assert_true(popover.matches(':popover-open'));
+ const changesFocus = !popover.hasAttribute('data-no-focus');
+ if (changesFocus) {
+ assert_not_equals(document.activeElement, priorFocus, 'focus should shift for this element');
+ }
+ await clickOn(button);
+ assert_false(popover.matches(':popover-open'), 'clicking button should hide the popover');
+ assert_equals(document.activeElement, priorFocus, 'Contained button should return focus to the previously focused element');
+ assert_false(isElementVisible(popover));
+
+ // Same thing, but the button is unrelated (no popovertarget)
+ button = document.createElement('button');
+ button.setAttribute("tabindex", "0");
+ document.body.appendChild(button);
+ priorFocus.focus();
+ popover.showPopover();
+ assert_true(popover.matches(':popover-open'));
+ await clickOn(button); // This will light dismiss the popover, focus the prior focus, then focus this button.
+ assert_false(popover.matches(':popover-open'), 'clicking button should hide the popover (via light dismiss)');
+ assert_equals(document.activeElement, button, 'Focus should go to unrelated button on light dismiss');
+ assert_false(isElementVisible(popover));
+ }, "Popover button click focus test: " + testName);
+
+ promise_test(async t => {
+ if (popover.hasAttribute('data-no-focus')) {
+ // This test only applies if the popover changes focus
+ return;
+ }
+ const priorFocus = addPriorFocus(t);
+ assert_false(popover.matches(':popover-open'), 'popover should start out hidden');
+
+ // Move the prior focus out of the document
+ priorFocus.focus();
+ popover.showPopover();
+ assert_true(popover.matches(':popover-open'));
+ const newFocus = document.activeElement;
+ assert_not_equals(newFocus, priorFocus, 'focus should shift for this element');
+ priorFocus.remove();
+ assert_equals(document.activeElement, newFocus, 'focus should not change when prior focus is removed');
+ popover.hidePopover();
+ assert_not_equals(document.activeElement, priorFocus, 'focused element has been removed');
+ assert_false(isElementVisible(popover));
+ document.body.appendChild(priorFocus); // Put it back
+
+ // Move the prior focus inside the (already open) popover
+ priorFocus.focus();
+ popover.showPopover();
+ assert_true(popover.matches(':popover-open'));
+ assert_false(popover.contains(priorFocus), 'Start with a non-contained prior focus');
+ popover.appendChild(priorFocus); // Move inside the popover
+ assert_true(popover.contains(priorFocus));
+ assert_true(popover.matches(':popover-open'), 'popover should stay open');
+ popover.hidePopover();
+ assert_false(isElementVisible(popover));
+ assert_not_equals(document.activeElement, priorFocus, 'focused element is display:none inside the popover');
+ document.body.appendChild(priorFocus); // Put it back
+ }, "Popover corner cases test: " + testName);
+ }
+
+ document.querySelectorAll('body > [popover]').forEach(popover => activateAndVerify(popover));
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-shadowhost-focus.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-shadowhost-focus.html
new file mode 100644
index 00000000000..33fe83c70ba
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/popover-shadowhost-focus.html
@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<link rel=author href="mailto:[email protected]">
+<link rel=help href="https://github.com/whatwg/html/issues/8994">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<div popover=auto tabindex=0 data-test="autofocus=true, delegatesfocus=false" autofocus class=should-be-focused>
+ <template shadowrootmode=open>
+ <button autofocus>autofocus button</button>
+ </template>
+</div>
+
+<!-- The autofocus popover is what focus() gets called on, but since it has a
+ delegatesFocus shadowroot, focus() itself goes into the shadowroot. -->
+<div popover=auto tabindex=0 data-test="autofocus=true, delegatesfocus=true" autofocus>
+ <template shadowrootmode=open shadowrootdelegatesfocus>
+ <button autofocus class=should-be-focused>autofocus button</button>
+ </template>
+</div>
+
+<div popover=auto tabindex=0 data-test="autofocus=false, delegatesfocus=false">
+ <template shadowrootmode=open>
+ <button autofocus>autofocus button</button>
+ </template>
+</div>
+
+<div popover=auto tabindex=0 data-test="autofocus=false, delegatesfocus=true">
+ <template shadowrootmode=open shadowrootdelegatesfocus>
+ <button autofocus>autofocus button</button>
+ </template>
+</div>
+
+<script>
+document.querySelectorAll('body > [popover]').forEach(popover => {
+ promise_test(async () => {
+ const expectedFocusedElement = (popover.matches('.should-be-focused') ? popover : null)
+ || popover.querySelector('.should-be-focused')
+ || popover.shadowRoot.querySelector('.should-be-focused')
+ || document.body;
+
+ popover.showPopover();
+
+ let actualFocusedElement = document.activeElement;
+ if (actualFocusedElement.shadowRoot && actualFocusedElement.shadowRoot.activeElement) {
+ actualFocusedElement = actualFocusedElement.shadowRoot.activeElement;
+ }
+
+ popover.hidePopover();
+
+ // Resetting focus may happen asynchronously
+ await new Promise(resolve => requestAnimationFrame(resolve));
+
+ assert_equals(actualFocusedElement, expectedFocusedElement);
+ }, popover.getAttribute('data-test'));
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/resources/popover-utils.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/resources/popover-utils.js
new file mode 100644
index 00000000000..dec91ed7d10
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/popovers/resources/popover-utils.js
@@ -0,0 +1,174 @@
+function waitForRender() {
+ return new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+}
+
+async function clickOn(element, useTouch) {
+ await waitForRender();
+ let rect = element.getBoundingClientRect();
+ let actions = new test_driver.Actions();
+ // FIXME: Switch to pointerMove(0, 0, {origin: element}) once
+ // https://github.com/web-platform-tests/wpt/issues/41257 is fixed.
+ if (useTouch) {
+ actions.addPointer('touch1', 'touch');
+ }
+ await actions
+ .pointerMove(Math.round(rect.x + rect.width / 2), Math.round(rect.y + rect.height / 2), {})
+ .pointerDown({button: actions.ButtonType.LEFT})
+ .pointerUp({button: actions.ButtonType.LEFT})
+ .send();
+ await waitForRender();
+}
+async function sendTab() {
+ await waitForRender();
+ const kTab = '\uE004';
+ await test_driver.send_keys(document.activeElement || document.documentElement, kTab);
+ await waitForRender();
+}
+async function sendShiftTab() {
+ await waitForRender();
+ const kShift = '\uE008';
+ const kTab = '\uE004';
+ await new test_driver.Actions()
+ .keyDown(kShift)
+ .keyDown(kTab)
+ .keyUp(kTab)
+ .keyUp(kShift)
+ .send();
+ await waitForRender();
+}
+async function sendEscape() {
+ await waitForRender();
+ await test_driver.send_keys(document.activeElement || document.documentElement,'\uE00C'); // Escape
+ await waitForRender();
+}
+async function sendEnter() {
+ await waitForRender();
+ await test_driver.send_keys(document.activeElement || document.documentElement,'\uE007'); // Enter
+ await waitForRender();
+}
+function isElementVisible(el) {
+ return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
+}
+async function finishAnimations(popover) {
+ popover.getAnimations({subtree: true}).forEach(animation => animation.finish());
+ await waitForRender();
+}
+
+// This is a "polyfill" of sorts for the `defaultopen` attribute.
+// It can be called before window.load is complete, and it will
+// show defaultopen popovers according to the rules previously part
+// of the popover API: any popover=manual popover can be shown this
+// way, and only the first popover=auto popover.
+function showDefaultopenPopoversOnLoad() {
+ function show() {
+ const popovers = Array.from(document.querySelectorAll('[popover][defaultopen]'));
+ popovers.forEach((p) => {
+ // The showPopover calls below aren't guarded by a check on the popover
+ // open/closed status. If they throw exceptions, this function was
+ // probably called at a bad time. However, a check is made for open
+ // <dialog open> elements.
+ if (p instanceof HTMLDialogElement && p.hasAttribute('open'))
+ return;
+ switch (p.popover) {
+ case 'auto':
+ if (!document.querySelector('[popover]:popover-open'))
+ p.showPopover();
+ return;
+ case 'manual':
+ p.showPopover();
+ return;
+ default:
+ assert_unreached(`Unknown popover type ${p.popover}`);
+ }
+ });
+ }
+ if (document.readyState === 'complete') {
+ show();
+ } else {
+ window.addEventListener('load',show,{once:true});
+ }
+}
+
+function assertPopoverVisibility(popover, isPopover, expectedVisibility, message) {
+ const isVisible = isElementVisible(popover);
+ assert_equals(isVisible, expectedVisibility,`${message}: Expected this element to be ${expectedVisibility ? "visible" : "not visible"}`);
+ // Check other things related to being visible or not:
+ if (isVisible) {
+ assert_not_equals(window.getComputedStyle(popover).display,'none');
+ assert_equals(popover.matches(':popover-open'),isPopover,`${message}: Visible popovers should match :popover-open`);
+ } else {
+ assert_equals(window.getComputedStyle(popover).display,'none',`${message}: Non-showing popovers should have display:none`);
+ assert_false(popover.matches(':popover-open'),`${message}: Non-showing popovers should *not* match :popover-open`);
+ }
+}
+
+function assertIsFunctionalPopover(popover, checkVisibility) {
+ assertPopoverVisibility(popover, /*isPopover*/true, /*expectedVisibility*/false, 'A popover should start out hidden');
+ popover.showPopover();
+ if (checkVisibility) assertPopoverVisibility(popover, /*isPopover*/true, /*expectedVisibility*/true, 'After showPopover(), a popover should be visible');
+ popover.showPopover(); // Calling showPopover on a showing popover should not throw.
+ popover.hidePopover();
+ if (checkVisibility) assertPopoverVisibility(popover, /*isPopover*/true, /*expectedVisibility*/false, 'After hidePopover(), a popover should be hidden');
+ popover.hidePopover(); // Calling hidePopover on a hidden popover should not throw.
+ popover.togglePopover();
+ if (checkVisibility) assertPopoverVisibility(popover, /*isPopover*/true, /*expectedVisibility*/true, 'After togglePopover() on hidden popover, it should be visible');
+ popover.togglePopover();
+ if (checkVisibility) assertPopoverVisibility(popover, /*isPopover*/true, /*expectedVisibility*/false, 'After togglePopover() on visible popover, it should be hidden');
+ popover.togglePopover(/*force=*/true);
+ if (checkVisibility) assertPopoverVisibility(popover, /*isPopover*/true, /*expectedVisibility*/true, 'After togglePopover(true) on hidden popover, it should be visible');
+ popover.togglePopover(/*force=*/true);
+ if (checkVisibility) assertPopoverVisibility(popover, /*isPopover*/true, /*expectedVisibility*/true, 'After togglePopover(true) on visible popover, it should be visible');
+ popover.togglePopover(/*force=*/false);
+ if (checkVisibility) assertPopoverVisibility(popover, /*isPopover*/true, /*expectedVisibility*/false, 'After togglePopover(false) on visible popover, it should be hidden');
+ popover.togglePopover(/*force=*/false);
+ if (checkVisibility) assertPopoverVisibility(popover, /*isPopover*/true, /*expectedVisibility*/false, 'After togglePopover(false) on hidden popover, it should be hidden');
+ const parent = popover.parentElement;
+ popover.remove();
+ assert_throws_dom("InvalidStateError",() => popover.showPopover(),'Calling showPopover on a disconnected popover should throw InvalidStateError');
+ popover.hidePopover(); // Calling hidePopover on a disconnected popover should not throw.
+ assert_throws_dom("InvalidStateError",() => popover.togglePopover(),'Calling hidePopover on a disconnected popover should throw InvalidStateError');
+ parent.appendChild(popover);
+}
+
+function assertNotAPopover(nonPopover) {
+ // If the non-popover element nonetheless has a 'popover' attribute, it should
+ // be invisible. Otherwise, it should be visible.
+ const expectVisible = !nonPopover.hasAttribute('popover');
+ assertPopoverVisibility(nonPopover, /*isPopover*/false, expectVisible, 'A non-popover should start out visible');
+ assert_throws_dom("NotSupportedError",() => nonPopover.showPopover(),'Calling showPopover on a non-popover should throw NotSupported');
+ assertPopoverVisibility(nonPopover, /*isPopover*/false, expectVisible, 'Calling showPopover on a non-popover should leave it visible');
+ assert_throws_dom("NotSupportedError",() => nonPopover.hidePopover(),'Calling hidePopover on a non-popover should throw NotSupported');
+ assertPopoverVisibility(nonPopover, /*isPopover*/false, expectVisible, 'Calling hidePopover on a non-popover should leave it visible');
+ assert_throws_dom("NotSupportedError",() => nonPopover.togglePopover(),'Calling togglePopover on a non-popover should throw NotSupported');
+ assertPopoverVisibility(nonPopover, /*isPopover*/false, expectVisible, 'Calling togglePopover on a non-popover should leave it visible');
+}
+
+function shadowIncludingActiveElement() {
+ let element = document.activeElement;
+ while (element.shadowRoot?.activeElement) {
+ element = element.shadowRoot.activeElement;
+ }
+ return element;
+}
+
+async function verifyFocusOrder(order,description) {
+ order[0].focus();
+ for(let i=0;i<order.length;++i) {
+ // Press tab between each check, excluding first (because it should already be focused)
+ // and the last (because tabbing after the last element may send focus into browser chrome).
+ if (i != 0) {
+ await sendTab();
+ }
+ const control = order[i];
+ assert_equals(shadowIncludingActiveElement(),control,`${description}: Step ${i+1}`);
+ }
+ for(let i=order.length-1;i>=0;--i) {
+ const control = order[i];
+ assert_equals(shadowIncludingActiveElement(),control,`${description}: Step ${i+1} (backwards)`);
+ // Press shift+tab between each check, excluding last (because it should already be focused)
+ // and the first (because shift+tabbing after the last element may send focus into browser chrome).
+ if (i != 0) {
+ await sendShiftTab();
+ }
+ }
+}