From: <mz...@us...> - 2008-12-10 15:16:06
|
Revision: 2772 http://abbot.svn.sourceforge.net/abbot/?rev=2772&view=rev Author: mzwps Date: 2008-12-10 15:16:00 +0000 (Wed, 10 Dec 2008) Log Message: ----------- mz: Additional test example for App purposes. Modified Paths: -------------- abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/HelloShell.java Added Paths: ----------- abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/HelloShellApplication.java abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/MyThirdTestCaseAsApplication.java Modified: abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/HelloShell.java =================================================================== --- abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/HelloShell.java 2008-11-09 00:32:53 UTC (rev 2771) +++ abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/HelloShell.java 2008-12-10 15:16:00 UTC (rev 2772) @@ -69,5 +69,4 @@ } }); } - } Added: abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/HelloShellApplication.java =================================================================== --- abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/HelloShellApplication.java (rev 0) +++ abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/HelloShellApplication.java 2008-12-10 15:16:00 UTC (rev 2772) @@ -0,0 +1,39 @@ +package net.sf.abbot.swt.one.examples.case3; + +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; + +public class HelloShellApplication { + ////// AS APPLICATION ////// + + /** + * Prepares the App. This indirection is required to separate UI event loop from creation. + * + * @param display The display the master shell should be created on. + * @param args The cmd-line args. + * @return the master shell. + */ + public static Shell prepareApplication(Display display, String[] args) { + Shell shell = new Shell(display); + shell.setText("Hello"); + shell.setLayout(new GridLayout(1, true)); + final HelloShell helloShell = new HelloShell(shell); + helloShell.createControls(); + shell.open(); + return shell; + } + + /** + * The main with the master shells event loop. This method should NOT be tested with Abbot. + */ + public static void main(String[] args) { + Display display = new Display(); + Shell masterShell = prepareApplication(display, args); + while (!masterShell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } +} Added: abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/MyThirdTestCaseAsApplication.java =================================================================== --- abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/MyThirdTestCaseAsApplication.java (rev 0) +++ abbot.swt/trunk/net.sf.abbot.swt.one.examples/src/net/sf/abbot/swt/one/examples/case3/MyThirdTestCaseAsApplication.java 2008-12-10 15:16:00 UTC (rev 2772) @@ -0,0 +1,236 @@ +package net.sf.abbot.swt.one.examples.case3; + +import junit.framework.Assert; + +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; +import org.eclipse.swt.widgets.Widget; +import org.junit.After; +import org.junit.Before; + +import abbot.swt.finder.WidgetFinder; +import abbot.swt.finder.generic.Matcher; +import abbot.swt.junit.extensions.SWTTestCase; +import abbot.swt.tester.ButtonTester; +import abbot.swt.tester.LabelTester; +import abbot.swt.tester.ShellTester; +import abbot.swt.tester.TextTester; + +public class MyThirdTestCaseAsApplication extends SWTTestCase { + + /** + * Just to take a look by your-self. Set to 0 to "drive" with original "speed". + */ + private static final int TIMEOUT = 1000; + + private Shell shell; + + private Button sayHelloButton; + private Text eingabefeld; + private Button okButtonInDialog; + protected Label labelInDialog; + protected Label labelEingabe; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + } + + @Override + protected void setUpDisplay() { + super.setUpDisplay(); + + // Instead of creating "by hand" prepare the App with the Apps prepare method. + String[] myArgs = new String[0]; + this.shell = HelloShellApplication.prepareApplication(getDisplay(), myArgs ); + } + + @Override + @After + public void tearDown() throws Exception { + super.tearDown(); + } + + @Override + protected void tearDownDisplay() { + this.shell.setVisible(false); + this.shell.dispose(); + + super.tearDownDisplay(); + } + + public void testNumberOne() throws Throwable { + // just show the shell a moment... + SWTTestCase.sleep(MyThirdTestCaseAsApplication.TIMEOUT); + + // Find the Widgets... + final WidgetFinder widgetFinder = this.getFinder(); + + this.sayHelloButton = (Button) widgetFinder.find(this.shell, + new Matcher<Widget>() { + public boolean matches(final Widget widget) { + if (!(widget instanceof Button)) { + return false; + } + + // Tester have to be used to avoid thread access + // problems... + final ButtonTester tester = (ButtonTester) MyThirdTestCaseAsApplication.this + .getTester(Button.class); + return tester.getText((Button) widget).equals( + HelloShell.KNOPF_HELLO_TEXT); + } + }); + + this.eingabefeld = (Text) widgetFinder.find(this.shell, + new Matcher<Widget>() { + public boolean matches(final Widget widget) { + if (!(widget instanceof Text)) { + return false; + } + + return true; + } + }); + + this.labelEingabe = (Label) widgetFinder.find(this.shell, + new Matcher<Widget>() { + public boolean matches(final Widget widget) { + if (!(widget instanceof Label)) { + return false; + } + + final LabelTester tester = (LabelTester) MyThirdTestCaseAsApplication.this + .getTester(Label.class); + return tester.getText((Label) widget).equals( + HelloShell.BITTE_NAMEN_EINGEBEN); + } + }); + + // Just check if label to textfield is present + Assert.assertNotNull(widgetFinder.find(this.shell, + new Matcher<Widget>() { + public boolean matches(final Widget widget) { + if (!(widget instanceof Label)) { + return false; + } + + final LabelTester tester = (LabelTester) MyThirdTestCaseAsApplication.this + .getTester(Label.class); + return tester.getText((Label) widget).equals( + HelloShell.IHR_NAME); + } + })); + + // Now check initial state + final ButtonTester buttonTester = (ButtonTester) this + .getTester(Button.class); + final TextTester textfeldTester = (TextTester) this + .getTester(Text.class); + final LabelTester labelEingabeErgebnis = (LabelTester) this + .getTester(Label.class); + + Assert.assertTrue(buttonTester.isEnabled(this.sayHelloButton)); + Assert.assertEquals("", textfeldTester.getText(this.eingabefeld)); + Assert.assertEquals(HelloShell.BITTE_NAMEN_EINGEBEN, + labelEingabeErgebnis.getWidgetText(this.labelEingabe)); + + // Prepare typing + textfeldTester.click(this.eingabefeld); + + // Start to type + textfeldTester.actionKeyChar('F'); + Assert.assertEquals(HelloShell.NAME_LABEL_PREFIX + "F", + labelEingabeErgebnis.getWidgetText(this.labelEingabe)); + + textfeldTester.actionKeyChar('r'); + Assert.assertEquals(HelloShell.NAME_LABEL_PREFIX + "Fr", + labelEingabeErgebnis.getWidgetText(this.labelEingabe)); + + textfeldTester.actionKeyChar('i'); + Assert.assertEquals(HelloShell.NAME_LABEL_PREFIX + "Fri", + labelEingabeErgebnis.getWidgetText(this.labelEingabe)); + + textfeldTester.actionKeyChar('t'); + Assert.assertEquals(HelloShell.NAME_LABEL_PREFIX + "Frit", + labelEingabeErgebnis.getWidgetText(this.labelEingabe)); + + textfeldTester.actionKeyChar('z'); + Assert.assertEquals(HelloShell.NAME_LABEL_PREFIX + "Fritz", + labelEingabeErgebnis.getWidgetText(this.labelEingabe)); + + // Wait for all to be done... + textfeldTester.waitForIdle(); + + SWTTestCase.sleep(MyThirdTestCaseAsApplication.TIMEOUT); + + // Check Textfeld & button state + Assert.assertTrue(buttonTester.isEnabled(this.sayHelloButton)); + Assert.assertEquals("Fritz", textfeldTester.getText(this.eingabefeld)); + + // Let us ask to say hello + buttonTester.actionClick(this.sayHelloButton); + + // Wait for all to be done... + textfeldTester.waitForIdle(); + + SWTTestCase.sleep(MyThirdTestCaseAsApplication.TIMEOUT); + + // Find the message box... + final ShellTester shellTester = (ShellTester) this + .getTester(Shell.class); + final Shell[] dialogShells = shellTester.getShells(this.shell); + Assert.assertEquals(1, dialogShells.length); + final Shell messageBox = dialogShells[0]; + Assert.assertNotNull(messageBox); + + // find the message box' widgets + this.okButtonInDialog = (Button) widgetFinder.find(messageBox, + new Matcher<Widget>() { + public boolean matches(final Widget widget) { + if (!(widget instanceof Button)) { + return false; + } + + // There should only be one button in an OK message box... + return true; + } + }); + + this.labelInDialog = (Label) widgetFinder.find(messageBox, + new Matcher<Widget>() { + public boolean matches(final Widget widget) { + if (!(widget instanceof Label)) { + return false; + } + + // do not return the icon label! Check if visible text is + // present (icon label have no visible text) + final LabelTester tester = (LabelTester) MyThirdTestCaseAsApplication.this + .getTester(Label.class); + return tester.getText((Label) widget).length() > 0; + } + }); + + Assert.assertNotNull(this.okButtonInDialog); + Assert.assertNotNull(this.labelInDialog); + + // now check the message box content + final LabelTester labelInDialogTester = (LabelTester) this + .getTester(Label.class); + labelInDialogTester.actionClick(this.labelInDialog); + Assert.assertEquals(HelloShell.DIALOG_TEXT_PREFIX + "Fritz", + labelInDialogTester.getText(this.labelInDialog)); + + SWTTestCase.sleep(MyThirdTestCaseAsApplication.TIMEOUT); + + // click ok button + buttonTester.actionClick(this.okButtonInDialog); + + // Done. + SWTTestCase.sleep(MyThirdTestCaseAsApplication.TIMEOUT); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |