diff --git a/src/main/java/org/jabref/gui/help/VersionWorker.java b/src/main/java/org/jabref/gui/help/VersionWorker.java index b4760f58c9b..1636ca85924 100644 --- a/src/main/java/org/jabref/gui/help/VersionWorker.java +++ b/src/main/java/org/jabref/gui/help/VersionWorker.java @@ -52,7 +52,7 @@ public VersionWorker(Version installedVersion, * Returns a newer version excluding any non-stable versions, except if the installed one is unstable too. If no * newer version was found, then an empty optional is returned. */ - private Optional getNewVersion() throws IOException { + protected Optional getNewVersion() throws IOException { List availableVersions = Version.getAllAvailableVersions(); return installedVersion.shouldBeUpdatedTo(availableVersions); } @@ -87,7 +87,7 @@ private void showConnectionError(Exception exception, boolean manualExecution) { * Prints up-to-date to the status bar (and shows a dialog it was executed manually) if there is now new version. * Shows a "New Version" Dialog to the user if there is. */ - private void showUpdateInfo(Optional newerVersion, boolean manualExecution) { + protected void showUpdateInfo(Optional newerVersion, boolean manualExecution) { // no new version could be found, only respect the ignored version on automated version checks if (newerVersion.isEmpty() || (newerVersion.get().equals(internalPreferences.getIgnoredVersion()) && !manualExecution)) { if (manualExecution) { diff --git a/src/test/java/org/jabref/gui/help/VersionWorkerTest.java b/src/test/java/org/jabref/gui/help/VersionWorkerTest.java new file mode 100644 index 00000000000..d281db6d25d --- /dev/null +++ b/src/test/java/org/jabref/gui/help/VersionWorkerTest.java @@ -0,0 +1,77 @@ +package org.jabref.gui.help; + +import java.io.IOException; +import java.util.List; +import java.util.Optional; + +import javafx.application.Platform; +import javafx.stage.Stage; + +import org.jabref.gui.DialogService; +import org.jabref.gui.util.TaskExecutor; +import org.jabref.logic.util.Version; +import org.jabref.preferences.InternalPreferences; +import org.jabref.testutils.category.GUITest; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.testfx.framework.junit5.ApplicationExtension; +import org.testfx.framework.junit5.Start; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@GUITest +@ExtendWith(ApplicationExtension.class) +class VersionWorkerTest { + + InternalPreferences internalPreferences; + DialogService dialogService; + VersionWorker versionWorker; + + @Start + void onStart(Stage stage) { + // Needed to init JavaFX thread + stage.show(); + } + + @BeforeEach + void setUp() { + internalPreferences = mock(InternalPreferences.class); + dialogService = mock(DialogService.class, Answers.RETURNS_DEEP_STUBS); + versionWorker = new VersionWorker(Version.parse("1.0.0"), + mock(DialogService.class, Answers.RETURNS_DEEP_STUBS), + mock(TaskExecutor.class), + internalPreferences); + } + + @Test + void getNewVersionTest() throws IOException { + List availVersions = List.of(Version.parse("1.0.0"), Version.parse("2.0.0")); + try (MockedStatic version = Mockito.mockStatic(Version.class)) { + version.when(Version::getAllAvailableVersions) + .thenReturn(availVersions); + + assertEquals("2.0.0", versionWorker.getNewVersion().map(Version::toString).orElse("")); + } + } + + @Test + void setIgnoredVersionTest() { + Platform.runLater(() -> { + versionWorker.showUpdateInfo(Optional.of(Version.parse("1.0.0")), true); + + when(dialogService.showCustomDialogAndWait(any())).thenReturn(Optional.of(false)); + + verify(internalPreferences).setIgnoredVersion(eq(Version.parse("2.0.0"))); + }); + } +}