1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/**
* Copyright 2010 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package com.jogamp.test.junit.util;
import java.lang.reflect.InvocationTargetException;
import java.awt.AWTException;
import java.awt.Component;
import java.awt.Container;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Window;
import java.awt.event.InputEvent;
import javax.swing.JFrame;
public class AWTRobotUtil {
public static int TIME_OUT = 1000; // 1s
public static Point getCenterLocation(Object obj, boolean frameTitlebar)
throws InterruptedException, InvocationTargetException {
Component comp = null;
com.jogamp.newt.Window win = null;
if(obj instanceof com.jogamp.newt.Window) {
win = (com.jogamp.newt.Window) obj;
} else if(obj instanceof Component) {
comp = (Component) obj;
} else {
throw new RuntimeException("Neither AWT nor NEWT: "+obj);
}
int x0, y0;
if(null!=comp) {
Point p0 = comp.getLocationOnScreen();
Rectangle r0 = comp.getBounds();
if( frameTitlebar && comp instanceof JFrame ) {
JFrame jFrame = (JFrame) comp;
Container cont = jFrame.getContentPane();
Point p1 = cont.getLocationOnScreen();
int dx = (int) ( r0.getWidth() / 2.0 + .5 );
int dy = (int) ( ( p1.getY() - p0.getY() ) / 2.0 + .5 );
x0 = (int) ( p0.getX() + dx + .5 ) ;
y0 = (int) ( p0.getY() + dy + .5 ) ;
} else {
x0 = (int) ( p0.getX() + r0.getWidth() / 2.0 + .5 ) ;
y0 = (int) ( p0.getY() + r0.getHeight() / 2.0 + .5 ) ;
}
} else {
x0 = win.getX() + win.getWidth() / 2 ;
y0 = win.getY() + win.getHeight() / 2 ;
}
return new Point(x0, y0);
}
/**
* toFront, call setVisible(true) and toFront(),
* after positioning the mouse in the middle of the window via robot.
* If the given robot is null, a new one is created (waitForIdle=true).
*
* @return True if the Window became the global focused Window within TIME_OUT
*/
public static boolean toFront(Robot robot, Window window)
throws AWTException, InterruptedException, InvocationTargetException {
if(null == robot) {
robot = new Robot();
robot.setAutoWaitForIdle(true);
}
Point p0 = getCenterLocation(window, false);
System.err.println("robot pos: "+p0);
robot.mouseMove( (int) p0.getX(), (int) p0.getY() );
robot.delay(50);
final Window f_window = window;
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
f_window.setVisible(true);
f_window.toFront();
f_window.requestFocus();
}});
robot.delay(200);
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
int wait;
for (wait=0; wait<10 && window != kfm.getFocusedWindow(); wait++) {
Thread.sleep(TIME_OUT/10);
}
return wait<10;
}
/**
* centerMouse
*/
public static void centerMouse(Robot robot, Object obj)
throws AWTException, InterruptedException, InvocationTargetException {
Component comp = null;
com.jogamp.newt.Window win = null;
if(null == robot) {
robot = new Robot();
robot.setAutoWaitForIdle(true);
}
if(obj instanceof com.jogamp.newt.Window) {
win = (com.jogamp.newt.Window) obj;
} else if(obj instanceof Component) {
comp = (Component) obj;
} else {
throw new RuntimeException("Neither AWT nor NEWT: "+obj);
}
Point p0 = getCenterLocation(obj, false);
System.err.println("robot pos: "+p0);
robot.mouseMove( (int) p0.getX(), (int) p0.getY() );
robot.delay(50);
}
/**
* requestFocus, if robot is valid, use mouse operation,
* otherwise programatic, ie call requestFocus
*/
public static void requestFocus(Robot robot, Object obj)
throws AWTException, InterruptedException, InvocationTargetException {
Component comp = null;
com.jogamp.newt.Window win = null;
if(obj instanceof com.jogamp.newt.Window) {
win = (com.jogamp.newt.Window) obj;
} else if(obj instanceof Component) {
comp = (Component) obj;
} else {
throw new RuntimeException("Neither AWT nor NEWT: "+obj);
}
if(null == robot) {
if(null!=comp) {
final Component f_comp = comp;
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
f_comp.requestFocus();
}});
} else {
win.requestFocus();
}
return;
}
centerMouse(robot, obj);
robot.delay(50);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(50);
}
/**
*
* @return True if the Window became the global focused Window within TIME_OUT
*/
public static boolean waitForFocus(Object obj) throws InterruptedException {
int wait;
if(obj instanceof Component) {
Component comp = (Component) obj;
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
for (wait=0; wait<10 && comp != kfm.getPermanentFocusOwner(); wait++) {
Thread.sleep(TIME_OUT/10);
}
} else if(obj instanceof com.jogamp.newt.Window) {
com.jogamp.newt.Window win = (com.jogamp.newt.Window) obj;
for (wait=0; wait<10 && !win.hasFocus(); wait++) {
Thread.sleep(TIME_OUT/10);
}
} else {
throw new RuntimeException("Neither AWT nor NEWT: "+obj);
}
return wait<10;
}
public static boolean requestFocusAndWait(Robot robot, Object requestFocus, Object waitForFocus)
throws AWTException, InterruptedException, InvocationTargetException {
requestFocus(robot, requestFocus);
return waitForFocus(waitForFocus);
}
/**
* @param keyTypedCounter shall return the number of keys typed (press + release)
* @return True if typeCount keys within TIME_OUT has been received
*/
public static int testKeyType(Robot robot, int typeCount, Object obj, EventCountAdapter keyTypedCounter)
throws AWTException, InterruptedException, InvocationTargetException {
Component comp = null;
com.jogamp.newt.Window win = null;
if(null == robot) {
robot = new Robot();
robot.setAutoWaitForIdle(true);
}
centerMouse(robot, obj);
int c0 = keyTypedCounter.getCount();
for(int i=0; i<typeCount; i++) {
robot.keyPress(java.awt.event.KeyEvent.VK_A);
robot.delay(50);
robot.keyRelease(java.awt.event.KeyEvent.VK_A);
robot.delay(50);
}
// Wait for the key events to be processed.
int wait;
for (wait=0; wait<10 && (keyTypedCounter.getCount()-c0)<typeCount; wait++) {
Thread.sleep(TIME_OUT/10);
}
return keyTypedCounter.getCount()-c0;
}
/**
* @param mouseButton ie InputEvent.BUTTON1_MASK
* @param clickCount ie 1, or 2
* @return True if the desired clickCount within TIME_OUT has been received
*/
public static int testMouseClick(Robot robot, int mouseButton, int clickCount,
Object obj, EventCountAdapter mouseClickCounter)
throws AWTException, InterruptedException, InvocationTargetException {
if(null == robot) {
robot = new Robot();
robot.setAutoWaitForIdle(true);
}
final int clickTO = com.jogamp.newt.event.MouseEvent.getClickTimeout();
centerMouse(robot, obj);
robot.delay(2*clickTO);
int c0 = mouseClickCounter.getCount();
for(int i=0; i<clickCount; i++) {
robot.mousePress(mouseButton);
robot.delay(clickTO/4);
robot.mouseRelease(mouseButton);
robot.delay(clickTO/4);
}
robot.delay(50);
// Wait for the key events to be processed.
int wait;
for (wait=0; wait<10 && (mouseClickCounter.getCount()-c0)<clickCount; wait++) {
Thread.sleep(TIME_OUT/10);
}
return mouseClickCounter.getCount()-c0;
}
}
|