Exforsys.com
 

Sponsored Links

 

Swing Testing Tutorials

 
Home Tutorials Swing Testing
 

Swing Extreme Testing - Outline of the Unit Test

 

Outline of the Unit Test

The things we want to test are:


  • Initial settings:
    • The text field is empty.
    • The text field is a sensible size.
    • The Ok button is disabled.
    • The Cancel button is enabled.
    • The dialog is a sensible size.


  • Usability:
    • The Escape key cancels the dialog.
    • The Enter key activates the Ok button.
    • The mnemonics for Ok and Cancel work.
      .
  • Correctness. The Ok button is disabled if the entered name:
    • Contains characters such as '*', '\', '/'. ° Is just white-space.
    • Is one already being used.
      .
  • API test: unit tests for each of the public methods.

As with most unit tests, our test class has an init() method for getting an object into a known state, and a cleanup() method called at the end of each test. The instance variables are:


  • A JFrame and a set of IkonNames from which the SaveAsDialog can be constructed.
  • A SaveAsDialog, which is the object under test.
  • A UserStrings and a UISaveAsDialog (listed later on) for manipulating the SaveAsDialog with keystrokes.
  • A ShowerThread, which is a Thread for showing the SaveAsDialog. This is listed later on.

The outline of the unit test is:


Sample Code
  1. public class SaveAsDialogTest {
  2.  
  3.  private JFrame frame
  4.  
  5.  private SaveAsDialog sad
  6.  
  7.  private IkonMakerUserStrings =
  8.  
  9. IkonMakerUserStrings.instance()
  10.  
  11.  private SortedSet<ikonname></ikonname> names
  12.  
  13.  private UISaveAsDialog ui
  14.  
  15.  private Shower shower
  16.  
  17.  ...
  18.  
  19.  private void init() {
  20.  
  21.  ...
  22. }
  23. private void cleanup() {
  24. ...
  25. }
  26. private class ShowerThread extends Thread {
  27. ...
  28. }
  29. }
Copyright exforsys.com


UI Helper Methods

A lot of the work in this unit test will be done by the static methods in our helper class, UI. We looked at some of these (isEnabled(), runInEventThread(), and findNamedComponent()) in Chapter 8. The new methods are listed now, according to their function.


Dialogs

If a dialog is showing, we can search for a dialog by name, get its size, and read its title:


Sample Code
  1. public final class UI {
  2. ...
  3. /**
  4.  
  5.  
  6.  * Safely read the showing state of the given window.
  7. */
  8. public static boolean isShowing( final Window window ) {
  9. final boolean[] resultHolder = new boolean[]{false}
  10. runInEventThread( new Runnable() {
  11.  
  12.  public void run() {
  13. resultHolder[0] = window.isShowing()
  14.  
  15.  }
  16. } )
  17. return resultHolder[0]
  18.  
  19.  
  20.  }
  21. /**
  22.  
  23.  
  24.  * The first found dialog that has the given name and
  25. * is showing (though the owning frame need not be showing).
  26. */
  27. public static Dialog findNamedDialog( String name ) {
  28. Frame[] allFrames = Frame.getFrames()
  29. for (Frame allFrame : allFrames) {
  30.  
  31.  Window[] subWindows = allFrame.getOwnedWindows()
  32. for (Window subWindow : subWindows) {
  33.  
  34.  if (subWindow instanceof Dialog) {
  35. Dialog d = (Dialog) subWindow
  36. if (name.equals( d.getName() )
  37.  
  38. && d.isShowing()) {
  39. return (Dialog) subWindow
  40. }
  41. }
  42.  
  43.  }
  44. }
  45. return null
  46.  
  47.  
  48.  }
  49. /**
  50.  
  51.  
  52.  * Safely read the size of the given component.
  53. */
  54. public static Dimension getSize( final Component component ) {
  55. final Dimension[] resultHolder = new Dimension[]{null}
  56.  
  57. runInEventThread( new Runnable() {
  58. public void run() {
  59. resultHolder[0] = component.getSize()
  60.  
  61.  }
  62. } )
  63. return resultHolder[0]
  64.  
  65.  
  66.  }
  67. /**
  68.  
  69.  
  70.  * Safely read the title of the given dialog.
  71. */
  72. public static String getTitle( final Dialog dialog ) {
  73. final String[] resultHolder = new String[]{null}
  74. runInEventThread( new Runnable() {
  75.  
  76.  public void run() {
  77. resultHolder[0] = dialog.getTitle()
  78.  
  79.  }
  80. } )
  81. return resultHolder[0]
  82.  
  83.  
  84.  } ...
  85. }
Copyright exforsys.com



Read Next: Swing Extreme Testing - Getting the Text of a Text Field



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape