Exforsys

Home arrow Reviews arrow Swing Extreme Testing

Swing Extreme Testing - The ShowerThread Class

Author: Packt Publishing     Published on: 11th Jul 2008

The ShowerThread Class

Since SaveAsDialog.show() blocks, we cannot call this from our main thread; instead we spawn a new thread. This thread could just be an anonymous inner class in the init()method:

Ads

Sample Code
  1. private void init() {
  2. //Not really what we do...
  3. //setup...then launch a thread to show the dialog.
  4. //Start a thread to show the dialog (it is modal).
  5. new Thread( "SaveAsDialogShower" ) {
  6.  
  7.  
  8.  public void run() {
  9. sad = new SaveAsDialog( frame, names )
  10. sad.show()
  11.  
  12.  }
  13. }.start()
  14. //Now wait for the dialog to show...
  15. }
Copyright exforsys.com


The problem with this approach is that it does not allow us to investigate the state of the Thread that called the show() method. We want to write tests that check that this thread is blocked while the dialog is showing.

Our solution is a simple inner class:

Sample Code
  1. private class ShowerThread extends Thread {
  2. private boolean isAwakened
  3.  
  4.  
  5.  public ShowerThread() {
  6. super( "Shower" )
  7. setDaemon( true )
  8.  
  9.  
  10.  }
  11. public void run() {
  12. Runnable runnable = new Runnable() {
  13. public void run() {
  14. sad.show()
  15. }
  16. }
  17.  
  18.  
  19. UI.runInEventThread( runnable )
  20. isAwakened = true
  21. }
  22. public boolean isAwakened() {
  23. return Waiting.waitFor( new Waiting.ItHappened() {
  24. public boolean itHappened() {
  25. return isAwakened
  26. }
  27. }, 1000 )
  28. }
  29. }
Copyright exforsys.com


Ads

The method of most interest here is isAwakened(), which waits for up to one second for the awake flag to have been set. This uses a class, Waiting, that is discussed in Chapter 12. We'll look at tests that use this isAwakened() method later on. Another point of interest is that we've given our new thread a name (by the call super("Shower") in the constructor). It's really useful to give each thread we create a name, for reasons that will be discussed in Chapter 20.



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

Swing Extreme Testing

 

Comments