1 /* |
1 /* |
2 * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
49 import java.awt.Robot; |
49 import java.awt.Robot; |
50 import java.awt.Toolkit; |
50 import java.awt.Toolkit; |
51 import java.awt.IllegalComponentStateException; |
51 import java.awt.IllegalComponentStateException; |
52 import java.awt.AWTException; |
52 import java.awt.AWTException; |
53 import java.awt.AWTEvent; |
53 import java.awt.AWTEvent; |
|
54 import java.awt.Color; |
54 |
55 |
55 import java.awt.event.InputEvent; |
56 import java.awt.event.InputEvent; |
56 import java.awt.event.WindowAdapter; |
57 import java.awt.event.WindowAdapter; |
57 import java.awt.event.WindowEvent; |
58 import java.awt.event.WindowEvent; |
58 import java.awt.event.ActionEvent; |
59 import java.awt.event.ActionEvent; |
180 robot.delay(50); |
181 robot.delay(50); |
181 robot.mousePress(InputEvent.BUTTON1_MASK); |
182 robot.mousePress(InputEvent.BUTTON1_MASK); |
182 robot.delay(50); |
183 robot.delay(50); |
183 robot.mouseRelease(InputEvent.BUTTON1_MASK); |
184 robot.mouseRelease(InputEvent.BUTTON1_MASK); |
184 } |
185 } |
|
186 } |
|
187 |
|
188 /** |
|
189 * Tests whether screen pixel has the expected color performing several |
|
190 * attempts. This method is useful for asynchronous window manager where |
|
191 * it's impossible to determine when drawing actually takes place. |
|
192 * |
|
193 * @param x X position of pixel |
|
194 * @param y Y position of pixel |
|
195 * @param color expected color |
|
196 * @param attempts number of attempts to undertake |
|
197 * @param delay delay before each attempt |
|
198 * @param robot a robot to use for retrieving pixel color |
|
199 * @return true if pixel color matches the color expected, otherwise false |
|
200 */ |
|
201 public static boolean testPixelColor(int x, int y, final Color color, int attempts, int delay, final Robot robot) { |
|
202 while (attempts-- > 0) { |
|
203 robot.delay(delay); |
|
204 Color screen = robot.getPixelColor(x, y); |
|
205 if (screen.equals(color)) { |
|
206 return true; |
|
207 } |
|
208 } |
|
209 return false; |
|
210 } |
|
211 |
|
212 /** |
|
213 * Tests whether the area within boundaries has the expected color |
|
214 * performing several attempts. This method is useful for asynchronous |
|
215 * window manager where it's impossible to determine when drawing actually |
|
216 * takes place. |
|
217 * |
|
218 * @param bounds position of area |
|
219 * @param color expected color |
|
220 * @param attempts number of attempts to undertake |
|
221 * @param delay delay before each attempt |
|
222 * @param robot a robot to use for retrieving pixel color |
|
223 * @return true if area color matches the color expected, otherwise false |
|
224 */ |
|
225 public static boolean testBoundsColor(final Rectangle bounds, final Color color, int attempts, int delay, final Robot robot) { |
|
226 int right = bounds.x + bounds.width - 1; |
|
227 int bottom = bounds.y + bounds.height - 1; |
|
228 while (attempts-- > 0) { |
|
229 if (testPixelColor(bounds.x, bounds.y, color, 1, delay, robot) |
|
230 && testPixelColor(right, bounds.y, color, 1, 0, robot) |
|
231 && testPixelColor(right, bottom, color, 1, 0, robot) |
|
232 && testPixelColor(bounds.x, bottom, color, 1, 0, robot)) { |
|
233 return true; |
|
234 } |
|
235 } |
|
236 return false; |
185 } |
237 } |
186 |
238 |
187 public static void waitForIdle(final Robot robot) { |
239 public static void waitForIdle(final Robot robot) { |
188 // we do not use robot for now, use SunToolkit.realSync() instead |
240 // we do not use robot for now, use SunToolkit.realSync() instead |
189 ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync(); |
241 ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync(); |