/* * Copyright (c) 2006, 2014, 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; /** *
This class contains utilities useful for regression testing. *
When using jtreg you would include this class into the build * list via something like: *
@library ../../../regtesthelpers @build Util @run main YourTest* 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. *
And put an
* import test.java.awt.regtesthelpers.Util;
* into the java source of test.
*/
import java.awt.Component;
import java.awt.Frame;
import java.awt.Dialog;
import java.awt.Window;
import java.awt.Button;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.IllegalComponentStateException;
import java.awt.AWTException;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.event.InputEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowFocusListener;
import java.awt.event.FocusListener;
import java.awt.event.ActionListener;
import java.awt.peer.FramePeer;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.PrivilegedAction;
import java.security.AccessController;
import java.util.concurrent.atomic.AtomicBoolean;
public final class Util {
private Util() {} // this is a helper class with static methods :)
/*
* @throws RuntimeException when creation failed
*/
public static Robot createRobot() {
try {
return new Robot();
} catch (AWTException e) {
throw new RuntimeException("Error: unable to create robot", e);
}
}
public static Frame createEmbeddedFrame(final Frame embedder)
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException,
InstantiationException, InvocationTargetException
{
Toolkit tk = Toolkit.getDefaultToolkit();
FramePeer frame_peer = (FramePeer) embedder.getPeer();
System.out.println("frame's peer = " + frame_peer);
if ("sun.awt.windows.WToolkit".equals(tk.getClass().getName())) {
Class comp_peer_class =
Class.forName("sun.awt.windows.WComponentPeer");
System.out.println("comp peer class = " + comp_peer_class);
Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");
hwnd_field.setAccessible(true);
System.out.println("hwnd_field =" + hwnd_field);
long hwnd = hwnd_field.getLong(frame_peer);
System.out.println("hwnd = " + hwnd);
Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");
Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE});
return (Frame) constructor.newInstance (new Object[] {hwnd});
} else if ("sun.awt.X11.XToolkit".equals(tk.getClass().getName())) {
Class x_base_window_class = Class.forName("sun.awt.X11.XBaseWindow");
System.out.println("x_base_window_class = " + x_base_window_class);
Method get_window = x_base_window_class.getMethod("getWindow", new Class[0]);
System.out.println("get_window = " + get_window);
long window = (Long) get_window.invoke(frame_peer, new Object[0]);
System.out.println("window = " + window);
Class clazz = Class.forName("sun.awt.X11.XEmbeddedFrame");
Constructor constructor = clazz.getConstructor (new Class [] {Long.TYPE, Boolean.TYPE});
return (Frame) constructor.newInstance (new Object[] {window, true});
}
throw new RuntimeException("Unexpected toolkit - " + tk);
}
/**
* Makes the window visible and waits until it's shown.
*/
public static void showWindowWait(Window win) {
win.setVisible(true);
waitTillShown(win);
}
/**
* Moves mouse pointer in the center of given {@code comp} component
* using {@code robot} parameter.
*/
public static void 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.
*/
public static void 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.
*/
public static void clickOnComp(final Component comp, final Robot robot) {
clickOnComp(comp, robot, 50);
}
public static Point getTitlePoint(Window decoratedWindow) {
Point p = decoratedWindow.getLocationOnScreen();
Dimension d = decoratedWindow.getSize();
return new 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.
*/
public static void 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
*/
public static boolean 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)) {
return true;
}
}
return false;
}
/**
* 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
*/
public static boolean 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)) {
return true;
}
}
return false;
}
public static void waitForIdle(final Robot robot) {
// we do not use robot for now, use SunToolkit.realSync() instead
((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync();
}
public static Field getField(final Class klass, final String fieldName) {
return AccessController.doPrivileged(new PrivilegedActiontask
on the EDT thread.
*
* @return result of the task
*/
public static