001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package java.awt;
018:
019: import java.awt.event.InputEvent;
020: import java.awt.event.KeyAdapter;
021: import java.awt.event.KeyEvent;
022: import java.awt.event.MouseAdapter;
023: import java.awt.event.MouseEvent;
024: import java.awt.event.MouseWheelEvent;
025: import java.awt.event.MouseWheelListener;
026: import java.awt.image.BufferedImage;
027: import java.awt.image.ColorModel;
028: import java.math.BigInteger;
029: import java.security.Permission;
030:
031: import junit.framework.TestCase;
032:
033: /**
034: * RobotTest
035: */
036: public class RobotTest extends TestCase {
037:
038: Robot robot;
039: Frame f;
040: Throwable throwable;
041: boolean exceptionCaught;
042: MouseEvent mouseEvent;
043: Button b;
044: Object lock;
045: KeyEvent keyEvent;
046:
047: public static void main(String[] args) {
048: junit.textui.TestRunner.run(RobotTest.class);
049: }
050:
051: @Override
052: protected void setUp() throws Exception {
053: super .setUp();
054: System.setSecurityManager(null);
055: robot = new Robot();
056: f = new Frame("Robot test");
057: throwable = null;
058: exceptionCaught = false;
059: mouseEvent = null;
060: keyEvent = null;
061: lock = new Object();
062: b = new Button();
063: }
064:
065: @Override
066: protected void tearDown() throws Exception {
067: super .tearDown();
068: if (f != null) {
069: f.dispose();
070: }
071: }
072:
073: @SuppressWarnings("deprecation")
074: private void waitForButton() {
075: int timeout = 16, time = 0;
076: int nAttempts = 10;
077: f.add(b);
078: f.setSize(100, 100);
079: int x = 50;
080: int y = 50;
081: Color bkColor = robot.getPixelColor(x, y);
082: b.setBackground(getNextColor(bkColor));
083: f.show();
084:
085: for (int i = 0; i < nAttempts; i++) {
086: robot.delay(timeout);
087: time += timeout;
088: Point fLoc = f.getLocation();
089: x = fLoc.x + f.getWidth() / 2;
090: y = fLoc.y + f.getHeight() / 2;
091: if (robot.getPixelColor(x, y).equals(b.getBackground())) {
092: break;
093: }
094: timeout <<= 1;
095: }
096:
097: assertEquals("button is shown", b.getBackground(), robot
098: .getPixelColor(x, y));
099: }
100:
101: private int inv(int val) {
102: return ~val & 0xFF;
103: }
104:
105: private Color getNextColor(Color bkColor) {
106: Color color = bkColor;
107: ColorModel cm = f.getGraphicsConfiguration().getColorModel();
108: Object pixel = null;
109: while (color.equals(bkColor)) {
110:
111: color = new Color(inv(color.getRed()),
112: inv(color.getGreen()), inv(color.getBlue()));
113:
114: pixel = cm.getDataElements(color.getRGB(), pixel);
115: color = new Color(cm.getRGB(pixel));
116: }
117: return color;
118: }
119:
120: public final void testToString() {
121: String str = robot.toString();
122: assertNotNull(str);
123: assertTrue(str.startsWith(robot.getClass().getName()));
124: assertTrue(str.indexOf("autoDelay = 0,") > 0);
125: assertTrue(str.indexOf("autoWaitForIdle = false") > 0);
126: }
127:
128: /*
129: * Class under test for void Robot()
130: */
131: public final void testRobot() {
132: Runnable cons = new Runnable() {
133: public void run() {
134: try {
135: robot = new Robot();
136: } catch (AWTException e) {
137: e.printStackTrace();
138: }
139: }
140: };
141: assertTrue(isDenied(new MySecurityManager(), cons));
142: assertFalse(isDenied(null, cons));
143: }
144:
145: private boolean isDenied(SecurityManager sm, Runnable code) {
146: exceptionCaught = false;
147: System.setSecurityManager(sm);
148: try {
149: code.run();
150: } catch (SecurityException se) {
151: exceptionCaught = true;
152: }
153: return exceptionCaught;
154: }
155:
156: /*
157: * Class under test for void Robot(java.awt.GraphicsDevice)
158: */
159: public final void testRobotGraphicsDevice() throws AWTException {
160: try {
161: robot = new Robot(new PrintDevice());
162: } catch (IllegalArgumentException iae) {
163: exceptionCaught = true;
164: } catch (AWTException ae) {
165: exceptionCaught = false;
166: }
167: assertTrue(exceptionCaught);
168: exceptionCaught = false;
169: assertNotNull(f);
170: try {
171: robot = new Robot(f.getGraphicsConfiguration().getDevice());
172: } catch (IllegalArgumentException iae) {
173: exceptionCaught = true;
174: } catch (AWTException ae) {
175: exceptionCaught = true;
176: }
177: assertFalse(exceptionCaught);
178:
179: // Regression test for HARMONY-2442
180: try {
181: new Robot(null);
182: fail("IllegalArgumentException was not thrown"); //$NON-NLS-1$
183: } catch (IllegalArgumentException ex) {
184: // expected
185: }
186: }
187:
188: public final void testCreateScreenCapture() {
189: Runnable capture = new Runnable() {
190: public void run() {
191: robot.createScreenCapture(new Rectangle(10, 10, 100,
192: 200));
193: }
194: };
195: assertTrue(isDenied(new MySecurityManager(), capture));
196: assertFalse(isDenied(null, capture));
197:
198: BufferedImage img = null;
199: Rectangle rect = new Rectangle();
200: try {
201: img = robot.createScreenCapture(rect);
202: } catch (IllegalArgumentException iae) {
203: exceptionCaught = true;
204: }
205: assertTrue(exceptionCaught);
206: assertNull(img);
207: exceptionCaught = false;
208: img = null;
209: rect.width = 100;
210: try {
211: img = robot.createScreenCapture(rect);
212: } catch (IllegalArgumentException iae) {
213: exceptionCaught = true;
214: }
215: assertTrue(exceptionCaught);
216: assertNull(img);
217: exceptionCaught = false;
218: img = null;
219: waitForButton();
220: rect.setBounds(f.getBounds());
221:
222: try {
223: img = robot.createScreenCapture(rect);
224: } catch (IllegalArgumentException iae) {
225: exceptionCaught = true;
226: }
227: assertFalse(exceptionCaught);
228: assertNotNull(img);
229: int rgb = img.getRGB(30, 50);
230: assertEquals(rect.width, img.getWidth(null));
231: assertEquals(rect.height, img.getHeight(null));
232:
233: assertEquals("RGB", b.getBackground().getRGB(), rgb);
234: }
235:
236: public final void testDelay() {
237: int delay = 2000;
238: long startTime = System.currentTimeMillis();
239: robot.delay(delay);
240: long dTime = System.currentTimeMillis() - startTime;
241: assertTrue(dTime - delay < delay / 10);
242: try {
243: robot.delay(delay = 60001);
244: } catch (IllegalArgumentException iae) {
245: exceptionCaught = true;
246: }
247: assertTrue(exceptionCaught);
248: exceptionCaught = false;
249: try {
250: robot.delay(delay = -1);
251: } catch (IllegalArgumentException iae) {
252: exceptionCaught = true;
253: }
254: assertTrue(exceptionCaught);
255: }
256:
257: public final void testGetAutoDelay() {
258: assertEquals(0, robot.getAutoDelay());
259: }
260:
261: public final void testGetPixelColor() {
262: b.setBackground(Color.BLUE);
263: waitForButton();
264: int centerX = b.getX() + b.getWidth() / 2;
265: int centerY = b.getY() + b.getHeight() / 2;
266: assertEquals(b.getBackground(), robot.getPixelColor(centerX,
267: centerY));
268: }
269:
270: public final void testIsAutoWaitForIdle() {
271: assertFalse(robot.isAutoWaitForIdle());
272: }
273:
274: public final void testKeyPress() {
275: try {
276: robot.keyPress(-1000);
277: } catch (IllegalArgumentException iae) {
278: exceptionCaught = true;
279: }
280: assertTrue(exceptionCaught);
281:
282: waitForButton();
283: waitFocus(5000);
284: b.addKeyListener(new KeyAdapter() {
285: @Override
286: public void keyPressed(KeyEvent ke) {
287: keyEvent = ke;
288: }
289: });
290: robot.setAutoWaitForIdle(true);
291: robot.setAutoDelay(1000);
292: int key = KeyEvent.VK_SPACE;
293: robot.delay(500);
294: robot.keyPress(key);
295: robot.delay(1000);
296: assertNotNull("key event was dispatched", keyEvent);
297: assertEquals("key pressed", KeyEvent.KEY_PRESSED, keyEvent
298: .getID());
299: assertEquals("proper key is pressed", key, keyEvent
300: .getKeyCode());
301: robot.keyRelease(key);
302: }
303:
304: private void waitFocus(int delay) {
305: int timeout = 250;
306: int time = 0;
307: while (!b.isFocusOwner() && time < delay) {
308: robot.delay(timeout);
309: b.requestFocusInWindow();
310: time += timeout;
311: }
312: assertTrue("button has focus", b.isFocusOwner());
313: }
314:
315: public final void testKeyRelease() {
316: try {
317: robot.keyRelease(666);
318: } catch (IllegalArgumentException iae) {
319: exceptionCaught = true;
320: }
321: assertTrue(exceptionCaught);
322: waitForButton();
323: waitFocus(5000);
324: b.addKeyListener(new KeyAdapter() {
325: @Override
326: public void keyReleased(KeyEvent ke) {
327: keyEvent = ke;
328: }
329: });
330: robot.setAutoDelay(100);
331: int key = KeyEvent.VK_SPACE;
332: robot.keyPress(key);
333: robot.keyRelease(key);
334: robot.delay(1000);
335: assertNotNull(keyEvent);
336: assertEquals(KeyEvent.KEY_RELEASED, keyEvent.getID());
337: assertEquals(key, keyEvent.getKeyCode());
338: }
339:
340: private int sgn(int n) {
341: return BigInteger.valueOf(n).signum();
342: }
343:
344: private void movePointer(Point p1, Point p2) {
345: p2.translate(b.getWidth() / 2, b.getHeight() / 2);
346: float k = (p2.y - p1.y) / (float) (p2.x - p1.x);
347: float b = p2.y - k * p2.x;
348: robot.setAutoDelay(20);
349: int dx = 3 * sgn(p2.x - p1.x);
350: for (int x = p1.x; Math.abs(x - p2.x) >= 5; x += dx) {
351: robot.mouseMove(x, (int) (k * x + b));
352: }
353: }
354:
355: public final void testMouseMove() {
356: Point p = new Point(100, 200);
357: robot.setAutoDelay(0);
358: robot.mouseMove(p.x, p.y);
359: assertEquals(p, MouseInfo.getPointerInfo().getLocation());
360: }
361:
362: public final void testMousePress() {
363: try {
364: robot.mousePress(-1);
365: } catch (IllegalArgumentException iae) {
366: exceptionCaught = true;
367: }
368: assertTrue(exceptionCaught);
369:
370: waitForButton();
371: Point p1 = MouseInfo.getPointerInfo().getLocation();
372: Point p2 = b.getLocationOnScreen();
373: movePointer(p1, p2);
374: b.addMouseListener(new MouseAdapter() {
375: @Override
376: public void mousePressed(MouseEvent me) {
377: mouseEvent = me;
378: }
379: });
380: robot.setAutoDelay(0);
381: int mask = InputEvent.BUTTON1_MASK;
382: robot.mousePress(mask);
383: robot.delay(5000);
384: assertNotNull(mouseEvent);
385: assertEquals(MouseEvent.MOUSE_PRESSED, mouseEvent.getID());
386: assertEquals(MouseEvent.BUTTON1, mouseEvent.getButton());
387: robot.mouseRelease(mask);
388:
389: }
390:
391: public final void testMouseRelease() {
392: try {
393: robot.mouseRelease(-1);
394: } catch (IllegalArgumentException iae) {
395: exceptionCaught = true;
396: }
397: assertTrue("exception(IAE) is thrown", exceptionCaught);
398:
399: waitForButton();
400: Point p1 = MouseInfo.getPointerInfo().getLocation();
401: Point p2 = b.getLocationOnScreen();
402: int mask = InputEvent.BUTTON3_MASK;
403: movePointer(p1, p2);
404: b.addMouseListener(new MouseAdapter() {
405: @Override
406: public void mouseReleased(MouseEvent me) {
407: mouseEvent = me;
408: }
409: });
410: robot.setAutoDelay(0);
411: robot.mousePress(mask);
412: robot.mouseRelease(mask);
413: robot.delay(5000);
414: assertNotNull("event is not null", mouseEvent);
415: assertEquals("mouse released", MouseEvent.MOUSE_RELEASED,
416: mouseEvent.getID());
417: assertEquals("proper button is released", MouseEvent.BUTTON3,
418: mouseEvent.getButton());
419: }
420:
421: public final void testMouseWheel() {
422: waitForButton();
423: Point p1 = MouseInfo.getPointerInfo().getLocation();
424: Point p2 = b.getLocationOnScreen();
425: int scroll = -15;
426: movePointer(p1, p2);
427: f.toFront();
428: robot.delay(100);
429: b.addMouseWheelListener(new MouseWheelListener() {
430:
431: public void mouseWheelMoved(MouseWheelEvent mwe) {
432: mouseEvent = mwe;
433: }
434: });
435: robot.setAutoDelay(100);
436: robot.mouseWheel(scroll);
437: robot.delay(1000);
438: assertNotNull(mouseEvent);
439: assertTrue(mouseEvent instanceof MouseWheelEvent);
440: assertEquals(MouseEvent.MOUSE_WHEEL, mouseEvent.getID());
441: MouseWheelEvent mwe = (MouseWheelEvent) mouseEvent;
442: assertEquals(sgn(scroll), sgn(mwe.getWheelRotation()));
443: }
444:
445: public final void testSetAutoDelay() {
446: int delay = 2000;
447: robot.setAutoDelay(delay);
448: assertEquals(delay, robot.getAutoDelay());
449: try {
450: robot.setAutoDelay(-666);
451: } catch (IllegalArgumentException iae) {
452: exceptionCaught = true;
453: }
454: assertTrue(exceptionCaught);
455: assertEquals(delay, robot.getAutoDelay());
456: exceptionCaught = false;
457: try {
458: robot.setAutoDelay(66666);
459: } catch (IllegalArgumentException iae) {
460: exceptionCaught = true;
461: }
462: assertTrue(exceptionCaught);
463: assertEquals(delay, robot.getAutoDelay());
464:
465: }
466:
467: public final void testSetAutoWaitForIdle() {
468: robot.setAutoWaitForIdle(true);
469: assertTrue(robot.isAutoWaitForIdle());
470: robot.setAutoWaitForIdle(false);
471: assertFalse(robot.isAutoWaitForIdle());
472: }
473:
474: public final void testWaitForIdle() throws Throwable {
475:
476: // test exceptional case:
477: EventQueue.invokeAndWait(new Runnable() {
478:
479: public void run() {
480: boolean itseCaught = false;
481: try {
482: robot.waitForIdle();
483: } catch (IllegalThreadStateException itse) {
484: itseCaught = true;
485: }
486: try {
487: assertTrue(itseCaught);
488: } catch (Throwable thr) {
489: throwable = thr;
490: }
491: }
492:
493: });
494: if (throwable != null) {
495: throw throwable;
496: }
497: }
498:
499: }
500:
501: /**
502: *
503: * A modified default security manager which allows to set current
504: * security manager and denies Robot operations
505: * without changing the security policy
506: *
507: */
508: class MySecurityManager extends SecurityManager {
509:
510: @Override
511: public void checkPermission(Permission p) {
512: if (p.equals(new RuntimePermission("setSecurityManager"))
513: || p.equals(new RuntimePermission(
514: "createSecurityManager"))) {
515: return;
516: }
517: if (p.equals(new AWTPermission("createRobot"))
518: || p.equals(new AWTPermission("readDisplayPixels"))) {
519: throw new SecurityException(
520: "All Robot operations are denied");
521: }
522: super .checkPermission(p);
523: }
524: }
525:
526: class PrintDevice extends GraphicsDevice {
527:
528: @Override
529: public int getType() {
530: return GraphicsDevice.TYPE_PRINTER;
531: }
532:
533: @Override
534: public GraphicsConfiguration getDefaultConfiguration() {
535: return null;
536: }
537:
538: @Override
539: public GraphicsConfiguration[] getConfigurations() {
540: return null;
541: }
542:
543: @Override
544: public String getIDstring() {
545: return null;
546: }
547:
548: }
|