browser_jsterm_autocomplete_inside_text.js (6117B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test that editing text inside parens behave as expected, i.e. 7 // - it does not show the autocompletion text 8 // - show popup when there's properties to complete 9 // - insert the selected item from the popup in the input 10 // - right arrow dismiss popup and don't autocomplete 11 // - tab key when there is not visible autocomplete suggestion insert a tab 12 // See Bug 812618, 1479521 and 1334130. 13 14 const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html> 15 <head> 16 <script> 17 window.testBugAA = "hello world"; 18 window.testBugBB = "hello world 2"; 19 window.x = "hello world 3"; 20 </script> 21 </head> 22 <body>bug 812618 - test completion inside text</body>`; 23 24 add_task(async function () { 25 const hud = await openNewTabAndConsole(TEST_URI); 26 const { jsterm } = hud; 27 info("web console opened"); 28 29 const { autocompletePopup: popup } = jsterm; 30 31 await setInitialState(hud); 32 33 ok(popup.isOpen, "popup is open"); 34 is(popup.itemCount, 2, "popup.itemCount is correct"); 35 is(popup.selectedIndex, 0, "popup.selectedIndex is correct"); 36 ok(!getInputCompletionValue(hud), "there is no completion text"); 37 38 info("Pressing arrow right"); 39 let onPopupClose = popup.once("popup-closed"); 40 EventUtils.synthesizeKey("KEY_ArrowRight"); 41 await onPopupClose; 42 ok(true, "popup was closed"); 43 checkInputValueAndCursorPosition( 44 hud, 45 "dump(window.testB)|", 46 "input wasn't modified" 47 ); 48 49 await setInitialState(hud); 50 EventUtils.synthesizeKey("KEY_ArrowDown"); 51 is(popup.selectedIndex, 1, "popup.selectedIndex is correct"); 52 ok(!getInputCompletionValue(hud), "completeNode.value is empty"); 53 54 ok( 55 hasExactPopupLabels(popup, ["testBugAA", "testBugBB"]), 56 "getItems returns the items we expect" 57 ); 58 59 info("press Tab and wait for popup to hide"); 60 onPopupClose = popup.once("popup-closed"); 61 EventUtils.synthesizeKey("KEY_Tab"); 62 await onPopupClose; 63 64 // At this point the completion suggestion should be accepted. 65 ok(!popup.isOpen, "popup is not open"); 66 checkInputValueAndCursorPosition( 67 hud, 68 "dump(window.testBugBB|)", 69 "completion was successful after VK_TAB" 70 ); 71 ok(!getInputCompletionValue(hud), "there is no completion text"); 72 73 info("Test ENTER key when popup is visible with a selected item"); 74 await setInitialState(hud); 75 info("press Enter and wait for popup to hide"); 76 onPopupClose = popup.once("popup-closed"); 77 EventUtils.synthesizeKey("KEY_Enter"); 78 await onPopupClose; 79 80 ok(!popup.isOpen, "popup is not open"); 81 checkInputValueAndCursorPosition( 82 hud, 83 "dump(window.testBugAA|)", 84 "completion was successful after Enter" 85 ); 86 ok(!getInputCompletionValue(hud), "there is no completion text"); 87 88 info("Test autocomplete inside parens"); 89 await setInputValueForAutocompletion(hud, "dump()", -1); 90 let onAutocompleteUpdated = jsterm.once("autocomplete-updated"); 91 EventUtils.sendString("window.testBugA"); 92 await onAutocompleteUpdated; 93 ok(popup.isOpen, "popup is open"); 94 ok(!getInputCompletionValue(hud), "there is no completion text"); 95 96 info("Matching the completion proposal should close the popup"); 97 onPopupClose = popup.once("popup-closed"); 98 EventUtils.sendString("A"); 99 await onPopupClose; 100 101 info("Test TAB key when there is no autocomplete suggestion"); 102 ok(!popup.isOpen, "popup is not open"); 103 ok(!getInputCompletionValue(hud), "there is no completion text"); 104 105 EventUtils.synthesizeKey("KEY_Tab"); 106 checkInputValueAndCursorPosition( 107 hud, 108 "dump(window.testBugAA\t|)", 109 "completion was successful after Enter" 110 ); 111 112 info("Check that we don't show the popup when editing words"); 113 await setInputValueForAutocompletion(hud, "estBug", 0); 114 onAutocompleteUpdated = jsterm.once("autocomplete-updated"); 115 EventUtils.sendString("t"); 116 await onAutocompleteUpdated; 117 is(getInputValue(hud), "testBug", "jsterm has expected value"); 118 is(popup.isOpen, false, "popup is not open"); 119 ok(!getInputCompletionValue(hud), "there is no completion text"); 120 121 await setInputValueForAutocompletion(hud, "__foo", 1); 122 onAutocompleteUpdated = jsterm.once("autocomplete-updated"); 123 EventUtils.sendString("t"); 124 await onAutocompleteUpdated; 125 is(getInputValue(hud), "_t_foo", "jsterm has expected value"); 126 is(popup.isOpen, false, "popup is not open"); 127 ok(!getInputCompletionValue(hud), "there is no completion text"); 128 129 await setInputValueForAutocompletion(hud, "$$bar", 1); 130 onAutocompleteUpdated = jsterm.once("autocomplete-updated"); 131 EventUtils.sendString("t"); 132 await onAutocompleteUpdated; 133 is(getInputValue(hud), "$t$bar", "jsterm has expected value"); 134 is(popup.isOpen, false, "popup is not open"); 135 ok(!getInputCompletionValue(hud), "there is no completion text"); 136 137 await setInputValueForAutocompletion(hud, "99luftballons", 1); 138 onAutocompleteUpdated = jsterm.once("autocomplete-updated"); 139 EventUtils.sendString("t"); 140 await onAutocompleteUpdated; 141 is(getInputValue(hud), "9t9luftballons", "jsterm has expected value"); 142 is(popup.isOpen, false, "popup is not open"); 143 ok(!getInputCompletionValue(hud), "there is no completion text"); 144 145 info("Check that typing the closing paren closes the autocomplete window"); 146 await setInputValueForAutocompletion(hud, "dump()", -1); 147 const onPopupOpen = popup.once("popup-opened"); 148 EventUtils.sendString("x"); 149 await onPopupOpen; 150 151 onPopupClose = popup.once("popup-closed"); 152 // Since the paren is already here, it won't add any new character 153 EventUtils.sendString(")"); 154 checkInputValueAndCursorPosition( 155 hud, 156 "dump(x)|", 157 "the input is the expected one after typing the closing paren" 158 ); 159 await onPopupClose; 160 ok(true, "popup was closed when typing the closing paren"); 161 }); 162 163 async function setInitialState(hud) { 164 const { jsterm } = hud; 165 await setInputValueForAutocompletion(hud, "dump()", -1); 166 167 const onAutocompleteUpdated = jsterm.once("autocomplete-updated"); 168 EventUtils.sendString("window.testB"); 169 checkInputValueAndCursorPosition(hud, "dump(window.testB|)"); 170 await onAutocompleteUpdated; 171 }