browser_webconsole_reverse_search_mouse_navigation.js (3535B)
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 mouse navigation. 6 7 "use strict"; 8 9 const TEST_URI = `data:text/html,<!DOCTYPE html><meta charset=utf8>Test reverse search`; 10 11 add_task(async function () { 12 const hud = await openNewTabAndConsole(TEST_URI); 13 14 const jstermHistory = [ 15 `document`, 16 `document 17 .querySelectorAll("span") 18 .forEach(console.log)`, 19 `Dog = "Snoopy"`, 20 ]; 21 22 const onLastMessage = waitForMessageByType(hud, `"Snoopy"`, ".result"); 23 for (const input of jstermHistory) { 24 execute(hud, input); 25 } 26 await onLastMessage; 27 28 await openReverseSearch(hud); 29 EventUtils.sendString("d"); 30 const infoElement = await waitFor(() => getReverseSearchInfoElement(hud)); 31 is( 32 infoElement.textContent, 33 "3 of 3 results", 34 "The reverse info has the expected text" 35 ); 36 37 is(getInputValue(hud), jstermHistory[2], "JsTerm has the expected input"); 38 is( 39 hud.jsterm.autocompletePopup.isOpen, 40 false, 41 "Setting the input value did not trigger the autocompletion" 42 ); 43 44 await navigateResultsAndCheckState(hud, { 45 direction: "previous", 46 expectedInfoText: "2 of 3 results", 47 expectedJsTermInputValue: jstermHistory[1], 48 }); 49 50 await navigateResultsAndCheckState(hud, { 51 direction: "previous", 52 expectedInfoText: "1 of 3 results", 53 expectedJsTermInputValue: jstermHistory[0], 54 }); 55 56 info( 57 "Check that we go back to the last matching item if we were at the first" 58 ); 59 await navigateResultsAndCheckState(hud, { 60 direction: "previous", 61 expectedInfoText: "3 of 3 results", 62 expectedJsTermInputValue: jstermHistory[2], 63 }); 64 65 await navigateResultsAndCheckState(hud, { 66 direction: "next", 67 expectedInfoText: "1 of 3 results", 68 expectedJsTermInputValue: jstermHistory[0], 69 }); 70 71 await navigateResultsAndCheckState(hud, { 72 direction: "next", 73 expectedInfoText: "2 of 3 results", 74 expectedJsTermInputValue: jstermHistory[1], 75 }); 76 77 await navigateResultsAndCheckState(hud, { 78 direction: "next", 79 expectedInfoText: "3 of 3 results", 80 expectedJsTermInputValue: jstermHistory[2], 81 }); 82 }); 83 84 async function navigateResultsAndCheckState( 85 hud, 86 { direction, expectedInfoText, expectedJsTermInputValue } 87 ) { 88 const onJsTermValueChanged = hud.jsterm.once("set-input-value"); 89 if (direction === "previous") { 90 clickPreviousButton(hud); 91 } else { 92 clickNextButton(hud); 93 } 94 await onJsTermValueChanged; 95 96 is(getInputValue(hud), expectedJsTermInputValue, "JsTerm has expected value"); 97 98 const infoElement = getReverseSearchInfoElement(hud); 99 is( 100 infoElement.textContent, 101 expectedInfoText, 102 "The reverse info has the expected text" 103 ); 104 is( 105 isReverseSearchInputFocused(hud), 106 true, 107 "reverse search input is still focused" 108 ); 109 } 110 111 function clickPreviousButton(hud) { 112 const reverseSearchElement = getReverseSearchElement(hud); 113 if (!reverseSearchElement) { 114 return; 115 } 116 const button = reverseSearchElement.querySelector( 117 ".search-result-button-prev" 118 ); 119 if (!button) { 120 return; 121 } 122 123 button.click(); 124 } 125 126 function clickNextButton(hud) { 127 const reverseSearchElement = getReverseSearchElement(hud); 128 if (!reverseSearchElement) { 129 return; 130 } 131 const button = reverseSearchElement.querySelector( 132 ".search-result-button-next" 133 ); 134 if (!button) { 135 return; 136 } 137 138 button.click(); 139 }