browser_focus_steal_from_chrome_during_mousedown.js (2570B)
1 add_task(async function test() { 2 const kTestURI = 3 "data:text/html," + 4 '<script type="text/javascript">' + 5 " function onMouseDown(aEvent) {" + 6 " document.getElementById('willBeFocused').focus();" + 7 " aEvent.preventDefault();" + 8 " }" + 9 "</script>" + 10 '<body id="body">' + 11 '<button onmousedown="onMouseDown(event);" style="width: 100px; height: 100px;">click here</button>' + 12 '<input id="willBeFocused"></body>'; 13 14 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, kTestURI); 15 16 let fm = Services.focus; 17 18 for (var button = 0; button < 3; button++) { 19 // Set focus to a chrome element before synthesizing a mouse down event. 20 gURLBar.focus(); 21 22 is( 23 fm.focusedElement, 24 gURLBar.inputField, 25 "Failed to move focus to search bar: button=" + button 26 ); 27 28 // We intentionally turn off this a11y check, because the following click 29 // is sent on an arbitrary web content that is not expected to be tested 30 // by itself with the browser mochitests, therefore this rule check shall 31 // be ignored by a11y-checks suite. 32 AccessibilityUtils.setEnv({ mustHaveAccessibleRule: false }); 33 // Synthesize mouse down event on browser object over the button, such that 34 // the event propagates through both processes. 35 EventUtils.synthesizeMouse(tab.linkedBrowser, 20, 20, { button }); 36 AccessibilityUtils.resetEnv(); 37 38 isnot( 39 fm.focusedElement, 40 gURLBar.inputField, 41 "Failed to move focus away from search bar: button=" + button 42 ); 43 44 await SpecialPowers.spawn( 45 tab.linkedBrowser, 46 [button], 47 async function (button) { 48 let fm = Services.focus; 49 50 let attempts = 10; 51 await new Promise(resolve => { 52 function check() { 53 if ( 54 attempts > 0 && 55 content.document.activeElement.id != "willBeFocused" 56 ) { 57 attempts--; 58 content.window.setTimeout(check, 100); 59 return; 60 } 61 62 Assert.equal( 63 content.document.activeElement.id, 64 "willBeFocused", 65 "The input element isn't active element: button=" + button 66 ); 67 Assert.equal( 68 fm.focusedElement, 69 content.document.activeElement, 70 "The active element isn't focused element in App level: button=" + 71 button 72 ); 73 resolve(); 74 } 75 check(); 76 }); 77 } 78 ); 79 } 80 81 gBrowser.removeTab(tab); 82 });