browser_webconsole_input_focus.js (2831B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Tests that the input field is focused when the console is opened. 5 6 "use strict"; 7 8 const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html>Test input focused 9 <script> 10 console.log("console message 1"); 11 </script>`; 12 13 add_task(async function () { 14 const hud = await openNewTabAndConsole(TEST_URI); 15 16 info("Focus after console is opened"); 17 ok(isInputFocused(hud), "input node is focused after console is opened"); 18 19 info("Set the input value and select the first line"); 20 const expression = `x = 10;x; 21 x = 20;x;`; 22 setInputValue(hud, expression); 23 hud.ui.jsterm.editor.setSelection( 24 { line: 0, ch: 0 }, 25 { line: 0, ch: expression.split("\n")[0].length } 26 ); 27 28 await clearOutput(hud); 29 ok(isInputFocused(hud), "input node is focused after output is cleared"); 30 31 info("Focus during message logging"); 32 SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () { 33 content.wrappedJSObject.console.log("console message 2"); 34 }); 35 const msg = await waitFor(() => 36 findConsoleAPIMessage(hud, "console message 2") 37 ); 38 ok(isInputFocused(hud), "input node is focused, first time"); 39 40 // Checking that there's still a selection in the input 41 is( 42 hud.ui.jsterm.editor.getSelection(), 43 "x = 10;x;", 44 "editor has the expected selection" 45 ); 46 47 info("Focus after clicking in the output area"); 48 await waitForBlurredInput(hud); 49 AccessibilityUtils.setEnv({ 50 actionCountRule: false, 51 focusableRule: false, 52 interactiveRule: false, 53 labelRule: false, 54 }); 55 EventUtils.sendMouseEvent({ type: "click" }, msg); 56 AccessibilityUtils.resetEnv(); 57 ok(isInputFocused(hud), "input node is focused, second time"); 58 59 is( 60 hud.ui.jsterm.editor.getSelection(), 61 "", 62 "input selection was removed when the input was blurred" 63 ); 64 65 info("Setting a text selection and making sure a click does not re-focus"); 66 await waitForBlurredInput(hud); 67 const selection = hud.iframeWindow.getSelection(); 68 selection.selectAllChildren(msg.querySelector(".message-body")); 69 AccessibilityUtils.setEnv({ 70 actionCountRule: false, 71 focusableRule: false, 72 interactiveRule: false, 73 labelRule: false, 74 }); 75 EventUtils.sendMouseEvent({ type: "click" }, msg); 76 AccessibilityUtils.resetEnv(); 77 ok(!isInputFocused(hud), "input node not focused after text is selected"); 78 }); 79 80 function waitForBlurredInput(hud) { 81 const node = hud.jsterm.node; 82 return new Promise(resolve => { 83 const lostFocus = () => { 84 ok(!isInputFocused(hud), "input node is not focused"); 85 resolve(); 86 }; 87 node.addEventListener("focusout", lostFocus, { once: true }); 88 89 // The 'focusout' event fires if we focus e.g. the filter box. 90 getFilterInput(hud).focus(); 91 }); 92 }