focus-visible-008.html (2411B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5 <title>CSS Test (Selectors): Keyboard focus enables :focus-visible</title> 6 <link rel="author" title="Alice Boxhall" href="aboxhall@chromium.org" /> 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 #el:focus { 15 outline: red solid 5px; 16 background-color: red; 17 } 18 } 19 20 :focus-visible { 21 outline: green solid 5px; 22 } 23 24 #el:focus:not(:focus-visible) { 25 background-color: red; 26 outline: 0; 27 } 28 </style> 29 </head> 30 <body> 31 This test checks that programmatically focusing an element after a keypress causes <code>:focus-visible</code> to match. 32 <ol id="instructions"> 33 <li>Use the tab key to move focus to the button below that says "Tab to me and press ENTER."</li> 34 <li>Press ENTER.</li> 35 <li>If the element that says "I will be focused programmatically." has a red background, then the test result is FAILURE. If the element has a green outline, then the test result is SUCCESS.</li> 36 </ol> 37 <br /> 38 <button id="button">Tab to me and press ENTER.</button> 39 <div id="el" tabindex="-1">I will be focused programmatically.</div> 40 <script> 41 if ("async_test" in window) { 42 async_test(function(t) { 43 button.addEventListener("click", t.step_func(() => { 44 el.focus(); 45 })); 46 el.addEventListener("focus", t.step_func(function() { 47 assert_equals(getComputedStyle(el).outlineColor, "rgb(0, 128, 0)", `outlineColor for ${el.tagName}#${el.id} should be green`); 48 assert_not_equals(getComputedStyle(el).backgroundColor, "rgb(255, 0, 0)", `backgroundColor for ${el.tagName}#${el.id} should NOT be red`); 49 t.done(); 50 })); 51 test_driver.send_keys(el, "\ue004\ue007"); // TAB and ENTER 52 }, "Programmatic focus after keypress should match :focus-visible"); 53 } else { 54 button.addEventListener("click", () => { 55 el.focus(); 56 }); 57 } 58 59 const tab_key = '\ue004'; 60 const enter_key = '\uE007'; 61 test_driver.send_keys(el, tab_key).then(() => { 62 test_driver.send_keys(el, enter_key); 63 }); 64 </script> 65 </body> 66 </html>