Package generic.test

Class AbstractGuiTest

Direct Known Subclasses:
AbstractDockingTest

public class AbstractGuiTest extends AbstractGenericTest
Base class for tests that need swing support methods. Tests that don't involve Swing/Gui elements should use AbstractGenericTest instead
  • Constructor Details

    • AbstractGuiTest

      public AbstractGuiTest()
  • Method Details

    • createApplicationConfiguration

      protected ApplicationConfiguration createApplicationConfiguration()
      Overrides:
      createApplicationConfiguration in class AbstractGenericTest
    • getAllWindows

      public static Set<Window> getAllWindows()
      Gets all windows in the system (including Frames).
      Returns:
      all windows
    • waitForTasks

      public static void waitForTasks()
      Waits for all system tasks to complete. These tasks are tracked by the SystemUtilities during testing only.
      Throws:
      junit.framework.AssertionFailedError - if the timeout period expires while waiting for tasks
    • waitForTasks

      public static void waitForTasks(long timeout)
    • waitForPostedSwingRunnables

      @Deprecated(forRemoval=true, since="10.3") public static void waitForPostedSwingRunnables()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use waitForSwing() instead
    • findComponent

      public static <T extends Component> T findComponent(Container parent, Class<T> desiredClass)
    • findComponent

      public static <T extends Component> T findComponent(Container parent, Class<T> desiredClass, boolean checkOwnedWindows)
    • findComponents

      public static <T extends Component> List<T> findComponents(Container parent, Class<T> desiredClass)
    • findComponents

      public static <T extends Component> List<T> findComponents(Container parent, Class<T> desiredClass, boolean checkOwnedWindows)
    • findOwnedWindows

      public static void findOwnedWindows(Window win, Set<Window> winList)
    • pressButtonByText

      public static void pressButtonByText(Container container, String buttonText)
      Finds the button with the indicated TEXT that is a sub-component of the indicated container, and then programmatically presses the button.
      The following is a sample JUnit test use:
       env.showTool();
       OptionDialog dialog = (OptionDialog) env.waitForDialog(OptionDialog.class, 1000);
       assertNotNull(dialog);
       pressButtonByText(dialog, "OK");
       
      Parameters:
      container - the container to search. (Typically a dialog.)
      buttonText - the text on the desired JButton.
      Throws:
      AssertionError - if the button isn't found, isn't showing or isn't enabled
    • pressButtonByText

      public static void pressButtonByText(Container container, String buttonText, boolean waitForCompletion)
      Finds the button with the indicated TEXT that is a sub-component of the indicated container, and then programmatically presses the button.
      Parameters:
      container - the container to search. (Typically a dialog.)
      buttonText - the text on the desired JButton.
      waitForCompletion - if true wait for action to complete before returning, otherwise schedule action to be performed and return immediately.
      Throws:
      AssertionError - if the button isn't found, isn't showing or isn't enabled
    • pressButtonByName

      public static void pressButtonByName(Container container, String buttonName)
      Finds the button with the indicated NAME that is a subcomponent of the indicated container, and then programmatically presses the button.
      Parameters:
      container - the container to search. (Typically a dialog)
      buttonName - the name on the desired AbstractButton (see Component.setName())
    • pressButtonByName

      public static void pressButtonByName(Container container, String buttonName, boolean waitForCompletion)
      Finds the button with the indicated NAME that is a subcomponent of the indicated container, and then programmatically presses the button.
      Parameters:
      container - the container to search. (Typically a dialog.)
      buttonName - the name on the desired AbstractButton (see Component.setName()).
      waitForCompletion - if true wait for action to complete before returning, otherwise schedule action to be performed and return immediately
    • pressButton

      public static void pressButton(AbstractButton button)
      Programmatically presses the indicated button.
      Parameters:
      button - the button
    • pressButton

      public static void pressButton(AbstractButton button, boolean waitForCompletion)
      Programmatically presses the indicated button.
      Parameters:
      button - the button
      waitForCompletion - if true wait for action to complete before returning, otherwise schedule action to be performed and return immediately.
    • findComponentByName

      public static Component findComponentByName(Container container, String componentName)
      Searches the subcomponents of the indicated container and returns the component with the specified name.
      Parameters:
      container - the container to search
      componentName - the name of the desired component
      Returns:
      the component, or null if not found
    • findComponentByName

      public static Component findComponentByName(Container container, String componentName, boolean checkOwnedWindows)
    • findComponentsByName

      public static List<Component> findComponentsByName(Container container, String componentName, boolean checkOwnedWindows)
    • findButtonByIcon

      public static JButton findButtonByIcon(Container container, Icon icon)
    • findButtonByText

      public static JButton findButtonByText(Container container, String text)
      Searches the subcomponents of the the given container and returns the JButton that has the specified text.
      Parameters:
      container - the container to search
      text - the button text
      Returns:
      the JButton, or null the button was not found
    • findAbstractButtonByText

      public static AbstractButton findAbstractButtonByText(Container container, String text)
      Searches the sub-components of the given container and returns the AbstractButton that has the specified text.

      This differs from findButtonByText(Container, String) in that this method will find buttons that do not extend from JButton. That method is convenient when you do not wish to cast the result from AbstractButton to JButton. Other than that, this method can handle all cases the other method cannot.

      Parameters:
      container - container to search
      text - button text
      Returns:
      null if the button was not found
    • findAbstractButtonByName

      public static AbstractButton findAbstractButtonByName(Container container, String name)
      Searches the sub-components of the given container and returns the AbstractButton that has the specified name.
      Parameters:
      container - container to search
      name - the button name (you must set this manually).
      Returns:
      null if the button was not found
    • leftClick

      public static void leftClick(JComponent comp, int x, int y)
    • middleClick

      public static void middleClick(JComponent comp, int x, int y)
    • rightClick

      public static void rightClick(JComponent comp, int x, int y)
    • doubleClick

      public static void doubleClick(JComponent comp, int x, int y)
    • clickMouse

      public static void clickMouse(Component comp, int button, int x, int y, int clickCount, int modifiers, boolean popupTrigger)
      Simulates click the mouse button.
      Parameters:
      comp - the component to click on.
      button - the mouse button (1, 2, or 3)
      x - the x coordinate of the click location
      y - the y coordinate of the click location
      clickCount - the number of clicks (2 = double click)
      modifiers - additional modifiers (e.g. MouseEvent.SHIFT_MASK)
      popupTrigger - a boolean, true if this event is a trigger for a popup menu
    • clickMouse

      public static void clickMouse(Component comp, int button, int x, int y, int clickCount, int modifiers)
      Simulates click the mouse button.
      Parameters:
      comp - the component to click on.
      button - the mouse button (1, 2, or 3)
      x - the x coordinate of the click location
      y - the y coordinate of the click location
      clickCount - the number of clicks (2 = double click)
      modifiers - additional modifiers (e.g. MouseEvent.SHIFT_MASK)
    • dragMouse

      public static void dragMouse(Component comp, int button, int startX, int startY, int endX, int endY, int modifiers)
      Simulates a mouse drag action
      Parameters:
      comp - the component to drag on.
      button - the mouse button (1, 2, or 3)
      startX - the x coordinate of the start drag location
      startY - the y coordinate of the start drag location
      endX - the x coordinate of the end drag location
      endY - the y coordinate of the end drag location
      modifiers - additional modifiers (e.g. MouseEvent.SHIFT_MASK)
    • moveMouse

      public static void moveMouse(Component comp, int x, int y)
      Fire a mouse moved event for the given component.
      Parameters:
      comp - source of the event.
      x - x position relative to the component
      y - y position relative to the component
    • postEvent

      public static void postEvent(AWTEvent ev)
    • getSwing

      public static <T> T getSwing(Supplier<T> s)
      Returns the value from the given Supplier, invoking the call in the Swing thread. This is useful when you may have values that are being changed on the Swing thread and you need the test thread to see the changes.
      Parameters:
      s - the supplier
      Returns:
      the value returned by the supplier
    • runSwing

      public static <T> T runSwing(Supplier<T> s)
      Returns the value from the given Supplier, invoking the call in the Swing thread. This is useful when you may have values that are being changed on the Swing thread and you need the test thread to see the changes.
      Parameters:
      s - the supplier
      Returns:
      the value returned by the supplier
      See Also:
    • runSwing

      public static void runSwing(Runnable r)
      Run the given code snippet on the Swing thread and wait for it to finish
      Parameters:
      r - the runnable code snippet
    • runSwingLater

      public void runSwingLater(Runnable r)
      Run the given code snippet on the Swing thread later, not blocking the current thread. Use this if the code snippet causes a blocking operation.

      This is a shortcut for runSwing(r, false);.

      Parameters:
      r - the runnable code snippet
    • runSwingWithException

      public static <E extends Exception> void runSwingWithException(ExceptionalCallback<E> callback) throws Exception
      Call this version of runSwing(Runnable) when you expect your runnable may throw exceptions
      Parameters:
      callback - the runnable code snippet to call
      Throws:
      Exception - any exception that is thrown on the Swing thread
    • runSwing

      public static void runSwing(Runnable runnable, boolean wait)
    • executeOnSwingWithoutBlocking

      public static void executeOnSwingWithoutBlocking(Runnable runnable)
      Launches the runnable on a new thread so as to not block the calling thread. This is very useful for performing actions on the Swing thread that show modal dialogs, which would otherwise block the calling thread, such as a testing thread.
      Parameters:
      runnable - The runnable that will be executed in a new Thread that will place the runnable on the Swing thread.
    • clickTableCell

      public static void clickTableCell(JTable table, int row, int col, int clickCount)
    • clickListRange

      public static void clickListRange(JList<?> list, int row, int count)
      Clicks a range of items in a list (simulates holding SHIFT and selecting each item in the range in-turn)
      Parameters:
      list - the list to select from
      row - the initial index
      count - the number of rows to select
    • clickTableRange

      public static void clickTableRange(JTable table, int row, int count)
      Clicks a range of items in a table (simulates holding SHIFT and selecting each item in the range)
      Parameters:
      table - the table to select
      row - the starting row index
      count - the number of rows to select
    • editCell

      public static TableCellEditor editCell(JTable table, int row, int col)
    • getRenderedTableCellValue

      public static String getRenderedTableCellValue(JTable table, int row, int column)
      Gets the rendered value for the specified table cell. The actual value at the cell may not be a String. This method will get the String display value, as created by the table.
      Parameters:
      table - the table to query
      row - the row to query
      column - the column to query
      Returns:
      the String value
      Throws:
      IllegalArgumentException - if there is no renderer or the rendered component is something from which this method can get a String (such as a JLabel)
    • setComboBoxSelection

      public static <T> void setComboBoxSelection(JComboBox<T> comboField, T selection)
    • setText

      public static void setText(JTextComponent field, String text)
    • getText

      public static String getText(JTextComponent field)
    • findTreePathToText

      public static TreePath findTreePathToText(JTree tree, String text)
      Finds the path of a tree node in the indicated tree with the specified text. The matching tree node is determined by comparing the specified text with the string returned by the tree node's toString() method.
      Note: This method affects the expansion state of the tree. It will expand nodes starting at the root until a match is found or all of the tree is checked.
      Parameters:
      tree - the tree
      text - the tree node's text
      Returns:
      the tree path
    • findPathToText

      protected static TreePath findPathToText(JTree tree, TreePath startTreePath, String text)
      Performs a depth first search for the named tree node.
      Parameters:
      tree - the tree to search
      startTreePath - path indicating node to begin searching from in the tree
      text - the name of the node to find
      Returns:
      the path to the named node or null if it can't be found.
    • fixupGUI

      public static void fixupGUI() throws InterruptedException, InvocationTargetException
      Invoke fixupGUI at the beginning of your JUnit test or in its setup() method to make your GUI for the JUnit test appear using the system Look and Feel. The system look and feel is the default that Ghidra uses. This will also change the default fonts for the JUnit test to be the same as those in Ghidra.
      Throws:
      InterruptedException - if we're interrupted while waiting for the event dispatching thread to finish excecuting doRun.run()
      InvocationTargetException - if an exception is thrown while running doRun
    • assertColorsEqual

      public void assertColorsEqual(Color expected, Color actual)
      Asserts that the two colors have the same rgb values (handles GColor)
      Parameters:
      expected - the expected color
      actual - the actual color
    • printMemory

      public static void printMemory()
    • waitForSwing

      public static boolean waitForSwing()
      Waits for the Swing thread to process any pending events. This method also waits for any SwingUpdateManagers that have pending events to be flushed.
      Returns:
      true if the any SwingUpdateManagers were busy.
    • privatewaitForSwing_SwingSafe

      @Deprecated public static void privatewaitForSwing_SwingSafe()
      Deprecated.
      This is not a test writer's method, but instead an infrastructure method.
      This is only for internal use. If you need to wait for the Swing thread from your test, then use waitForSwing().
    • yieldToSwing

      protected static void yieldToSwing()