checkable-active-space-key-being-disabled.html (2905B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Tests active state of checkbox/radio when pressing space key but it's disabled by a keydown event listener</title> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="/resources/testdriver.js"></script> 9 <script src="/resources/testdriver-actions.js"></script> 10 <script src="/resources/testdriver-vendor.js"></script> 11 </head> 12 <body> 13 <input type="checkbox"> 14 <input type="radio"> 15 <script> 16 const spaceKey = "\uE00D"; 17 18 function disableTarget(event) { 19 event.target.disabled = true; 20 } 21 22 // If a `keydown` event listener disables the event target, default event 23 // handler in browser shouldn't activate the disabled element. Otherwise, 24 // the browser loses a chance to inactivate the disabled element because 25 // it won't get keyup events until it's enabled again. 26 27 promise_test(async t => { 28 const checkbox = document.querySelector("input[type=checkbox]"); 29 checkbox.focus(); 30 checkbox.addEventListener("keydown", disableTarget); 31 await (new test_driver.Actions()).keyDown(spaceKey).send(); 32 let released = false; 33 t.add_cleanup(async () => { 34 if (!released) { 35 await (new test_driver.Actions()).keyUp(spaceKey).send(); 36 } 37 checkbox.removeEventListener("keydown", disableTarget); 38 checkbox.remove(); 39 }); 40 test(() => { 41 assert_equals( 42 document.querySelector("input:active"), 43 null, 44 "The checkbox shouldn't be activated" 45 ); 46 }, "Space key press shouldn't activate the disabled checkbox"); 47 48 await (new test_driver.Actions()).keyUp(spaceKey).send(); 49 released = true; 50 51 assert_equals( 52 document.querySelector("input:active"), 53 null, 54 "The disabled checkbox should be inactivated even if activated accidentally" 55 ); 56 }, "Space key shouldn't active the checkbox when it's disabled by a keydown event listener"); 57 58 promise_test(async t => { 59 const radio = document.querySelector("input[type=radio]"); 60 radio.focus(); 61 radio.addEventListener("keydown", disableTarget); 62 await (new test_driver.Actions()).keyDown(spaceKey).send(); 63 let released = false; 64 t.add_cleanup(async () => { 65 if (!released) { 66 await (new test_driver.Actions()).keyUp(spaceKey).send(); 67 } 68 radio.removeEventListener("keydown", disableTarget); 69 radio.disabled = false; 70 }); 71 test(() => { 72 assert_equals( 73 document.querySelector("input:active"), 74 null, 75 "The radio shouldn't be activated" 76 ); 77 }, "Space key press shouldn't activate the disabled radio"); 78 79 await (new test_driver.Actions()).keyUp(spaceKey).send(); 80 released = true; 81 82 assert_equals( 83 document.querySelector("input:active"), 84 null, 85 "The disabled radio should be inactivated even if it's accidentally activated" 86 ); 87 }, "Space key shouldn't active the radio when it's disabled by a keydown event listener"); 88 </script> 89 </body> 90 </html>