org.uispec4j.interception
Class WindowInterceptor

java.lang.Object
  extended by org.uispec4j.interception.WindowInterceptor

public final class WindowInterceptor
extends java.lang.Object

Intercepts popped-up windows such as JFrame or JDialog.

There are two main usage scenarios for this class: intercepting "frames", i.e. non-modal windows, and intercepting modal dialogs.

Non-modal windows can be intercepted and used directly from within the test using the following construct:

   Window window = WindowInterceptor.run(panel.getButton("open").triggerClick());
 

Modal dialogs cannot be intercepted this way, because the thread from which the test is run will likely be blocked in the production code until the dialog is closed. To intercept a sequence of popped-up windows, use the following construct:

   WindowInterceptor
    .init(new Trigger() {
      public void run() throws Exception {
        // ... trigger something that will cause the first window to be shown ...
      }
    })
    .process(new WindowHandler("first dialog") {
      public Trigger process(Window window) {
        // ... perform some operations on the first window ...
        return window.getButton("OK").triggerClick(); // return a trigger that will close it
      }
    })
    .process(new WindowHandler("second dialog") {
      public Trigger process(Window window) {
        // ... perform some operations on the second window ...
        return window.getButton("OK").triggerClick(); // return a trigger that will close it
      }
    })
   .run();
 
This class uses a timeout (see UISpec4J.setWindowInterceptionTimeLimit(long)) to make sure that windows appear within a given time limit, and that modal windows are closed before the end of the interception.

See Also:
Intercepting windows

Method Summary
static Window getModalDialog(Trigger trigger)
          Performs a "quick&dirty" interception of a modal dialog.
static WindowInterceptor init(Trigger trigger)
          Starts the interception of a modal dialog.
 WindowInterceptor process(java.lang.String title, WindowHandler handler)
          Processes a modal dialog after having checked its title first.
 WindowInterceptor process(WindowHandler handler)
          Processes a modal dialog.
 WindowInterceptor process(WindowHandler[] handlers)
          Processes a sequence of dialogs (one handler per dialog).
 WindowInterceptor processTransientWindow()
          Processes a dialog that will be closed automatically.
 WindowInterceptor processTransientWindow(java.lang.String title)
          Processes a dialog that will be closed automatically, and checks its name.
 WindowInterceptor processWithButtonClick(java.lang.String buttonName)
          Processes a dialog by clicking on a given button.
 WindowInterceptor processWithButtonClick(java.lang.String title, java.lang.String buttonName)
          Processes a dialog by checking its title and clicking on a given button.
 void run()
          Starts the interception prepared with the init(Trigger)/process(WindowHandler) call sequence.
static Window run(Trigger trigger)
          Intercepts a non-modal window by running a trigger and returning the displayed window.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

init

public static WindowInterceptor init(Trigger trigger)
Starts the interception of a modal dialog. The returned interceptor must be used for processing the displayed window, for instance:
   WindowInterceptor
    .init(new Trigger() {
      public void run() throws Exception {
        // ... trigger something that will cause the first window to be shown ...
      }
    })
    .process(new WindowHandler("my dialog") {
      public Trigger process(Window window) {
        // ... perform some operations on the shown window ...
        return window.getButton("OK").triggerClick(); // return a trigger that will close it
      }
    })
   .run();
 

See Also:
process(WindowHandler)

process

public WindowInterceptor process(WindowHandler handler)
Processes a modal dialog. The provided WindowHandler must return a trigger that will cause the window to be closed, in order to prevent the application to be stopped.


process

public WindowInterceptor process(java.lang.String title,
                                 WindowHandler handler)
Processes a modal dialog after having checked its title first.

See Also:
process(WindowHandler)

process

public WindowInterceptor process(WindowHandler[] handlers)
Processes a sequence of dialogs (one handler per dialog).

See Also:
process(WindowHandler)

processTransientWindow

public WindowInterceptor processTransientWindow(java.lang.String title)
Processes a dialog that will be closed automatically, and checks its name.


processTransientWindow

public WindowInterceptor processTransientWindow()
Processes a dialog that will be closed automatically.


processWithButtonClick

public WindowInterceptor processWithButtonClick(java.lang.String buttonName)
Processes a dialog by clicking on a given button. This a shortcut that prevents you from implementing your own WindowHandler for simple cases such as confirmation dialogs.

See Also:
process(WindowHandler)

processWithButtonClick

public WindowInterceptor processWithButtonClick(java.lang.String title,
                                                java.lang.String buttonName)
Processes a dialog by checking its title and clicking on a given button.

See Also:
processWithButtonClick(String)

run

public void run()
Starts the interception prepared with the init(Trigger)/process(WindowHandler) call sequence. This method will fail if no window was shown by the trigger under the time limit. defined with UISpec4J.setWindowInterceptionTimeLimit(long).


run

public static Window run(Trigger trigger)
Intercepts a non-modal window by running a trigger and returning the displayed window. This method will fail if no window was shown by the trigger under the time limit defined with UISpec4J.setWindowInterceptionTimeLimit(long), or if it is used with a modal dialog (modal dialogs should be intercepted using init(Trigger)).

Note: the trigger is run in the current thread.


getModalDialog

public static Window getModalDialog(Trigger trigger)
Performs a "quick&dirty" interception of a modal dialog.

Warning: This method should be handled with care and especially avoided in cases where the application code is blocked while the dialog is displayed, because it could result in deadlocks.

Modal dialogs should rather be intercepted using init(Trigger)

This method will fail if no window was shown by the trigger under the time limit, or if it is used with a non-modal window.



Copyright © 2004-2010. All Rights Reserved.