blob: 05c89cadee5dab80fd91ab1118ee8f3781e9cc03 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CSS Test (Selectors): Keyboard use triggers :focus-visible</title>
<link rel="author" title="Alice Boxhall" href="aboxhall@chromium.org" />
<link rel="help" href="https://drafts.csswg.org/selectors-4/#the-focus-visible-pseudo" />
<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>
<style>
:root {
--focus-background: hsl(50, 94%, 72%);
--focus-ring-color: rgb(59, 153, 252);
}
:focus-visible {
outline: var(--focus-ring-color) auto 5px;
}
* {
outline-width: 0;
}
:focus {
background-color: var(--focus-background);
}
</style>
</head>
<body>
This test checks that using the keyboard in a way that does not move focus still causes <code>:focus-visible</code> matching to trigger.
<ol id="instructions">
<li>If the user-agent does not claim to support the <code>:focus-visible</code> pseudo-class then SKIP this test.</li>
<li>Use the mouse to focus the element below that says "Click me first."</li>
<li>If the element has a blue outline, then the test result is FAILURE.</li>
<li>Press the SHIFT key.</li>
<li>If the element now DOES NOT have a blue outline, then the test result is FAILURE..</li>
<li>Use the mouse to click the element that says "Click me first." a second time</li>
<li>If the element now DOES NOT still have a blue outline, then the test result is FAILURE.</li>
<li>Use the mouse to click the element below that says "Click me second."</li>
<li>If the element does not have a blue outline then the test result is SUCCESS.</li>
</ol>
<div id="one" tabindex="0">Click me first.</div>
<div id="two" tabindex="0">Click me second.</div>
<script>
document.body.addEventListener("keydown", (e) => {
delete document.body.dataset.hadmousedown;
document.body.dataset.hadkeydown = "";
}, true);
document.body.addEventListener("mousedown", (e) => {
document.body.dataset.hadmousedown = "";
}, true);
document.body.addEventListener("focus", (e) => {
if ("hadmousedown" in document.body.dataset) {
delete document.body.dataset.hadmousedown;
delete document.body.dataset.hadkeydown;
}
}, true);
async_test(async function(t) {
let tested_modality_change = false;
let tested_modality_unchanged_by_mouse_click = false;
let tested_mouse_focus_after_modality_change = false;
// TODO(crbug.com/828858): Remove this check once bug is resolved.
test_driver.send_keys(document.body, "\uE050").then(t.step_func(() => {
const handle_initial_focus = t.step_func(() => {
one.addEventListener("keyup", t.step_func(test_modality_change));
one.removeEventListener("focus", handle_initial_focus);
test_driver.send_keys(one, "\uE050");
});
const test_modality_change = t.step_func(() => {
assert_equals(getComputedStyle(one).outlineColor, "rgb(59, 153, 252)");
tested_modality_change = true;
one.removeEventListener("keyup", test_modality_change);
one.addEventListener("click", test_modality_unchanged_by_mouse_click);
test_driver.click(one);
});
const test_modality_unchanged_by_mouse_click = t.step_func(() => {
assert_true(tested_modality_change);
assert_equals(getComputedStyle(one).outlineColor, "rgb(59, 153, 252)");
tested_modality_unchanged_by_mouse_click = true;
one.removeEventListener("click", test_modality_unchanged_by_mouse_click);
two.addEventListener("focus", test_mouse_focus_after_modality_change);
test_driver.click(two);
});
const test_mouse_focus_after_modality_change = t.step_func(() => {
assert_true(tested_modality_unchanged_by_mouse_click);
assert_false("hadkeydown" in document.body.dataset);
assert_equals(getComputedStyle(two).outlineColor, "rgb(59, 153, 252)");
tested_mouse_focus_after_modality_change = true;
t.done();
});
one.addEventListener("focus", handle_initial_focus);
test_driver.click(one);
})).catch(t.step_func((e) => {
// TODO(crbug.com/828858): Remove this check once bug is resolved.
assert_true(false, "send_keys not implmented yet");
t.done();
}));
}, "Using keyboard while element is focused should trigger :focus-visible; using mouse without moving focus should not cancel it; moving focus using mouse should cancel it.");
</script>
</body>
</html>