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

Scrollingthroughentrylisttest #1361

Merged
merged 3 commits into from
May 24, 2016
Merged
Show file tree
Hide file tree
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
108 changes: 108 additions & 0 deletions src/integrationTest/java/net/sf/jabref/gui/AbstractUITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package net.sf.jabref.gui;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import net.sf.jabref.JabRefMain;

import org.assertj.swing.fixture.AbstractWindowFixture;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.fixture.JFileChooserFixture;
import org.assertj.swing.fixture.JTableFixture;
import org.assertj.swing.image.ScreenshotTaker;
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
import org.assertj.swing.timing.Pause;
import org.junit.Assert;

import static org.assertj.swing.finder.WindowFinder.findFrame;
import static org.assertj.swing.launcher.ApplicationLauncher.application;

public abstract class AbstractUITest extends AssertJSwingJUnitTestCase {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for extracting this


protected final static int SPEED_NORMAL = 50;

protected AWTExceptionHandler awtExceptionHandler;
protected FrameFixture mainFrame;

@Override
protected void onSetUp() {
awtExceptionHandler = new AWTExceptionHandler();
awtExceptionHandler.installExceptionDetectionInEDT();
application(JabRefMain.class).start();

robot().waitForIdle();

robot().settings().timeoutToFindSubMenu(1_000);
robot().settings().delayBetweenEvents(SPEED_NORMAL);

mainFrame = findFrame(JabRefFrame.class).withTimeout(10_000).using(robot());
robot().waitForIdle();
}

/**
* Returns the absolute Path of the given relative Path
* The backlashes are replaced with forwardslashes b/c assertJ can't type the former one on windows
* @param relativePath the relative path to the resource database
*/
protected String getAbsolutePath(String relativePath) {
final URL resource = this.getClass().getClassLoader().getResource(relativePath);
try {
return Paths.get(resource.toURI()).toAbsolutePath().toString().replace("\\", "/");
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}

/**
* opens a database and gives JabRef a second to open it before proceeding
*/
protected void importBibIntoNewDatabase(String path) {
mainFrame.menuItemWithPath("File", "Import into new database").click();
JFileChooserFixture openFileDialog = mainFrame.fileChooser();
robot().settings().delayBetweenEvents(1);
openFileDialog.fileNameTextBox().enterText(path);
openFileDialog.approve();
Pause.pause(1_000);
}

protected void exitJabRef() {
mainFrame.menuItemWithPath("File", "Quit").click();
awtExceptionHandler.assertNoExceptions();
}

protected void newDatabase() {
mainFrame.menuItemWithPath("File", "New BibTeX database").click();
}

protected void closeDatabase() {
mainFrame.menuItemWithPath("File", "Close database").click();
}

protected void takeScreenshot(AbstractWindowFixture<?, ?, ?> dialog, String filename) throws IOException {
ScreenshotTaker screenshotTaker = new ScreenshotTaker();
Path folder = Paths.get("build", "screenshots");
// Create build/srceenshots folder if not present
if (!Files.exists(folder)) {
Files.createDirectory(folder);
}
Path file = folder.resolve(filename + ".png").toAbsolutePath();
// Delete already present file
if (Files.exists(file)) {
Files.delete(file);
}
screenshotTaker.saveComponentAsPng(dialog.target(), file.toString());
}

protected void assertColumnValue(JTableFixture table, int rowIndex, int columnIndex, String selectionValue){
String[][] tableContent;
tableContent = table.contents();

String value = tableContent[rowIndex][columnIndex];
Assert.assertEquals(value, selectionValue);
}
}
64 changes: 64 additions & 0 deletions src/integrationTest/java/net/sf/jabref/gui/EntryTableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.sf.jabref.gui;

import java.awt.event.KeyEvent;
import java.util.regex.Pattern;

import org.assertj.swing.fixture.JTableCellFixture;
import org.assertj.swing.fixture.JTableFixture;
import org.junit.Assert;
import org.junit.Test;

/**
* Specific Use-Case:
* I import a database. Then I doubleclick on the first entry in the table to open the entry editor.
* Then I click on the first entry again, and scroll through all of the lists entries, without having to click
* on the table again.
*/
public class EntryTableTest extends AbstractUITest{

private final static int SCROLL_ACTION_EXECUTION = 5;
private final static String TEST_FILE_NAME = "testbib/testjabref.bib";
private final static int DOWN = KeyEvent.VK_DOWN;
private final static int UP = KeyEvent.VK_UP;
private final static int TITLE_COLUMN_INDEX = 5;

@Test
public void scrollThroughEntryList() {
String path = getAbsolutePath(TEST_FILE_NAME);

importBibIntoNewDatabase(path);

JTableFixture entryTable = mainFrame.table();

//use a pattern from the first row to select it since it seems to be the best way to get the cell object
Pattern pattern = Pattern.compile("256.*");
JTableCellFixture firstCell = entryTable.cell(pattern);
Copy link
Contributor

Choose a reason for hiding this comment

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

would be nice if you would found out a way to always select the first cell and that it not depends on the content.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried using entryTable().selectRows(0) but that did not set the focus right. The pattern seems to be the easiest way to get the cell object.


entryTable.selectRows(0).doubleClick();
//delay has to be shortened so that double click is recognized
robot().settings().delayBetweenEvents(0);
firstCell.doubleClick();
robot().settings().delayBetweenEvents(SPEED_NORMAL);

firstCell.click();
//is the first table entry selected?
assertColumnValue(entryTable, 0, TITLE_COLUMN_INDEX, entryTable.selectionValue());

//go throught the table and check if the entry with the correct index is selected
for (int i=0; i < SCROLL_ACTION_EXECUTION; i++) {
robot().pressAndReleaseKey(DOWN);
Assert.assertTrue(entryTable.selectionValue() != null);
assertColumnValue(entryTable, i+1, TITLE_COLUMN_INDEX, entryTable.selectionValue());
}
//do the same going up again
for (int i = SCROLL_ACTION_EXECUTION; i > 0; i--) {
robot().pressAndReleaseKey(UP);
Assert.assertTrue(entryTable.selectionValue() != null);
Copy link
Contributor

Choose a reason for hiding this comment

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

can you assert that the selected value is the actual number X from the bib file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it sufficient to check if the value in the selected row is the same as in the row with the next index?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you need to be sure that the next entry is selected, not that any entry is selected.

assertColumnValue(entryTable, i-1, TITLE_COLUMN_INDEX, entryTable.selectionValue());
}

closeDatabase();
exitJabRef();
}

}
104 changes: 28 additions & 76 deletions src/integrationTest/java/net/sf/jabref/gui/GUITest.java
Original file line number Diff line number Diff line change
@@ -1,136 +1,103 @@
package net.sf.jabref.gui;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.swing.JButton;

import net.sf.jabref.JabRefMain;
import net.sf.jabref.gui.dbproperties.DatabasePropertiesDialog;
import net.sf.jabref.gui.preftabs.PreferencesDialog;

import org.assertj.swing.core.GenericTypeMatcher;
import org.assertj.swing.dependency.jsr305.Nonnull;
import org.assertj.swing.fixture.AbstractWindowFixture;
import org.assertj.swing.fixture.DialogFixture;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.image.ScreenshotTaker;
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
import org.assertj.swing.timing.Pause;
import org.junit.Ignore;
import org.junit.Test;

import static org.assertj.swing.finder.WindowFinder.findDialog;
import static org.assertj.swing.finder.WindowFinder.findFrame;
import static org.assertj.swing.launcher.ApplicationLauncher.application;

public class GUITest extends AssertJSwingJUnitTestCase {
private AWTExceptionHandler awtExceptionHandler;

@Override
protected void onSetUp() {
awtExceptionHandler = new AWTExceptionHandler();
awtExceptionHandler.installExceptionDetectionInEDT();
application(JabRefMain.class).start();

robot().waitForIdle();

robot().settings().timeoutToFindSubMenu(1_000);
robot().settings().delayBetweenEvents(50);
}
public class GUITest extends AbstractUITest {

@Test
public void testExit() {
FrameFixture mainFrame = findFrame(JabRefFrame.class).withTimeout(10_000).using(robot());
Pause.pause(1_000);
exitJabRef(mainFrame);
}

private void exitJabRef(FrameFixture mainFrame) {
mainFrame.menuItemWithPath("File", "Quit").click();
awtExceptionHandler.assertNoExceptions();
exitJabRef();
}

@Test
public void testNewFile() {
FrameFixture mainFrame = findFrame(JabRefFrame.class).withTimeout(10_000).using(robot());
newDatabase(mainFrame);

mainFrame.menuItemWithPath("File", "Close database").click();
exitJabRef(mainFrame);
}

private void newDatabase(FrameFixture mainFrame) {
mainFrame.menuItemWithPath("File", "New BibTeX database").click();
newDatabase();
closeDatabase();
exitJabRef();
}

@Test
public void testCreateBibtexEntry() throws IOException {
FrameFixture mainFrame = findFrame(JabRefFrame.class).withTimeout(10_000).using(robot());

newDatabase(mainFrame);
newDatabase();

mainFrame.menuItemWithPath("BibTeX", "New entry...").click();
findDialog(EntryTypeDialog.class).withTimeout(10_000).using(robot())
.button(new GenericTypeMatcher<JButton>(JButton.class) {

@Override
protected boolean isMatching(@Nonnull JButton jButton) {
return "Book".equals(jButton.getText());
}
}).click();
takeScreenshot(mainFrame, "MainWindowWithOneDatabase");
exitJabRef(mainFrame);
}

@Ignore
@Test
public void testOpenAndSavePreferences() throws IOException {
FrameFixture mainFrame = findFrame(JabRefFrame.class).withTimeout(10_000).using(robot());
mainFrame.menuItemWithPath("Options", "Preferences").click();

robot().waitForIdle();

DialogFixture preferencesDialog = findDialog(PreferencesDialog.class).withTimeout(10_000).using(robot());
takeScreenshot(preferencesDialog, "PreferencesDialog");
preferencesDialog.button(new GenericTypeMatcher<JButton>(JButton.class) {

@Override
protected boolean isMatching(@Nonnull JButton jButton) {
return "OK".equals(jButton.getText());
}
}).click();

exitJabRef(mainFrame);
exitJabRef();
}

/**
* tests different buttons
* sometimes this test clicks some buttons twice to reverse their effect and leaves JabRef as it was before
*/
@Test
public void testViewChanges() {
FrameFixture mainFrame = findFrame(JabRefFrame.class).withTimeout(10_000).using(robot());

newDatabase(mainFrame);
newDatabase();

mainFrame.menuItemWithPath("View", "Increase table font size").click();
mainFrame.menuItemWithPath("View", "Decrease table font size").click();

mainFrame.menuItemWithPath("View", "Web search").click();
mainFrame.menuItemWithPath("View", "Web search").click();

mainFrame.menuItemWithPath("View", "Toggle groups interface").click();
mainFrame.menuItemWithPath("View", "Toggle groups interface").click();

mainFrame.menuItemWithPath("View", "Toggle entry preview").click();
mainFrame.menuItemWithPath("View", "Toggle entry preview").click();

mainFrame.menuItemWithPath("View", "Switch preview layout").click();
mainFrame.menuItemWithPath("View", "Switch preview layout").click();

mainFrame.menuItemWithPath("View", "Hide/show toolbar").click();
mainFrame.menuItemWithPath("View", "Hide/show toolbar").click();

mainFrame.menuItemWithPath("View", "Focus entry table").click();

newDatabase(mainFrame);
mainFrame.menuItemWithPath("File", "Close database").click();
exitJabRef(mainFrame);
closeDatabase();
exitJabRef();
}

@Test
public void testDatabasePropertiesDialog() throws IOException {

FrameFixture mainFrame = findFrame(JabRefFrame.class).withTimeout(10_000).using(robot());
newDatabase(mainFrame);
newDatabase();

mainFrame.menuItemWithPath("File", "Database properties").click();

Expand All @@ -139,28 +106,13 @@ public void testDatabasePropertiesDialog() throws IOException {
DialogFixture databasePropertiesDialog = findDialog(DatabasePropertiesDialog.class).withTimeout(10_000).using(robot());
takeScreenshot(databasePropertiesDialog, "DatabasePropertiesDialog");
databasePropertiesDialog.button(new GenericTypeMatcher<JButton>(JButton.class) {

@Override
protected boolean isMatching(@Nonnull JButton jButton) {
return "OK".equals(jButton.getText());
}
}).click();

exitJabRef(mainFrame);
}

private void takeScreenshot(AbstractWindowFixture<?, ?, ?> dialog, String filename) throws IOException {
ScreenshotTaker screenshotTaker = new ScreenshotTaker();
Path folder = Paths.get("build", "screenshots");
// Create build/srceenshots folder if not present
if (!Files.exists(folder)) {
Files.createDirectory(folder);
}
Path file = folder.resolve(filename + ".png").toAbsolutePath();
// Delete already present file
if (Files.exists(file)) {
Files.delete(file);
}
screenshotTaker.saveComponentAsPng(dialog.target(), file.toString());
closeDatabase();
exitJabRef();
}
}
Loading