Exforsys

Home arrow Reviews arrow Swing Extreme Testing

Swing Extreme Testing - The init() Method

Author: Packt Publishing     Published on: 11th Jul 2008    |   Last Updated on: 12th Jul 2008

The init() Method

The job of the init() method is to create and show the SaveAsDialog instance so that it can be tested:

Ads

Sample Code
  1. private void init() {
  2.  
  3. //Note 1
  4.  
  5.  names = new TreeSet<ikonname></ikonname>()
  6. names.add( new IkonName( "Albus" ) )
  7. names.add( new IkonName( "Minerva" ) )
  8. names.add( new IkonName( "Severus" ) )
  9. names.add( new IkonName( "Alastair" ) )
  10.  
  11.  
  12. //Note 2
  13.  
  14.  Runnable creator = new Runnable() {
  15.  
  16.  public void run() {
  17. frame = new JFrame( "SaveAsDialogTest" )
  18. frame.setVisible( true )
  19. sad = new SaveAsDialog( frame, names )
  20.  
  21.  }
  22. }
  23. UI.runInEventThread( creator )
  24.  
  25.  
  26. //Note 3
  27.  
  28.  //Start a thread to show the dialog (it is modal).
  29. shower = new ShowerThread()
  30.  
  31.  
  32.  
  33.  
  34.  
  35. shower.start()
  36.  
  37. //Note 4
  38.  
  39.  //Wait for the dialog to be showing.
  40. Waiting.waitFor( new Waiting.ItHappened() {
  41. public boolean itHappened() {
  42. return UI.findNamedFrame(
  43. SaveAsDialog.DIALOG_NAME ) != null
  44. }
  45. }, 1000 )
  46.  
  47. //Note 5
  48.  
  49.  ui = new UISaveAsDialog()
  50. }
Copyright exforsys.com


Now let's look at some of the key points in this code.

Note 1: In this block of code we create a set of IkonNames with which our SaveAsDialog can be created.

Note 2: It's convenient to create and show the owning frame and create the SaveAsDialog in a single Runnable. An alternative would be to create and show the frame with a UI call and use the Runnable just for creating the SaveAsDialog.

Note 3: Here we start our Shower, which will call the blocking show() method of SaveAsDialog from the event thread.

Note 4: Having called show() via the event dispatch thread from our Shower thread, we need to wait for the dialog to actually be showing on the screen. The way we do this is to search for a dialog that is on the screen and has the correct name.

Note 5: Once the SaveAsDialog is showing, we can create our UI Wrapper for it.

The cleanup() Method

The cleanup() method closes all frames in a thread-safe manner:

private void cleanup() { UI.disposeOfAllFrames(); }

Ads

The Unit Tests

We've now done all the hard work of building an infrastructure that will make our tests very simple to write. Let's now look at these tests.



 
This tutorial is part of a Swing Extreme Testing tutorial series. Read it from the beginning and learn yourself.

Swing Extreme Testing

 

Comments