Most applications have a login screen - so let's see what sort of tests can be done for this window.
In the interest of brevity, lets skip the import statements and the class declaration. The following lines of code are used to setup the test fixture and tear it down before and after each test.
import junit.extensions.jfcunit.*; import junit.extensions.jfcunit.finder.*; import junit.extensions.jfcunit.eventdata.*;
public LoginScreenTest extends JFCTestCase {
private LoginScreen loginScreen = null; public LoginScreenTest( String name ) { super( name ); } protected void setUp( ) throws Exception { super.setUp( );
// Choose the text Helper setHelper( new JFCTestHelper( ) ); // Uses the AWT Event Queue. // setHelper( new RobotTestHelper( ) ); // Uses the OS Event Queue.
loginScreen = new LoginScreen( "LoginScreenTest: " + getName( ) ); loginScreen.setVisible( true ); } protected void tearDown( ) throws Exception { loginScreen = null; getHelper.cleanUp( this ); super.tearDown( ); }
...Tests...
}Now, lets list all the possible tests:
public void testUserAndPasswordEmpty() {
1: JDialog dialog; 2: NamedComponentFinder finder = new NamedComponentFinder(JComponent.class, "ExitButton" ); 3: JButton exitButton = ( JButton ) finder.find( loginScreen, 0); 4: assertNotNull( "Could not find the Exit button", exitButton ); 5: finder.setName( "EnterButton" ); 6: JButton enterButton = ( JButton ) finder.find( loginScreen, 0 ); 7: assertNotNull( "Could not find the Enter button", enterButton ); 8: finder.setName( "LoginNameTextField" ); 9: JTextField userNameField = ( JTextField ) finder.find( loginScreen, 0 ); 10: assertNotNull( "Could not find the userNameField", userNameField ); 11: assertEquals( "Username field is empty", "", userNameField.getText( ) ); 12: finder.setName( "PasswordTextField" ); 13: JTextField passwordField = ( JTextField ) finder.find( loginScreen, 0 ); 14: assertNotNull( "Could not find the passwordField", passwordField ); 15: assertEquals( "Password field is empty", "", passwordField.getText( ) ); 16: getHelper().enterClickAndLeave( new MouseEventData( this, enterButton ) ); 17: DialogFinder dFinder = new DialogFinder( loginScreen ); 18: showingDialogs = dFinder.findAll(); 19: assertEquals( "Number of dialogs showing is wrong", 1, showingDialogs.size( ) ); 20: dialog = ( JDialog )showingDialogs.get( 0 ); 21: assertEquals( "Wrong dialog showing up", "Login Error", dialog.getTitle( ) ); 22: getHelper().disposeWindow( dialog, this );
}As seen on line 2-3, the
NamedComponentFinder()
is used to find a component
named "ExitButton". In line 4, we make sure that the button was found and is not null. In lines
5
and 6, we reconfigure the finder to find the "EnterButton". Once we find the userNameField
in lines
8 thru 11, we also check that the field is empty. This is repeated for the passwordField
in lines 12 thru 15. Once we have found all the components that are needed for the test, we simulate
a mouse click in line 16 by calling the TestHelper.enterClickAndLeave()
method. In line
17 and 18, we use the DialogFinder
to find the list of showing dialogs. The next
few lines are used to check that the number of dialogs is 1 and that the expected dialog is what is shown.
The last line of the method is used to dispose of the dialog.
public void testEmptyPassword() {
1: JDialog dialog; 2: NamedComponentFinder finder = new NamedComponentFinder( JComponent.class, "ExitButton" ); 3: JButton exitButton = ( JButton ) finder.findNamedComponent(loginScreen, 0 ); 4: assertNotNull( "Could not find the Exit button", exitButton ); 5: finder.setName( "EnterButton" ); 6: JButton enterButton = ( JButton ) finder.find( loginScreen, 0 ); 7: assertNotNull( "Could not find the Enter button", enterButton ); 8: finder.setName( "LoginNameTextField" ); 9: JTextField userNameField = ( JTextField ) finder.find( loginScreen, 0 ); 10: assertNotNull( "Could not find the userNameField", userNameField ); 11: getHelper().sendString( new StringEventData( this, userNameField, "admin" ) ); 12: finder.setName( "PasswordTextField" ); 13: JTextField passwordField = ( JTextField ) finder.find( loginScreen, 0 ); 14: assertNotNull( "Could not find the passwordField", passwordField ); 15: assertEquals( "Password field is empty", "", passwordField.getText( ) ); 16: getHelper().enterClickAndLeave( new MouseEventData( this, enterButton ) ); 17: DialogFinder dFinder = new DialogFinder(); 18: showingDialogs = dFinder.findAlll( loginScreen ); 19: assertEquals( "Number of dialogs showing is wrong", 1, showingDialogs.size( ) ); 20: dialog = ( JDialog )showingDialogs.get( 0 ); 21: assertEquals( "Wrong dialog showing up", "Login Error", dialog.getTitle( ) ); 22: getHelper().disposeWindow( dialog, this );
}As in the previous test, we find the required components and assert that they are non-null. We can also assert that the components have any required states or default values, etc. Going to line 11, we call the
TestHelper.sendString
method to simulate key strokes on the TextComponent.
After entering the user name alone, we simulate a button click on the "Enter" button. This should
show an error dialog, which is what is checked in lines 17 through 22. The
line 22 is then
used to destroy the error message dialog box.
public void testInvalid() {
1: JDialog dialog; 2: NamedComponentFinder finder = new NamedComponentFinder( JComponent.class, "ExitButton" ); 3: JButton exitButton = ( JButton ) finder.find( loginScreen, 0 ); 4: assertNotNull( "Could not find the Exit button", exitButton ); 5: finder.setName( "EnterButton" ); 6: JButton enterButton = ( JButton ) helper.findNamedComponent( "EnterButton", loginScreen, 0 ); 7: assertNotNull( "Could not find the Enter button", enterButton ); 8: finder.setName( "LoginNameTextField" ); 9: JTextField userNameField = ( JTextField ) finder.find( loginScreen, 0 ); 10: assertNotNull( "Could not find the userNameField", userNameField ); 11: getHelper().sendString( new StringEventData( this, userNameField, "Random: " + Math.random( ) ) ); 12: finder.setName( "PasswordTextField" ); 13: JTextField passwordField = ( JTextField ) finder.find( loginScreen, 0 ); 14: assertNotNull( "Could not find the passwordField", passwordField ); 15: getHelper().sendString( new StringEventData( this, passwordField, "Random: " + Math.random( ) ) ); 16: helper.enterClickAndLeave( new MouseEventData( this, enterButton ) ); 17: DialogFinder dFinder = new DialogFinder(null); 18: showingDialogs = dFinder.findAll( loginScreen ); 19: assertEquals( "Number of dialogs showing is wrong", 1, showingDialogs.size( ) ); 20: dialog = ( JDialog )showingDialogs.get( 0 ); 21: assertEquals( "Wrong dialog showing up", "Login Error", dialog.getTitle( ) ); 22: getHelper().disposeWindow( dialog, this );After finding the user name text field, the
TestHelper.sendString
method is called
to simulate a valid user name in line 11. Then (after finding the password field) an invalid
password is "typed" in line 15. Upon clicking the "Enter" button, the test then checks that once
again we get an error message dialog.
Note: If your application's login box shows different error messages depending upon the
circumstance, the TestHelper.getMessageFromJDialog
method can be used to access
the message and assert that it is the expected message.
public void testValid() {
1: List showingWindows; 2: JFrame frame; 3: JDialog dialog; 4: NamedComponentFinder finder = new NamedComponentFinder( JComponent.class, "ExitButton" ); 5: JButton exitButton = ( JButton ) finder.find( loginScreen, 0 ); 6: assertNotNull( "Could not find the Exit button", exitButton ); 7: finder.setName( "EnterButton" ); 8: JButton enterButton = ( JButton ) finder.find( loginScreen, 0 ); 9: assertNotNull( "Could not find the Enter button", enterButton ); 10: finder.setName( "LoginNameTextField" ); 11: JTextField userNameField = ( JTextField ) finder.find( loginScreen, 0 ); 12: assertNotNull( "Could not find the userNameField", userNameField ); 13: finder.setName( "PasswordTextField" ); 14: JTextField passwordField = ( JTextField ) finder.find( loginScreen, 0 ); 15: assertNotNull( "Could not find the passwordField", passwordField ); 16: getHelper().sendString( new StringEventData( this, userNameField, validUser.getLoginName( ) ) ); 17: getHelper().sendString( new StringEventData( this, passwordField, validUser.getPassword( ) ) ); 18: getHelper().enterCickAndLeave( new MouseEventData( this, enterButton ) ); 19: DialogFinder dFinder = new DialogFinder(null); 20: dFinder.setWait(0); 21: showingDialogs = dFinder.findAll( loginScreen ); 22: assertEquals( "Number of dialogs showing is wrong", 0, showingDialogs.size( ) ); 23: assertNotNull( "Login Screen has been destroyed", loginScreen ); 24: assertTrue( "Login screen is showing still", !loginScreen.isShowing( ) ); 25: FrameFinder fFinder = new FrameFinder(null); 26: showingWindows = fFinder.findAll(); 27: assertEquals( "Number of windows showing is wrong", 1, showingWindows.size( ) );
}After finding the required components, the
getHelper().sendString
method is called
to simulate a valid user name with a valid password. The test then checks that there are no
error dialogs that show up. In lines 19 through 27, the test then checks that the login screen
is no longer visible and the application's next window shows up. Line 20
configures the DialogFinder with a Zero wait so that the finder returns
immediately.
public void testFocus() {
1: JDialog dialog; 2: NamedComponentFinder finder = new NamedComponentFinder( JComponent.class, "ExitButton" ); 3: JButton exitButton = ( JButton ) finder.find( loginScreen, 0 ); 4: assertNotNull( "Could not find the Exit button", exitButton ); 5: finder.setName( "EnterButton" ); 6: JButton enterButton = ( JButton ) finder.find( loginScreen, 0 ); 7: assertNotNull( "Could not find the Enter button", enterButton ); 8: finder.setName( "LoginNameTextField" ); 9: JTextField userNameField = ( JTextField ) finder.find( loginScreen, 0 ); 10: assertNotNull( "Could not find the userNameField", userNameField ); 11: finder.setName( "PasswordTextField" ); 12: JTextField passwordField = ( JTextField ) finder.find( loginScreen, 0 ); 13: assertNotNull( "Could not find the passwordField", passwordField );
14: DialogFinder dFinder = new DialogFinder(null); 15: dFinder.setWait(0);
16: // ( Keyboard ) Enter pressed on the userName field - focus should go to the passwordField 17: pauseAWT( ); 18: userNameField.requestFocus( ); 19: flushAWT( ); 20: assertTrue( "userNameField does not have focus", userNameField.hasFocus( ) ); 21: getHelper().sendKeyAction( new KeyEventData( this, userNameField, KeyEvent.VK_ENTER ) ); 22: showingDialogs = dFinder.findAll( loginScreen ); 23: assertEquals( "( 1 )Number of dialogs showing is wrong", 0, showingDialogs.size( ) ); 24: assertTrue( "passwordField does not have focus", passwordField.hasFocus( ) );
25: // ( Keyboard ) Enter pressed on the password field - error dialog should appear 26: pauseAWT( ); 27: passwordField.requestFocus( ); 28: flushAWT( ); 29: assertTrue( "passwordField does not have focus", passwordField.hasFocus( ) ); 30: getHelper().sendKeyAction( new KeyEventData( this, passwordField, KeyEvent.VK_ENTER ) );
31: showingDialogs = dFinder.findAll( loginScreen ); 32: assertEquals( "( 2 )Number of dialogs showing is wrong", 1, showingDialogs.size( ) ); 33: dialog = ( JDialog )showingDialogs.get( 0 ); 34: assertEquals( "( 2 )Wrong dialog showing up", "Login Error", dialog.getTitle( ) ); 35: getHelper().disposeWindow( dialog, this );
}After finding the required components, the user name text field is given focus by calling it's
requestFocus
method. This is also verified by calling assertTrue
.
Then the TestHelper.sendKeyAction
method is called to send an Enter
key event and we check that the password field has the focus after this event is consumed.
The same is repeated so that the "Enter" button receives focus (since that is a requirement of
the example login screen).