0N/A/*
2611N/A * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/Apackage test.java.awt.regtesthelpers;
0N/A/**
0N/A * <p>This class contains utilities useful for regression testing.
0N/A * <p>When using jtreg you would include this class into the build
0N/A * list via something like:
0N/A * <pre>
0N/A @library ../../../regtesthelpers
0N/A @build Util
0N/A @run main YourTest
0N/A </pre>
0N/A * Note that if you are about to create a test based on
0N/A * Applet-template, then put those lines into html-file, not in java-file.
0N/A * <p> And put an
0N/A * import test.java.awt.regtesthelpers.Util;
0N/A * into the java source of test.
0N/A*/
0N/A
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Frame;
0N/Aimport java.awt.Dialog;
0N/Aimport java.awt.Window;
0N/Aimport java.awt.Button;
0N/Aimport java.awt.Point;
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Robot;
0N/Aimport java.awt.Toolkit;
0N/Aimport java.awt.IllegalComponentStateException;
0N/Aimport java.awt.AWTException;
0N/Aimport java.awt.AWTEvent;
0N/A
0N/Aimport java.awt.event.InputEvent;
0N/Aimport java.awt.event.WindowAdapter;
0N/Aimport java.awt.event.WindowEvent;
0N/Aimport java.awt.event.ActionEvent;
0N/Aimport java.awt.event.FocusEvent;
0N/Aimport java.awt.event.WindowListener;
0N/Aimport java.awt.event.WindowFocusListener;
0N/Aimport java.awt.event.FocusListener;
0N/Aimport java.awt.event.ActionListener;
0N/A
0N/Aimport java.awt.peer.FramePeer;
0N/A
0N/Aimport java.lang.reflect.Constructor;
0N/Aimport java.lang.reflect.Field;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport java.lang.reflect.Method;
0N/A
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.AccessController;
0N/A
0N/Aimport java.util.concurrent.atomic.AtomicBoolean;
0N/A
0N/Apublic final class Util {
0N/A private Util() {} // this is a helper class with static methods :)
0N/A
0N/A /*
0N/A * @throws RuntimeException when creation failed
0N/A */
0N/A public static Robot createRobot() {
0N/A try {
0N/A return new Robot();
0N/A } catch (AWTException e) {
0N/A throw new RuntimeException("Error: unable to create robot", e);
0N/A }
0N/A }
0N/A
0N/A public static Frame createEmbeddedFrame(final Frame embedder)
0N/A throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException,
0N/A InstantiationException, InvocationTargetException
0N/A {
0N/A Toolkit tk = Toolkit.getDefaultToolkit();
0N/A FramePeer frame_peer = (FramePeer) embedder.getPeer();
0N/A System.out.println("frame's peer = " + frame_peer);
0N/A if ("sun.awt.windows.WToolkit".equals(tk.getClass().getName())) {
0N/A Class comp_peer_class =
0N/A Class.forName("sun.awt.windows.WComponentPeer");
0N/A System.out.println("comp peer class = " + comp_peer_class);
0N/A Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");
0N/A hwnd_field.setAccessible(true);
0N/A System.out.println("hwnd_field =" + hwnd_field);
0N/A long hwnd = hwnd_field.getLong(frame_peer);
0N/A System.out.println("hwnd = " + hwnd);
0N/A
0N/A Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");
0N/A Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE});
0N/A return (Frame) constructor.newInstance (new Object[] {hwnd});
0N/A } else if ("sun.awt.X11.XToolkit".equals(tk.getClass().getName())) {
0N/A Class x_base_window_class = Class.forName("sun.awt.X11.XBaseWindow");
0N/A System.out.println("x_base_window_class = " + x_base_window_class);
0N/A Method get_window = x_base_window_class.getMethod("getWindow", new Class[0]);
0N/A System.out.println("get_window = " + get_window);
0N/A long window = (Long) get_window.invoke(frame_peer, new Object[0]);
0N/A System.out.println("window = " + window);
0N/A Class clazz = Class.forName("sun.awt.X11.XEmbeddedFrame");
0N/A Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE, Boolean.TYPE});
0N/A return (Frame) constructor.newInstance (new Object[] {window, true});
0N/A }
0N/A
0N/A throw new RuntimeException("Unexpected toolkit - " + tk);
0N/A }
0N/A
0N/A /**
0N/A * Makes the window visible and waits until it's shown.
0N/A */
0N/A public static void showWindowWait(Window win) {
0N/A win.setVisible(true);
0N/A waitTillShown(win);
0N/A }
0N/A
0N/A /**
0N/A * Moves mouse pointer in the center of given {@code comp} component
0N/A * using {@code robot} parameter.
0N/A */
0N/A public static void pointOnComp(final Component comp, final Robot robot) {
0N/A Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
0N/A robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
0N/A }
0N/A
0N/A /**
0N/A * Moves mouse pointer in the center of a given {@code comp} component
0N/A * and performs a left mouse button click using the {@code robot} parameter
0N/A * with the {@code delay} delay between press and release.
0N/A */
0N/A public static void clickOnComp(final Component comp, final Robot robot, int delay) {
0N/A pointOnComp(comp, robot);
0N/A robot.delay(delay);
0N/A robot.mousePress(InputEvent.BUTTON1_MASK);
0N/A robot.delay(delay);
0N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
0N/A }
0N/A
0N/A /**
0N/A * Moves mouse pointer in the center of a given {@code comp} component
0N/A * and performs a left mouse button click using the {@code robot} parameter
0N/A * with the default delay between press and release.
0N/A */
0N/A public static void clickOnComp(final Component comp, final Robot robot) {
0N/A clickOnComp(comp, robot, 50);
0N/A }
0N/A
0N/A public static Point getTitlePoint(Window decoratedWindow) {
0N/A Point p = decoratedWindow.getLocationOnScreen();
0N/A Dimension d = decoratedWindow.getSize();
0N/A return new Point(p.x + (int)(d.getWidth()/2),
0N/A p.y + (int)(decoratedWindow.getInsets().top/2));
0N/A }
0N/A
0N/A /*
0N/A * Clicks on a title of Frame/Dialog.
0N/A * WARNING: it may fail on some platforms when the window is not wide enough.
0N/A */
0N/A public static void clickOnTitle(final Window decoratedWindow, final Robot robot) {
0N/A if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
0N/A Point p = getTitlePoint(decoratedWindow);
0N/A robot.mouseMove(p.x, p.y);
0N/A robot.delay(50);
0N/A robot.mousePress(InputEvent.BUTTON1_MASK);
0N/A robot.delay(50);
0N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
0N/A }
0N/A }
0N/A
0N/A public static void waitForIdle(final Robot robot) {
0N/A // we do not use robot for now, use SunToolkit.realSync() instead
0N/A ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync();
0N/A }
0N/A
0N/A public static Field getField(final Class klass, final String fieldName) {
0N/A return AccessController.doPrivileged(new PrivilegedAction<Field>() {
0N/A public Field run() {
0N/A try {
0N/A Field field = klass.getDeclaredField(fieldName);
0N/A assert (field != null);
0N/A field.setAccessible(true);
0N/A return field;
0N/A } catch (SecurityException se) {
0N/A throw new RuntimeException("Error: unexpected exception caught!", se);
0N/A } catch (NoSuchFieldException nsfe) {
0N/A throw new RuntimeException("Error: unexpected exception caught!", nsfe);
0N/A }
0N/A }
0N/A });
0N/A }
0N/A
0N/A /*
0N/A * Waits for a notification and for a boolean condition to become true.
0N/A * The method returns when the above conditions are fullfilled or when the timeout
0N/A * occurs.
0N/A *
0N/A * @param condition the object to be notified and the booelan condition to wait for
0N/A * @param timeout the maximum time to wait in milliseconds
0N/A * @param catchExceptions if {@code true} the method catches InterruptedException
0N/A * @return the final boolean value of the {@code condition}
0N/A * @throws InterruptedException if the awaiting proccess has been interrupted
0N/A */
0N/A public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
0N/A throws InterruptedException
0N/A {
0N/A synchronized (condition) {
0N/A long startTime = System.currentTimeMillis();
0N/A while (!condition.get()) {
0N/A condition.wait(timeout);
0N/A if (System.currentTimeMillis() - startTime >= timeout ) {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A return condition.get();
0N/A }
0N/A
0N/A /*
0N/A * The same as {@code waitForConditionEx(AtomicBoolean, long)} except that it
0N/A * doesn't throw InterruptedException.
0N/A */
0N/A public static boolean waitForCondition(final AtomicBoolean condition, long timeout) {
0N/A try {
0N/A return waitForConditionEx(condition, timeout);
0N/A } catch (InterruptedException e) {
0N/A throw new RuntimeException("Error: unexpected exception caught!", e);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * The same as {@code waitForConditionEx(AtomicBoolean, long)} but without a timeout.
0N/A */
0N/A public static void waitForConditionEx(final AtomicBoolean condition)
0N/A throws InterruptedException
0N/A {
0N/A synchronized (condition) {
0N/A while (!condition.get()) {
0N/A condition.wait();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * The same as {@code waitForConditionEx(AtomicBoolean)} except that it
0N/A * doesn't throw InterruptedException.
0N/A */
0N/A public static void waitForCondition(final AtomicBoolean condition) {
0N/A try {
0N/A waitForConditionEx(condition);
0N/A } catch (InterruptedException e) {
0N/A throw new RuntimeException("Error: unexpected exception caught!", e);
0N/A }
0N/A }
0N/A
0N/A public static void waitTillShownEx(final Component comp) throws InterruptedException {
0N/A while (true) {
0N/A try {
0N/A Thread.sleep(100);
0N/A comp.getLocationOnScreen();
0N/A break;
0N/A } catch (IllegalComponentStateException e) {}
0N/A }
0N/A }
0N/A public static void waitTillShown(final Component comp) {
0N/A try {
0N/A waitTillShownEx(comp);
0N/A } catch (InterruptedException e) {
0N/A throw new RuntimeException("Error: unexpected exception caught!", e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Drags from one point to another with the specified mouse button pressed.
0N/A *
0N/A * @param robot a robot to use for moving the mouse, etc.
0N/A * @param startPoint a start point of the drag
0N/A * @param endPoint an end point of the drag
0N/A * @param button one of {@code InputEvent.BUTTON1_MASK},
0N/A * {@code InputEvent.BUTTON2_MASK}, {@code InputEvent.BUTTON3_MASK}
0N/A *
0N/A * @throws IllegalArgumentException if {@code button} is not one of
0N/A * {@code InputEvent.BUTTON1_MASK}, {@code InputEvent.BUTTON2_MASK},
0N/A * {@code InputEvent.BUTTON3_MASK}
0N/A */
0N/A public static void drag(Robot robot, Point startPoint, Point endPoint, int button) {
0N/A if (!(button == InputEvent.BUTTON1_MASK || button == InputEvent.BUTTON2_MASK
0N/A || button == InputEvent.BUTTON3_MASK))
0N/A {
0N/A throw new IllegalArgumentException("invalid mouse button");
0N/A }
0N/A
0N/A robot.mouseMove(startPoint.x, startPoint.y);
0N/A robot.mousePress(button);
0N/A try {
0N/A mouseMove(robot, startPoint, endPoint);
0N/A } finally {
0N/A robot.mouseRelease(button);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Moves the mouse pointer from one point to another.
0N/A * Uses Bresenham's algorithm.
0N/A *
0N/A * @param robot a robot to use for moving the mouse
0N/A * @param startPoint a start point of the drag
0N/A * @param endPoint an end point of the drag
0N/A */
0N/A public static void mouseMove(Robot robot, Point startPoint, Point endPoint) {
0N/A int dx = endPoint.x - startPoint.x;
0N/A int dy = endPoint.y - startPoint.y;
0N/A
0N/A int ax = Math.abs(dx) * 2;
0N/A int ay = Math.abs(dy) * 2;
0N/A
0N/A int sx = signWOZero(dx);
0N/A int sy = signWOZero(dy);
0N/A
0N/A int x = startPoint.x;
0N/A int y = startPoint.y;
0N/A
0N/A int d = 0;
0N/A
0N/A if (ax > ay) {
0N/A d = ay - ax/2;
0N/A while (true){
0N/A robot.mouseMove(x, y);
0N/A robot.delay(50);
0N/A
0N/A if (x == endPoint.x){
0N/A return;
0N/A }
0N/A if (d >= 0){
0N/A y = y + sy;
0N/A d = d - ax;
0N/A }
0N/A x = x + sx;
0N/A d = d + ay;
0N/A }
0N/A } else {
0N/A d = ax - ay/2;
0N/A while (true){
0N/A robot.mouseMove(x, y);
0N/A robot.delay(50);
0N/A
0N/A if (y == endPoint.y){
0N/A return;
0N/A }
0N/A if (d >= 0){
0N/A x = x + sx;
0N/A d = d - ay;
0N/A }
0N/A y = y + sy;
0N/A d = d + ax;
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static int signWOZero(int i){
0N/A return (i > 0)? 1: -1;
0N/A }
0N/A
0N/A private static int sign(int n) {
0N/A return n < 0 ? -1 : n == 0 ? 0 : 1;
0N/A }
0N/A
0N/A /** Returns {@code WindowListener} instance that diposes {@code Window} on
0N/A * "window closing" event.
0N/A *
0N/A * @return the {@code WindowListener} instance that could be set
0N/A * on a {@code Window}. After that
0N/A * the {@code Window} is disposed when "window closed"
2611N/A * event is sent to the {@code Window}
0N/A */
0N/A public static WindowListener getClosingWindowAdapter() {
0N/A return new WindowAdapter () {
0N/A public void windowClosing(WindowEvent e) {
0N/A e.getWindow().dispose();
0N/A }
0N/A };
0N/A }
0N/A
0N/A /*
0N/A * The values directly map to the ones of
0N/A * sun.awt.X11.XWM & sun.awt.motif.MToolkit classes.
0N/A */
0N/A public final static int
0N/A UNDETERMINED_WM = 1,
0N/A NO_WM = 2,
0N/A OTHER_WM = 3,
0N/A OPENLOOK_WM = 4,
0N/A MOTIF_WM = 5,
0N/A CDE_WM = 6,
0N/A ENLIGHTEN_WM = 7,
0N/A KDE2_WM = 8,
0N/A SAWFISH_WM = 9,
0N/A ICE_WM = 10,
0N/A METACITY_WM = 11,
0N/A COMPIZ_WM = 12,
0N/A LG3D_WM = 13,
0N/A CWM_WM = 14,
0N/A MUTTER_WM = 15;
0N/A
0N/A /*
0N/A * Returns -1 in case of not X Window or any problems.
0N/A */
0N/A public static int getWMID() {
0N/A Class clazz = null;
0N/A try {
0N/A if ("sun.awt.X11.XToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
0N/A clazz = Class.forName("sun.awt.X11.XWM");
0N/A } else if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
0N/A clazz = Class.forName("sun.awt.motif.MToolkit");
0N/A }
0N/A } catch (ClassNotFoundException cnfe) {
0N/A cnfe.printStackTrace();
0N/A }
0N/A if (clazz == null) {
0N/A return -1;
0N/A }
0N/A
0N/A try {
0N/A final Class _clazz = clazz;
0N/A Method m_getWMID = (Method)AccessController.doPrivileged(new PrivilegedAction() {
0N/A public Object run() {
0N/A try {
Method method = _clazz.getDeclaredMethod("getWMID", new Class[] {});
if (method != null) {
method.setAccessible(true);
}
return method;
} catch (NoSuchMethodException e) {
assert false;
} catch (SecurityException e) {
assert false;
}
return null;
}
});
return ((Integer)m_getWMID.invoke(null, new Object[] {})).intValue();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
}
return -1;
}
////////////////////////////
// Some stuff to test focus.
////////////////////////////
private static WindowGainedFocusListener wgfListener = new WindowGainedFocusListener();
private static FocusGainedListener fgListener = new FocusGainedListener();
private static ActionPerformedListener apListener = new ActionPerformedListener();
private abstract static class EventListener {
AtomicBoolean notifier = new AtomicBoolean(false);
Component comp;
boolean printEvent;
public void listen(Component comp, boolean printEvent) {
this.comp = comp;
this.printEvent = printEvent;
notifier.set(false);
setListener(comp);
}
public AtomicBoolean getNotifier() {
return notifier;
}
abstract void setListener(Component comp);
void printAndNotify(AWTEvent e) {
if (printEvent) {
System.err.println(e);
}
synchronized (notifier) {
notifier.set(true);
notifier.notifyAll();
}
}
}
private static class WindowGainedFocusListener extends EventListener implements WindowFocusListener {
void setListener(Component comp) {
((Window)comp).addWindowFocusListener(this);
}
public void windowGainedFocus(WindowEvent e) {
((Window)comp).removeWindowFocusListener(this);
printAndNotify(e);
}
public void windowLostFocus(WindowEvent e) {}
}
private static class FocusGainedListener extends EventListener implements FocusListener {
void setListener(Component comp) {
comp.addFocusListener(this);
}
public void focusGained(FocusEvent e) {
comp.removeFocusListener(this);
printAndNotify(e);
}
public void focusLost(FocusEvent e) {}
}
private static class ActionPerformedListener extends EventListener implements ActionListener {
void setListener(Component comp) {
((Button)comp).addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
((Button)comp).removeActionListener(this);
printAndNotify(e);
}
}
private static boolean trackEvent(int eventID, Component comp, Runnable action, int time, boolean printEvent) {
EventListener listener = null;
switch (eventID) {
case WindowEvent.WINDOW_GAINED_FOCUS:
listener = wgfListener;
break;
case FocusEvent.FOCUS_GAINED:
listener = fgListener;
break;
case ActionEvent.ACTION_PERFORMED:
listener = apListener;
break;
}
listener.listen(comp, printEvent);
action.run();
return Util.waitForCondition(listener.getNotifier(), time);
}
/*
* 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
*/
public static boolean 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
*/
public static boolean trackFocusGained(Component comp, Runnable action, int time, boolean printEvent) {
return trackEvent(FocusEvent.FOCUS_GAINED, comp, action, time, printEvent);
}
/*
* Tracks ACTION_PERFORMED event for a button caused by an action.
* @see #trackWindowGainedFocus
*/
public static boolean 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.
*/
public static boolean focusComponent(Component comp, int time) {
return focusComponent(comp, time, false);
}
public static boolean focusComponent(final Component comp, int time, boolean printEvent) {
return trackFocusGained(comp,
new Runnable() {
public void run() {
comp.requestFocus();
}
},
time, printEvent);
}
/**
* Invokes the <code>task</code> on the EDT thread.
*
* @return result of the <code>task</code>
*/
public static <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];
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
result.add(task.call());
} catch (Exception e) {
exception[0] = e;
}
}
});
if (exception[0] != null) {
throw exception[0];
}
return result.get(0);
}
}