Exforsys.com
 

Sponsored Links

 

Swing Testing Tutorials

 
Home Tutorials Swing Testing
 

Swing Extreme Testing - The ShowerThread Class

 

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:



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


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.



Read Next: Swing Extreme Testing - The init() Method



 

 

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