browser_editor_find.js (4314B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const L10N = new LocalizationHelper( 7 "devtools/client/locales/sourceeditor.properties" 8 ); 9 const FIND_KEY = L10N.getStr("find.key"); 10 11 add_task(async function () { 12 const { ed, win } = await setup(); 13 ed.setText(` 14 // line 1 15 // line 2 16 // line 3 17 // line 4 18 // line 5 19 `); 20 21 await promiseWaitForFocus(); 22 23 const editorDocument = ed.container.contentDocument; 24 const editorWindow = editorDocument.defaultView; 25 26 ok( 27 !getSearchInput(editorDocument), 28 "Search input isn't visible in the beginning" 29 ); 30 31 // The editor needs the focus to properly receive the `synthesizeKey` 32 ed.focus(); 33 synthesizeKeyShortcut(FIND_KEY, editorWindow); 34 35 let input = getSearchInput(editorDocument); 36 ok(!!input, "Search input is visible after hitting the keyboard shortcut"); 37 38 info(`Search for "line"`); 39 input.value = "line"; 40 41 info("Hit Enter to trigger the search"); 42 EventUtils.synthesizeKey("VK_RETURN", {}, editorWindow); 43 ch( 44 ed.getCursor(), 45 { line: 1, ch: 11 }, 46 `Editor cursor is on the first result` 47 ); 48 ok( 49 !!getSearchInput(editorDocument), 50 "Search input is still visible after hitting Enter" 51 ); 52 53 info("Hit Enter again to navigate to next result"); 54 EventUtils.synthesizeKey("VK_RETURN", {}, editorWindow); 55 ch( 56 ed.getCursor(), 57 { line: 2, ch: 12 }, 58 `Editor cursor moved to the second result` 59 ); 60 ok( 61 !!getSearchInput(editorDocument), 62 "Search input is still visible after hitting Enter a second time" 63 ); 64 65 info("Hit Shift+Enter again to navigate to previous result"); 66 EventUtils.synthesizeKey("VK_RETURN", { shiftKey: true }, editorWindow); 67 ch( 68 ed.getCursor(), 69 { line: 1, ch: 11 }, 70 `Editor cursor is back on the first result` 71 ); 72 ok( 73 !!getSearchInput(editorDocument), 74 "Search input is still visible after hitting Shift+Enter" 75 ); 76 77 info("Hit Escape to close the search input"); 78 getSearchInput(editorDocument).focus(); 79 EventUtils.synthesizeKey("VK_ESCAPE", {}, editorWindow); 80 await waitFor(() => !getSearchInput(editorDocument)); 81 ok(true, "Search input is hidden after hitting Escape"); 82 83 info("Display the search input again"); 84 85 synthesizeKeyShortcut(FIND_KEY, editorWindow); 86 input = getSearchInput(editorDocument); 87 ok(!!input, "Search input is visible after hitting the keyboard shortcut"); 88 is(input.value, "line", "input still has the expected value"); 89 90 info("Hit Enter to trigger the search"); 91 EventUtils.synthesizeKey("VK_RETURN", {}, editorWindow); 92 ch( 93 ed.getCursor(), 94 { line: 2, ch: 12 }, 95 `Editor cursor is on the second result` 96 ); 97 ok( 98 !!getSearchInput(editorDocument), 99 "Search input is still visible after hitting Enter" 100 ); 101 102 info("Hit Enter again to navigate to next result"); 103 EventUtils.synthesizeKey("VK_RETURN", {}, editorWindow); 104 ch( 105 ed.getCursor(), 106 { line: 3, ch: 13 }, 107 `Editor cursor moved to the third result` 108 ); 109 ok( 110 !!getSearchInput(editorDocument), 111 "Search input is still visible after hitting Enter a second time" 112 ); 113 114 info( 115 "Check that triggering the Search again when the input is visible works as expected" 116 ); 117 EventUtils.synthesizeKey("VK_RIGHT", {}, editorWindow); 118 is( 119 input.selectionStart, 120 input.selectionEnd, 121 "Search input text isn't selected" 122 ); 123 synthesizeKeyShortcut(FIND_KEY, editorWindow); 124 input = getSearchInput(editorDocument); 125 ok( 126 !!input, 127 "Search input is still visible after hitting the keyboard shortcut" 128 ); 129 is(input.value, "line", "input still has the expected value"); 130 is( 131 input.selectionEnd - input.selectionStart, 132 "line".length, 133 "Search input text is selected after hitting the keyboard shortcut" 134 ); 135 136 info("Hit Enter again to navigate to next result"); 137 EventUtils.synthesizeKey("VK_RETURN", {}, editorWindow); 138 ch( 139 ed.getCursor(), 140 { line: 4, ch: 14 }, 141 `Editor cursor moved to the fourth result` 142 ); 143 ok( 144 !!getSearchInput(editorDocument), 145 "Search input is still visible after pressing Enter after the search shortcut was hit" 146 ); 147 148 teardown(ed, win); 149 }); 150 151 function getSearchInput(editorDocument) { 152 return editorDocument.querySelector("input[type=search]"); 153 }