Swing Testing Tutorials
Tutorials
Swing TestingSwing Extreme Testing - Unit Test Infrastructure
Unit Test Infrastructure
Having seen the broad outline of the test class and the UI methods needed, we can look closely at the implementation of the test. We'll start with the UI Wrapper class and the init() and cleanup()methods.
The UISaveAsDialog Class
UISaveAsDialog has methods for entering a name and for accessing the dialog, buttons, and text field. The data entry methods use a Cyborg, while the component accessor methods use UI:
- public class UISaveAsDialog {
- Cyborg robot = new Cyborg()
- private IkonMakerUserStrings us =
- IkonMakerUserStrings.instance()
- protected Dialog namedDialog
- public UISaveAsDialog() {
- namedDialog = UI.findNamedDialog(
- SaveAsDialog.DIALOG_NAME )
- Waiting.waitFor( new Waiting.ItHappened() {
- public boolean itHappened() {
- return nameField().hasFocus()
- }
- }, 1000 )
- }
- public JButton okButton() {
- return (JButton) UI.findNamedComponent(
- IkonMakerUserStrings.OK )
- }
- public Dialog dialog() {
- return namedDialog
- }
- public JButton cancelButton() {
- return (JButton) UI.findNamedComponent(
- IkonMakerUserStrings.CANCEL )
- }
- public JTextField nameField() {
- return (JTextField) UI.findNamedComponent(
- IkonMakerUserStrings.NAME )
- }
- public void saveAs( String newName ) {
- enterName( newName )
- robot.enter()
- }
- public void enterName( String newName ) {
- robot.selectAllText()
- robot.type( newName )
- }
- public void ok() {
- robot.altChar( us.mnemonic( IkonMakerUserStrings.OK ) )
- }
- public void cancel() {
- robot.altChar( us.mnemonic( IkonMakerUserStrings.CANCEL ) )
- }
- }
A point to note here is the code in the constructor that waits for the name text field to have focus. This is necessary because the inner workings of Swing set the focus within a shown modal dialog as a separate event. That is, we can't assume that showing the dialog and setting the focus within it happen within a single atomic event. Apart from this wrinkle, all of the methods of UISaveDialog are straightforward applications of UI methods.
Comments
Weekly Offers
Sponsored Links
