Package org.fest.swing.core
Class BasicRobot
java.lang.Object
org.fest.swing.core.BasicRobot
- All Implemented Interfaces:
Robot
Understands simulation of user events on a GUI
Component
.-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private static final int
private static final Runnable
private final InputEventGenerator
private final AWTEventPoster
private final ComponentFinder
Looks up
s.Component
private final ComponentHierarchy
Provides access to all the components in the hierarchy.private static InputState
private static final int
private static final ComponentMatcher
private static final int
private final Object
private final Settings
private static Toolkit
private final UnexpectedJOptionPaneFinder
private static final int
private static WindowMonitor
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static Object
private void
Activates the given
.Window
private void
activateWindowOfFocusTarget
(Component target, Component currentOwner) private JPopupMenu
void
cleanUp()
Cleans up any used resources (keyboard, mouse, open windows and
) used by this robot.ScreenLock
private void
cleanUp
(boolean disposeWindows) void
Cleans up any used resources (keyboard, mouse and
) used by this robot.ScreenLock
void
Simulates a user clicking once the given
using the left mouse button.Component
void
Simulates a user clicking at the given position on the given
.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
.Component
void
click
(Component c, MouseButton button) Simulates a user clicking once the given
using the given mouse button.Component
void
click
(Component c, MouseButton button, int times) Simulates a user clicking the given mouse button, the given times on the given
.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
Simulates a user closing the given window.private static void
dispose
(ComponentHierarchy hierarchy, Window w) private static void
disposeWindows
(ComponentHierarchy hierarchy) private void
doPressKey
(int keyCode) void
Simulates a user double-clicking the given
.Component
void
Simulates a user entering the given text.Returns the currently active pop-up menu, if any.private Applet
finder()
Returns the
being used by this robot.ComponentFinder
void
Gives input focus to the given
.Component
private void
void
Returns the
being used by this robot.ComponentHierarchy
invokerAndCenterOfInvoker
(JPopupMenu popupMenu) boolean
isActive()
Indicates whether thisRobot
is active.boolean
Indicates whether the robot is currently in a dragging operation.boolean
Indicates whether the given
is ready for input.Component
private boolean
void
Makes the mouse pointer show small quick jumpy movements on the given
.Component
void
Makes the mouse pointer show small quick jumpy movements on the given
at the given point.Component
private KeyEvent
keyEventFor
(Component c, char character) private void
keyPressAndRelease
(int keyCode, int modifiers) private void
mouseRelease
(int buttons) void
moveMouse
(int x, int y) Simulates a user moving the mouse pointer to the given coordinates.void
Simulates a user moving the mouse pointer to the center of the given
.Component
void
Simulates a user moving the mouse pointer to the given coordinates relative to the given
.Component
void
Simulates a user moving the mouse pointer to the given coordinates relative to the given
.Component
void
Simulates a user moving the mouse pointer to the given coordinates.private void
private boolean
postInvocationEvent
(EventQueue eventQueue, long timeout) 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
.Component
void
pressMouse
(Component c, Point where, MouseButton button) Simulates a user pressing the given mouse button on the given
.Component
void
pressMouse
(Point where, MouseButton button) Simulates a user pressing the given mouse button on the given coordinates.void
pressMouse
(MouseButton button) Simulates a user pressing a mouse button.printer()
Returns the
being used by this robot.BasicComponentPrinter
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
Releases any mouse button(s) used by the robot.private void
void
Ensures that there is no
showing, and potentially blocking GUI tests.JOptionPane
void
Simulates a user right-clicking the given
.Component
static Robot
Creates a new
that has access to all the GUI components in the AWT hierarchy.Robot
static Robot
Creates a new
with a new AWT hierarchy.Robot
void
rotateMouseWheel
(int amount) Rotates the scroll wheel on wheel-equipped mice.void
rotateMouseWheel
(Component c, int amount) Moves the mouse pointer over to the given
and rotates the scroll wheel on wheel-equipped mice.Component
(package private) final Object
private Point
scrollIfNecessary
(JComponent c, Point p) settings()
Returns the configuration settings for this
.Robot
private boolean
showPopupMenu
(Component invoker) Shows a pop-up menu.showPopupMenu
(Component invoker, Point location) Shows a pop-up menu at the given coordinates.void
showWindow
(Window w) Safely display a window with proper EDT synchronization.void
showWindow
(Window w, Dimension size) Safely display a window with proper EDT synchronization.void
showWindow
(Window w, Dimension size, boolean pack) Safely display a window with proper EDT synchronization.void
type
(char character) Types the given character.private boolean
waitForComponentToBeReady
(Component c, long timeout) void
Wait for an idle AWT event queue.private void
waitForIdle
(EventQueue eventQueue) private void
private void
windowAncestorsOf
(Component one, Component two)
-
Field Details
-
POPUP_DELAY
private static final int POPUP_DELAY- See Also:
-
POPUP_TIMEOUT
private static final int POPUP_TIMEOUT- See Also:
-
WINDOW_DELAY
private static final int WINDOW_DELAY- See Also:
-
POPUP_MATCHER
-
active
private volatile boolean active -
EMPTY_RUNNABLE
-
BUTTON_MASK
private static final int BUTTON_MASK- See Also:
-
toolkit
-
windowMonitor
-
inputState
-
hierarchy
Provides access to all the components in the hierarchy. -
screenLockOwner
-
finder
Looks up
s.Component
-
settings
-
eventPoster
-
eventGenerator
-
unexpectedJOptionPaneFinder
-
-
Constructor Details
-
BasicRobot
BasicRobot(Object screenLockOwner, ComponentHierarchy hierarchy)
-
-
Method Details
-
robotWithNewAwtHierarchy
Creates a new
with a new AWT hierarchy. The createdRobot
Robot
will not be able to access any components that were created before it.- Returns:
- the created
Robot
.
-
robotWithCurrentAwtHierarchy
Creates a new
that has access to all the GUI components in the AWT hierarchy.Robot
- Returns:
- the created
Robot
.
-
acquireScreenLock
-
printer
Returns the
being used by this robot.BasicComponentPrinter
-
finder
Returns the
being used by this robot.ComponentFinder
-
showWindow
Safely display a window with proper EDT synchronization. This method blocks until the
is showing and ready for input.Window
- Specified by:
showWindow
in interfaceRobot
- Parameters:
w
- the window to display.
-
showWindow
Safely display a window with proper EDT synchronization. This method blocks until the
is showing and ready for input.Window
- Specified by:
showWindow
in interfaceRobot
- Parameters:
w
- the window to display.size
- the size of the window to display.
-
showWindow
Safely display a 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. The window will be packed if the pack flag is set, and set to the given size if it is non-
Modal dialogs may be shown with this method without blocking.null
.- Specified by:
showWindow
in interfaceRobot
- Parameters:
w
- the window to display.size
- the size of the window to display.pack
- flag that indicates if the window should be packed or not. By packed we mean callingw.pack()
.
-
packAndEnsureSafePosition
-
waitForWindow
-
close
Simulates a user closing the given window. -
findAppletDescendent
- Parameters:
c
- the givenContainer
.- Returns:
- the
Applet
descendant of the givenContainer
, ornull
if none is found.
-
focusAndWaitForFocusGain
- Specified by:
focusAndWaitForFocusGain
in interfaceRobot
- Parameters:
c
- the component to give focus to.
-
focus
Gives input focus to the given
. Note that the component may not yet have focus when this method returns.Component
-
focus
-
activateWindowOfFocusTarget
-
windowAncestorsOf
-
activate
Activates the given
. "Activate" means that the given window gets the keyboard focus.Window
- Parameters:
w
- the window to activate.
-
cleanUp
Cleans up any used resources (keyboard, mouse, open windows and
) used by this robot.ScreenLock
-
cleanUpWithoutDisposingWindows
Cleans up any used resources (keyboard, mouse and
) used by this robot. This method does not dispose any open windows.ScreenLock
Note: The preferred method to use to clean up resources is
. UsingRobot.cleanUp()
may leave many windows open after each test. Use it on very special cases. Please read bug 138 for more details.Robot.cleanUpWithoutDisposingWindows()
- Specified by:
cleanUpWithoutDisposingWindows
in interfaceRobot
-
cleanUp
-
releaseScreenLock
private void releaseScreenLock() -
disposeWindows
-
dispose
-
click
Simulates a user clicking once the given
using the left mouse button.Component
-
rightClick
Simulates a user right-clicking the given
.Component
- Specified by:
rightClick
in interfaceRobot
- Parameters:
c
- theComponent
to click on.
-
click
Simulates a user clicking once the given
using the given mouse button.Component
-
doubleClick
Simulates a user double-clicking the given
.Component
- Specified by:
doubleClick
in interfaceRobot
- Parameters:
c
- theComponent
to click on.
-
click
Simulates a user clicking the given mouse button, the given times on the given
.Component
-
scrollIfNecessary
-
click
Simulates a user clicking at the given position on the given
.Component
-
click
Simulates a user clicking the given mouse button, the given times at the given absolute coordinates. -
click
Simulates a user clicking the given mouse button, the given times at the given position on the given
.Component
-
shouldSetDelayBetweenEventsToZeroWhenClicking
private boolean shouldSetDelayBetweenEventsToZeroWhenClicking(int times) -
pressModifiers
public void pressModifiers(int modifierMask) Presses the appropriate modifiers corresponding to the given mask. Use mask values from
.InputEvent
- Specified by:
pressModifiers
in interfaceRobot
- Parameters:
modifierMask
- the given mask.- See Also:
-
releaseModifiers
public void releaseModifiers(int modifierMask) Releases the appropriate modifiers corresponding to the given mask. Use mask values from
.InputEvent
- Specified by:
releaseModifiers
in interfaceRobot
- Parameters:
modifierMask
- the given mask.- See Also:
-
moveMouse
Simulates a user moving the mouse pointer to the center of the given
.Component
-
moveMouse
Simulates a user moving the mouse pointer to the given coordinates relative to the given
.Component
-
moveMouse
Simulates a user moving the mouse pointer to the given coordinates relative to the given
.Component
-
moveMouse
Simulates a user moving the mouse pointer to the given coordinates. -
moveMouse
public void moveMouse(int x, int y) Simulates a user moving the mouse pointer to the given coordinates. -
pressMouse
Simulates a user pressing a mouse button.- Specified by:
pressMouse
in interfaceRobot
- Parameters:
button
- the mouse button to press.
-
pressMouse
Simulates a user pressing the left mouse button on the given
.Component
- Specified by:
pressMouse
in interfaceRobot
- Parameters:
c
- theComponent
to click on.where
- the given coordinates, relative to the givenComponent
.
-
pressMouse
Simulates a user pressing the given mouse button on the given
.Component
- Specified by:
pressMouse
in interfaceRobot
- Parameters:
c
- theComponent
to click on.where
- the given coordinates, relative to the givenComponent
.button
- the mouse button to press.
-
pressMouse
Simulates a user pressing the given mouse button on the given coordinates.- Specified by:
pressMouse
in interfaceRobot
- Parameters:
where
- the position where to press the given mouse button.button
- the mouse button to press.
-
releaseMouse
Releases the given mouse button.- Specified by:
releaseMouse
in interfaceRobot
- Parameters:
button
- the mouse button to release.
-
releaseMouseButtons
Releases any mouse button(s) used by the robot.- Specified by:
releaseMouseButtons
in interfaceRobot
-
rotateMouseWheel
Moves the mouse pointer over to the given
and rotates the scroll wheel on wheel-equipped mice.Component
- Specified by:
rotateMouseWheel
in interfaceRobot
- Parameters:
c
- the givenComponent
.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) Rotates the scroll wheel on wheel-equipped mice.- Specified by:
rotateMouseWheel
in interfaceRobot
- 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
Makes the mouse pointer show small quick jumpy movements on the given
.Component
-
jitter
Makes the mouse pointer show small quick jumpy movements on the given
at the given point.Component
-
waitForComponentToBeReady
-
invokerAndCenterOfInvoker
-
enterText
Simulates a user entering the given text. Note that this method the key strokes to the component that has input focus. -
type
Types the given character. Note that this method sends the key strokes to the component that has input focus. -
keyEventFor
-
pressAndReleaseKey
Type the given key code with the given modifiers. Modifiers is a mask from the available
masks.InputEvent
- Specified by:
pressAndReleaseKey
in interfaceRobot
- Parameters:
keyCode
- the code of the key to press.modifiers
- the given modifiers.
-
pressAndReleaseKeys
Simulates a user pressing and releasing the given keys. This method does not affect the current focus.- Specified by:
pressAndReleaseKeys
in interfaceRobot
- Parameters:
keyCodes
- one or more codes of the keys to press.- See Also:
-
keyPressAndRelease
-
pressKey
Simulates a user pressing given key. This method does not affect the current focus. -
doPressKey
-
releaseKey
Simulates a user releasing the given key. This method does not affect the current focus.- Specified by:
releaseKey
in interfaceRobot
- Parameters:
keyCode
- the code of the key to release.- See Also:
-
mouseRelease
-
waitForIdle
Wait for an idle AWT event queue. Note that this is different from the implementation ofjava.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 interfaceRobot
-
waitIfNecessary
private void waitIfNecessary() -
waitForIdle
-
postInvocationEvent
-
isDragging
public boolean isDragging()Indicates whether the robot is currently in a dragging operation.- Specified by:
isDragging
in interfaceRobot
- Returns:
true
if the robot is currently in a dragging operation,false
otherwise.
-
showPopupMenu
Shows a pop-up menu.- Specified by:
showPopupMenu
in interfaceRobot
- Parameters:
invoker
- the component to invoke the pop-up menu from.- Returns:
- the displayed pop-up menu.
-
showPopupMenu
Shows a pop-up menu at the given coordinates.- Specified by:
showPopupMenu
in interfaceRobot
- Parameters:
invoker
- the component to invoke the pop-up menu from.location
- the given coordinates for the pop-up menu.- Returns:
- the displayed pop-up menu.
-
isWindowAncestorReadyForInput
-
isReadyForInput
Indicates whether the given
is ready for input.Component
Note: This method is not executed in the event dispatch thread (EDT.) Clients are responsible for invoking this method in the EDT.
- Specified by:
isReadyForInput
in interfaceRobot
- Parameters:
c
- the givenComponent
.- Returns:
true
if the givenComponent
is ready for input,false
otherwise.- Throws:
ActionFailedException
- if the givenComponent
does not have aWindow
ancestor.
-
findActivePopupMenu
Returns the currently active pop-up menu, if any. If no pop-up is currently showing, returnsnull
.- Specified by:
findActivePopupMenu
in interfaceRobot
- Returns:
- the currently active pop-up menu or
null
, if no pop-up is currently showing.
-
activePopupMenu
-
requireNoJOptionPaneIsShowing
Ensures that there is no
showing, and potentially blocking GUI tests.JOptionPane
- Specified by:
requireNoJOptionPaneIsShowing
in interfaceRobot
-
settings
Returns the configuration settings for this
.Robot
-
hierarchy
Returns the
being used by this robot.ComponentHierarchy
-
isActive
public boolean isActive()Indicates whether thisRobot
is active. Being "active" means that
has not been called yet.Robot.cleanUp()
-
screenLockOwner
-