Class BasicRobot

java.lang.Object
org.fest.swing.core.BasicRobot
All Implemented Interfaces:
Robot

public class BasicRobot extends Object implements Robot
Understands simulation of user events on a GUI Component.
  • Field Details

  • Constructor Details

  • Method Details

    • robotWithNewAwtHierarchy

      public static Robot robotWithNewAwtHierarchy()
      Creates a new Robot with a new AWT hierarchy. The created Robot will not be able to access any components that were created before it.
      Returns:
      the created Robot.
    • robotWithCurrentAwtHierarchy

      public static Robot robotWithCurrentAwtHierarchy()
      Creates a new Robot that has access to all the GUI components in the AWT hierarchy.
      Returns:
      the created Robot.
    • acquireScreenLock

      private static Object acquireScreenLock()
    • printer

      public ComponentPrinter printer()
      Returns the BasicComponentPrinter being used by this robot.
      Specified by:
      printer in interface Robot
      Returns:
      the ComponentPrinter being used by this robot.
    • finder

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

      @RunsInEDT public void showWindow(Window w)
      Safely display a 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(Window w, Dimension size)
      Safely display a 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(Window w, Dimension size, boolean pack)

      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-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.
      pack - flag that indicates if the window should be packed or not. By packed we mean calling w.pack().
    • packAndEnsureSafePosition

      @RunsInCurrentThread private void packAndEnsureSafePosition(Window w)
    • waitForWindow

      @RunsInEDT private void waitForWindow(Window w)
    • close

      @RunsInEDT public void close(Window w)
      Simulates a user closing the given window.
      Specified by:
      close in interface Robot
      Parameters:
      w - the window to close.
    • findAppletDescendent

      @RunsInEDT private Applet findAppletDescendent(Container c)
      Returns the Applet descendant of the given Container, if any.
      Parameters:
      c - the given Container.
      Returns:
      the Applet descendant of the given Container, or null if none is found.
    • focusAndWaitForFocusGain

      @RunsInEDT public void focusAndWaitForFocusGain(Component c)
      Gives input focus to the given 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(Component c)
      Gives input focus to the given 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.
    • focus

      @RunsInEDT private void focus(Component target, boolean wait)
    • activateWindowOfFocusTarget

      @RunsInEDT private void activateWindowOfFocusTarget(Component target, Component currentOwner)
    • windowAncestorsOf

      @RunsInEDT private static Pair<Window,Window> windowAncestorsOf(Component one, Component two)
    • activate

      @RunsInEDT private void activate(Window w)
      Activates the given Window. "Activate" means that the given window gets the keyboard focus.
      Parameters:
      w - the window to activate.
    • cleanUp

      @RunsInEDT public void cleanUp()
      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()
      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
    • cleanUp

      @RunsInEDT private void cleanUp(boolean disposeWindows)
    • releaseScreenLock

      private void releaseScreenLock()
    • disposeWindows

      @RunsInEDT private static void disposeWindows(ComponentHierarchy hierarchy)
    • dispose

      @RunsInCurrentThread private static void dispose(ComponentHierarchy hierarchy, Window w)
    • click

      @RunsInEDT public void click(Component c)
      Simulates a user clicking once the given Component using the left mouse button.
      Specified by:
      click in interface Robot
      Parameters:
      c - the Component to click on.
    • rightClick

      @RunsInEDT public void rightClick(Component c)
      Simulates a user right-clicking the given Component.
      Specified by:
      rightClick in interface Robot
      Parameters:
      c - the Component to click on.
    • click

      @RunsInEDT public void click(Component c, MouseButton button)
      Simulates a user clicking once the given 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(Component c)
      Simulates a user double-clicking the given Component.
      Specified by:
      doubleClick in interface Robot
      Parameters:
      c - the Component to click on.
    • click

      @RunsInEDT public void click(Component c, MouseButton button, int times)
      Simulates a user clicking the given mouse button, the given times on the given 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.
    • scrollIfNecessary

      private Point scrollIfNecessary(JComponent c, Point p)
    • click

      @RunsInEDT public void click(Component c, Point where)
      Simulates a user clicking at the given position on the given 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(Point where, MouseButton button, int times)
      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(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.
      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.
    • 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 interface Robot
      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 interface Robot
      Parameters:
      modifierMask - the given mask.
      See Also:
    • moveMouse

      @RunsInEDT public void moveMouse(Component c)
      Simulates a user moving the mouse pointer to the center of the given Component.
      Specified by:
      moveMouse in interface Robot
      Parameters:
      c - the given Component.
    • moveMouse

      @RunsInEDT public void moveMouse(Component c, Point p)
      Simulates a user moving the mouse pointer to the given coordinates relative to the given 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(Component c, int x, int y)
      Simulates a user moving the mouse pointer to the given coordinates relative to the given 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(Point p)
      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)
      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(MouseButton button)
      Simulates a user pressing a mouse button.
      Specified by:
      pressMouse in interface Robot
      Parameters:
      button - the mouse button to press.
    • pressMouse

      public void pressMouse(Component c, Point where)
      Simulates a user pressing the left mouse button on the given 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(Component c, Point where, MouseButton button)
      Simulates a user pressing the given mouse button on the given 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(Point where, MouseButton button)
      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(MouseButton button)
      Releases the given mouse button.
      Specified by:
      releaseMouse in interface Robot
      Parameters:
      button - the mouse button to release.
    • releaseMouseButtons

      @RunsInEDT public void releaseMouseButtons()
      Releases any mouse button(s) used by the robot.
      Specified by:
      releaseMouseButtons in interface Robot
    • rotateMouseWheel

      public void rotateMouseWheel(Component c, int amount)
      Moves the mouse pointer over to the given 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)
      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(Component c)
      Makes the mouse pointer show small quick jumpy movements on the given Component.
      Specified by:
      jitter in interface Robot
      Parameters:
      c - the given Component.
    • jitter

      @RunsInEDT public void jitter(Component c, Point where)
      Makes the mouse pointer show small quick jumpy movements on the given Component at the given point.
      Specified by:
      jitter in interface Robot
      Parameters:
      c - the given Component.
      where - the given point.
    • waitForComponentToBeReady

      @RunsInEDT private boolean waitForComponentToBeReady(Component c, long timeout)
    • invokerAndCenterOfInvoker

      @RunsInEDT private static Pair<Component,Point> invokerAndCenterOfInvoker(JPopupMenu popupMenu)
    • enterText

      @RunsInEDT public void enterText(String text)
      Simulates a user entering the given text. Note that this method the key strokes to the component that has input focus.
      Specified by:
      enterText in interface Robot
      Parameters:
      text - the text to enter.
    • type

      @RunsInEDT public void type(char character)
      Types the given character. Note that this method sends the key strokes to the component that has input focus.
      Specified by:
      type in interface Robot
      Parameters:
      character - the character to type.
    • keyEventFor

      private KeyEvent keyEventFor(Component c, char character)
    • pressAndReleaseKey

      @RunsInEDT public void pressAndReleaseKey(int keyCode, int... modifiers)
      Type the given key code with the given modifiers. Modifiers is a mask from the available 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(int... keyCodes)
      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:
    • keyPressAndRelease

      @RunsInEDT private void keyPressAndRelease(int keyCode, int modifiers)
    • pressKey

      @RunsInEDT public void pressKey(int keyCode)
      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:
    • doPressKey

      @RunsInEDT private void doPressKey(int keyCode)
    • releaseKey

      @RunsInEDT public void releaseKey(int keyCode)
      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:
    • mouseRelease

      @RunsInEDT private void mouseRelease(int buttons)
    • waitForIdle

      @RunsInEDT public void waitForIdle()
      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
    • waitIfNecessary

      private void waitIfNecessary()
    • waitForIdle

      private void waitForIdle(EventQueue eventQueue)
    • postInvocationEvent

      @RunsInEDT private boolean postInvocationEvent(EventQueue eventQueue, long timeout)
    • isDragging

      public boolean isDragging()
      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 public JPopupMenu showPopupMenu(Component invoker)
      Shows a pop-up menu.
      Specified by:
      showPopupMenu in interface Robot
      Parameters:
      invoker - the component to invoke the pop-up menu from.
      Returns:
      the displayed pop-up menu.
    • showPopupMenu

      @RunsInEDT public JPopupMenu showPopupMenu(Component invoker, Point location)
      Shows a pop-up menu at the given coordinates.
      Specified by:
      showPopupMenu in interface Robot
      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

      @RunsInEDT private boolean isWindowAncestorReadyForInput(JPopupMenu popup)
    • isReadyForInput

      @RunsInCurrentThread public boolean isReadyForInput(Component c)
      Indicates whether the given Component is ready for input.

      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 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 public JPopupMenu findActivePopupMenu()
      Returns the currently active pop-up menu, if any. If no pop-up is currently showing, returns null.
      Specified by:
      findActivePopupMenu in interface Robot
      Returns:
      the currently active pop-up menu or null, if no pop-up is currently showing.
    • activePopupMenu

      @RunsInEDT private JPopupMenu activePopupMenu()
    • requireNoJOptionPaneIsShowing

      @RunsInEDT public void requireNoJOptionPaneIsShowing()
      Ensures that there is no JOptionPane showing, and potentially blocking GUI tests.
      Specified by:
      requireNoJOptionPaneIsShowing in interface Robot
    • settings

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

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

      public boolean isActive()
      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.
    • screenLockOwner

      final Object screenLockOwner()