|
| 1 | +package org.jabref.gui.search; |
| 2 | + |
| 3 | +import java.util.EnumSet; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import javafx.scene.Scene; |
| 7 | +import javafx.scene.control.TextInputControl; |
| 8 | +import javafx.scene.layout.HBox; |
| 9 | +import javafx.stage.Stage; |
| 10 | + |
| 11 | +import org.jabref.gui.DialogService; |
| 12 | +import org.jabref.gui.JabRefFrame; |
| 13 | +import org.jabref.gui.StateManager; |
| 14 | +import org.jabref.gui.undo.CountingUndoManager; |
| 15 | +import org.jabref.gui.util.DefaultTaskExecutor; |
| 16 | +import org.jabref.model.database.BibDatabaseContext; |
| 17 | +import org.jabref.model.search.rules.SearchRules; |
| 18 | +import org.jabref.preferences.PreferencesService; |
| 19 | +import org.jabref.preferences.SearchPreferences; |
| 20 | +import org.jabref.testutils.category.GUITest; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.mockito.Answers; |
| 25 | +import org.testfx.api.FxRobot; |
| 26 | +import org.testfx.framework.junit5.ApplicationExtension; |
| 27 | +import org.testfx.framework.junit5.Start; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | +import static org.mockito.Mockito.when; |
| 33 | + |
| 34 | +@GUITest |
| 35 | +@ExtendWith(ApplicationExtension.class) |
| 36 | +public class GlobalSearchBarTest { |
| 37 | + private Stage stage; |
| 38 | + private Scene scene; |
| 39 | + private HBox hBox; |
| 40 | + |
| 41 | + private GlobalSearchBar searchBar; |
| 42 | + private StateManager stateManager; |
| 43 | + |
| 44 | + @Start |
| 45 | + public void onStart(Stage stage) { |
| 46 | + SearchPreferences searchPreferences = mock(SearchPreferences.class); |
| 47 | + when(searchPreferences.getSearchFlags()).thenReturn(EnumSet.noneOf(SearchRules.SearchFlags.class)); |
| 48 | + PreferencesService prefs = mock(PreferencesService.class, Answers.RETURNS_DEEP_STUBS); |
| 49 | + when(prefs.getSearchPreferences()).thenReturn(searchPreferences); |
| 50 | + |
| 51 | + stateManager = new StateManager(); |
| 52 | + // Need for active database, otherwise the searchField will be disabled |
| 53 | + stateManager.setActiveDatabase(new BibDatabaseContext()); |
| 54 | + |
| 55 | + // Instantiate GlobalSearchBar class, so the change listener is registered |
| 56 | + searchBar = new GlobalSearchBar( |
| 57 | + mock(JabRefFrame.class), |
| 58 | + stateManager, |
| 59 | + prefs, |
| 60 | + mock(CountingUndoManager.class), |
| 61 | + mock(DialogService.class) |
| 62 | + ); |
| 63 | + |
| 64 | + hBox = new HBox(searchBar); |
| 65 | + |
| 66 | + scene = new Scene(hBox, 400, 400); |
| 67 | + this.stage = stage; |
| 68 | + stage.setScene(scene); |
| 69 | + |
| 70 | + stage.show(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void recordingSearchQueriesOnFocusLostOnly(FxRobot robot) throws InterruptedException { |
| 75 | + stateManager.clearSearchHistory(); |
| 76 | + String searchQuery = "Smith"; |
| 77 | + // Track the node, that the search query will be typed into |
| 78 | + TextInputControl searchField = robot.lookup("#searchField").queryTextInputControl(); |
| 79 | + |
| 80 | + // The focus is on searchField node, as we click on the search box |
| 81 | + var searchFieldRoboto = robot.clickOn(searchField); |
| 82 | + for (char c : searchQuery.toCharArray()) { |
| 83 | + searchFieldRoboto.write(String.valueOf(c)); |
| 84 | + Thread.sleep(401); |
| 85 | + assertTrue(stateManager.getWholeSearchHistory().isEmpty()); |
| 86 | + } |
| 87 | + |
| 88 | + // Set the focus to another node to trigger the listener and finally record the query. |
| 89 | + DefaultTaskExecutor.runAndWaitInJavaFXThread(() -> hBox.requestFocus()); |
| 90 | + List<String> lastSearchHistory = stateManager.getWholeSearchHistory().stream().toList(); |
| 91 | + |
| 92 | + assertEquals(List.of("Smith"), lastSearchHistory); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void emptyQueryIsNotRecorded(FxRobot robot) { |
| 97 | + stateManager.clearSearchHistory(); |
| 98 | + String searchQuery = ""; |
| 99 | + TextInputControl searchField = robot.lookup("#searchField").queryTextInputControl(); |
| 100 | + |
| 101 | + var searchFieldRoboto = robot.clickOn(searchField); |
| 102 | + searchFieldRoboto.write(searchQuery); |
| 103 | + |
| 104 | + DefaultTaskExecutor.runAndWaitInJavaFXThread(() -> hBox.requestFocus()); |
| 105 | + List<String> lastSearchHistory = stateManager.getWholeSearchHistory().stream().toList(); |
| 106 | + |
| 107 | + assertEquals(List.of(), lastSearchHistory); |
| 108 | + } |
| 109 | +} |
0 commit comments