-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8150564: Migrate useful ExtendedRobot methods into awt.Robot #22044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
514a813
b21c56b
b60c093
633318c
b39bd2e
1c54001
955f551
4cb44bc
c0733de
a11b8ac
adb0f58
d330af3
830a5b5
a8482f5
84384f9
32b7079
420c422
449b1d5
bbfef4a
97e2424
c3e5671
30ca6a6
c0cd6e7
cbcf3fb
7eab183
a46b067
0abc123
9aa8201
8347d2f
9f19885
3a9372c
c02d418
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,17 @@ public class Robot { | |
|
||
private DirectColorModel screenCapCM = null; | ||
|
||
/** | ||
* Default delay for mouse {@code click} and | ||
* step delay for mouse {@code glide} in milliseconds. | ||
*/ | ||
public static final int DEFAULT_DELAY = 20; | ||
|
||
/** | ||
* Default pixel step length for mouse {@code glide}. | ||
*/ | ||
public static final int DEFAULT_STEP_LENGTH = 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to make the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "glide has over-rides step length" Sorry, that text was not clear. What I mean is that glide(..) accepts delay and step parameters, and so at least in that case you can over-ride with your preferred values. Other than that, it isn't configurable in the ExtendedRobot, so the need isn't clear. Even so, we could do this but whether now or later, but in either case, In ExtendedRobot the equivalent fields are private but here they are public. Then all the methods that now mention DEFAULT_ would probably need to mention something like So it may be better to do it now ? Thoughts ? |
||
|
||
/** | ||
* Constructs a Robot object in the coordinate system of the primary screen. | ||
* | ||
|
@@ -773,4 +784,203 @@ public synchronized String toString() { | |
String params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle(); | ||
return getClass().getName() + "[ " + params + " ]"; | ||
} | ||
|
||
/** | ||
* A convenience method that simulates clicking a mouse button by calling {@code mousePress}, {@code mouseRelease}, | ||
* and {@code waitForIdle}. Invokes {@code waitForIdle} with a default {@link #DEFAULT_DELAY delay} after | ||
* {@code mousePress} and {@code mouseRelease} calls. For specifics on valid inputs see | ||
* {@link java.awt.Robot#mousePress(int)}. | ||
* | ||
* @param buttons The button mask; a combination of one or more mouse button masks. | ||
* @throws IllegalArgumentException if the {@code buttons} mask contains the mask for | ||
* extra mouse button and support for extended mouse buttons is | ||
* {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java | ||
* @throws IllegalArgumentException if the {@code buttons} mask contains the mask for | ||
* extra mouse button that does not exist on the mouse and support for extended | ||
* mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} | ||
* by Java | ||
* @throws IllegalThreadStateException if called on the AWT event dispatching thread | ||
* @see #mousePress(int) | ||
* @see #mouseRelease(int) | ||
* @see #DEFAULT_DELAY | ||
* @see InputEvent#getMaskForButton(int) | ||
* @see Toolkit#areExtraMouseButtonsEnabled() | ||
* @see java.awt.event.MouseEvent | ||
* @since 25 | ||
*/ | ||
public void click(int buttons) { | ||
mousePress(buttons); | ||
waitForIdle(DEFAULT_DELAY); | ||
mouseRelease(buttons); | ||
waitForIdle(DEFAULT_DELAY); | ||
} | ||
|
||
/** | ||
* A convenience method that clicks mouse button 1. | ||
* | ||
* @throws IllegalThreadStateException if called on the AWT event dispatching thread | ||
* @see #click(int) | ||
* @since 25 | ||
*/ | ||
public void click() { | ||
click(InputEvent.BUTTON1_DOWN_MASK); | ||
} | ||
|
||
/** | ||
* A convenience method that calls {@code waitForIdle} then waits an additional specified | ||
* {@code delayValue} time in milliseconds. | ||
* | ||
* @param delayValue Additional delay length in milliseconds to wait until thread | ||
* sync been completed | ||
* @throws IllegalThreadStateException if called on the AWT event | ||
* dispatching thread | ||
* @throws IllegalArgumentException if {@code delayValue} is not between {@code 0} | ||
* and {@code 60,000} milliseconds inclusive | ||
* @since 25 | ||
*/ | ||
public synchronized void waitForIdle(int delayValue) { | ||
waitForIdle(); | ||
delay(delayValue); | ||
} | ||
|
||
/** | ||
* A convenience method that moves the mouse in multiple | ||
* steps from its current location to the destination coordinates | ||
* with a default {@link #DEFAULT_STEP_LENGTH step-length} and {@link #DEFAULT_DELAY delay}. | ||
* | ||
* @param x Destination point x coordinate | ||
* @param y Destination point y coordinate | ||
* | ||
* @throws IllegalThreadStateException if called on the AWT event dispatching | ||
* thread and {@code isAutoWaitForIdle} would return true | ||
* @see #DEFAULT_STEP_LENGTH | ||
* @see #DEFAULT_DELAY | ||
* @see #glide(int, int, int, int, int, int) | ||
* @since 25 | ||
*/ | ||
public void glide(int x, int y) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've taken a look at all the tests running glide and very few of them actually used glide(Point dest) and glide(Point src, Point dest) so I decided to remove them from them from the migration |
||
Point p = MouseInfo.getPointerInfo().getLocation(); | ||
glide(p.x, p.y, x, y); | ||
} | ||
|
||
/** | ||
* A convenience method that moves the mouse in multiple steps | ||
* from source coordinates to the destination coordinates with | ||
* a default {@link #DEFAULT_STEP_LENGTH step-length} and {@link #DEFAULT_DELAY delay}. | ||
* | ||
* @param fromX Source point x coordinate | ||
* @param fromY Source point y coordinate | ||
* @param toX Destination point x coordinate | ||
* @param toY Destination point y coordinate | ||
* | ||
* @throws IllegalThreadStateException if called on the AWT event dispatching | ||
* thread and {@code isAutoWaitForIdle} would return true | ||
* @see #DEFAULT_STEP_LENGTH | ||
* @see #DEFAULT_DELAY | ||
* @see #glide(int, int, int, int, int, int) | ||
* @since 25 | ||
*/ | ||
public void glide(int fromX, int fromY, int toX, int toY) { | ||
glide(fromX, fromY, toX, toY, DEFAULT_STEP_LENGTH, DEFAULT_DELAY); | ||
} | ||
|
||
/** | ||
* A convenience method that moves the mouse in multiple | ||
* steps from source point to the destination point with | ||
* given {@code stepLength} and {@code stepDelay}. | ||
* | ||
* @param srcX Source point x coordinate | ||
* @param srcY Source point y coordinate | ||
* @param destX Destination point x coordinate | ||
* @param destY Destination point y coordinate | ||
* @param stepLength Preferred length of one step in pixels | ||
* @param stepDelay Delay between steps in milliseconds | ||
* | ||
* @throws IllegalArgumentException if {@code stepLength} is a negative value or | ||
* greater than the distance between source and destination points | ||
* @throws IllegalArgumentException if {@code stepDelay} is not between {@code 0} | ||
* and {@code 60,000} milliseconds inclusive | ||
* @throws IllegalThreadStateException if called on the AWT event dispatching | ||
* thread and {@code isAutoWaitForIdle} would return true | ||
* @see #mouseMove(int, int) | ||
* @see #delay(int) | ||
* @since 25 | ||
*/ | ||
public void glide(int srcX, int srcY, int destX, int destY, int stepLength, int stepDelay) { | ||
int stepNum; | ||
double tDx, tDy; | ||
double dx, dy, ds; | ||
double x, y; | ||
|
||
dx = (destX - srcX); | ||
dy = (destY - srcY); | ||
ds = Math.sqrt(dx*dx + dy*dy); | ||
|
||
tDx = dx / ds * stepLength; | ||
tDy = dy / ds * stepLength; | ||
|
||
int stepsCount = (int) ds / stepLength; | ||
|
||
// Walk the mouse to the destination one step at a time | ||
mouseMove(srcX, srcY); | ||
|
||
for (x = srcX, y = srcY, stepNum = 0; | ||
stepNum < stepsCount; | ||
stepNum++) { | ||
x += tDx; | ||
y += tDy; | ||
mouseMove((int)x, (int)y); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes should be added to all glide API methods |
||
delay(stepDelay); | ||
} | ||
|
||
// Ensure the mouse moves to the right destination. | ||
// The steps may have led the mouse to a slightly wrong place. | ||
if (x != destX || y != destY) mouseMove(destX, destY); | ||
} | ||
|
||
/** | ||
* A convenience method that simulates typing a key by calling {@code keyPress} | ||
* and {@code keyRelease}. Invokes {@code waitForIdle} with a default {@link #DEFAULT_DELAY delay} | ||
* after {@code keyPress} and {@code keyRelease} calls. | ||
* <p> | ||
* Key codes that have more than one physical key associated with them | ||
* (e.g. {@code KeyEvent.VK_SHIFT} could mean either the | ||
* left or right shift key) will map to the left key. | ||
* | ||
* @param keycode Key to type (e.g. {@code KeyEvent.VK_A}) | ||
* @throws IllegalArgumentException if {@code keycode} is not | ||
* a valid key | ||
* @throws IllegalThreadStateException if called on the AWT event dispatching thread | ||
* @see #keyPress(int) | ||
* @see #keyRelease(int) | ||
* @see java.awt.event.KeyEvent | ||
* @see #DEFAULT_DELAY | ||
* @since 25 | ||
*/ | ||
public synchronized void type(int keycode) { | ||
keyPress(keycode); | ||
waitForIdle(DEFAULT_DELAY); | ||
keyRelease(keycode); | ||
waitForIdle(DEFAULT_DELAY); | ||
} | ||
|
||
/** | ||
* A convenience method that simulates typing a char by calling {@code keyPress} | ||
* and {@code keyRelease}. Gets the ExtendedKeyCode for the char and calls | ||
* type(int keycode). | ||
* | ||
* @param c Character to be typed (e.g. {@code 'a'}) | ||
* @throws IllegalArgumentException if {@code keycode} is not | ||
* a valid key | ||
* @throws IllegalThreadStateException if called on the AWT event dispatching thread | ||
* @see #type(int) | ||
* @see #keyPress(int) | ||
* @see #keyRelease(int) | ||
* @see java.awt.event.KeyEvent | ||
* @see #DEFAULT_DELAY | ||
* @since 25 | ||
*/ | ||
public synchronized void type(char c) { | ||
type(KeyEvent.getExtendedKeyCodeForChar(c)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
import java.awt.Label; | ||
import java.awt.List; | ||
import java.awt.Panel; | ||
import java.awt.Robot; | ||
import java.awt.ScrollPane; | ||
import java.awt.Scrollbar; | ||
import java.awt.TextArea; | ||
|
@@ -47,8 +48,6 @@ | |
@bug 6596915 | ||
@summary Test Component.paintAll() method | ||
@author [email protected]: area=awt.component | ||
@library /lib/client/ | ||
@build ExtendedRobot | ||
@run main PaintAll | ||
*/ | ||
public class PaintAll { | ||
|
@@ -67,7 +66,7 @@ public class PaintAll { | |
private static volatile boolean scrollPanePainted; | ||
private static volatile boolean textAreaPainted; | ||
private static volatile boolean textFieldPainted; | ||
private static ExtendedRobot robot = null; | ||
private static Robot robot = null; | ||
|
||
private static final Button buttonStub = new Button() { | ||
@Override | ||
|
@@ -287,7 +286,7 @@ private static void validation() { | |
private static void sleep() { | ||
if(robot == null) { | ||
try { | ||
robot = new ExtendedRobot(); | ||
robot = new Robot(); | ||
}catch(Exception ex) { | ||
ex.printStackTrace(); | ||
throw new RuntimeException("Unexpected failure"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,13 +29,11 @@ | |
* @summary An attempt to set non-trivial background, shape, or translucency | ||
* to a decorated toplevel should end with an exception. | ||
* @author Dmitriy Ermashov ([email protected]) | ||
* @library /lib/client | ||
* @build ExtendedRobot | ||
* @run main DecoratedExceptions | ||
*/ | ||
public class DecoratedExceptions { | ||
public static void main(String args[]) throws Exception{ | ||
ExtendedRobot robot = new ExtendedRobot(); | ||
Robot robot = new Robot(); | ||
Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(() -> { | ||
Frame frame = new Frame("Frame"); | ||
frame.setBounds(50,50,400,200); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ | |
* @author Jitender([email protected]) area=AWT | ||
* @author yan | ||
* @library /lib/client | ||
* @build ExtendedRobot | ||
* @run main ActiveSwingWindowTest | ||
*/ | ||
|
||
|
@@ -45,7 +44,7 @@ public class ActiveSwingWindowTest { | |
private JButton button, button2; | ||
private JTextField textField, textField2; | ||
private int eventType, eventType1; | ||
private ExtendedRobot robot; | ||
private Robot robot; | ||
private Object lock1 = new Object(); | ||
private Object lock2 = new Object(); | ||
private Object lock3 = new Object(); | ||
|
@@ -151,7 +150,7 @@ public void actionPerformed(ActionEvent e) { | |
|
||
public void doTest() { | ||
try { | ||
robot = new ExtendedRobot(); | ||
robot = new Robot(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new RuntimeException("Cannot create robot"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ | |
* @author Jitender([email protected]) area=AWT* | ||
* @author yan | ||
* @library /lib/client | ||
* @build ExtendedRobot | ||
* @run main FrameCloseTest | ||
*/ | ||
|
||
|
@@ -43,7 +42,7 @@ public class FrameCloseTest { | |
private Frame frame, frame2; | ||
private Component button, dummyButton; | ||
private int eventType, eventType1, eventType2; | ||
private ExtendedRobot robot; | ||
private Robot robot; | ||
private Object lock1 = new Object(); | ||
private Object lock2 = new Object(); | ||
private Object lock3 = new Object(); | ||
|
@@ -156,7 +155,7 @@ public void run() { | |
throw new RuntimeException("Interrupted or unexpected Exception occured"); | ||
} | ||
try { | ||
robot = new ExtendedRobot(); | ||
robot = new Robot(); | ||
robot.waitForIdle(1000); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.