/* * Copyright (c) 2006, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
package test.java.awt.regtesthelpers; /** * <p>This class contains utilities useful for regression testing. * <p>When using jtreg you would include this class into the build * list via something like: * <pre> @library ../../../regtesthelpers @build Util @run main YourTest </pre> * Note that if you are about to create a test based on * Applet-template, then put those lines into html-file, not in java-file. * <p> And put an * import test.java.awt.regtesthelpers.Util; * into the java source of test.
*/
publicfinalclass Util { private Util() {} // this is a helper class with static methods :)
privatevolatilestatic Robot robot;
/* * @throws RuntimeException when creation failed
*/ publicstatic Robot createRobot() { try { if (robot == null) {
robot = new Robot();
} return robot;
} catch (AWTException e) { thrownew RuntimeException("Error: unable to create robot", e);
}
}
/** * Makes the window visible and waits until it's shown.
*/ publicstaticvoid showWindowWait(Window win) {
win.setVisible(true);
waitTillShown(win);
}
/** * Moves mouse pointer in the center of given {@code comp} component * using {@code robot} parameter.
*/ publicstaticvoid pointOnComp(final Component comp, final Robot robot) {
Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
}
/** * Moves mouse pointer in the center of a given {@code comp} component * and performs a left mouse button click using the {@code robot} parameter * with the {@code delay} delay between press and release.
*/ publicstaticvoid clickOnComp(final Component comp, final Robot robot, int delay) {
pointOnComp(comp, robot);
robot.delay(delay);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(delay);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
/** * Moves mouse pointer in the center of a given {@code comp} component * and performs a left mouse button click using the {@code robot} parameter * with the default delay between press and release.
*/ publicstaticvoid clickOnComp(final Component comp, final Robot robot) {
clickOnComp(comp, robot, 50);
}
publicstatic Point getTitlePoint(Window decoratedWindow) {
Point p = decoratedWindow.getLocationOnScreen();
Dimension d = decoratedWindow.getSize(); returnnew Point(p.x + (int)(d.getWidth()/2),
p.y + (int)(decoratedWindow.getInsets().top/2));
}
/* * Clicks on a title of Frame/Dialog. * WARNING: it may fail on some platforms when the window is not wide enough.
*/ publicstaticvoid clickOnTitle(final Window decoratedWindow, final Robot robot) { if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
Point p = getTitlePoint(decoratedWindow);
robot.mouseMove(p.x, p.y);
robot.delay(50);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
/** * Tests whether screen pixel has the expected color performing several * attempts. This method is useful for asynchronous window manager where * it's impossible to determine when drawing actually takes place. * * @param x X position of pixel * @param y Y position of pixel * @param color expected color * @param attempts number of attempts to undertake * @param delay delay before each attempt * @param robot a robot to use for retrieving pixel color * @return true if pixel color matches the color expected, otherwise false
*/ publicstaticboolean testPixelColor(int x, int y, final Color color, int attempts, int delay, final Robot robot) { while (attempts-- > 0) {
robot.delay(delay);
Color screen = robot.getPixelColor(x, y); if (screen.equals(color)) { returntrue;
}
} returnfalse;
}
/** * Tests whether the area within boundaries has the expected color * performing several attempts. This method is useful for asynchronous * window manager where it's impossible to determine when drawing actually * takes place. * * @param bounds position of area * @param color expected color * @param attempts number of attempts to undertake * @param delay delay before each attempt * @param robot a robot to use for retrieving pixel color * @return true if area color matches the color expected, otherwise false
*/ publicstaticboolean testBoundsColor(final Rectangle bounds, final Color color, int attempts, int delay, final Robot robot) { int right = bounds.x + bounds.width - 1; int bottom = bounds.y + bounds.height - 1; while (attempts-- > 0) { if (testPixelColor(bounds.x, bounds.y, color, 1, delay, robot)
&& testPixelColor(right, bounds.y, color, 1, 0, robot)
&& testPixelColor(right, bottom, color, 1, 0, robot)
&& testPixelColor(bounds.x, bottom, color, 1, 0, robot)) { returntrue;
}
} returnfalse;
}
/* * Waits for a notification and for a boolean condition to become true. * The method returns when the above conditions are fullfilled or when the timeout * occurs. * * @param condition the object to be notified and the booelan condition to wait for * @param timeout the maximum time to wait in milliseconds * @param catchExceptions if {@code true} the method catches InterruptedException * @return the final boolean value of the {@code condition} * @throws InterruptedException if the awaiting proccess has been interrupted
*/ publicstaticboolean waitForConditionEx(final AtomicBoolean condition, long timeout) throws InterruptedException
{ synchronized (condition) { long startTime = System.currentTimeMillis(); while (!condition.get()) {
condition.wait(timeout); if (System.currentTimeMillis() - startTime >= timeout ) { break;
}
}
} return condition.get();
}
/* * The same as {@code waitForConditionEx(AtomicBoolean, long)} except that it * doesn't throw InterruptedException.
*/ publicstaticboolean waitForCondition(final AtomicBoolean condition, long timeout) { try { return waitForConditionEx(condition, timeout);
} catch (InterruptedException e) { thrownew RuntimeException("Error: unexpected exception caught!", e);
}
}
/* * The same as {@code waitForConditionEx(AtomicBoolean, long)} but without a timeout.
*/ publicstaticvoid waitForConditionEx(final AtomicBoolean condition) throws InterruptedException
{ synchronized (condition) { while (!condition.get()) {
condition.wait();
}
}
}
/* * The same as {@code waitForConditionEx(AtomicBoolean)} except that it * doesn't throw InterruptedException.
*/ publicstaticvoid waitForCondition(final AtomicBoolean condition) { try {
waitForConditionEx(condition);
} catch (InterruptedException e) { thrownew RuntimeException("Error: unexpected exception caught!", e);
}
}
/** * Drags from one point to another with the specified mouse button pressed. * * @param robot a robot to use for moving the mouse, etc. * @param startPoint a start point of the drag * @param endPoint an end point of the drag * @param button one of {@code InputEvent.BUTTON1_MASK}, * {@code InputEvent.BUTTON2_MASK}, {@code InputEvent.BUTTON3_MASK} * * @throws IllegalArgumentException if {@code button} is not one of * {@code InputEvent.BUTTON1_MASK}, {@code InputEvent.BUTTON2_MASK}, * {@code InputEvent.BUTTON3_MASK}
*/ publicstaticvoid drag(Robot robot, Point startPoint, Point endPoint, int button) { if (!(button == InputEvent.BUTTON1_MASK
|| button == InputEvent.BUTTON2_MASK
|| button == InputEvent.BUTTON3_MASK
|| button == InputEvent.BUTTON1_DOWN_MASK
|| button == InputEvent.BUTTON2_DOWN_MASK
|| button == InputEvent.BUTTON3_DOWN_MASK
))
{ thrownew IllegalArgumentException("invalid mouse button");
}
/** * Moves the mouse pointer from one point to another. * Uses Bresenham's algorithm. * * @param robot a robot to use for moving the mouse * @param startPoint a start point of the drag * @param endPoint an end point of the drag
*/ publicstaticvoid mouseMove(Robot robot, Point startPoint, Point endPoint) { int dx = endPoint.x - startPoint.x; int dy = endPoint.y - startPoint.y;
int ax = Math.abs(dx) * 2; int ay = Math.abs(dy) * 2;
int sx = signWOZero(dx); int sy = signWOZero(dy);
int x = startPoint.x; int y = startPoint.y;
int d = 0;
if (ax > ay) {
d = ay - ax/2; while (true){
robot.mouseMove(x, y);
robot.delay(50);
if (x == endPoint.x){ return;
} if (d >= 0){
y = y + sy;
d = d - ax;
}
x = x + sx;
d = d + ay;
}
} else {
d = ax - ay/2; while (true){
robot.mouseMove(x, y);
robot.delay(50);
if (y == endPoint.y){ return;
} if (d >= 0){
x = x + sx;
d = d - ay;
}
y = y + sy;
d = d + ax;
}
}
}
privatestaticint signWOZero(int i){ return (i > 0)? 1: -1;
}
privatestaticint sign(int n) { return n < 0 ? -1 : n == 0 ? 0 : 1;
}
/** Returns {@code WindowListener} instance that diposes {@code Window} on * "window closing" event. * * @return the {@code WindowListener} instance that could be set * on a {@code Window}. After that * the {@code Window} is disposed when "window closed" * event is sent to the {@code Window}
*/ publicstatic WindowListener getClosingWindowAdapter() { returnnew WindowAdapter () { publicvoid windowClosing(WindowEvent e) {
e.getWindow().dispose();
}
};
}
/* * Returns -1 in case of not X Window or any problems.
*/ publicstaticint getWMID() { Class clazz = null; try { if ("sun.awt.X11.XToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
clazz = Class.forName("sun.awt.X11.XWM");
}
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} if (clazz == null) { return -1;
}
try { finalClass _clazz = clazz;
Method m_addExports = Class.forName("java.awt.Helper").getDeclaredMethod("addExports", String.class, java.lang.Module.class); // We may be called from non-X11 system, and this permission cannot be delegated to a test.
m_addExports.invoke(null, "sun.awt.X11", Util.class.getModule());
@SuppressWarnings("removal")
Method m_getWMID = (Method)AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try {
Method method = _clazz.getDeclaredMethod("getWMID", newClass[] {}); if (method != null) {
method.setAccessible(true);
} return method;
} catch (NoSuchMethodException e) { assertfalse;
} catch (SecurityException e) { assertfalse;
} returnnull;
}
}); return ((Integer)m_getWMID.invoke(null, new Object[] {})).intValue();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (NoSuchMethodException nsme) {
nsme.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
} return -1;
}
//Cleans all the references publicstaticvoid cleanUp() {
apListener = null;
fgListener = null;
wgfListener = null;
}
//////////////////////////// // Some stuff to test focus. ////////////////////////////
privatestatic WindowGainedFocusListener wgfListener = new WindowGainedFocusListener(); privatestatic FocusGainedListener fgListener = new FocusGainedListener(); privatestatic ActionPerformedListener apListener = new ActionPerformedListener();
/* * Tracks WINDOW_GAINED_FOCUS event for a window caused by an action. * @param window the window to track the event for * @param action the action to perform * @param time the max time to wait for the event * @param printEvent should the event received be printed or doesn't * @return true if the event has been received, otherwise false
*/ publicstaticboolean trackWindowGainedFocus(Window window, Runnable action, int time, boolean printEvent) { return trackEvent(WindowEvent.WINDOW_GAINED_FOCUS, window, action, time, printEvent);
}
/* * Tracks FOCUS_GAINED event for a component caused by an action. * @see #trackWindowGainedFocus
*/ publicstaticboolean trackFocusGained(Component comp, Runnable action, int time, booleanprintEvent) { return trackEvent(FocusEvent.FOCUS_GAINED, comp, action, time, printEvent);
}
/* * Tracks ACTION_PERFORMED event for a button caused by an action. * @see #trackWindowGainedFocus
*/ publicstaticboolean trackActionPerformed(Button button, Runnable action, int time, boolean printEvent) { return trackEvent(ActionEvent.ACTION_PERFORMED, button, action, time, printEvent);
}
/* * Requests focus on the component provided and waits for the result. * @return true if the component has been focused, false otherwise.
*/ publicstaticboolean focusComponent(Component comp, int time) { return focusComponent(comp, time, false);
} publicstaticboolean focusComponent(final Component comp, int time, boolean printEvent) { return trackFocusGained(comp, new Runnable() { publicvoid run() {
comp.requestFocus();
}
},
time, printEvent);
}
/** * Invokes the <code>task</code> on the EDT thread. * * @return result of the <code>task</code>
*/ publicstatic <T> T invokeOnEDT(final java.util.concurrent.Callable<T> task) throws Exception { final java.util.List<T> result = new java.util.ArrayList<T>(1); final Exception[] exception = new Exception[1];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.