Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test UI popup window with TestFX (ReplaceStringView.java) #7654

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/test/java/org/jabref/gui/edit/ReplaceStringViewTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.jabref.gui.edit;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javafx.scene.control.*;
import javafx.stage.Stage;

import org.jabref.gui.LibraryTab;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

import org.testfx.framework.junit5.ApplicationTest;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ReplaceStringViewTest extends ApplicationTest {

private ReplaceStringView replaceStringView;
private LibraryTab libraryTab = mock(LibraryTab.class);
private BibDatabaseContext databaseContext = mock(BibDatabaseContext.class);
private DialogPane dialogPane;
private BibEntry entry;

@Override
public void start (Stage stage) throws Exception {
// Globals.prefs = JabRefPreferences.getInstance();
// Globals.startBackgroundTasks();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the lines of code that helped my test to run, because it needs Globals.prefs and Globals.fileUpdateMonitor to not be null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea, can you try if Globals.prefs = mock(JabRefPreferences.class) works?
for Globals.fileUpdateMonitor you can try to use the DummyFileUpdateMonitor

Copy link
Contributor Author

@ningxie1991 ningxie1991 Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Siedlerchr Yes it would work if we mock it up but I think the point is that we should not assign new value to Globals.prefs like this "Globals.prefs = ...." in the test. Because it's not like creating an instance of the object Globals and mock its prefs, for example, Globals globals = mock(Globals.class); globals.prefs = mock(JabRefPreferences.class);, but now we are directly setting the value of this global variable which is not ideal I think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JabRef/developers anyone an idea how to either extract the globals or how to test it?


entry = new BibEntry(StandardEntryType.Misc)
.withField(StandardField.AUTHOR, "Souti Chattopadhyay and Nicholas Nelson and Audrey Au and Natalia Morales and Christopher Sanchez and Rahul Pandita and Anita Sarma")
.withField(StandardField.TITLE, "A tale from the trenches")
.withField(StandardField.YEAR, "2020")
.withField(StandardField.DOI, "10.1145/3377811.3380330")
.withField(StandardField.SUBTITLE, "cognitive biases and software development")
.withCitationKey("abc");
List<BibEntry> entries = new ArrayList<>();
entries.add(entry);

when(libraryTab.getSelectedEntries()).thenReturn(entries);
when(libraryTab.getDatabase()).thenReturn(new BibDatabase(entries));
replaceStringView = new ReplaceStringView(libraryTab);
dialogPane = replaceStringView.getDialogPane();
stage.setScene(dialogPane.getScene());
stage.show();
}

@Test
public void testReplace() throws Exception {

// verify that the Year field text is of the initial value (2020)
assertEquals(Optional.of("2020"), entry.getField(StandardField.YEAR));

// enter find field
clickOn("#findField");
write("2020");

// enter replace field
clickOn("#replaceField");
write("2021");

// click on "Replace" button
for (ButtonType buttonType : dialogPane.getButtonTypes()) {
if (buttonType.getText().equals("Replace")) {
Button replaceButton = (Button) dialogPane.lookupButton(buttonType);
clickOn(replaceButton);
}
}

// verify that the Year field text has been replaced by new value (2021)
assertEquals(Optional.of("2021"), entry.getField(StandardField.YEAR));
}
}