Skip to content

Commit

Permalink
Add UI test (JabRef#7452)
Browse files Browse the repository at this point in the history
Add test ensuring the UI warns the user if they download a linked HTML file (i.e. a web page).
  • Loading branch information
grundb committed Mar 4, 2021
1 parent a4c3008 commit 24669c8
Showing 1 changed file with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import java.util.Collections;
import java.util.Optional;
import java.util.TreeSet;
import java.util.regex.Pattern;

import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;

import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.externalfiletype.StandardExternalFileType;
import org.jabref.gui.util.BackgroundTask;
Expand All @@ -24,6 +26,7 @@
import org.jabref.model.entry.LinkedFile;
import org.jabref.preferences.FilePreferences;

import org.jabref.preferences.JabRefPreferences;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -32,13 +35,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

class LinkedFileViewModelTest {

Expand All @@ -62,7 +60,9 @@ void setUp(@TempDir Path tempFolder) throws Exception {

when(externalFileType.getExternalFileTypeSelection()).thenReturn(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()));
when(externalFileType.getExternalFileTypeByMimeType("application/pdf")).thenReturn(Optional.of(StandardExternalFileType.PDF));
when(externalFileType.getExternalFileTypeByMimeType(contains("text/html"))).thenReturn(Optional.of(StandardExternalFileType.URL));
when(externalFileType.getExternalFileTypeByExt("pdf")).thenReturn(Optional.of(StandardExternalFileType.PDF));
when(externalFileType.getExternalFileTypeByExt("html")).thenReturn(Optional.of(StandardExternalFileType.URL));
tempFile = tempFolder.resolve("temporaryFile");
Files.createFile(tempFile);
}
Expand Down Expand Up @@ -151,6 +151,31 @@ void deleteWhenDialogCancelledReturnsFalseAndDoesNotRemoveFile() {
assertTrue(Files.exists(tempFile));
}

// Structure taken from deleteWhenDialogCancelledReturnsFalseAndDoesNotRemoveFile method
@Test
void downloadHtmlFileCausesWarningDisplay() throws MalformedURLException {
Globals.prefs = JabRefPreferences.getInstance(); // required for task execution

// From BibDatabaseContextTest::setUp
when(filePreferences.shouldStoreFilesRelativeToBib()).thenReturn(true);

when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); // used in other tests in this class
when(filePreferences.getFileDirectoryPattern()).thenReturn("[entrytype]"); // used in movesFileWithFileDirPattern at MoveFilesCleanupTest.java

databaseContext.setDatabasePath(tempFile);

URL url = new URL( "https://www.google.com/");
String fileType = StandardExternalFileType.URL.getName();
linkedFile = new LinkedFile(url, fileType);

LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, xmpPreferences, filePreferences, externalFileType);

viewModel.download();

Pattern warningPattern = Pattern.compile(".*HTML.*", Pattern.CASE_INSENSITIVE);
verify(dialogService, atLeastOnce()).notify(matches(warningPattern));
}

void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException {
linkedFile = new LinkedFile(new URL("http://arxiv.org/pdf/1207.0408v1"), "");

Expand Down

0 comments on commit 24669c8

Please sign in to comment.