|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
JFCTestHelper.java | 78.6% | 94.9% | 100% | 91.8% |
|
1 |
package junit.extensions.jfcunit;
|
|
2 |
|
|
3 |
import junit.extensions.jfcunit.keyboard.JFCKeyStroke;
|
|
4 |
|
|
5 |
import java.awt.AWTEvent;
|
|
6 |
import java.awt.Component;
|
|
7 |
import java.awt.EventQueue;
|
|
8 |
import java.awt.Point;
|
|
9 |
import java.awt.Toolkit;
|
|
10 |
import java.awt.event.KeyEvent;
|
|
11 |
import java.awt.event.MouseEvent;
|
|
12 |
|
|
13 |
import java.lang.reflect.Constructor;
|
|
14 |
|
|
15 |
|
|
16 |
/**
|
|
17 |
* Class that provides facilities for locating components within a GUI. To use
|
|
18 |
* create a new instance of JFCTestHelper in your setUp. Windows can only be
|
|
19 |
* located once they have been shown once to the user. Provide methods for
|
|
20 |
* entering clicks and drag operations.
|
|
21 |
*
|
|
22 |
* Note: java.awt.dnd functions are tightly integrated with the OS and are
|
|
23 |
* not dependent on the AWTEventQueue processing. Thus these interactions
|
|
24 |
* can only be done with the RobotTestHelper which sends events in at the
|
|
25 |
* OS level.
|
|
26 |
*
|
|
27 |
* @author Matt Caswell
|
|
28 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
29 |
* @author Kevin Wilson
|
|
30 |
*/
|
|
31 |
public final class JFCTestHelper extends TestHelper { |
|
32 |
// though this is a helper class, these "instance" variables are used
|
|
33 |
// to "display" the difference between the previous "state" and the
|
|
34 |
// current one.
|
|
35 |
|
|
36 |
/**
|
|
37 |
* The coordinate where the last event was fired.
|
|
38 |
*/
|
|
39 |
private static Point s_last = new Point(0, 0); |
|
40 |
|
|
41 |
/**
|
|
42 |
* The EventQueue on which all events are processed.
|
|
43 |
*/
|
|
44 |
private EventQueue m_queue;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* The popup that appeared in the previous call.
|
|
48 |
*/
|
|
49 |
private boolean m_lastPopup = false; |
|
50 |
|
|
51 |
/** Last number of clicks. */
|
|
52 |
private int m_lastClicks; |
|
53 |
|
|
54 |
/**
|
|
55 |
* The mouse buttons that were pressed in the previous call.
|
|
56 |
*/
|
|
57 |
private int m_lastMouseModifiers = 0; |
|
58 |
|
|
59 |
/**
|
|
60 |
* The mouse buttons that were pressed in the previous call.
|
|
61 |
*/
|
|
62 |
private long m_lastPressed = 0; |
|
63 |
|
|
64 |
/**
|
|
65 |
* Constructor.
|
|
66 |
*/
|
|
67 | 1258 |
public JFCTestHelper() {
|
68 | 1258 |
super();
|
69 | 1258 |
m_queue = Toolkit.getDefaultToolkit().getSystemEventQueue(); |
70 |
} |
|
71 |
|
|
72 |
/**
|
|
73 |
* Process a key press event on a component.
|
|
74 |
*
|
|
75 |
* @param ultimate The ultimate parent Component
|
|
76 |
* @param stroke The JFCKeyStroke to be performed.
|
|
77 |
*/
|
|
78 | 329 |
protected void keyPressed(final Component ultimate, |
79 |
final JFCKeyStroke stroke) { |
|
80 | 329 |
postEvent( |
81 |
new KeyEvent(
|
|
82 |
ultimate, |
|
83 |
KeyEvent.KEY_PRESSED, |
|
84 |
System.currentTimeMillis(), |
|
85 |
stroke.getModifiers(), |
|
86 |
stroke.getKeyCode(), |
|
87 |
stroke.getKeyChar())); |
|
88 |
} |
|
89 |
|
|
90 |
/**
|
|
91 |
* Process a key release event on a component.
|
|
92 |
*
|
|
93 |
* @param ultimate The ultimate parent Component
|
|
94 |
* @param stroke The JFCKeyStroke to be performed.
|
|
95 |
*/
|
|
96 | 329 |
protected void keyReleased(final Component ultimate, |
97 |
final JFCKeyStroke stroke) { |
|
98 | 329 |
if (stroke.isTyped()) {
|
99 | 285 |
postEvent( |
100 |
new KeyEvent(
|
|
101 |
ultimate, |
|
102 |
KeyEvent.KEY_TYPED, |
|
103 |
System.currentTimeMillis(), |
|
104 |
stroke.getModifiers(), |
|
105 |
KeyEvent.VK_UNDEFINED, |
|
106 |
stroke.getKeyChar())); |
|
107 |
} |
|
108 |
|
|
109 | 329 |
postEvent( |
110 |
new KeyEvent(
|
|
111 |
ultimate, |
|
112 |
KeyEvent.KEY_RELEASED, |
|
113 |
System.currentTimeMillis(), |
|
114 |
stroke.getModifiers(), |
|
115 |
stroke.getKeyCode(), |
|
116 |
stroke.getKeyChar())); |
|
117 |
} |
|
118 |
|
|
119 |
/**
|
|
120 |
* Process a mouse move event on a component.
|
|
121 |
*
|
|
122 |
* @param ultimate The ultimate parent Component
|
|
123 |
* @param x The x coordinate of the point where the mouse is being moved to.
|
|
124 |
* @param y The y coordinate of the point where the mouse is being moved to.
|
|
125 |
*/
|
|
126 | 1554 |
protected void mouseMoved(final Component ultimate, final int x, final int y) { |
127 | 1554 |
Point dest = new Point(x, y);
|
128 | 1554 |
dest.translate(-ultimate.getLocationOnScreen().x, |
129 |
-ultimate.getLocationOnScreen().y); |
|
130 |
|
|
131 | 1554 |
int mouseEventType = MouseEvent.MOUSE_MOVED;
|
132 |
|
|
133 | 1554 |
if (m_lastPressed != 0) {
|
134 | 54 |
mouseEventType = MouseEvent.MOUSE_DRAGGED; |
135 |
} |
|
136 |
|
|
137 | 1554 |
while ((s_last.x != dest.x) || (s_last.y != dest.y)) {
|
138 | 18545 |
s_last = calcNextPoint( |
139 |
s_last, |
|
140 |
dest, |
|
141 |
getStep()); |
|
142 | 18545 |
postEvent( |
143 |
new MouseEvent(
|
|
144 |
ultimate, |
|
145 |
mouseEventType, |
|
146 |
System.currentTimeMillis(), |
|
147 |
m_lastMouseModifiers, |
|
148 |
s_last.x, |
|
149 |
s_last.y, |
|
150 |
0, |
|
151 |
m_lastPopup)); |
|
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
/**
|
|
156 |
* Process a mouse press event on a component.
|
|
157 |
*
|
|
158 |
* @param ultimate The ultimate parent Component
|
|
159 |
* @param modifiers The modifiers associated with this mouse event.
|
|
160 |
* @param click The number of clicks associated with this mouse event.
|
|
161 |
* @param isPopupTrigger Whether this mouse event will generate a popup.
|
|
162 |
*/
|
|
163 | 896 |
protected void mousePressed(final Component ultimate, final int modifiers, |
164 |
final int click, final boolean isPopupTrigger) { |
|
165 | 896 |
m_lastPressed = System.currentTimeMillis(); |
166 | 896 |
m_lastPopup = isPopupTrigger; |
167 | 896 |
m_lastMouseModifiers = modifiers; |
168 |
|
|
169 | 896 |
if (modifiers != 0) {
|
170 | 896 |
postEvent( |
171 |
new MouseEvent(ultimate, MouseEvent.MOUSE_PRESSED,
|
|
172 |
m_lastPressed, modifiers, s_last.x, s_last.y, click, |
|
173 |
isPopupTrigger)); |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
/**
|
|
178 |
* Process a mouse release event on a component.
|
|
179 |
*
|
|
180 |
* @param ultimate The ultimate parent Component
|
|
181 |
* @param modifiers The modifiers associated with this mouse event.
|
|
182 |
* @param click The number of clicks associated with this mouse event.
|
|
183 |
* @param isPopupTrigger Whether this mouse event will generate a popup.
|
|
184 |
*/
|
|
185 | 896 |
protected void mouseReleased(final Component ultimate, final int modifiers, |
186 |
final int click, final boolean isPopupTrigger) { |
|
187 | 896 |
m_lastMouseModifiers = modifiers; |
188 | 896 |
m_lastPopup = isPopupTrigger; |
189 | 896 |
m_lastClicks = click; |
190 |
|
|
191 | 896 |
if (modifiers != 0) {
|
192 | 896 |
postEvent( |
193 |
new MouseEvent(
|
|
194 |
ultimate, |
|
195 |
MouseEvent.MOUSE_RELEASED, |
|
196 |
System.currentTimeMillis(), |
|
197 |
modifiers, |
|
198 |
s_last.x, |
|
199 |
s_last.y, |
|
200 |
click, |
|
201 |
isPopupTrigger)); |
|
202 |
|
|
203 | 896 |
long delta = m_lastPressed - System.currentTimeMillis();
|
204 |
|
|
205 | 896 |
if ((click > 0) && (delta < 100)) {
|
206 | 894 |
postEvent( |
207 |
new MouseEvent(
|
|
208 |
ultimate, |
|
209 |
MouseEvent.MOUSE_CLICKED, |
|
210 |
System.currentTimeMillis(), |
|
211 |
modifiers, |
|
212 |
s_last.x, |
|
213 |
s_last.y, |
|
214 |
click, |
|
215 |
isPopupTrigger)); |
|
216 |
} |
|
217 |
} |
|
218 |
|
|
219 | 896 |
m_lastPressed = 0; |
220 |
} |
|
221 |
|
|
222 |
/**
|
|
223 |
* Simulate rotating the mouseWheel
|
|
224 |
* Only supported in Java 1.4.
|
|
225 |
*
|
|
226 |
* @param ultimate Component to fire the events on.
|
|
227 |
* @param amount Amount to rotate the wheel.
|
|
228 |
* @param wheelRotation amount to rotate the wheel. positive for
|
|
229 |
* rotation towards the user. Negative for rotation away from the user.
|
|
230 |
*/
|
|
231 | 200 |
protected void mouseWheel(final Component ultimate, final int amount, |
232 |
final int wheelRotation) {
|
|
233 |
// This class is coded with reflection to allow the class
|
|
234 |
// to compile on versions of the JDK less than 1.4.
|
|
235 | 200 |
try {
|
236 | 200 |
Class mouseWheelEventClass = Class.forName( |
237 |
"java.awt.event.MouseWheelEvent");
|
|
238 | 200 |
Constructor constructor = mouseWheelEventClass.getConstructor( |
239 |
new Class[] {
|
|
240 |
Component.class, int.class, long.class, int.class, |
|
241 |
int.class, int.class, int.class, boolean.class, |
|
242 |
int.class, int.class, int.class |
|
243 |
}); |
|
244 |
|
|
245 | 200 |
int scrollType = 0;
|
246 |
|
|
247 | 200 |
int eventId = mouseWheelEventClass.getField("MOUSE_WHEEL").getInt(null); |
248 |
|
|
249 | 200 |
if ((m_lastMouseModifiers & MouseEvent.SHIFT_MASK) > 1) {
|
250 | 0 |
scrollType = mouseWheelEventClass.getField("WHEEL_BLOCK_SCROLL")
|
251 |
.getInt(null);
|
|
252 |
} else {
|
|
253 | 200 |
scrollType = mouseWheelEventClass.getField("WHEEL_UNIT_SCROLL")
|
254 |
.getInt(null);
|
|
255 |
} |
|
256 |
|
|
257 | 200 |
postEvent((AWTEvent) constructor.newInstance( |
258 |
new Object[] {
|
|
259 |
ultimate, new Integer(eventId),
|
|
260 |
new Long(System.currentTimeMillis()),
|
|
261 |
new Integer(m_lastMouseModifiers), new Integer(s_last.x), |
|
262 |
new Integer(s_last.y), new Integer(m_lastClicks), |
|
263 |
new Boolean(m_lastPopup), new Integer(scrollType), |
|
264 |
new Integer(amount), new Integer(wheelRotation) |
|
265 |
})); |
|
266 |
} catch (Exception ex) {
|
|
267 | 0 |
throw new RuntimeException("Mouse Wheel not supported" |
268 |
+ ex.toString()); |
|
269 |
} |
|
270 |
|
|
271 |
//new MouseWheelEvent(Component source,
|
|
272 |
// int id,
|
|
273 |
// long when,
|
|
274 |
// int modifiers,
|
|
275 |
// int x,
|
|
276 |
// int y,
|
|
277 |
// int clickCount,
|
|
278 |
// boolean popupTrigger,
|
|
279 |
// int scrollType,
|
|
280 |
// int scrollAmount,
|
|
281 |
// int wheelRotation
|
|
282 |
//throw new RuntimeException("Method is not supported by abstract class: mouseWheel");
|
|
283 |
} |
|
284 |
|
|
285 |
/**
|
|
286 |
* This method is just present so as to put debug statements in one central place,
|
|
287 |
* without repeating everywhere.
|
|
288 |
*
|
|
289 |
* @param evt The event to be posted.
|
|
290 |
*/
|
|
291 | 22374 |
private void postEvent(final AWTEvent evt) { |
292 | 22374 |
m_queue.postEvent(evt); |
293 |
} |
|
294 |
} |
|
295 |
|
|