1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
<!DOCTYPE HTML>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/wai-aria/scripts/aria-utils.js"></script>
</head>
<body>
<custom-element id="customElement"></custom-element>
<custom-button id="customButton"></custom-button>
<!-- Targets -->
<div id="target" popover>Popover content</div>
<dialog id="dialog">Dialog content</dialog>
<script>
class CustomElement extends HTMLElement {
constructor() {
super();
this.internals_ = this.attachInternals();
}
}
customElements.define('custom-element', CustomElement);
class CustomButton extends HTMLElement {
static formAssociated = true;
constructor() {
super();
this.internals_ = this.attachInternals();
this.internals_.type = "button";
}
get disabled() {
return this.hasAttribute('disabled');
}
set disabled(value) {
if (value) {
this.setAttribute('disabled', '');
} else {
this.removeAttribute('disabled');
}
}
}
customElements.define('custom-button', CustomButton);
function resetState() {
document.getElementById("target").hidePopover();
document.getElementById("dialog").close();
document.getElementById('customButton').outerHTML = '<custom-button id="customButton">';
document.getElementById('customElement').outerHTML = '<custom-element id="customElement">';
}
// Test popover commands
test(t => {
const customElement = document.getElementById("customElement");
const target = document.getElementById("target");
customElement.setAttribute('commandfor', 'target');
customElement.setAttribute('command', 'toggle-popover');
t.add_cleanup(() => resetState());
// Custom element without type=button should not work with commandfor
assert_false(target.matches(':popover-open'), "Popover should start closed");
customElement.click();
assert_false(target.matches(':popover-open'), "Custom element without type=button should not trigger popover");
}, "Custom element without type=button does not support commandfor attribute.");
test(t => {
const customButton = document.getElementById("customButton");
const target = document.getElementById("target");
customButton.setAttribute('commandfor', 'target');
customButton.setAttribute('command', 'toggle-popover');
t.add_cleanup(() => resetState());
// Custom button with type=button should work
assert_false(target.matches(':popover-open'), "Popover should start closed");
customButton.click();
assert_true(target.matches(':popover-open'), "Custom button with type=button should open popover");
// Second click should close
customButton.click();
assert_false(target.matches(':popover-open'), "Second click should close popover");
}, "Custom button with type=button and command='toggle-popover' toggles popover.");
test(t => {
const customButton = document.getElementById("customButton");
const target = document.getElementById("target");
customButton.setAttribute('commandfor', 'target');
customButton.setAttribute('command', 'show-popover');
t.add_cleanup(() => resetState());
assert_false(target.matches(':popover-open'), "Popover should start closed");
customButton.click();
assert_true(target.matches(':popover-open'), "show-popover should open popover");
// Second click should not close (show action only opens)
customButton.click();
assert_true(target.matches(':popover-open'), "show-popover should not close popover on second click");
}, "Custom button with command='show-popover' only opens popover.");
test(t => {
const customButton = document.getElementById("customButton");
const target = document.getElementById("target");
customButton.setAttribute('commandfor', 'target');
customButton.setAttribute('command', 'hide-popover');
t.add_cleanup(() => resetState());
// Start with popover open
target.showPopover();
assert_true(target.matches(':popover-open'), "Popover should start open");
customButton.click();
assert_false(target.matches(':popover-open'), "hide-popover should close popover");
// Second click should not open (hide action only closes)
customButton.click();
assert_false(target.matches(':popover-open'), "hide-popover should not open popover on second click");
}, "Custom button with command='hide-popover' only closes popover.");
test(t => {
const customButton = document.getElementById("customButton");
const target = document.getElementById("target");
customButton.setAttribute('commandfor', 'target');
customButton.setAttribute('command', 'toggle-popover');
customButton.disabled = true;
t.add_cleanup(() => resetState());
// Disabled custom button should not work
assert_true(customButton.disabled, "Custom button should be disabled");
assert_false(target.matches(':popover-open'), "Popover should start closed");
customButton.click();
assert_false(target.matches(':popover-open'), "Disabled custom button should not open popover");
}, "Disabled custom button does not support commandfor.");
// Test dialog commands
test(t => {
const customButton = document.getElementById("customButton");
const dialog = document.getElementById("dialog");
customButton.setAttribute('commandfor', 'dialog');
customButton.setAttribute('command', 'show-modal');
t.add_cleanup(() => resetState());
assert_false(dialog.open, "Dialog should start closed");
customButton.click();
assert_true(dialog.open, "show-modal should open dialog");
}, "Custom button with command='show-modal' opens dialog.");
test(t => {
const customButton = document.getElementById("customButton");
const dialog = document.getElementById("dialog");
customButton.setAttribute('commandfor', 'dialog');
customButton.setAttribute('command', 'close');
t.add_cleanup(() => resetState());
// Start with dialog open
dialog.showModal();
assert_true(dialog.open, "Dialog should start open");
customButton.click();
assert_false(dialog.open, "close command should close dialog");
}, "Custom button with command='close' closes dialog.");
test(t => {
const customButton = document.getElementById("customButton");
const dialog = document.getElementById("dialog");
customButton.setAttribute('commandfor', 'dialog');
customButton.setAttribute('command', 'request-close');
t.add_cleanup(() => resetState());
// Test with dialog open - should close it
dialog.showModal();
assert_true(dialog.open, "Dialog should start open");
customButton.click();
assert_false(dialog.open, "request-close command should close open dialog");
// Test with dialog already closed - should do nothing
assert_false(dialog.open, "Dialog should be closed");
customButton.click();
assert_false(dialog.open, "request-close command should not open closed dialog");
}, "Custom button with command='request-close' closes open dialog and does nothing when dialog is already closed.");
// Test custom commands
test(t => {
const customButton = document.getElementById("customButton");
const target = document.getElementById("target");
let commandEventFired = false;
let receivedCommand = '';
customButton.setAttribute('commandfor', 'target');
customButton.setAttribute('command', '--my-custom-action');
const commandListener = (e) => {
commandEventFired = true;
receivedCommand = e.command;
};
target.addEventListener('command', commandListener, { signal: t.get_signal() });
t.add_cleanup(() => resetState());
customButton.click();
assert_true(commandEventFired, "Custom command should fire command event");
assert_equals(receivedCommand, '--my-custom-action', "Command event should contain custom command");
}, "Custom button with custom command fires command event.");
// Test invalid commands
test(t => {
const customButton = document.getElementById("customButton");
const target = document.getElementById("target");
let commandEventFired = false;
customButton.setAttribute('commandfor', 'target');
customButton.setAttribute('command', 'invalid-action');
const commandListener = (e) => {
commandEventFired = true;
};
target.addEventListener('command', commandListener, { signal: t.get_signal() });
t.add_cleanup(() => resetState());
customButton.click();
assert_false(commandEventFired, "Invalid command should not fire command event");
}, "Custom button with invalid command does not fire command event.");
</script>
</body>
</html>
|