public class GUITestCase extends TestCase
TestCase
to support
testing of custom Swing components, panels, and main programs
with graphical user interfaces (GUIs). GUI testing support is based
on abbot's testing infrastructure, and works equally well for
Swing and/or AWT components.Modifier and Type | Class and Description |
---|---|
protected static class |
GUITestCase.EventDispatchException
Represents an exception that occurred on the event dispatch thread.
|
TestCase.AssertMethodCalledIncorrectly
Modifier and Type | Field and Description |
---|---|
protected static Class<?>[] |
DISPOSE_CLASSES
Any member data derived from these classes will be automatically set
to
null after the test has run. |
GUIFilter.Operator |
where
This field re-exports the
where operator from
GUIFilter so that it is available
in test methods without requiring a static import. |
Constructor and Description |
---|
GUITestCase()
Creates a new GUITestCase object.
|
GUITestCase(String name)
Creates a new GUITestCase object.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
callGUIIOMethod(Object receiver,
Class<T> returnType,
String methodName,
Object... params)
Call a method that involves window-based I/O, so that the
method is executed on the GUI event thread, and return its value.
|
void |
callGUIIOMethod(Object reciever,
String methodName,
Object... params)
Call a method that involves window-based I/O, so that the
method is executed on the GUI event thread.
|
void |
callGUIIOMethod(Runnable r)
A more primitive version of
callGUIIOMethod() that
takes a Runnable instead of a receiver, method name, and
parameters. |
void |
click(Component component)
Left-click on the center of the component (mouse button 1 press and
release).
|
void |
click(Component component,
int x,
int y)
Left-click at the given location on a component (mouse button 1
press and release).
|
void |
click(Component component,
int x,
int y,
int buttons,
int count)
Click on a component, specifying the location, specific mouse buttons,
and click count.
|
void |
clickDialogButton(String buttonText)
Assuming that a Dialog of some sort is currently
open, this method clicks the specified button.
|
protected abbot.finder.Hierarchy |
createHierarchy()
Create the component hierarchy object that will be used in this
test class.
|
void |
doubleClick(Component component)
Left-double-click on the center of the component (mouse button 1
press and release twice).
|
void |
doubleClick(Component component,
int x,
int y)
Left-double-click at the given location on a component (mouse button 1
press and release twice).
|
void |
enterText(JTextComponent component,
String text)
Type the given text into the given component, replacing any
existing text already there.
|
protected void |
fixtureSetUp()
Ensure proper test harness setup that won't
be inadvertently overridden by a derived class.
|
protected void |
fixtureTearDown()
Handles restoration of system state.
|
void |
focus(Component component)
Give the focus to a specific component.
|
<T extends Component> |
getAllComponentsMatching(Class<T> type)
Look up all components in the GUI being tested by specifying their
class.
|
<T extends Component> |
getAllComponentsMatching(Class<T> type,
GUIFilter filter)
Look up all components in the GUI being tested by specifying their
class and a
GUIFilter . |
List<Component> |
getAllComponentsMatching(GUIFilter filter)
Look up all components in the GUI being tested by specifying
a
GUIFilter . |
<T extends Component> |
getComponent(Class<T> type)
Look up a component in the GUI being tested by specifying its class.
|
<T extends Component> |
getComponent(Class<T> type,
GUIFilter filter)
Look up a component in the GUI being tested by specifying its class
and a
GUIFilter . |
<T extends Component> |
getComponent(Class<T> type,
String name)
Look up a component in the GUI being tested by specifying its class
and its name (as returned by the component's
getName() method). |
Component |
getComponent(GUIFilter filter)
Look up a component in the GUI being tested, using a filter to
specify which component you want.
|
List<String> |
getContents(JComboBox component)
Get the contents of a combo box, in the form of a list of strings.
|
List<String> |
getContents(JList component)
Get the contents of a list control, in the form of a list of strings.
|
protected abbot.finder.ComponentFinder |
getFinder()
Obtain a component finder to look up components.
|
<T extends Component> |
getFirstComponentMatching(Class<T> type)
Look up a component in the GUI being tested by specifying its class.
|
<T extends Component> |
getFirstComponentMatching(Class<T> type,
GUIFilter filter)
Look up a component in the GUI being tested by specifying its class
and a
GUIFilter . |
Component |
getFirstComponentMatching(GUIFilter filter)
Look up a component in the GUI being tested by specifying
a
GUIFilter . |
protected abbot.finder.Hierarchy |
getHierarchy()
Get the hierarchy used by finders in this test class.
|
String |
getPathFor(Component component)
Get the hierarchical path to the given component, starting from the
outermost component in which it is contained,
e.g., "JFrame:JRootPane:JPanel:JButton".
|
protected abbot.tester.Robot |
getRobot()
Return an Abbot
Robot for basic event
generation. |
protected abbot.tester.WindowTracker |
getWindowTracker()
Return a WindowTracker instance.
|
void |
keyPress(Component component,
int keyCode)
Send a single key press (key down action) to a component.
|
void |
keyRelease(Component component,
int keyCode)
Send a single key release (key up action) to a component.
|
void |
keyString(Component component,
String sequence)
Type a sequence of characters on the given component, where
the contents are specified as a string.
|
void |
keyStroke(Component component,
int keyCode)
Send a single keystroke to a component.
|
void |
keyStroke(Component component,
int keyCode,
int modifiers)
Send a single keystroke to a component.
|
void |
mouseDragFrom(Component component,
int x,
int y)
Initiate a drag action at a specific location on a component.
|
void |
mouseDragFrom(Component component,
int x,
int y,
int buttons)
Initiate a drag action at a specific location on a component, with
a specified mouse button.
|
void |
mouseDragOver(Component component,
int x,
int y)
Drag the currently dragged object over the given location on
the specified component.
|
void |
mouseDropOn(Component destination)
End a drag operation currently in progress by "dropping" at the
in the center of the specified component.
|
void |
mouseDropOn(Component destination,
int x,
int y)
End a drag operation currently in progress by "dropping" at the
specified coordinates on the given component.
|
void |
mouseExit(Component component)
Move the mouse pointer out of the given component.
|
void |
mouseMove(Component component,
int x,
int y)
Move the mouse pointer to the given location on the specified
component.
|
void |
mousePress(Component component,
int x,
int y)
Press the left mouse button at the given location on a component
(mouse button 1).
|
void |
mousePress(Component component,
int x,
int y,
int buttons)
Press one or more mouse buttons at the given location on a component.
|
void |
mouseRelease()
Release any currently held mouse buttons.
|
static GUIFilter |
not(GUIFilter otherFilter)
The "not" operator for negating an existing filter, when the not
operation is at the very beginning of the expression, re-exported
from
GUIFilter so that it is
available in test methods without requiring a static import. |
void |
rightClick(Component component)
Right-click on the center of the component (mouse button 3 press
and release).
|
void |
rightClick(Component component,
int x,
int y)
Right-click at the given location on a component (mouse button 3
press and release).
|
void |
runBare() |
protected void |
runTestMethod(Statement statement)
Hook into the custom test execution machinery provided by MixRunner
to ensure proper test harness setup and tear down that won't
likely be accidentally overridden by a derived class.
|
void |
selectColorInChooser(Color color)
Assuming that a JColorChooser is currently open, chooses the given
color and closes the JColorChooser
|
void |
selectFileInChooser(String fileName)
Assuming that a JFileChoosers is currently open, selects the given
file and closes the JFileChooser
|
void |
selectFilesInChooser(String... files)
Assuming that a JFileChoosers is currently open, selects the given
files and closes the JFileChooser
|
void |
selectItem(JComboBox component,
String item)
Select a specific item in a combo box or selection list.
|
void |
selectItem(JList component,
String item)
Select a specific item in a selection list.
|
void |
selectMenuItem(JMenuItem item)
Select a given menu item.
|
void |
setInputDialogText(String text)
Assuming a
JOptionPane is currently open and waiting for
input, this method enters the given text into the pane's text
field and then closes the JOptionPane. |
void |
setModifiers(int modifiers,
boolean pressed)
Set any key modifiers for future events.
|
void |
setSwitchFocus(boolean switchFocus) |
Frame |
showInFrame(Component component)
Displays a specific component that you wish to test, such as a panel,
a custom text field, or some other component, wrapping it in a
Frame . |
Frame |
showInFrame(Component component,
Dimension size)
Displays a specific component that you wish to test, such as a panel,
a custom text field, or some other component, wrapping it in a
Frame with the size you specify. |
void |
showWindow(Window window)
Safely display a window with proper EDT synchronization.
|
void |
showWindow(Window window,
Dimension size)
Safely display a window with proper EDT synchronization.
|
protected void |
sleep()
Convenience method to sleep for a UI interval
(same as getRobot().sleep()).
|
String |
toString(Component c)
A shorter, simpler way to get a human-readable representation of a
component.
|
String |
toString(Component[] components)
Get a human-reable representation of an array of components using
toString(Component) . |
<T extends Component> |
toString(List<T> components)
Get a human-readable representation of a List of components using
toString(Component) . |
assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertFuzzyEquals, assertFuzzyEquals, assertSame, assertSame, assertTrue, assertTrue, compact, compact, contains, containsRegex, containsRegex, equals, equalsRegex, equalsRegex, fromFile, fromFile, fuzzyContains, fuzzyContainsRegex, fuzzyContainsRegex, fuzzyEquals, fuzzyEqualsRegex, fuzzyEqualsRegex, in, installExitHandler, instrumentIO, multiline, out, prepareIO, resetIO, resetSystemIO, setIn, setIn, setNormalizeLineEndings, setSystemIn, setUp, setUseFuzzyStringComparisons, stringNormalizer, systemErr, systemOut, tearDown, uninstallExitHandler
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runTest, setName, toString
public final GUIFilter.Operator where
where
operator from
GUIFilter
so that it is available
in test methods without requiring a static import.protected static final Class<?>[] DISPOSE_CLASSES
null
after the test has run. This enables GC of said
classes without GC of the test itself (the default JUnit runners never
release their references to the tests) or requiring explicit
null
-setting in the TestCase.tearDown()
method.public GUITestCase()
public GUITestCase(String name)
name
- The name of this test case.public Frame showInFrame(Component component)
Frame
. The frame's size will be its preferred size. This
method will return when the enclosing Frame
is showing and
ready for input.component
- The component to display.public Frame showInFrame(Component component, Dimension size)
Frame
with the size you specify. This method will return when
the enclosing Frame
is showing and ready for input.component
- The component to display.size
- The desired size of the enclosing frame, or
null
to use the component's preferred size.public void showWindow(Window window)
Window
is showing and ready for input.window
- The window to show.public void showWindow(Window window, Dimension size)
Window
is showing and ready for input.window
- The window to show.size
- The desired size of the enclosing frame, or
null
to use the component's preferred size.public void setModifiers(int modifiers, boolean pressed)
modifiers
- A mask indicating which modifier keys to use.pressed
- Whether the modifiers should be in the pressed state.protected void runTestMethod(Statement statement) throws Throwable
If any exceptions are thrown on the event dispatch thread, they count as errors. They will not, however supersede any failures/errors thrown by the test itself unless thrown prior to the main test failure.
statement
- The (JUnit4-style) test method to be executed.Throwable
- If any exception occurs in the test case or on the
event dispatch thread.public void runBare() throws Throwable
runBare
in class junit.framework.TestCase
Throwable
public static GUIFilter not(GUIFilter otherFilter)
GUIFilter
so that it is
available in test methods without requiring a static import. This
method is designed to be used in expressions like
not(where.enabledIs(true).or.hasFocusIs(true))
.otherFilter
- The filter to negatepublic <T extends Component> T getComponent(Class<T> type)
If no matching component exists, the test case will fail with an appropriate message. If more than one matching component exists, the test case will fail with an appropriate message. test case failure results).
T
- This method is a template method, and the type T used for
the return value is implicitly deduced from the provided
argument type
.type
- The type (class) of the component you wish to retrieve, and
also the way you specify the return type of this method.getFirstComponentMatching(Class)
,
getAllComponentsMatching(Class)
public <T extends Component> T getComponent(Class<T> type, String name)
getName()
method).
This method expects the given class and name together to identify a
unique component, meaning that there should only be one instance of
the given class with the given name in the entire GUI. Normally,
that will always be the case, since names are used as unique
identifiers for testing purposes.
If no matching component exists, the test case will fail with an appropriate message. If more than one matching component exists, the test case will fail with an appropriate message. test case failure results).
T
- This method is a template method, and the type T used for
the return value is implicitly deduced from the provided
argument type
.type
- The type (class) of the component you wish to retrieve, and
also the way you specify the return type of this method.name
- The name of the desired componentpublic <T extends Component> T getComponent(Class<T> type, GUIFilter filter)
GUIFilter
.
This method expects exactly one component to match your criteria.
If no matching component exists, the test case will fail with an
appropriate message. If more than one matching component exists,
the test case will fail with an appropriate message.
test case failure results).T
- This method is a template method, and the type T used for
the return value is implicitly deduced from the provided
argument type
.type
- The type (class) of the component you wish to retrieve, and
also the way you specify the return type of this method.filter
- The search criteria.getFirstComponentMatching(Class,GUIFilter)
,
getAllComponentsMatching(Class,GUIFilter)
public Component getComponent(GUIFilter filter)
getComponent(Class,GUIFilter)
, since no class needs to be
specified, but that also means the return type is less specific
(it is always Component
).
This method expects the given filter
to identify a unique component. If no matching component exists,
the test case will fail with an appropriate message. If more than
one matching component exists, the test case will fail with an
appropriate message.filter
- The search criteria.getFirstComponentMatching(GUIFilter)
,
getAllComponentsMatching(GUIFilter)
public <T extends Component> T getFirstComponentMatching(Class<T> type)
T
- This method is a template method, and the type T used for
the return value is implicitly deduced from the provided
argument type
.type
- The type (class) of the component you wish to retrieve, and
also the way you specify the return type of this method.getComponent(Class)
,
getAllComponentsMatching(Class)
public <T extends Component> T getFirstComponentMatching(Class<T> type, GUIFilter filter)
GUIFilter
.
This method expects the given criteria to identify at least one such
component. If no matching component exists, the test case will fail
with an appropriate message. If more than one matching component
exists, the first one found will be returned (although client code
should not expect a specific search order).T
- This method is a template method, and the type T used for
the return value is implicitly deduced from the provided
argument type
.type
- The type (class) of the component you wish to retrieve, and
also the way you specify the return type of this method.filter
- The search criteria.getComponent(Class,GUIFilter)
,
getAllComponentsMatching(Class,GUIFilter)
public Component getFirstComponentMatching(GUIFilter filter)
GUIFilter
. This method is more general
than getFirstComponentMatching(Class,GUIFilter)
, since no
class needs to be specified, but that also means the return type
is less specific (it is always Component
).
This method expects the given criteria to identify at least one such
component. If no matching component exists, the test case will fail
with an appropriate message. If more than one matching component
exists, the first one found will be returned (although client code
should not expect a specific search order).filter
- The search criteria.getComponent(GUIFilter)
,
getAllComponentsMatching(GUIFilter)
public <T extends Component> List<T> getAllComponentsMatching(Class<T> type)
T
- This method is a template method, and the type T used as
the List
element type in
the return value is implicitly deduced from the provided
argument type
.type
- The type (class) of the components you wish to retrieve,
and also the way you specify the type of elements in
the list returned by this method.getComponent(Class)
,
getFirstComponentMatching(Class)
public <T extends Component> List<T> getAllComponentsMatching(Class<T> type, GUIFilter filter)
GUIFilter
. All matching objects are returned in
a list.T
- This method is a template method, and the type T used as
the List
element type in
the return value is implicitly deduced from the provided
argument type
.type
- The type (class) of the components you wish to retrieve,
and also the way you specify the type of elements in
the list returned by this method.filter
- The search criteria.getComponent(Class,GUIFilter)
,
getAllComponentsMatching(Class,GUIFilter)
public List<Component> getAllComponentsMatching(GUIFilter filter)
GUIFilter
.
All matching objects are returned in a list.
This method is more general than
getAllComponentsMatching(Class,GUIFilter)
, since no
class needs to be specified, but that also means the return type
is less specific (it is always List<Component>
).filter
- The search criteria.getComponent(GUIFilter)
,
getAllComponentsMatching(GUIFilter)
public void click(Component component)
component
- The component to click on.public void click(Component component, int x, int y)
component
- The component to click on.x
- The x-coordinate of the click location.y
- The y-coordinate of the click location.public void doubleClick(Component component)
component
- The component to click on.public void doubleClick(Component component, int x, int y)
component
- The component to click on.x
- The x-coordinate of the click location.y
- The y-coordinate of the click location.public void rightClick(Component component)
component
- The component to click on.public void rightClick(Component component, int x, int y)
component
- The component to click on.x
- The x-coordinate of the click location.y
- The y-coordinate of the click location.public void click(Component component, int x, int y, int buttons, int count)
component
- The component to click on.x
- The x-coordinate of the click location.y
- The y-coordinate of the click location.buttons
- The button mask indicating which button(s) are being
clicked. Use InputEvent
constants (just like
in a mouse listener) to identify which button(s) to
click simultaneously.count
- The number of clicks for this event
(single, double, more ...).public void mousePress(Component component, int x, int y)
component
- The component to press the mouse button on.x
- The x-coordinate of the click location.y
- The y-coordinate of the click location.public void mousePress(Component component, int x, int y, int buttons)
component
- The component to press the mouse button on.x
- The x-coordinate of the click location.y
- The y-coordinate of the click location.buttons
- The button mask indicating which button(s) are being
clicked. Use InputEvent
constants (just like
in a mouse listener) to identify which button(s) to
click simultaneously.public void mouseRelease()
public void mouseMove(Component component, int x, int y)
mouseEntered
event will be
automatically generated as well, if the mouse was not already
over the specified component before the move. Further, if the mouse
is already inside the component and the coordinates specified lie
outside it, a mouseExited
event will be generated
automatically.component
- The component to move the mouse button on.x
- The x-coordinate of the destination.y
- The y-coordinate of the destination.public void mouseExit(Component component)
mouseExited
event to be generated.
This might also cause a mouseEntered
event in a surrounding
or neighboring component--use mouseMove()
with other
specific coordinates if you want a different effect.component
- The component to exit from.public void mouseDragFrom(Component component, int x, int y)
component
- The component to begin the drag fromx
- The x-coordinate for the mouse press that initiates
the drag, which is specified relative to the
component
.y
- The y-coordinate for the mouse press that initiates
the drag, which is specified relative to the
component
.public void mouseDragFrom(Component component, int x, int y, int buttons)
component
- The component to begin the drag fromx
- The x-coordinate for the mouse press that initiates
the drag, which is specified relative to the
component
.y
- The y-coordinate for the mouse press that initiates
the drag, which is specified relative to the
component
.buttons
- The button mask indicating which button(s) are
pressed to initiate this mouse drag action. Use
InputEvent
constants (just like
in a mouse listener) to identify which button(s) to
press simultaneously.public void mouseDragOver(Component component, int x, int y)
startDragFrom()
) and before the "drop" (mouse
release).component
- The component to drag over.x
- The x-coordinate of the location to drag over,
relative to the given component
.y
- The y-coordinate of the location to drag over,
relative to the given component
.public void mouseDropOn(Component destination)
startDragFrom()
.
Calling this method implicitly causes a mouseMoved
event to get to the center of the given component, followed by a
mouse release of whatever mouse buttons are currently pressed as
part of the drag operation that was in progress.destination
- The component to drop on.public void mouseDropOn(Component destination, int x, int y)
startDragFrom()
.
Calling this method implicitly causes a mouseMoved
event to get to the specified coordinates, followed by a
mouse release of whatever mouse buttons are currently pressed as
part of the drag operation that was in progress.destination
- The component to drop on.x
- The x-coordinate of the drop location,
relative to the given destination
.y
- The y-coordinate of the drop location,
relative to the given destination
.public void focus(Component component)
component
- The component to receive focus.public void enterText(JTextComponent component, String text)
null
is given, then this method
simply removes all existing text.component
- The component to enter text on.text
- The text to enter on the component.public void keyStroke(Component component, int keyCode)
component
- The component that will receive the keystroke--focus
will be automatically given to this component first,
before the keystroke.keyCode
- The keystroke to send. Use the
KeyEvent
VK_*
constants.public void keyStroke(Component component, int keyCode, int modifiers)
component
- The component that will receive the keystroke--focus
will be automatically given to this component first,
before the keystroke.keyCode
- The keystroke to send. Use the
KeyEvent
VK_*
constants.modifiers
- The keyboard modifier keys to press simultaneously
(e.g., shift, control, etc.). For modifiers,
use the InputEvent
modifier mask constants.public void keyPress(Component component, int keyCode)
component
- The component that will receive the key press--focus
will be automatically given to this component first,
before the key is pressed.keyCode
- The key to press. Use the
KeyEvent
VK_*
constants.public void keyRelease(Component component, int keyCode)
component
- The component that will receive the key release--focus
will be automatically given to this component first,
before the key is released.keyCode
- The key to release. Use the
KeyEvent
VK_*
constants.public void keyString(Component component, String sequence)
component
- The component to type on (or to). It will receive
focus first, if necessary.sequence
- The content to type on the component.public void selectItem(JComboBox component, String item)
component
- The combo box to select from.item
- The item to select.public void selectItem(JList component, String item)
component
- The list to select from.item
- The item to select.public List<String> getContents(JComboBox component)
component
- The combo box.public List<String> getContents(JList component)
component
- The list.public void selectMenuItem(JMenuItem item)
item
- The menu item to select.public String getPathFor(Component component)
component
- The componentpublic String toString(Component c)
toString()
method on Component
prints a huge amount of information, and ironically often makes it
difficult to understand exactly which component you've printed.
This version of toString()
prints only the component's
class, name, and hash code.c
- The componentJButton[button1, 6546787]
.public String toString(Component[] components)
toString(Component)
.components
- The array(JButton[button1, 6546787], JTextField[input, 6881863])
.public <T extends Component> String toString(List<T> components)
toString(Component)
.T
- This method is a template method, and the type T is
implicitly deduced from the type of elements in the
provided List
argument. It represents the
specific subtype of Component
in the
List
.components
- The list(JButton[button1, 6546787], JTextField[input, 6881863])
.public void callGUIIOMethod(Object reciever, String methodName, Object... params)
reciever
- The object to which the method belongs.methodName
- The name of the method to call.params
- A list of zero or more parameters to pass to the method.public <T> T callGUIIOMethod(Object receiver, Class<T> returnType, String methodName, Object... params)
T
- This method is a template method, and the type T is
implicitly deduced from the returnType
argument.receiver
- The object to which the method belongs.returnType
- The expected type of the method's return value.methodName
- The name of the method to call.params
- A list of zero or more parameters to pass to the method.public void callGUIIOMethod(Runnable r)
callGUIIOMethod()
that
takes a Runnable
instead of a receiver, method name, and
parameters. Students should use one of the other versions instead.r
- The runnable to invoke.public void selectFileInChooser(String fileName)
fileName
- the name of the file to choosepublic void selectFilesInChooser(String... files)
fileName
- the name of the file to choosepublic void selectColorInChooser(Color color)
color
- the color to choose in the JColorChooserpublic void clickDialogButton(String buttonText)
buttonText
- The text on the button to click.public void setInputDialogText(String text)
JOptionPane
is currently open and waiting for
input, this method enters the given text into the pane's text
field and then closes the JOptionPane.text
- The text to enter into the JOptionPane.public void setSwitchFocus(boolean switchFocus)
protected abbot.tester.Robot getRobot()
Robot
for basic event
generation.protected abbot.tester.WindowTracker getWindowTracker()
protected void sleep()
protected void fixtureSetUp()
protected void fixtureTearDown()
protected abbot.finder.ComponentFinder getFinder()
protected abbot.finder.Hierarchy createHierarchy()
Hierarchy
if necessary.protected abbot.finder.Hierarchy getHierarchy()