test_focus_autocomplete.html (2758B)
1 <!doctype html> 2 3 <head> 4 <title>Form Autocomplete Tests</title> 5 6 <link rel="stylesheet" 7 href="chrome://mochikit/content/tests/SimpleTest/test.css" /> 8 9 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 10 <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script> 11 12 <script src="../common.js"></script> 13 <script src="../promisified-events.js"></script> 14 <script src="../role.js"></script> 15 16 <script type="application/javascript"> 17 const { TestUtils } = ChromeUtils.importESModule( 18 "resource://testing-common/TestUtils.sys.mjs"); 19 20 async function waitForFocusOnOptionWithname(name) { 21 let event = await waitForEvent( 22 EVENT_FOCUS, 23 evt => evt.accessible.role == ROLE_COMBOBOX_OPTION 24 ); 25 while (!event.accessible.name) { 26 // Sometimes, the name is null for a very short time after the focus 27 // event. 28 await waitForEvent(EVENT_NAME_CHANGE, event.accessible); 29 } 30 is(event.accessible.name, name, "Got focus on option with name " + name); 31 } 32 33 async function doTests() { 34 const input = getNode("input"); 35 info("Focusing the input"); 36 let focused = waitForEvent(EVENT_FOCUS, input); 37 input.focus(); 38 await focused; 39 40 let shown = waitForEvent(EVENT_SHOW, event => 41 event.accessible.role == ROLE_GROUPING && 42 event.accessible.firstChild.role == ROLE_COMBOBOX_LIST); 43 info("Pressing ArrowDown to open the popup"); 44 synthesizeKey("KEY_ArrowDown"); 45 await shown; 46 // The popup still doesn't seem to be ready even once it's fired an a11y 47 // show event! 48 const controller = Cc["@mozilla.org/autocomplete/controller;1"]. 49 getService(Ci.nsIAutoCompleteController); 50 info("Waiting for popup to be fully open and ready"); 51 await TestUtils.waitForCondition(() => controller.input.popupOpen); 52 53 focused = waitForFocusOnOptionWithname("a"); 54 info("Pressing ArrowDown to focus first item"); 55 synthesizeKey("KEY_ArrowDown"); 56 await focused; 57 58 focused = waitForFocusOnOptionWithname("b"); 59 info("Pressing ArrowDown to focus the second item"); 60 synthesizeKey("KEY_ArrowDown"); 61 await focused; 62 63 focused = waitForEvent(EVENT_FOCUS, input); 64 info("Pressing enter to select the second item"); 65 synthesizeKey("KEY_Enter"); 66 await focused; 67 is(input.value, "b", "input value filled with second item"); 68 69 SimpleTest.finish(); 70 } 71 72 SimpleTest.waitForExplicitFinish(); 73 addA11yLoadEvent(doTests); 74 </script> 75 </head> 76 <body> 77 <input id="input" list="list"> 78 <datalist id="list"> 79 <option id="a" value="a"> 80 <option id="b" value="b"> 81 </datalist> 82 </body> 83 </html>