org.assertj.swing.core
Class BasicRobot

java.lang.Object
  extended by org.assertj.swing.core.BasicRobot
All Implemented Interfaces:
Robot

public class BasicRobot
extends Object
implements Robot

Default implementation of Robot.

Author:
Alex Ruiz, Yvonne Wang
See Also:
Robot

Method Summary
 void cleanUp()
          Cleans up any used resources (keyboard, mouse, open windows and ScreenLock) used by this Robot.
 void cleanUpWithoutDisposingWindows()
           Cleans up any used resources (keyboard, mouse and ScreenLock) used by this Robot.
 void click(Component c)
          Simulates a user clicking once the given AWT or Swing Component using the left mouse button.
 void click(Component c, MouseButton button)
          Simulates a user clicking once the given AWT or Swing Component using the given mouse button.
 void click(Component c, MouseButton button, int times)
          Simulates a user clicking the given mouse button, the given times on the given AWT or Swing Component.
 void click(Component c, Point where)
          Simulates a user clicking at the given position on the given AWT or Swing Component.
 void click(Component c, Point where, MouseButton button, int times)
          Simulates a user clicking the given mouse button, the given times at the given position on the given AWT or Swing Component.
 void click(Point where, MouseButton button, int times)
          Simulates a user clicking the given mouse button, the given times at the given absolute coordinates.
 void close(Window w)
          Simulates a user closing the given AWT or Swing Window.
 void doubleClick(Component c)
          Simulates a user double-clicking the given AWT or Swing Component.
 void enterText(String text)
          Simulates a user entering the given text.
 JPopupMenu findActivePopupMenu()
           
 ComponentFinder finder()
           
 void focus(Component c)
          Gives input focus to the given AWT or Swing Component.
 void focusAndWaitForFocusGain(Component c)
          Gives input focus to the given AWT or Swing Component and waits until the Component has focus.
 ComponentHierarchy hierarchy()
           
 boolean isActive()
          Indicates whether this Robot is active.
 boolean isDragging()
          Indicates whether the robot is currently in a dragging operation.
 boolean isReadyForInput(Component c)
           Indicates whether the given AWT or Swing Component is ready for input.
 void jitter(Component c)
          Makes the mouse pointer show small quick jumpy movements on the given AWT or Swing Component.
 void jitter(Component c, Point where)
          Makes the mouse pointer show small quick jumpy movements on the given AWT or Swing Component at the given point.
 void moveMouse(Component c)
          Simulates a user moving the mouse pointer to the center of the given AWT or Swing Component.
 void moveMouse(Component c, int x, int y)
          Simulates a user moving the mouse pointer to the given coordinates relative to the given AWT or Swing Component.
 void moveMouse(Component c, Point p)
          Simulates a user moving the mouse pointer to the given coordinates relative to the given AWT or Swing Component.
 void moveMouse(int x, int y)
          Simulates a user moving the mouse pointer to the given coordinates.
 void moveMouse(Point p)
          Simulates a user moving the mouse pointer to the given coordinates.
 void pressAndReleaseKey(int keyCode, int... modifiers)
          Type the given key code with the given modifiers.
 void pressAndReleaseKeys(int... keyCodes)
          Simulates a user pressing and releasing the given keys.
 void pressKey(int keyCode)
          Simulates a user pressing given key.
 void pressModifiers(int modifierMask)
          Presses the appropriate modifiers corresponding to the given mask.
 void pressMouse(Component c, Point where)
          Simulates a user pressing the left mouse button on the given AWT or Swing Component.
 void pressMouse(Component c, Point where, MouseButton button)
          Simulates a user pressing the given mouse button on the given AWT or Swing Component.
 void pressMouse(MouseButton button)
          Simulates a user pressing a mouse button.
 void pressMouse(Point where, MouseButton button)
          Simulates a user pressing the given mouse button on the given coordinates.
 ComponentPrinter printer()
           
 void releaseKey(int keyCode)
          Simulates a user releasing the given key.
 void releaseModifiers(int modifierMask)
          Releases the appropriate modifiers corresponding to the given mask.
 void releaseMouse(MouseButton button)
          Releases the given mouse button.
 void releaseMouseButtons()
          Releases any mouse button(s) used by the robot.
 void requireNoJOptionPaneIsShowing()
          Ensures that there is no JOptionPane showing, and potentially blocking GUI tests.
 void rightClick(Component c)
          Simulates a user right-clicking the given AWT or Swing Component.
static Robot robotWithCurrentAwtHierarchy()
          Creates a new Robot that has access to all the AWT and Swing Components in the AWT hierarchy.
static Robot robotWithCurrentAwtHierarchyWithoutScreenLock()
           
static Robot robotWithNewAwtHierarchy()
          Creates a new Robot with a new AWT hierarchy.
static Robot robotWithNewAwtHierarchyWithoutScreenLock()
           
 void rotateMouseWheel(Component c, int amount)
          Moves the mouse pointer over to the given AWT or Swing Component and rotates the scroll wheel on wheel-equipped mice.
 void rotateMouseWheel(int amount)
          Rotates the scroll wheel on wheel-equipped mice.
 Settings settings()
           
 JPopupMenu showPopupMenu(Component invoker)
          Shows a JPopupMenu.
 JPopupMenu showPopupMenu(Component invoker, Point location)
          Shows a JPopupMenu at the given coordinates.
 void showWindow(Window w)
          Safely display an AWT or Swing Window with proper EDT synchronization.
 void showWindow(Window w, Dimension size)
          Safely display an AWT or Swing Window with proper EDT synchronization.
 void showWindow(Window w, Dimension size, boolean pack)
           Safely display an AWT or Swing Window with proper EDT synchronization.
 void type(char character)
          Types the given character.
 void waitForIdle()
          Wait for an idle AWT event queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

robotWithNewAwtHierarchy

@Nonnull
public static Robot robotWithNewAwtHierarchy()
Creates a new Robot with a new AWT hierarchy. The created Robot will not be able to access any AWT and Swing Components that were created before it.

Returns:
the created Robot.

robotWithNewAwtHierarchyWithoutScreenLock

@Nonnull
public static Robot robotWithNewAwtHierarchyWithoutScreenLock()

robotWithCurrentAwtHierarchy

@Nonnull
public static Robot robotWithCurrentAwtHierarchy()
Creates a new Robot that has access to all the AWT and Swing Components in the AWT hierarchy.

Returns:
the created Robot.

robotWithCurrentAwtHierarchyWithoutScreenLock

@Nonnull
public static Robot robotWithCurrentAwtHierarchyWithoutScreenLock()

printer

@Nonnull
public ComponentPrinter printer()
Specified by:
printer in interface Robot
Returns:
the ComponentPrinter being used by this robot.

finder

@Nonnull
public ComponentFinder finder()
Specified by:
finder in interface Robot
Returns:
the ComponentFinder being used by this robot.

showWindow

@RunsInEDT
public void showWindow(@Nonnull
                                 Window w)
Description copied from interface: Robot
Safely display an AWT or Swing Window with proper EDT synchronization. This method blocks until the Window is showing and ready for input.

Specified by:
showWindow in interface Robot
Parameters:
w - the window to display.

showWindow

@RunsInEDT
public void showWindow(@Nonnull
                                 Window w,
                                 @Nonnull
                                 Dimension size)
Description copied from interface: Robot
Safely display an AWT or Swing Window with proper EDT synchronization. This method blocks until the Window is showing and ready for input.

Specified by:
showWindow in interface Robot
Parameters:
w - the window to display.
size - the size of the window to display.

showWindow

@RunsInEDT
public void showWindow(@Nonnull
                                 Window w,
                                 @Nullable
                                 Dimension size,
                                 boolean pack)
Description copied from interface: Robot

Safely display an AWT or Swing Window with proper EDT synchronization. This method blocks until the Window is showing. This method will return even when the window is a modal dialog, since the show method is called on the event dispatch thread (EDT.) The Window will be packed if the pack flag is set, and set to the given size if it is non-null.

Modal dialogs may be shown with this method without blocking.

Specified by:
showWindow in interface Robot
Parameters:
w - the window to display.
size - the size of the window to display. A null value indicates that the window does not need to be resized.
pack - flag that indicates if the window should be packed or not. By packed we mean calling w.pack().

close

@RunsInEDT
public void close(@Nonnull
                            Window w)
Description copied from interface: Robot
Simulates a user closing the given AWT or Swing Window.

Specified by:
close in interface Robot
Parameters:
w - the Window to close.

focusAndWaitForFocusGain

@RunsInEDT
public void focusAndWaitForFocusGain(@Nonnull
                                               Component c)
Description copied from interface: Robot
Gives input focus to the given AWT or Swing Component and waits until the Component has focus.

Specified by:
focusAndWaitForFocusGain in interface Robot
Parameters:
c - the Component to give focus to.

focus

@RunsInEDT
public void focus(@Nonnull
                            Component c)
Description copied from interface: Robot
Gives input focus to the given AWT or Swing Component. Note that the Component may not yet have focus when this method returns.

Specified by:
focus in interface Robot
Parameters:
c - the Component to give focus to.

cleanUp

@RunsInEDT
public void cleanUp()
Description copied from interface: Robot
Cleans up any used resources (keyboard, mouse, open windows and ScreenLock) used by this Robot.

Specified by:
cleanUp in interface Robot

cleanUpWithoutDisposingWindows

@RunsInEDT
public void cleanUpWithoutDisposingWindows()
Description copied from interface: Robot

Cleans up any used resources (keyboard, mouse and ScreenLock) used by this Robot. This method does not dispose any open windows.

Note: The preferred method to use to clean up resources is Robot.cleanUp(). Using Robot.cleanUpWithoutDisposingWindows() may leave many windows open after each test. Use it on very special cases. Please read bug 138 for more details.

Specified by:
cleanUpWithoutDisposingWindows in interface Robot

click

@RunsInEDT
public void click(@Nonnull
                            Component c)
Description copied from interface: Robot
Simulates a user clicking once the given AWT or Swing Component using the left mouse button.

Specified by:
click in interface Robot
Parameters:
c - the Component to click on.

rightClick

@RunsInEDT
public void rightClick(@Nonnull
                                 Component c)
Description copied from interface: Robot
Simulates a user right-clicking the given AWT or Swing Component.

Specified by:
rightClick in interface Robot
Parameters:
c - the Component to click on.

click

@RunsInEDT
public void click(@Nonnull
                            Component c,
                            @Nonnull
                            MouseButton button)
Description copied from interface: Robot
Simulates a user clicking once the given AWT or Swing Component using the given mouse button.

Specified by:
click in interface Robot
Parameters:
c - the Component to click on.
button - the mouse button to use.

doubleClick

@RunsInEDT
public void doubleClick(@Nonnull
                                  Component c)
Description copied from interface: Robot
Simulates a user double-clicking the given AWT or Swing Component.

Specified by:
doubleClick in interface Robot
Parameters:
c - the Component to click on.

click

@RunsInEDT
public void click(@Nonnull
                            Component c,
                            @Nonnull
                            MouseButton button,
                            int times)
Description copied from interface: Robot
Simulates a user clicking the given mouse button, the given times on the given AWT or Swing Component.

Specified by:
click in interface Robot
Parameters:
c - the Component to click on.
button - the mouse button to click.
times - the number of times to click the given mouse button.

click

@RunsInEDT
public void click(@Nonnull
                            Component c,
                            @Nonnull
                            Point where)
Description copied from interface: Robot
Simulates a user clicking at the given position on the given AWT or Swing Component.

Specified by:
click in interface Robot
Parameters:
c - the Component to click on.
where - the given coordinates, relative to the given Component.

click

@RunsInEDT
public void click(@Nonnull
                            Point where,
                            @Nonnull
                            MouseButton button,
                            int times)
Description copied from interface: Robot
Simulates a user clicking the given mouse button, the given times at the given absolute coordinates.

Specified by:
click in interface Robot
Parameters:
where - the coordinates where to click.
button - the mouse button to click.
times - the number of times to click the given mouse button.

click

@RunsInEDT
public void click(@Nonnull
                            Component c,
                            @Nonnull
                            Point where,
                            @Nonnull
                            MouseButton button,
                            int times)
Description copied from interface: Robot
Simulates a user clicking the given mouse button, the given times at the given position on the given AWT or Swing Component.

Specified by:
click in interface Robot
Parameters:
c - the Component to click on.
where - the given coordinates, relative to the given Component.
button - the mouse button to click.
times - the number of times to click the given mouse button.

pressModifiers

public void pressModifiers(int modifierMask)
Description copied from interface: Robot
Presses the appropriate modifiers corresponding to the given mask. Use mask values from java.awt.event.InputEvent.

Specified by:
pressModifiers in interface Robot
Parameters:
modifierMask - the given mask.
See Also:
InputEvent

releaseModifiers

public void releaseModifiers(int modifierMask)
Description copied from interface: Robot
Releases the appropriate modifiers corresponding to the given mask. Use mask values from java.awt.event.InputEvent.

Specified by:
releaseModifiers in interface Robot
Parameters:
modifierMask - the given mask.
See Also:
InputEvent

moveMouse

@RunsInEDT
public void moveMouse(@Nonnull
                                Component c)
Description copied from interface: Robot
Simulates a user moving the mouse pointer to the center of the given AWT or Swing Component.

Specified by:
moveMouse in interface Robot
Parameters:
c - the given Component.

moveMouse

@RunsInEDT
public void moveMouse(@Nonnull
                                Component c,
                                @Nonnull
                                Point p)
Description copied from interface: Robot
Simulates a user moving the mouse pointer to the given coordinates relative to the given AWT or Swing Component.

Specified by:
moveMouse in interface Robot
Parameters:
c - the given Component.
p - the given coordinates, relative to the given Component.

moveMouse

@RunsInEDT
public void moveMouse(@Nonnull
                                Component c,
                                int x,
                                int y)
Description copied from interface: Robot
Simulates a user moving the mouse pointer to the given coordinates relative to the given AWT or Swing Component.

Specified by:
moveMouse in interface Robot
Parameters:
c - the given Component.
x - X coordinate, relative to the given Component.
y - Y coordinate, relative to the given Component.

moveMouse

public void moveMouse(@Nonnull
                      Point p)
Description copied from interface: Robot
Simulates a user moving the mouse pointer to the given coordinates.

Specified by:
moveMouse in interface Robot
Parameters:
p - the given coordinates.

moveMouse

public void moveMouse(int x,
                      int y)
Description copied from interface: Robot
Simulates a user moving the mouse pointer to the given coordinates.

Specified by:
moveMouse in interface Robot
Parameters:
x - X coordinate.
y - Y coordinate.

pressMouse

public void pressMouse(@Nonnull
                       MouseButton button)
Description copied from interface: Robot
Simulates a user pressing a mouse button.

Specified by:
pressMouse in interface Robot
Parameters:
button - the mouse button to press.

pressMouse

public void pressMouse(@Nonnull
                       Component c,
                       @Nonnull
                       Point where)
Description copied from interface: Robot
Simulates a user pressing the left mouse button on the given AWT or Swing Component.

Specified by:
pressMouse in interface Robot
Parameters:
c - the Component to click on.
where - the given coordinates, relative to the given Component.

pressMouse

public void pressMouse(@Nonnull
                       Component c,
                       @Nonnull
                       Point where,
                       @Nonnull
                       MouseButton button)
Description copied from interface: Robot
Simulates a user pressing the given mouse button on the given AWT or Swing Component.

Specified by:
pressMouse in interface Robot
Parameters:
c - the Component to click on.
where - the given coordinates, relative to the given Component.
button - the mouse button to press.

pressMouse

public void pressMouse(@Nonnull
                       Point where,
                       @Nonnull
                       MouseButton button)
Description copied from interface: Robot
Simulates a user pressing the given mouse button on the given coordinates.

Specified by:
pressMouse in interface Robot
Parameters:
where - the position where to press the given mouse button.
button - the mouse button to press.

releaseMouse

@RunsInEDT
public void releaseMouse(@Nonnull
                                   MouseButton button)
Description copied from interface: Robot
Releases the given mouse button.

Specified by:
releaseMouse in interface Robot
Parameters:
button - the mouse button to release.

releaseMouseButtons

@RunsInEDT
public void releaseMouseButtons()
Description copied from interface: Robot
Releases any mouse button(s) used by the robot.

Specified by:
releaseMouseButtons in interface Robot

rotateMouseWheel

public void rotateMouseWheel(@Nonnull
                             Component c,
                             int amount)
Description copied from interface: Robot
Moves the mouse pointer over to the given AWT or Swing Component and rotates the scroll wheel on wheel-equipped mice.

Specified by:
rotateMouseWheel in interface Robot
Parameters:
c - the given Component.
amount - number of "notches" to move the mouse wheel. Negative values indicate movement up/away from the user, while positive values indicate movement down/towards the user.

rotateMouseWheel

public void rotateMouseWheel(int amount)
Description copied from interface: Robot
Rotates the scroll wheel on wheel-equipped mice.

Specified by:
rotateMouseWheel in interface Robot
Parameters:
amount - number of "notches" to move the mouse wheel. Negative values indicate movement up/away from the user, while positive values indicate movement down/towards the user.

jitter

@RunsInEDT
public void jitter(@Nonnull
                             Component c)
Description copied from interface: Robot
Makes the mouse pointer show small quick jumpy movements on the given AWT or Swing Component.

Specified by:
jitter in interface Robot
Parameters:
c - the given Component.

jitter

@RunsInEDT
public void jitter(@Nonnull
                             Component c,
                             @Nonnull
                             Point where)
Description copied from interface: Robot
Makes the mouse pointer show small quick jumpy movements on the given AWT or Swing Component at the given point.

Specified by:
jitter in interface Robot
Parameters:
c - the given Component.
where - the given point.

enterText

@RunsInEDT
public void enterText(@Nonnull
                                String text)
Description copied from interface: Robot
Simulates a user entering the given text. Note that this method the key strokes to the AWT or Swing Component that has input focus.

Specified by:
enterText in interface Robot
Parameters:
text - the text to enter.

type

@RunsInEDT
public void type(char character)
Description copied from interface: Robot
Types the given character. Note that this method sends the key strokes to the AWT or Swing Component that has input focus.

Specified by:
type in interface Robot
Parameters:
character - the character to type.

pressAndReleaseKey

@RunsInEDT
public void pressAndReleaseKey(int keyCode,
                                         @Nonnull
                                         int... modifiers)
Description copied from interface: Robot
Type the given key code with the given modifiers. Modifiers is a mask from the available java.awt.event.InputEvent masks.

Specified by:
pressAndReleaseKey in interface Robot
Parameters:
keyCode - the code of the key to press.
modifiers - the given modifiers.

pressAndReleaseKeys

@RunsInEDT
public void pressAndReleaseKeys(@Nonnull
                                          int... keyCodes)
Description copied from interface: Robot
Simulates a user pressing and releasing the given keys. This method does not affect the current focus.

Specified by:
pressAndReleaseKeys in interface Robot
Parameters:
keyCodes - one or more codes of the keys to press.
See Also:
KeyEvent

pressKey

@RunsInEDT
public void pressKey(int keyCode)
Description copied from interface: Robot
Simulates a user pressing given key. This method does not affect the current focus.

Specified by:
pressKey in interface Robot
Parameters:
keyCode - the code of the key to press.
See Also:
KeyEvent

releaseKey

@RunsInEDT
public void releaseKey(int keyCode)
Description copied from interface: Robot
Simulates a user releasing the given key. This method does not affect the current focus.

Specified by:
releaseKey in interface Robot
Parameters:
keyCode - the code of the key to release.
See Also:
KeyEvent

waitForIdle

@RunsInEDT
public void waitForIdle()
Description copied from interface: Robot
Wait for an idle AWT event queue. Note that this is different from the implementation of java.awt.Robot.waitForIdle(), which may have events on the queue when it returns. Do NOT use this method if there are animations or other continual refreshes happening, since in that case it may never return.

Specified by:
waitForIdle in interface Robot

isDragging

public boolean isDragging()
Description copied from interface: Robot
Indicates whether the robot is currently in a dragging operation.

Specified by:
isDragging in interface Robot
Returns:
true if the robot is currently in a dragging operation, false otherwise.

showPopupMenu

@RunsInEDT
@Nonnull
public JPopupMenu showPopupMenu(@Nonnull
                                                  Component invoker)
Description copied from interface: Robot
Shows a JPopupMenu.

Specified by:
showPopupMenu in interface Robot
Parameters:
invoker - the Component to invoke the JPopupMenu from.
Returns:
the displayed JPopupMenu.

showPopupMenu

@RunsInEDT
@Nonnull
public JPopupMenu showPopupMenu(@Nonnull
                                                  Component invoker,
                                                  @Nonnull
                                                  Point location)
Description copied from interface: Robot
Shows a JPopupMenu at the given coordinates.

Specified by:
showPopupMenu in interface Robot
Parameters:
invoker - the Component to invoke the JPopupMenu from.
location - the given coordinates for the JPopupMenu.
Returns:
the displayed JPopupMenu.

isReadyForInput

@RunsInCurrentThread
public boolean isReadyForInput(@Nonnull
                                                   Component c)

Indicates whether the given AWT or Swing Component is ready for input.

Note: This method is accessed in the current executing thread. Such thread may or may not be the event dispatch thread (EDT.) Client code must call this method from the EDT.

Specified by:
isReadyForInput in interface Robot
Parameters:
c - the given Component.
Returns:
true if the given Component is ready for input, false otherwise.
Throws:
ActionFailedException - if the given Component does not have a Window ancestor.

findActivePopupMenu

@RunsInEDT
@Nullable
public JPopupMenu findActivePopupMenu()
Specified by:
findActivePopupMenu in interface Robot
Returns:
the currently active JPopupMenu or null, if no pop-up is currently showing.

requireNoJOptionPaneIsShowing

@RunsInEDT
public void requireNoJOptionPaneIsShowing()
Description copied from interface: Robot
Ensures that there is no JOptionPane showing, and potentially blocking GUI tests.

Specified by:
requireNoJOptionPaneIsShowing in interface Robot

settings

@Nonnull
public Settings settings()
Specified by:
settings in interface Robot
Returns:
the configuration settings for this Robot.

hierarchy

@Nonnull
public ComponentHierarchy hierarchy()
Specified by:
hierarchy in interface Robot
Returns:
the ComponentHierarchy being used by this robot.

isActive

public boolean isActive()
Description copied from interface: Robot
Indicates whether this Robot is active. Being "active" means that Robot.cleanUp() has not been called yet.

Specified by:
isActive in interface Robot
Returns:
true if this Robot is active, false otherwise.


Copyright © 2014–2015 AssertJ. All rights reserved.