junit.extensions.awt.tester
Class ComponentTester

java.lang.Object
  |
  +--junit.extensions.awt.tester.ComponentTester
Direct Known Subclasses:
ContainerTester

public class ComponentTester
extends java.lang.Object

Provide basic actions and assertions for anything of class Component. Derive from this class to implement actions and assertions specific to a given component class. Actions are generally user-driven actions such as menu selection, table selection, popup menus, etc. Assertions are either independent of any component (and should be implemented in this class), or take a component as the first argument, and perform some check on that component. Extensions must be in the junit.extensions.awt.tester package and be named the name of the Component subclass followed by "Tester". For example, the JButton tester class is junit.extensions.awt.tester.JButtonTester.

All actions should have the following signature:
public void actionWhat(Component c, ...); All assertions should have one of the following signatures:
public boolean assertWhat(...); public boolean assertWhat(Component c, ...); Property checks may also be implemented in cases where the component "property" might not be readily available or easily comparable, e.g. see JPopupMenuTester.getMenuLabels().
public Object getProperty(Component c); public boolean isProperty(Component c); Be careful not to name any support methods with the property signature, since these are scanned dynamically to populate the editor's action menus.


Field Summary
protected static int EM_AWT
          Post events to the AWT event queue.
protected static int EM_PROG
          Use programmatic control where possible.
protected static int EM_ROBOT
          Use java.awt.Robot to generate events.
protected static int eventMode
          Only robot mode is currently supported.
 
Constructor Summary
ComponentTester(ComponentFinder finder)
           
 
Method Summary
(package private) static void ()
          Set up the underlying robot and initialize descriptions.
 void actionClick(java.awt.Component comp)
          Click on the center of the component.
 void actionClick(java.awt.Component comp, int x, int y)
          Click on the component at the given location.
 void actionClick(java.awt.Component comp, int x, int y, int buttons)
          Click on the component at the given location.
 void actionDelay(int ms)
          Delay the given number of ms.
 void actionKeyStroke(java.lang.String kc, java.lang.String mods)
          Send the given keystroke to the program.
 void actionMenuItem(java.awt.Component item)
          Select the given menu item.
 void actionPopupMenuItem(java.awt.Component invoker, int x, int y, java.lang.String itemName)
          Pop up a menu at the given location on the given component; Select the given item.
 void actionShowPopupMenu(java.awt.Component invoker)
          Pop up a menu in the center of the given component.
 void actionShowPopupMenu(java.awt.Component invoker, int x, int y)
          Pop up a menu at the given location on the given component.
 boolean assertFrameShowing(java.lang.String title)
          Returns whether frame/dialog with the given title is showing.
protected  void click(java.awt.Component comp)
          Click in the center of the given component.
protected  void click(java.awt.Component comp, int buttons)
           
protected  void click(java.awt.Component comp, int x, int y)
           
protected  void click(java.awt.Component comp, int x, int y, int buttons)
          Click in the given part of the component.
 java.lang.String defaultTag(java.awt.Component comp)
          Component names take precedence over other identifying tags.
protected static void delay(int ms)
          Sleep for the given duration of ms.
 java.lang.String deriveTag(java.awt.Component comp)
          For a generic component, there's nothing we can really use to identify the component, except maybe toString, but that's probably too volatile.
 boolean equals(java.lang.Object obj1, java.lang.Object obj2)
          Perform piecewise comparisons of arrays in addition to regular comparisons.
static java.lang.Object[] eval(Resolver resolver, ComponentFinder finder, java.lang.String[] args, java.lang.Class[] params)
          Evaluate the given set of arguments into the given set of types.
static java.lang.Object eval(Resolver resolver, ComponentFinder finder, java.lang.String arg, java.lang.Class cls)
          Convert the given string into the given class, if possible, using any available converters if conversion to basic types fails.
 java.lang.reflect.Method[] getActions()
          Return a list of all actions defined by this class that don't depend on a component argument.
 java.lang.reflect.Method[] getAssertions()
          Return a list of all assertions defined by this class that don't depend on a component argument.
static int getAutoDelay()
           
 java.lang.reflect.Method[] getComponentActions()
          Return a list of all actions defined by this class that require a component argument.
 java.lang.reflect.Method[] getComponentAssertions()
          Return a list of all assertions defined by this class that require a component argument.
static Converter getConverter(java.lang.Class cls)
          Find a string converter for the given class.
static java.lang.String getEventID(java.lang.Class cls, int id, java.lang.String prefix)
           
static int getEventID(java.lang.Class cls, java.lang.String id)
          Return the numeric event ID corresponding to the given string.
protected  ComponentFinder getFinder()
           
static java.lang.String getKeyCode(int keycode)
           
static int getKeyCode(java.lang.String code)
           
static javax.swing.KeyStroke getKeyStroke(char ch)
          Return the keycode KeyStroke corresponding to the given character, as best we can guess it.
static java.lang.String getModifiers(int flags)
          Convert the integer flags into a string representation.
static int getModifiers(java.lang.String mods)
          Convert the string representation into the actual modifier mask.
 java.lang.reflect.Method[] getPropertyChecks()
          Return a list of all property checks defined by this class.
protected static void key(int keycode)
          Type the given keycode with no modifiers.
protected static void key(int keycode, int modifiers)
          Type the given keycode with the given modifiers.
protected static void keyPress(int keycode)
          Send a key press event.
protected static void keyRelease(int keycode)
          Send a key release event.
protected  void keyString(java.lang.String str)
          Type the given string.
protected  void keyStroke(char ch)
          Type the given character.
protected static void mouseMove(java.awt.Component comp)
          Move the pointer to the center of the given component.
protected static void mouseMove(java.awt.Component comp, int x, int y)
          Move the pointer to the given coordinates relative to the given component.
protected static void mouseMove(int x, int y)
          Move the mouse to the given location.
protected static void mousePress(int buttons)
          Send a button press event.
protected static void mouseRelease(int buttons)
          Send a button release event.
static java.lang.String[] parseArgs(java.lang.String value)
          Convert the comma-separated list into individual strings.
static void sendEvent(java.awt.AWTEvent event)
          Send the given event as appropriate to the event-generation mode.
protected static void setDocumentationProperties(java.lang.String base, java.lang.String menuName, java.lang.String desc, java.lang.String argDesc)
          Install the given documentation properties.
static void setRobotDelay(int ms)
          Allow this to be adjusted, mostly for testing.
protected static java.lang.String simpleClassName(java.lang.Class cls)
           
 boolean stringMatch(java.lang.String pattern, java.lang.String actual)
           
protected static void waitForIdle()
          Wait for an idle event queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EM_ROBOT

protected static int EM_ROBOT
Use java.awt.Robot to generate events.

EM_AWT

protected static int EM_AWT
Post events to the AWT event queue.

EM_PROG

protected static int EM_PROG
Use programmatic control where possible.

eventMode

protected static int eventMode
Only robot mode is currently supported.
Constructor Detail

ComponentTester

public ComponentTester(ComponentFinder finder)
Method Detail

getAutoDelay

public static int getAutoDelay()

static void ()
Set up the underlying robot and initialize descriptions.

setDocumentationProperties

protected static void setDocumentationProperties(java.lang.String base,
                                                 java.lang.String menuName,
                                                 java.lang.String desc,
                                                 java.lang.String argDesc)
Install the given documentation properties. Subclasses should invoke this for each exported action. These properties are for use by an editor to better describe the available methods.

getFinder

protected ComponentFinder getFinder()

defaultTag

public java.lang.String defaultTag(java.awt.Component comp)
Component names take precedence over other identifying tags. If there is no component name, let the derived class try to provide something to use. At worst, getName() will return null and we'll get no tag.

deriveTag

public java.lang.String deriveTag(java.awt.Component comp)
For a generic component, there's nothing we can really use to identify the component, except maybe toString, but that's probably too volatile.

setRobotDelay

public static void setRobotDelay(int ms)
Allow this to be adjusted, mostly for testing.

actionDelay

public void actionDelay(int ms)
Delay the given number of ms.

actionMenuItem

public void actionMenuItem(java.awt.Component item)
Select the given menu item.

actionPopupMenuItem

public void actionPopupMenuItem(java.awt.Component invoker,
                                int x,
                                int y,
                                java.lang.String itemName)
                         throws ComponentNotFoundException
Pop up a menu at the given location on the given component; Select the given item.

actionShowPopupMenu

public void actionShowPopupMenu(java.awt.Component invoker)
Pop up a menu in the center of the given component.

actionShowPopupMenu

public void actionShowPopupMenu(java.awt.Component invoker,
                                int x,
                                int y)
Pop up a menu at the given location on the given component.

actionClick

public void actionClick(java.awt.Component comp)
Click on the center of the component.

actionClick

public void actionClick(java.awt.Component comp,
                        int x,
                        int y)
Click on the component at the given location.

actionClick

public void actionClick(java.awt.Component comp,
                        int x,
                        int y,
                        int buttons)
Click on the component at the given location.

actionKeyStroke

public void actionKeyStroke(java.lang.String kc,
                            java.lang.String mods)
Send the given keystroke to the program. Note that this does not affect the current focus.

assertFrameShowing

public boolean assertFrameShowing(java.lang.String title)
Returns whether frame/dialog with the given title is showing.

getActions

public java.lang.reflect.Method[] getActions()
Return a list of all actions defined by this class that don't depend on a component argument.

getComponentActions

public java.lang.reflect.Method[] getComponentActions()
Return a list of all actions defined by this class that require a component argument.

getPropertyChecks

public java.lang.reflect.Method[] getPropertyChecks()
Return a list of all property checks defined by this class. The first argument must be a Component.

getAssertions

public java.lang.reflect.Method[] getAssertions()
Return a list of all assertions defined by this class that don't depend on a component argument.

getComponentAssertions

public java.lang.reflect.Method[] getComponentAssertions()
Return a list of all assertions defined by this class that require a component argument.

mouseMove

protected static void mouseMove(int x,
                                int y)
Move the mouse to the given location.

mousePress

protected static void mousePress(int buttons)
Send a button press event.

mouseRelease

protected static void mouseRelease(int buttons)
Send a button release event.

keyPress

protected static void keyPress(int keycode)
Send a key press event.

keyRelease

protected static void keyRelease(int keycode)
Send a key release event.

delay

protected static void delay(int ms)
Sleep for the given duration of ms.

waitForIdle

protected static void waitForIdle()
Wait for an idle event queue.

mouseMove

protected static void mouseMove(java.awt.Component comp)
Move the pointer to the center of the given component.

mouseMove

protected static void mouseMove(java.awt.Component comp,
                                int x,
                                int y)
Move the pointer to the given coordinates relative to the given component.

key

protected static void key(int keycode)
Type the given keycode with no modifiers.

key

protected static void key(int keycode,
                          int modifiers)
Type the given keycode with the given modifiers.

keyStroke

protected void keyStroke(char ch)
Type the given character.

keyString

protected void keyString(java.lang.String str)
Type the given string.

click

protected void click(java.awt.Component comp)
Click in the center of the given component.

click

protected void click(java.awt.Component comp,
                     int buttons)

click

protected void click(java.awt.Component comp,
                     int x,
                     int y)

click

protected void click(java.awt.Component comp,
                     int x,
                     int y,
                     int buttons)
Click in the given part of the component.

sendEvent

public static void sendEvent(java.awt.AWTEvent event)
Send the given event as appropriate to the event-generation mode.

simpleClassName

protected static java.lang.String simpleClassName(java.lang.Class cls)

getConverter

public static Converter getConverter(java.lang.Class cls)
Find a string converter for the given class. Returns null if none found.

parseArgs

public static java.lang.String[] parseArgs(java.lang.String value)
Convert the comma-separated list into individual strings. Allow arrays of the format "[el1,el2,el3]" FIXME: doesn't allow commas in args, so if you want commas in your string arguments, you'll have to fix this...

eval

public static java.lang.Object eval(Resolver resolver,
                                    ComponentFinder finder,
                                    java.lang.String arg,
                                    java.lang.Class cls)
                             throws java.lang.IllegalArgumentException,
                                    ComponentNotFoundException
Convert the given string into the given class, if possible, using any available converters if conversion to basic types fails. The Resolver could be a converter, except that if we add it to the converter list, we'd need to remove it when it's no longer in scope.

eval

public static java.lang.Object[] eval(Resolver resolver,
                                      ComponentFinder finder,
                                      java.lang.String[] args,
                                      java.lang.Class[] params)
                               throws java.lang.IllegalArgumentException,
                                      ComponentNotFoundException
Evaluate the given set of arguments into the given set of types.

getKeyStroke

public static javax.swing.KeyStroke getKeyStroke(char ch)
Return the keycode KeyStroke corresponding to the given character, as best we can guess it.

getEventID

public static java.lang.String getEventID(java.lang.Class cls,
                                          int id,
                                          java.lang.String prefix)

getEventID

public static int getEventID(java.lang.Class cls,
                             java.lang.String id)
Return the numeric event ID corresponding to the given string.

getModifiers

public static int getModifiers(java.lang.String mods)
Convert the string representation into the actual modifier mask.

getModifiers

public static java.lang.String getModifiers(int flags)
Convert the integer flags into a string representation.

getKeyCode

public static java.lang.String getKeyCode(int keycode)

getKeyCode

public static int getKeyCode(java.lang.String code)

equals

public boolean equals(java.lang.Object obj1,
                      java.lang.Object obj2)
Perform piecewise comparisons of arrays in addition to regular comparisons.

stringMatch

public boolean stringMatch(java.lang.String pattern,
                           java.lang.String actual)