browser_jsterm_history_arrow_keys.js (5146B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // See Bugs 594497 and 619598. 7 8 const TEST_URI = 9 "data:text/html;charset=utf-8,<!DOCTYPE html>Web Console test for " + 10 "bug 594497 and bug 619598"; 11 12 const TEST_VALUES = [ 13 "document", 14 "window", 15 "document.body", 16 "document;\nwindow;\ndocument.body", 17 "document.location", 18 ]; 19 20 add_task(async function () { 21 const hud = await openNewTabAndConsole(TEST_URI); 22 const { jsterm } = hud; 23 24 const checkInput = (expected, assertionInfo) => 25 checkInputValueAndCursorPosition(hud, expected, assertionInfo); 26 27 jsterm.focus(); 28 checkInput("|", "input is empty"); 29 30 info("Execute each test value in the console"); 31 for (const value of TEST_VALUES) { 32 await executeAndWaitForResultMessage(hud, value, ""); 33 } 34 35 EventUtils.synthesizeKey("KEY_ArrowUp"); 36 checkInput("document.location|", "↑: input #4 is correct"); 37 ok(inputHasNoSelection(jsterm)); 38 39 EventUtils.synthesizeKey("KEY_ArrowUp"); 40 checkInput("document;\nwindow;\ndocument.body|", "↑: input #3 is correct"); 41 ok(inputHasNoSelection(jsterm)); 42 43 info( 44 "Move cursor and ensure hitting arrow up twice won't navigate the history" 45 ); 46 EventUtils.synthesizeKey("KEY_ArrowLeft"); 47 EventUtils.synthesizeKey("KEY_ArrowLeft"); 48 checkInput("document;\nwindow;\ndocument.bo|dy"); 49 50 EventUtils.synthesizeKey("KEY_ArrowUp"); 51 EventUtils.synthesizeKey("KEY_ArrowUp"); 52 53 checkInput("document;|\nwindow;\ndocument.body", "↑↑: input #3 is correct"); 54 ok(inputHasNoSelection(jsterm)); 55 56 EventUtils.synthesizeKey("KEY_ArrowUp"); 57 checkInput( 58 "|document;\nwindow;\ndocument.body", 59 "↑ again: input #3 is correct" 60 ); 61 ok(inputHasNoSelection(jsterm)); 62 63 EventUtils.synthesizeKey("KEY_ArrowUp"); 64 checkInput("document.body|", "↑: input #2 is correct"); 65 66 EventUtils.synthesizeKey("KEY_ArrowUp"); 67 checkInput("window|", "↑: input #1 is correct"); 68 69 EventUtils.synthesizeKey("KEY_ArrowUp"); 70 checkInput("document|", "↑: input #0 is correct"); 71 ok(inputHasNoSelection(jsterm)); 72 73 EventUtils.synthesizeKey("KEY_ArrowDown"); 74 checkInput("window|", "↓: input #1 is correct"); 75 ok(inputHasNoSelection(jsterm)); 76 77 EventUtils.synthesizeKey("KEY_ArrowDown"); 78 checkInput("document.body|", "↓: input #2 is correct"); 79 80 EventUtils.synthesizeKey("KEY_ArrowDown"); 81 checkInput("document;\nwindow;\ndocument.body|", "↓: input #3 is correct"); 82 ok(inputHasNoSelection(jsterm)); 83 84 setCursorAtPosition(hud, 2); 85 checkInput("do|cument;\nwindow;\ndocument.body"); 86 87 EventUtils.synthesizeKey("KEY_ArrowDown"); 88 EventUtils.synthesizeKey("KEY_ArrowDown"); 89 checkInput("document;\nwindow;\ndo|cument.body", "↓↓: input #3 is correct"); 90 ok(inputHasNoSelection(jsterm)); 91 92 EventUtils.synthesizeKey("KEY_ArrowDown"); 93 checkInput( 94 "document;\nwindow;\ndocument.body|", 95 "↓ again: input #3 is correct" 96 ); 97 ok(inputHasNoSelection(jsterm)); 98 99 EventUtils.synthesizeKey("KEY_ArrowDown"); 100 checkInput("document.location|", "↓: input #4 is correct"); 101 102 EventUtils.synthesizeKey("KEY_ArrowDown"); 103 checkInput("|", "↓: input is empty"); 104 105 info("Test that Cmd + ArrowDown/Up works as expected on OSX"); 106 if (Services.appinfo.OS === "Darwin") { 107 const option = { metaKey: true }; 108 EventUtils.synthesizeKey("KEY_ArrowUp", option); 109 checkInput("document.location|", "Cmd+↑ : input is correct"); 110 111 EventUtils.synthesizeKey("KEY_ArrowUp", option); 112 checkInput( 113 "document;\nwindow;\ndocument.body|", 114 "Cmd+↑ : input is correct" 115 ); 116 117 EventUtils.synthesizeKey("KEY_ArrowUp", option); 118 checkInput( 119 "|document;\nwindow;\ndocument.body", 120 "Cmd+↑ : cursor is moved to the beginning of the input" 121 ); 122 123 EventUtils.synthesizeKey("KEY_ArrowUp", option); 124 checkInput("document.body|", "Cmd+↑: input is correct"); 125 126 EventUtils.synthesizeKey("KEY_ArrowDown", option); 127 checkInput( 128 "document;\nwindow;\ndocument.body|", 129 "Cmd+↓ : input is correct" 130 ); 131 132 EventUtils.synthesizeKey("KEY_ArrowUp", option); 133 checkInput( 134 "|document;\nwindow;\ndocument.body", 135 "Cmd+↑ : cursor is moved to the beginning of the input" 136 ); 137 138 EventUtils.synthesizeKey("KEY_ArrowDown", option); 139 checkInput( 140 "document;\nwindow;\ndocument.body|", 141 "Cmd+↓ : cursor is moved to the end of the input" 142 ); 143 144 EventUtils.synthesizeKey("KEY_ArrowDown", option); 145 checkInput("document.location|", "Cmd+↓ : input is correct"); 146 147 EventUtils.synthesizeKey("KEY_ArrowDown", option); 148 checkInput("|", "Cmd+↓: input is empty"); 149 } 150 }); 151 152 function setCursorAtPosition(hud, pos) { 153 const { editor } = hud.jsterm; 154 155 let line = 0; 156 let ch = 0; 157 let currentPos = 0; 158 getInputValue(hud) 159 .split("\n") 160 .every(l => { 161 if (l.length < pos - currentPos) { 162 line++; 163 currentPos += l.length; 164 return true; 165 } 166 ch = pos - currentPos; 167 return false; 168 }); 169 return editor.setCursor({ line, ch }); 170 } 171 172 function inputHasNoSelection(jsterm) { 173 return !jsterm.editor.getDoc().getSelection(); 174 }