### Eclipse Workspace Patch 1.0 #P org.eclipse.swtbot.swt.finder.test Index: src/org/eclipse/swtbot/swt/finder/junit4/AllTests.java =================================================================== --- src/org/eclipse/swtbot/swt/finder/junit4/AllTests.java (revision 0) +++ src/org/eclipse/swtbot/swt/finder/junit4/AllTests.java (revision 0) @@ -0,0 +1,15 @@ +package org.eclipse.swtbot.swt.finder.junit4; + +import junit.framework.JUnit4TestAdapter; +import junit.framework.Test; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(SWTBotSuite.class) +@SuiteClasses( { SWTBotSuiteTest.class }) +public class AllTests { + public static Test suite() { + return new JUnit4TestAdapter(AllTests.class); + } +} Index: src/org/eclipse/swtbot/swt/finder/junit4/SWTBotSuite.java =================================================================== --- src/org/eclipse/swtbot/swt/finder/junit4/SWTBotSuite.java (revision 0) +++ src/org/eclipse/swtbot/swt/finder/junit4/SWTBotSuite.java (revision 0) @@ -0,0 +1,100 @@ +package org.eclipse.swtbot.swt.finder.junit4; + +import java.io.File; + +import org.apache.log4j.Logger; +import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; +import org.eclipse.swtbot.swt.finder.utils.SWTUtils; +import org.junit.internal.runners.InitializationError; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunListener; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.Suite; + +/** + * The SWTBotSuite extends the JUnit Suite to provide extra capabilities.
+ * Usage example: + * + *
+ * @RunWith(SWTBotSuite.class)
+ * @SuiteClasses( { FirstTest.class, SecondTest.class, EtcTest.class })
+ * public class AllTests {
+ * 	public static Test suite() {
+ * 		return new JUnit4TestAdapter(AllTests.class);
+ * 	}
+ * }
+ * 
+ * + * @since 2.1 + */ +public class SWTBotSuite extends Suite { + /** The logger. */ + private static Logger log = Logger.getLogger(SWTBotSuite.class); + + /** Counts the screenshots to determine if maximum number is reached. */ + private static int screenshotCounter = 0; + + public SWTBotSuite(Class klass) throws InitializationError { + super(klass); + } + + protected SWTBotSuite(Class klass, Class[] annotatedClasses) throws InitializationError { + super(klass, annotatedClasses); + } + + @Override + public void run(RunNotifier notifier) { + addFailureSpy(notifier); + super.run(notifier); + } + + /** + * Add a extra listener to the RunNotifier in order to trigger a screenshot capture. + * + * @param notifier the RunNotifier + */ + private void addFailureSpy(RunNotifier notifier) { + RunListener failureSpy = new RunListener() { + @Override + public void testFailure(Failure failure) throws Exception { + captureScreenshot(failure); + super.testFailure(failure); + } + }; + notifier.addListener(failureSpy); + } + + /** + * Helper used by {@link #runBare()}. + * + * @see #runBare() + * @param failure used for naming the screenshot file + */ + private void captureScreenshot(Failure failure) { + try { + int maximumScreenshots = SWTBotPreferences.getMaximumScreenshots(); + String fileName = "screenshots/" + failure.getTestHeader() + "." + SWTBotPreferences.getScreenshotFormat().toLowerCase(); + if (++screenshotCounter <= maximumScreenshots) { + new File("screenshots").mkdirs(); + captureScreenshot(fileName); + } else { + log.info("No screenshot captured for '" + failure.getTestHeader() + "' because maximum number of screenshots reached: " + + maximumScreenshots); + } + } catch (Exception e) { + log.warn("Could not capture screenshot", e); + } + } + + /** + * Allows the screen shot to be captured and saved to the given file. + * + * @see SWTUtils#captureScreenshot(String) + * @param fileName the filename to save screenshot to. + * @return true if the screenshot was created and saved, false otherwise. + */ + public static boolean captureScreenshot(String fileName) { + return SWTUtils.captureScreenshot(fileName); + } + +} Index: src/org/eclipse/swtbot/swt/finder/junit4/SWTBotSuiteTest.java =================================================================== --- src/org/eclipse/swtbot/swt/finder/junit4/SWTBotSuiteTest.java (revision 0) +++ src/org/eclipse/swtbot/swt/finder/junit4/SWTBotSuiteTest.java (revision 0) @@ -0,0 +1,56 @@ +package org.eclipse.swtbot.swt.finder.junit4; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; + +import org.eclipse.swt.examples.addressbook.AddressBook; +import org.eclipse.swt.widgets.Display; +import org.junit.BeforeClass; +import org.junit.Test; + +public class SWTBotSuiteTest { + + @BeforeClass + public static void startClient() { + Runnable runnable = new Runnable() { + public void run() { + AddressBook.main(null); + } + }; + Thread guiThread = new Thread(runnable); + guiThread.start(); + + while (Display.findDisplay(guiThread) == null) { + } + ; + } + + @Test + public void test1() { + // pass + } + + @Test + public void test2() { + fail("This is no real error, ignore it"); + } + + @Test + public void test3() { + throw new RuntimeException("This is no real error, ignore it"); + } + + @Test + public void test4() { + String testClass = this.getClass().getName(); + File t1 = new File("screenshots/test1(" + testClass + ").jpeg"); + assertFalse(t1.exists()); + File t2 = new File("screenshots/test2(" + testClass + ").jpeg"); + assertTrue(t2.exists()); + File t3 = new File("screenshots/test3(" + testClass + ").jpeg"); + assertTrue(t3.exists()); + } +}