Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed May 24, 2024
1 parent 1a572c9 commit 7ec1dbc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,8 @@ public OptionalObjectProperty<KeyBindingViewModel> selectedKeyBindingProperty()
public ObjectProperty<KeyBindingViewModel> rootKeyBindingProperty() {
return rootKeyBinding;
}

public KeyBindingRepository getKeyBindingRepository() {
return keyBindingRepository;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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));

Expand Down Expand Up @@ -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());
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 7ec1dbc

Please sign in to comment.