Skip to content

Instantly share code, notes, and snippets.

@timmolderez
Created November 4, 2016 07:52
Show Gist options
  • Save timmolderez/64359c6671a6d454d789dcb6141ec93b to your computer and use it in GitHub Desktop.
Save timmolderez/64359c6671a6d454d789dcb6141ec93b to your computer and use it in GitHub Desktop.
Basic framework to perform Swing tests for the Pixelitor application
package pixelitor;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JMenuItem;
import org.assertj.swing.core.BasicRobot;
import org.assertj.swing.core.GenericTypeMatcher;
import org.assertj.swing.core.Robot;
import org.assertj.swing.finder.JFileChooserFinder;
import org.assertj.swing.finder.JOptionPaneFinder;
import org.assertj.swing.finder.WindowFinder;
import org.assertj.swing.fixture.DialogFixture;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.fixture.JFileChooserFixture;
import org.assertj.swing.fixture.JMenuItemFixture;
import org.assertj.swing.fixture.JOptionPaneFixture;
import org.assertj.swing.launcher.ApplicationLauncher;
import org.fest.util.Files;
import com.bric.util.JVM;
import pixelitor.gui.ImageComponents;
import pixelitor.gui.PixelitorWindow;
import pixelitor.tools.ShapesAction;
import pixelitor.utils.Utils;
import pixelitor.utils.test.PixelitorEventListener;
public class SwingTest {
private static final int ROBOT_DELAY_MILLIS_DEFAULT = 100;
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");
private Robot robot;
private FrameFixture pw;
private static File baseTestingDir;
private static File inputDir;
private static File batchResizeOutputDir;
private static File batchFilterOutputDir;
public void setUp() {
robot = BasicRobot.robotWithNewAwtHierarchy();
ApplicationLauncher
.application("pixelitor.Pixelitor")
.withArgs((new File(inputDir, "a.jpg")).getPath())
.start();
new PixelitorEventListener().register();
pw = WindowFinder.findFrame("frame0")
.withTimeout(15, SECONDS)
.using(robot);
PixelitorWindow.getInstance().setLocation(0, 0);
}
public static void main(String[] args) {
long startMillis = System.currentTimeMillis();
initialize(args);
SwingTest test = new SwingTest();
test.setUp();
test.testAll();
long totalTimeMillis = System.currentTimeMillis() - startMillis;
System.out.printf("AssertJSwingTest: finished at %s after %s, exiting in 5 seconds",
DATE_FORMAT.format(new Date()),
Utils.formatMillis(totalTimeMillis));
Utils.sleep(5, SECONDS);
test.exit();
}
private void testAll() {
setupRobotSpeed();
System.out.printf("AssertJSwingTest: started at %s%n", DATE_FORMAT.format(new Date()));
// testTools();
testFileMenu();
// testAutoPaint();
// testEditMenu();
// testImageMenu();
// testFilters();
//
// testViewMenu();
// testHelpMenu();
// testColors();
//
// testLayers();
}
private void testFileMenu() {
testNewImage();
// testSaveUnnamed();
// closeOneOfTwo();
// testFileOpen();
// closeOneOfTwo();
// testExportOptimizedJPEG();
// testExportOpenRaster();
// testExportLayerAnimation();
// testExportTweeningAnimation();
// testBatchResize();
// testBatchFilter();
// testExportLayerToPNG();
// testScreenCapture();
// testCloseAll();
}
private void testNewImage() {
findMenuItemByText("New Image...").click();
DialogFixture newImageDialog = findDialogByTitle("New Image");
newImageDialog.textBox("widthTF").deleteText().enterText("611");
newImageDialog.textBox("heightTF").deleteText().enterText("411");
newImageDialog.button("ok").click();
}
private static void initialize(String[] args) {
processCLArguments(args);
checkTestingDirs();
Utils.checkThatAssertionsAreEnabled();
}
private static void processCLArguments(String[] args) {
if (args.length != 1) {
System.err.println("Required argument: <base testing directory>");
System.exit(1);
}
baseTestingDir = new File(args[0]);
if (!baseTestingDir.exists()) {
System.err.printf("Base testing dir '%s' does not exist.%n",
baseTestingDir.getAbsolutePath());
System.exit(1);
}
inputDir = new File(baseTestingDir, "input");
batchResizeOutputDir = new File(baseTestingDir, "batch_resize_output");
batchFilterOutputDir = new File(baseTestingDir, "batch_filter_output");
}
private static void checkTestingDirs() {
assertThat(baseTestingDir).exists().isDirectory();
assertThat(inputDir).exists().isDirectory();
assertThat(batchResizeOutputDir).exists().isDirectory();
assertThat(batchFilterOutputDir).exists().isDirectory();
assertThat(Files.fileNamesIn(batchResizeOutputDir.getPath(), false)).isEmpty();
assertThat(Files.fileNamesIn(batchFilterOutputDir.getPath(), false)).isEmpty();
}
private void openFile(String fileName) {
JFileChooserFixture openDialog;
findMenuItemByText("Open...").click();
openDialog = JFileChooserFinder.findFileChooser("open").using(robot);
openDialog.selectFile(new File(inputDir, fileName));
openDialog.approve();
}
private void closeAll() {
runMenuCommand("Close All");
// close all warnings
boolean warnings = true;
while (warnings) {
try {
JOptionPaneFixture pane = findJOptionPane();
// click "Don't Save"
pane.button(new GenericTypeMatcher<JButton>(JButton.class) {
@Override
protected boolean isMatching(JButton button) {
return button.getText().equals("Don't Save");
}
}).click();
} catch (Exception e) { // no more JOptionPane found
warnings = false;
}
}
checkNumOpenImagesIs(0);
}
private static void checkNumOpenImagesIs(int expected) {
assertThat(ImageComponents.getNrOfOpenImages()).isEqualTo(expected);
}
private JOptionPaneFixture findJOptionPane() {
return JOptionPaneFinder.findOptionPane().withTimeout(10, SECONDS).using(robot);
}
private void runMenuCommand(String text) {
findMenuItemByText(text).click();
}
private JMenuItemFixture findMenuItemByText(String guiName) {
return new JMenuItemFixture(robot, robot.finder().find(new GenericTypeMatcher<JMenuItem>(JMenuItem.class) {
@Override
protected boolean isMatching(JMenuItem menuItem) {
return guiName.equals(menuItem.getText());
}
}));
}
private DialogFixture findDialogByTitle(String title) {
return new DialogFixture(robot, robot.finder().find(new GenericTypeMatcher<JDialog>(JDialog.class) {
@Override
protected boolean isMatching(JDialog dialog) {
return dialog.getTitle().equals(title);
}
}));
}
private void setupRobotSpeed() {
// for example -Drobot.delay.millis=500 could be added to
// the command line to slow it down
String s = System.getProperty("robot.delay.millis");
if (s == null) {
robot.settings().delayBetweenEvents(ROBOT_DELAY_MILLIS_DEFAULT);
} else {
int delay = Integer.parseInt(s);
robot.settings().delayBetweenEvents(delay);
}
}
private void exit() {
String exitMenuName = JVM.isMac ? "Quit" : "Exit";
runMenuCommand(exitMenuName);
findJOptionPane().yesButton().click();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment