Reviews
Swing Extreme TestingSwing 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:
- private void init() {
- //Not really what we do...
- //setup...then launch a thread to show the dialog.
- //Start a thread to show the dialog (it is modal).
- new Thread( "SaveAsDialogShower" ) {
- public void run() {
- sad = new SaveAsDialog( frame, names )
- sad.show()
- }
- }.start()
- //Now wait for the dialog to show...
- }
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:
- private class ShowerThread extends Thread {
- private boolean isAwakened
- public ShowerThread() {
- super( "Shower" )
- setDaemon( true )
- }
- public void run() {
- Runnable runnable = new Runnable() {
- public void run() {
- sad.show()
- }
- }
- UI.runInEventThread( runnable )
- isAwakened = true
- }
- public boolean isAwakened() {
- return Waiting.waitFor( new Waiting.ItHappened() {
- public boolean itHappened() {
- return isAwakened
- }
- }, 1000 )
- }
- }
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.
Swing Extreme Testing
- Swing Extreme Testing
- Swing Extreme Testing - Outline of the Unit Test
- Swing Extreme Testing - Getting the Text of a Text Field
- Swing Extreme Testing - Unit Test Infrastructure
- Swing Extreme Testing - The ShowerThread Class
- Swing Extreme Testing - The init() Method
- Swing Extreme Testing - The Constructor Test
- Swing Extreme Testing - The wasCancelled() Test
- Swing Extreme Testing - The name() Test
- Swing Extreme Testing - The Data Validation Test







