Class ComponentDriver

java.lang.Object
org.fest.swing.driver.ComponentDriver
Direct Known Subclasses:
ContainerDriver, JAppletDriver

public class ComponentDriver extends Object
Understands functional testing of Components:
  • user input simulation
  • state verification
  • property value query
This class is intended for internal use only. Please use the classes in the package org.fest.swing.fixture in your tests.
  • Field Details

  • Constructor Details

    • ComponentDriver

      public ComponentDriver(Robot robot)
      Creates a new ComponentDriver.
      Parameters:
      robot - the robot to use to simulate user input.
  • Method Details

    • click

      @RunsInEDT public void click(Component c)
      Simulates a user clicking once the given Component using the left mouse button.
      Parameters:
      c - the Component to click on.
      Throws:
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • click

      @RunsInEDT public void click(Component c, MouseButton button)
      Simulates a user clicking once the given Component using the given mouse button.
      Parameters:
      c - the Component to click on.
      button - the mouse button to use.
      Throws:
      NullPointerException - if the given MouseButton is null.
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • click

      @RunsInEDT public void click(Component c, MouseClickInfo mouseClickInfo)
      Simulates a user clicking the given mouse button, the given times on the given Component.
      Parameters:
      c - the Component to click on.
      mouseClickInfo - specifies the button to click and the times the button should be clicked.
      Throws:
      NullPointerException - if the given MouseClickInfo is null.
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • doubleClick

      @RunsInEDT public void doubleClick(Component c)
      Simulates a user double-clicking the given Component.
      Parameters:
      c - the Component to click on.
      Throws:
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • rightClick

      @RunsInEDT public void rightClick(Component c)
      Simulates a user right-clicking the given Component.
      Parameters:
      c - the Component to click on.
      Throws:
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • 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.
      Parameters:
      c - the Component to click on.
      button - the mouse button to click.
      times - the number of times to click the given mouse button.
      Throws:
      NullPointerException - if the given MouseButton is null.
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • click

      @RunsInEDT public void click(Component c, Point where)
      Simulates a user clicking at the given position on the given Component.
      Parameters:
      c - the Component to click on.
      where - the position where to click.
      Throws:
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • settings

      protected Settings settings()
    • requireSize

      @RunsInEDT public void requireSize(Component c, Dimension size)
      Asserts that the size of the Component is equal to given one.
      Parameters:
      c - the target component.
      size - the given size to match.
      Throws:
      AssertionError - if the size of the Window is not equal to the given size.
    • requireVisible

      @RunsInEDT public void requireVisible(Component c)
      Asserts that the Component is visible.
      Parameters:
      c - the target component.
      Throws:
      AssertionError - if the Component is not visible.
    • requireNotVisible

      @RunsInEDT public void requireNotVisible(Component c)
      Asserts that the Component is not visible.
      Parameters:
      c - the target component.
      Throws:
      AssertionError - if the Component is visible.
    • visibleProperty

      @RunsInEDT private static org.fest.assertions.Description visibleProperty(Component c)
    • requireFocused

      @RunsInEDT public void requireFocused(Component c)
      Asserts that the Component has input focus.
      Parameters:
      c - the target component.
      Throws:
      AssertionError - if the Component does not have input focus.
    • requiredFocusedErrorMessage

      private static org.fest.assertions.Description requiredFocusedErrorMessage(Component c)
    • requireEnabled

      @RunsInEDT public void requireEnabled(Component c)
      Asserts that the Component is enabled.
      Parameters:
      c - the target component.
      Throws:
      AssertionError - if the Component is disabled.
    • requireEnabled

      @RunsInEDT public void requireEnabled(Component c, Timeout timeout)
      Asserts that the Component is enabled.
      Parameters:
      c - the target component.
      timeout - the time this fixture will wait for the component to be enabled.
      Throws:
      WaitTimedOutError - if the Component is never enabled.
    • requireDisabled

      @RunsInEDT public void requireDisabled(Component c)
      Asserts that the Component is disabled.
      Parameters:
      c - the target component.
      Throws:
      AssertionError - if the Component is enabled.
    • enabledProperty

      @RunsInEDT private static org.fest.assertions.Description enabledProperty(Component c)
    • pressAndReleaseKeys

      @RunsInEDT public void pressAndReleaseKeys(Component c, int... keyCodes)
      Simulates a user pressing and releasing the given keys on the Component.
      Parameters:
      c - the target component.
      keyCodes - one or more codes of the keys to press.
      Throws:
      NullPointerException - if the given array of codes is null.
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
      IllegalArgumentException - if the given code is not a valid key code.
      See Also:
    • pressAndReleaseKey

      @RunsInEDT public void pressAndReleaseKey(Component c, KeyPressInfo keyPressInfo)
      Simulates a user pressing and releasing the given key on the Component. Modifiers is a mask from the available InputEvent masks.
      Parameters:
      c - the target component.
      keyPressInfo - specifies the key and modifiers to press.
      Throws:
      NullPointerException - if the given KeyPressInfo is null.
      IllegalArgumentException - if the given code is not a valid key code.
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
      See Also:
    • pressAndReleaseKey

      @RunsInEDT public void pressAndReleaseKey(Component c, int keyCode, int[] modifiers)
      Simulates a user pressing and releasing the given key on the Component. Modifiers is a mask from the available InputEvent masks.
      Parameters:
      c - the target component.
      keyCode - the code of the key to press.
      modifiers - the given modifiers.
      Throws:
      IllegalArgumentException - if the given code is not a valid key code. *
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
      See Also:
    • pressKey

      @RunsInEDT public void pressKey(Component c, int keyCode)
      Simulates a user pressing given key on the Component.
      Parameters:
      c - the target component.
      keyCode - the code of the key to press.
      Throws:
      IllegalArgumentException - if the given code is not a valid key code.
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
      See Also:
    • releaseKey

      @RunsInEDT public void releaseKey(Component c, int keyCode)
      Simulates a user releasing the given key on the Component.
      Parameters:
      c - the target component.
      keyCode - the code of the key to release.
      Throws:
      IllegalArgumentException - if the given code is not a valid key code.
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
      See Also:
    • focusAndWaitForFocusGain

      @RunsInEDT public void focusAndWaitForFocusGain(Component c)
      Gives input focus to the given Component and waits until the Component has focus.
      Parameters:
      c - the component to give focus to.
      Throws:
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • 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.
      Parameters:
      c - the component to give focus to.
      Throws:
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • drag

      @RunsInEDT protected final void drag(Component c, Point where)
      Performs a drag action at the given point.
      Parameters:
      c - the target component.
      where - the point where to start the drag action.
    • drop

      @RunsInEDT protected final void drop(Component c, Point where)
      Ends a drag operation, releasing the mouse button over the given target location.

      This method is tuned for native drag/drop operations, so if you get odd behavior, you might try using a simple Robot.moveMouse(Component, int, int) and Robot.releaseMouseButtons().

      Parameters:
      c - the target component.
      where - the point where the drag operation ends.
      Throws:
      ActionFailedException - if there is no drag action in effect.
    • dragOver

      protected final void dragOver(Component c, Point where)
      Move the mouse appropriately to get from the source to the destination. Enter/exit events will be generated where appropriate.
      Parameters:
      c - the target component.
      where - the point to drag over.
    • performAccessibleActionOf

      @RunsInCurrentThread protected final void performAccessibleActionOf(Component c)
      Performs the AccessibleAction in the given Component's event queue.

      Note: This method is not executed in the event dispatch thread (EDT.) Clients are responsible for invoking this method in the EDT.

      Parameters:
      c - the given Component.
      Throws:
      ActionFailedException - if action is null or empty.
    • waitForShowing

      @RunsInCurrentThread protected final boolean waitForShowing(Component c, long timeout)
      Wait the given number of milliseconds for the Component to be showing and ready. Returns false if the operation times out.

      Note: This method is not executed in the event dispatch thread (EDT.) Clients are responsible for invoking this method in the EDT.

      Parameters:
      c - the given Component.
      timeout - the time in milliseconds to wait for the Component to be showing and ready.
      Returns:
      true if the Component is showing and ready, false otherwise.
    • invokePopupMenu

      @RunsInEDT public JPopupMenu invokePopupMenu(Component c)
      Shows a pop-up menu using the given Component as the invoker of the pop-up menu.
      Parameters:
      c - the invoker of the JPopupMenu.
      Returns:
      the displayed pop-up menu.
      Throws:
      IllegalStateException - if the given Component is disabled.
      IllegalStateException - if the given Component is not showing on the screen.
      ComponentLookupException - if a pop-up menu cannot be found.
    • invokePopupMenu

      @RunsInEDT public JPopupMenu invokePopupMenu(Component c, Point p)
      Shows a pop-up menu at the given point using the given Component as the invoker of the pop-up menu.
      Parameters:
      c - the invoker of the JPopupMenu.
      p - the given point where to show the pop-up menu.
      Returns:
      the displayed pop-up menu.
      Throws:
      NullPointerException - if the given point is null.
      IllegalStateException - if the given Component is disabled.
      IllegalStateException - if the given Component is not showing on the screen.
      ComponentLookupException - if a pop-up menu cannot be found.
    • assertIsEnabledAndShowing

      @RunsInEDT protected static void assertIsEnabledAndShowing(Component c)
      Validates that the given Component is enabled and showing on the screen. This method is executed in the event dispatch thread.
      Parameters:
      c - the Component to check.
      Throws:
      IllegalStateException - if the Component is disabled.
      IllegalStateException - if the Component is not showing on the screen.
    • propertyName

      @RunsInEDT public static org.fest.assertions.Description propertyName(Component c, String propertyName)
      Formats the name of a property of the given Component by concatenating the value obtained from Formatting.format(Component) with the given property name.
      Parameters:
      c - the given Component.
      propertyName - the name of the property.
      Returns:
      the description of a property belonging to a Component.
      See Also:
    • moveMouseIgnoringAnyError

      @RunsInEDT protected final void moveMouseIgnoringAnyError(Component c, Point p)
      Simulates a user moving the mouse pointer to the given coordinates relative to the given Component. This method will not throw any exceptions if the it was not possible to move the mouse pointer.
      Parameters:
      c - the given Component.
      p - coordinates relative to the given Component.
    • moveMouseIgnoringAnyError

      @RunsInEDT protected final void moveMouseIgnoringAnyError(Component c, int x, int y)
      Simulates a user moving the mouse pointer to the given coordinates relative to the given Component. This method will not throw any exceptions if the it was not possible to move the mouse pointer.
      Parameters:
      c - the given Component.
      x - horizontal coordinate relative to the given Component.
      y - vertical coordinate relative to the given Component.