focus-visible-028.html (3329B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5 <title>CSS Test (Selectors): Programmatic focus causes :focus-visible to match after keyboard usage when preventDefault() is used</title> 6 <link rel="author" title="Tyler Jones" href="tylerjdev@github.com" /> 7 <link rel="help" href="https://drafts.csswg.org/selectors-4/#the-focus-visible-pseudo" /> 8 <script src="/resources/testharness.js"></script> 9 <script src="/resources/testharnessreport.js"></script> 10 <script src="/resources/testdriver.js"></script> 11 <script src="/resources/testdriver-vendor.js"></script> 12 <style> 13 @supports not selector(:focus-visible) { 14 :focus { 15 outline: red solid 5px; 16 background-color: red; 17 } 18 } 19 20 :focus-visible { 21 outline: blue solid 5px; 22 } 23 24 :focus:not(:focus-visible) { 25 outline: 0; 26 background-color: lime; 27 } 28 </style> 29 </head> 30 <body> 31 This test checks that programmatically focusing an element after a click and using keyboard afterwards triggers <code>:focus-visible</code> matching. 32 <ol id="instructions"> 33 <li>If the user-agent does not claim to support the <code>:focus-visible</code> pseudo-class then SKIP this test.</li> 34 <li>Click the button below that says "Click me".</li> 35 <li>Once focused on the button that says "I will be focused programmatically", use "Left" or "Right" arrow keys to navigate through the button group</li> 36 <li>If any button within the group has a blue outline after using either arrow key, then the test result is SUCCESS. If the element has a green background after using either arrow key, then the test result is FAILURE.</li> 37 </ol> 38 <br /> 39 <button id="button">Click me</button> 40 <div id="container"> 41 <button id="btn1">I will be focused programmatically.</button> 42 <button id="btn2">Button 2</button> 43 <button id="btn3">Button 3</button> 44 </div> 45 <script> 46 button.addEventListener("click", () => { 47 btn1.focus(); 48 }); 49 50 focusTrap(document.querySelector('#container')); 51 52 function focusTrap(container) { 53 container.addEventListener('keydown', (e) => { 54 if (e.key !== 'ArrowRight' && e.key !== 'ArrowLeft') return; 55 e.preventDefault(); 56 57 const btns = container.querySelectorAll('button'); 58 const currentIndex = Array.from(btns).indexOf(document.activeElement); 59 let nextIndex; 60 61 if (e.key === 'ArrowRight') { 62 nextIndex = (currentIndex + 1) % btns.length; 63 } else if (e.key === 'ArrowLeft') { 64 nextIndex = (currentIndex - 1 + btns.length) % btns.length; 65 } 66 67 btns[nextIndex].focus(); 68 }); 69 } 70 71 promise_test(async t => { 72 const arrow_right = "\uE014"; 73 btn2.addEventListener("focus", t.step_func_done((e) => { 74 assert_equals(getComputedStyle(btn2).outlineColor, "rgb(0, 0, 255)", `outlineColor for ${btn2.tagName}#${btn2.id} is blue`); 75 assert_not_equals(getComputedStyle(btn2).backgroundColor, "rgb(0, 255, 0)", `backgroundColor for ${btn2.tagName}#${btn2.id} is NOT lime`); 76 })); 77 78 await test_driver.click(button); 79 assert_equals(document.activeElement, btn1); 80 await test_driver.send_keys(btn1, arrow_right); 81 }, "Programmatic focus after click and keyboard interaction should match :focus-visible"); 82 </script> 83 </body> 84 </html>