|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| RobotTestHelper.java | 66.7% | 95.8% | 100% | 91.9% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit;
|
|
| 2 |
|
|
| 3 |
import junit.extensions.jfcunit.keyboard.JFCKeyStroke;
|
|
| 4 |
|
|
| 5 |
import java.awt.AWTException;
|
|
| 6 |
import java.awt.Component;
|
|
| 7 |
import java.awt.Point;
|
|
| 8 |
import java.awt.Robot;
|
|
| 9 |
import java.awt.event.InputEvent;
|
|
| 10 |
|
|
| 11 |
import java.lang.reflect.Method;
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
/**
|
|
| 15 |
* Class that provides facilities for locating components within a GUI. To use
|
|
| 16 |
* create a new instance of RobotTestHelper in your setUp. Windows can only be
|
|
| 17 |
* located once they have been shown once to the user.
|
|
| 18 |
*
|
|
| 19 |
* After calling the helper.enterClickAndLeave() the JFCTestCase.sleep(long delay)
|
|
| 20 |
* method should be called to prevent subsequent clicks from being treated as a
|
|
| 21 |
* double click. A sleep dalay of 300ms can be used as a guideline.
|
|
| 22 |
*
|
|
| 23 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
| 24 |
* @author Kevin Wilson
|
|
| 25 |
*/
|
|
| 26 |
public class RobotTestHelper extends TestHelper { |
|
| 27 |
/**
|
|
| 28 |
* Last location.
|
|
| 29 |
*/
|
|
| 30 |
private static Point s_last = new Point(-1, -1); |
|
| 31 |
|
|
| 32 |
/**
|
|
| 33 |
* Legal buttons.
|
|
| 34 |
*/
|
|
| 35 |
private static final int LEGAL_BUTTONS = (InputEvent.BUTTON1_MASK |
|
| 36 |
| InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK); |
|
| 37 |
|
|
| 38 |
/**
|
|
| 39 |
* Robot instance.
|
|
| 40 |
*/
|
|
| 41 |
private Robot m_robot = null; |
|
| 42 |
|
|
| 43 |
// Without this delay events tend to get lost somewhere between
|
|
| 44 |
// the robot and the event queue.
|
|
| 45 |
|
|
| 46 |
/**
|
|
| 47 |
* Mandatory delay to get thing working.
|
|
| 48 |
*/
|
|
| 49 |
private int m_delay = 1; |
|
| 50 |
|
|
| 51 |
/**
|
|
| 52 |
* Construct a new RobotTestHelper.
|
|
| 53 |
*
|
|
| 54 |
* @exception AWTException An instance of java.awt.AWTException can be thrown
|
|
| 55 |
* when initializing the Robot.
|
|
| 56 |
*/
|
|
| 57 | 102 |
public RobotTestHelper() throws AWTException { |
| 58 | 102 |
super();
|
| 59 | 102 |
m_robot = new Robot();
|
| 60 |
} |
|
| 61 |
|
|
| 62 |
/**
|
|
| 63 |
* Process a key press event on a component.
|
|
| 64 |
*
|
|
| 65 |
* @param ultimate Not used in the robot.
|
|
| 66 |
* @param stroke The JFCKeyStroke to be performed.
|
|
| 67 |
*/
|
|
| 68 | 638 |
protected void keyPressed(final Component ultimate, |
| 69 |
final JFCKeyStroke stroke) {
|
|
| 70 | 638 |
m_robot.delay(m_delay); |
| 71 | 638 |
m_robot.keyPress(stroke.getKeyCode()); |
| 72 |
} |
|
| 73 |
|
|
| 74 |
/**
|
|
| 75 |
* Process a key release event on a component.
|
|
| 76 |
*
|
|
| 77 |
* @param ultimate Not used in the robot.
|
|
| 78 |
* @param stroke The JFCKeyStroke to be performed.
|
|
| 79 |
*/
|
|
| 80 | 638 |
protected void keyReleased(final Component ultimate, |
| 81 |
final JFCKeyStroke stroke) {
|
|
| 82 | 638 |
m_robot.delay(m_delay); |
| 83 | 638 |
m_robot.keyRelease(stroke.getKeyCode()); |
| 84 |
} |
|
| 85 |
|
|
| 86 |
/**
|
|
| 87 |
* Process a mouse move event on a component.
|
|
| 88 |
*
|
|
| 89 |
* @param ultimate Not used in the robot.
|
|
| 90 |
* @param x The x coordinate of the point where the mouse is being moved to.
|
|
| 91 |
* @param y The y coordinate of the point where the mouse is being moved to.
|
|
| 92 |
*/
|
|
| 93 | 1436 |
protected void mouseMoved(final Component ultimate, final int x, final int y) { |
| 94 | 1436 |
Point dest = new Point(x, y);
|
| 95 |
|
|
| 96 | 1436 |
while ((s_last.x != dest.x) || (s_last.y != dest.y)) {
|
| 97 | 12912 |
s_last = calcNextPoint( |
| 98 |
s_last, |
|
| 99 |
dest, |
|
| 100 |
getStep()); |
|
| 101 | 12912 |
m_robot.delay(m_delay); |
| 102 | 12912 |
m_robot.mouseMove(s_last.x, s_last.y); |
| 103 |
} |
|
| 104 |
} |
|
| 105 |
|
|
| 106 |
/**
|
|
| 107 |
* Process a mouse press event on a component.
|
|
| 108 |
*
|
|
| 109 |
* @param ultimate Not used in the robot.
|
|
| 110 |
* @param modifiers The modifiers associated with this mouse event.
|
|
| 111 |
* @param click The number of clicks associated with this mouse event.
|
|
| 112 |
* @param isPopupTrigger Whether this mouse event will generate a popup.
|
|
| 113 |
*/
|
|
| 114 | 760 |
protected void mousePressed(final Component ultimate, final int modifiers, |
| 115 |
final int click, final boolean isPopupTrigger) { |
|
| 116 | 760 |
m_robot.delay(m_delay); |
| 117 |
|
|
| 118 | 760 |
if (modifiers != 0) {
|
| 119 | 760 |
int buttons = (modifiers & LEGAL_BUTTONS);
|
| 120 | 760 |
m_robot.mousePress(buttons); |
| 121 |
} |
|
| 122 |
} |
|
| 123 |
|
|
| 124 |
/**
|
|
| 125 |
* Process a mouse release event on a component.
|
|
| 126 |
*
|
|
| 127 |
* @param ultimate Not used in the robot.
|
|
| 128 |
* @param modifiers The modifiers associated with this mouse event.
|
|
| 129 |
* @param click The number of clicks associated with this mouse event.
|
|
| 130 |
* @param isPopupTrigger Whether this mouse event will generate a popup.
|
|
| 131 |
*/
|
|
| 132 | 760 |
protected void mouseReleased(final Component ultimate, final int modifiers, |
| 133 |
final int click, final boolean isPopupTrigger) { |
|
| 134 | 760 |
m_robot.delay(m_delay); |
| 135 |
|
|
| 136 | 760 |
if (modifiers != 0) {
|
| 137 | 760 |
int buttons = (modifiers & LEGAL_BUTTONS);
|
| 138 | 760 |
m_robot.mouseRelease(buttons); |
| 139 |
} |
|
| 140 |
} |
|
| 141 |
|
|
| 142 |
/**
|
|
| 143 |
* Simulate rotating the mouseWheel.
|
|
| 144 |
* Only supported in Java 1.4.
|
|
| 145 |
*
|
|
| 146 |
* @param ultimate Component to fire the events on.
|
|
| 147 |
* @param amount Amount to rotate the wheel. Ignored on the robot.
|
|
| 148 |
* The amount will be extracted from the OS Mouse configuration.
|
|
| 149 |
* @param wheelRotation Rotation of the mouse wheel.
|
|
| 150 |
*/
|
|
| 151 | 700 |
protected void mouseWheel(final Component ultimate, final int amount, |
| 152 |
final int wheelRotation) {
|
|
| 153 | 700 |
m_robot.delay(m_delay); |
| 154 |
|
|
| 155 | 700 |
try {
|
| 156 | 700 |
Method method = Robot.class.getMethod(
|
| 157 |
"mouseWheel",
|
|
| 158 |
new Class[] {int.class}); |
|
| 159 | 700 |
method.invoke( |
| 160 |
m_robot, |
|
| 161 |
new Object[] {new Integer(wheelRotation)}); |
|
| 162 |
} catch (Exception ex) {
|
|
| 163 | 0 |
throw new RuntimeException("Mouse Wheel not supported:" |
| 164 |
+ ex.toString()); |
|
| 165 |
} |
|
| 166 |
} |
|
| 167 |
} |
|
| 168 |
|
|
||||||||||