From 7ec1dbc22490039c15e4847a5659448c1f56572c Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Fri, 24 May 2024 22:40:50 +0200 Subject: [PATCH] Fix tests --- .../keybindings/KeyBindingsTabViewModel.java | 4 ++ .../gui/keyboard/KeyBindingsTabModelTest.java | 8 ++-- .../keybindings/KeyBindingViewModelTest.java | 42 +++++++++++-------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/jabref/gui/preferences/keybindings/KeyBindingsTabViewModel.java b/src/main/java/org/jabref/gui/preferences/keybindings/KeyBindingsTabViewModel.java index 47682335513..2cff10d5e7a 100644 --- a/src/main/java/org/jabref/gui/preferences/keybindings/KeyBindingsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/keybindings/KeyBindingsTabViewModel.java @@ -127,4 +127,8 @@ public OptionalObjectProperty selectedKeyBindingProperty() public ObjectProperty rootKeyBindingProperty() { return rootKeyBinding; } + + public KeyBindingRepository getKeyBindingRepository() { + return keyBindingRepository; + } } diff --git a/src/test/java/org/jabref/gui/keyboard/KeyBindingsTabModelTest.java b/src/test/java/org/jabref/gui/keyboard/KeyBindingsTabModelTest.java index 632e728d03a..a3c71d698b7 100644 --- a/src/test/java/org/jabref/gui/keyboard/KeyBindingsTabModelTest.java +++ b/src/test/java/org/jabref/gui/keyboard/KeyBindingsTabModelTest.java @@ -60,7 +60,7 @@ void specialKeysValidKeyBindingIsSaved() { assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.IMPORT_INTO_NEW_DATABASE, shortcutKeyEvent)); model.setNewBindingForCurrent(shortcutKeyEvent); - KeyCombination combination = KeyCombination.keyCombination(keyBindingRepository.get(KeyBinding.IMPORT_INTO_NEW_DATABASE).get()); + KeyCombination combination = KeyCombination.keyCombination(model.getKeyBindingRepository().get(KeyBinding.IMPORT_INTO_NEW_DATABASE).get()); assertTrue(KeyBindingRepository.checkKeyCombinationEquality(combination, shortcutKeyEvent)); @@ -86,7 +86,7 @@ void randomNewKeyKeyBindingInRepository() { assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.CLEANUP, shortcutKeyEvent)); model.setNewBindingForCurrent(shortcutKeyEvent); - KeyCombination combination = KeyCombination.keyCombination(keyBindingRepository.get(KeyBinding.CLEANUP).get()); + KeyCombination combination = KeyCombination.keyCombination(model.getKeyBindingRepository().get(KeyBinding.CLEANUP).get()); assertTrue(KeyBindingRepository.checkKeyCombinationEquality(combination, shortcutKeyEvent)); @@ -142,8 +142,7 @@ void setAllKeyBindingsToDefault() { @Test void closeEntryEditorCloseEntryKeybinding() { - KeyBindingViewModel viewModel = setKeyBindingViewModel(KeyBinding.CLOSE); - model.selectedKeyBindingProperty().set(Optional.of(viewModel)); + setKeyBindingViewModel(KeyBinding.CLOSE); KeyEvent closeEditorEvent = new KeyEvent(KeyEvent.KEY_PRESSED, "", "", KeyCode.ESCAPE, false, false, false, false); assertEquals(KeyBinding.CLOSE.getDefaultKeyBinding(), KeyCode.ESCAPE.getName()); @@ -159,7 +158,6 @@ void setSingleKeyBindingToDefault() { assumeFalse(OS.OS_X); KeyBindingViewModel viewModel = setKeyBindingViewModel(KeyBinding.ABBREVIATE); - model.selectedKeyBindingProperty().set(Optional.of(viewModel)); KeyEvent shortcutKeyEvent = new KeyEvent(KeyEvent.KEY_PRESSED, "C", "C", KeyCode.C, true, true, true, false); assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent)); diff --git a/src/test/java/org/jabref/gui/preferences/keybindings/KeyBindingViewModelTest.java b/src/test/java/org/jabref/gui/preferences/keybindings/KeyBindingViewModelTest.java index 54371690bd7..fbd0b36572f 100644 --- a/src/test/java/org/jabref/gui/preferences/keybindings/KeyBindingViewModelTest.java +++ b/src/test/java/org/jabref/gui/preferences/keybindings/KeyBindingViewModelTest.java @@ -10,37 +10,43 @@ import org.jabref.gui.keyboard.KeyBindingRepository; import org.jabref.preferences.PreferencesService; +import com.airhacks.afterburner.injection.Injector; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class KeyBindingViewModelTest { - @Test - void resetToDefault() { - // Set new key binding - KeyBindingRepository keyBindingRepository = new KeyBindingRepository(); - KeyBindingsTabViewModel keyBindingsTabViewModel = new KeyBindingsTabViewModel(keyBindingRepository, mock(DialogService.class), mock(PreferencesService.class)); - KeyBinding binding = KeyBinding.ABBREVIATE; + @Test + void resetToDefault() { + // Set new key binding + KeyBindingRepository keyBindingRepository = new KeyBindingRepository(); + PreferencesService preferencesService = mock(PreferencesService.class); + when(preferencesService.getKeyBindingRepository()).thenReturn(keyBindingRepository); + Injector.setModelOrService(PreferencesService.class, preferencesService); - KeyBindingViewModel viewModel = new KeyBindingViewModel(keyBindingRepository, binding, binding.getDefaultKeyBinding()); - keyBindingsTabViewModel.selectedKeyBindingProperty().set(Optional.of(viewModel)); + KeyBindingsTabViewModel keyBindingsTabViewModel = new KeyBindingsTabViewModel(keyBindingRepository, mock(DialogService.class), preferencesService); + KeyBinding binding = KeyBinding.ABBREVIATE; - KeyEvent shortcutKeyEvent = new KeyEvent(KeyEvent.KEY_PRESSED, "F1", "F1", KeyCode.F1, true, false, false, - false); + KeyBindingViewModel viewModel = new KeyBindingViewModel(keyBindingRepository, binding, binding.getDefaultKeyBinding()); + keyBindingsTabViewModel.selectedKeyBindingProperty().set(Optional.of(viewModel)); - assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent)); + KeyEvent shortcutKeyEvent = new KeyEvent(KeyEvent.KEY_PRESSED, "F1", "F1", KeyCode.F1, true, false, false, + false); - keyBindingsTabViewModel.setNewBindingForCurrent(shortcutKeyEvent); - keyBindingsTabViewModel.storeSettings(); + assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent)); - assertTrue(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent)); + keyBindingsTabViewModel.setNewBindingForCurrent(shortcutKeyEvent); + keyBindingsTabViewModel.storeSettings(); - // Reset to default - viewModel.resetToDefault(); + assertTrue(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent)); - assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent)); - } + // Reset to default + viewModel.resetToDefault(); + + assertFalse(keyBindingRepository.checkKeyCombinationEquality(KeyBinding.ABBREVIATE, shortcutKeyEvent)); + } }