browser_webconsole_reverse_search_keyboard_navigation.js (3934B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // Tests reverse search results keyboard navigation. 6 7 "use strict"; 8 9 const TEST_URI = `data:text/html,<!DOCTYPE html><meta charset=utf8>Test reverse search`; 10 const isMacOS = AppConstants.platform === "macosx"; 11 12 add_task(async function () { 13 const hud = await openNewTabAndConsole(TEST_URI); 14 15 const jstermHistory = [ 16 `document`, 17 `document 18 .querySelectorAll("*") 19 .forEach(console.log)`, 20 `Dog = "Snoopy"`, 21 ]; 22 23 const onLastMessage = waitForMessageByType(hud, `"Snoopy"`, ".result"); 24 for (const input of jstermHistory) { 25 execute(hud, input); 26 } 27 await onLastMessage; 28 29 await openReverseSearch(hud); 30 EventUtils.sendString("d"); 31 const infoElement = await waitFor(() => getReverseSearchInfoElement(hud)); 32 is( 33 infoElement.textContent, 34 "3 of 3 results", 35 "The reverse info has the expected text" 36 ); 37 38 is(getInputValue(hud), jstermHistory[2], "JsTerm has the expected input"); 39 is( 40 hud.jsterm.autocompletePopup.isOpen, 41 false, 42 "Setting the input value did not trigger the autocompletion" 43 ); 44 45 await navigateResultsAndCheckState(hud, { 46 direction: "previous", 47 expectedInfoText: "2 of 3 results", 48 expectedJsTermInputValue: jstermHistory[1], 49 }); 50 51 await navigateResultsAndCheckState(hud, { 52 direction: "previous", 53 expectedInfoText: "1 of 3 results", 54 expectedJsTermInputValue: jstermHistory[0], 55 }); 56 57 info( 58 "Check that we go back to the last matching item if we were at the first" 59 ); 60 await navigateResultsAndCheckState(hud, { 61 direction: "previous", 62 expectedInfoText: "3 of 3 results", 63 expectedJsTermInputValue: jstermHistory[2], 64 }); 65 66 await navigateResultsAndCheckState(hud, { 67 direction: "next", 68 expectedInfoText: "1 of 3 results", 69 expectedJsTermInputValue: jstermHistory[0], 70 }); 71 72 await navigateResultsAndCheckState(hud, { 73 direction: "next", 74 expectedInfoText: "2 of 3 results", 75 expectedJsTermInputValue: jstermHistory[1], 76 }); 77 78 await navigateResultsAndCheckState(hud, { 79 direction: "next", 80 expectedInfoText: "3 of 3 results", 81 expectedJsTermInputValue: jstermHistory[2], 82 }); 83 84 info( 85 "Check that trying to navigate when there's only 1 result does not throw" 86 ); 87 EventUtils.sendString("og"); 88 await waitFor( 89 () => getReverseSearchInfoElement(hud).textContent === "1 result" 90 ); 91 triggerPreviousResultShortcut(); 92 triggerNextResultShortcut(); 93 94 info("Check that trying to navigate when there's no result does not throw"); 95 EventUtils.sendString("g"); 96 await waitFor( 97 () => getReverseSearchInfoElement(hud).textContent === "No results" 98 ); 99 triggerPreviousResultShortcut(); 100 triggerNextResultShortcut(); 101 }); 102 103 async function navigateResultsAndCheckState( 104 hud, 105 { direction, expectedInfoText, expectedJsTermInputValue } 106 ) { 107 const onJsTermValueChanged = hud.jsterm.once("set-input-value"); 108 if (direction === "previous") { 109 triggerPreviousResultShortcut(); 110 } else { 111 triggerNextResultShortcut(); 112 } 113 await onJsTermValueChanged; 114 115 is(getInputValue(hud), expectedJsTermInputValue, "JsTerm has expected value"); 116 117 const infoElement = getReverseSearchInfoElement(hud); 118 is( 119 infoElement.textContent, 120 expectedInfoText, 121 "The reverse info has the expected text" 122 ); 123 is( 124 isReverseSearchInputFocused(hud), 125 true, 126 "reverse search input is still focused" 127 ); 128 } 129 130 function triggerPreviousResultShortcut() { 131 if (isMacOS) { 132 EventUtils.synthesizeKey("r", { ctrlKey: true }); 133 } else { 134 EventUtils.synthesizeKey("VK_F9"); 135 } 136 } 137 138 function triggerNextResultShortcut() { 139 if (isMacOS) { 140 EventUtils.synthesizeKey("s", { ctrlKey: true }); 141 } else { 142 EventUtils.synthesizeKey("VK_F9", { shiftKey: true }); 143 } 144 }