8354928: Clean up and open source some miscellaneous AWT tests

Reviewed-by: prr, dnguyen
This commit is contained in:
Alexander Zuev 2025-04-22 20:17:01 +00:00
parent 239760ac09
commit 1b8f760d1b
4 changed files with 499 additions and 0 deletions

View File

@ -0,0 +1,115 @@
/*
* Copyright (c) 1999, 2025, 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.
*/
/*
* @test
* @bug 4176525
* @summary InputEvent.getWhen() returns the wrong event time.
* @key headful
* @run main InputEventTimeTest
*/
import java.awt.AWTEvent;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
public class InputEventTimeTest extends Frame {
public void initUI() {
setTitle("Input Event Time Test");
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
enableEvents(AWTEvent.KEY_EVENT_MASK);
setSize(200, 200);
setLocationRelativeTo(null);
setVisible(true);
}
public void center(Point point) {
Point loc = getLocationOnScreen();
Dimension size = getSize();
point.setLocation(loc.x + (size.width / 2), loc.y + (size.height / 2));
}
public void processEvent(AWTEvent e) {
long currentTime;
long eventTime;
long difference;
if (!(e instanceof InputEvent)) {
return;
}
currentTime = (new Date()).getTime();
eventTime = ((InputEvent) e).getWhen();
difference = currentTime - eventTime;
if ((difference > 5000) || (difference < -5000)) {
throw new RuntimeException("The difference between current time" +
" and event creation time is " + difference + "ms");
}
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException, AWTException {
InputEventTimeTest test = new InputEventTimeTest();
try {
EventQueue.invokeAndWait(test::initUI);
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.waitForIdle();
robot.delay(1000);
Point center = new Point();
EventQueue.invokeAndWait(() -> test.center(center));
robot.mouseMove(center.x, center.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
robot.waitForIdle();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
for (int i = 0; i < 6; i++) {
robot.keyPress(KeyEvent.VK_A + i);
robot.keyRelease(KeyEvent.VK_A + i);
robot.waitForIdle();
}
for (int i = 0; i < 150; i += 5) {
robot.mouseMove(center.x - i, center.y - i);
}
for (int i = 150; i > 0; i -= 5) {
robot.mouseMove(center.x - i, center.y - i);
}
} finally {
EventQueue.invokeAndWait(test::dispose);
}
}
}

View File

@ -0,0 +1,160 @@
/*
* Copyright (c) 2001, 2025, 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.
*/
/*
* @test
* @bug 4425654
* @summary Test wheel scrolling of heavyweight components
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual HWWheelScroll
*/
import java.awt.Choice;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.List;
import java.awt.TextArea;
import java.awt.Window;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
public class HWWheelScroll {
public static final int TEXT_TALL = 0;
public static final int TEXT_WIDE = 1;
public static final int TEXT_SMALL = 2;
public static final int TEXT_BIG = 3;
static String INSTRUCTIONS = """
Test for mouse wheel scrolling of heavyweight components with built-in
scrollbars or similar functionality that is controlled by guestures
such as Apple Magic Mouse or trackpad scrolling guesture.
Several windows containing either a TextArea, List, Choice, or a
FileDialog will appear. For each window, use the mouse wheel to
scroll its content, and then minimize it or move away
and continue with the next window.
Do not close any of the opened windows except the FileDialog.
For the FileDialog, first change to a directory with enough items that a
scrollbar appears.
Some of the other windows don't have enough text to warrant scrollbars,
but should be tested anyway to make sure no crash or hang occurs.
If all scrollbars scroll correctly, press "Pass", otherwise press "Fail".
""";
public static ArrayList<Window> initUI() {
ArrayList<Window> retValue = new ArrayList<>();
retValue.add(makeTextFrame(TextArea.SCROLLBARS_BOTH, TEXT_BIG));
retValue.add(makeTextFrame(TextArea.SCROLLBARS_BOTH, TEXT_TALL));
retValue.add(makeTextFrame(TextArea.SCROLLBARS_BOTH, TEXT_SMALL));
retValue.add(makeTextFrame(TextArea.SCROLLBARS_BOTH, TEXT_WIDE));
retValue.add(makeTextFrame(TextArea.SCROLLBARS_VERTICAL_ONLY, TEXT_TALL));
retValue.add(makeTextFrame(TextArea.SCROLLBARS_VERTICAL_ONLY, TEXT_SMALL));
retValue.add(makeTextFrame(TextArea.SCROLLBARS_HORIZONTAL_ONLY, TEXT_SMALL));
retValue.add(makeTextFrame(TextArea.SCROLLBARS_HORIZONTAL_ONLY, TEXT_WIDE));
retValue.add(makeListFrame(TEXT_TALL));
retValue.add(makeListFrame(TEXT_WIDE));
retValue.add(makeListFrame(TEXT_SMALL));
Frame f = new Frame("File Dialog Owner");
f.setSize(150, 150);
f.setLocationRelativeTo(null);
FileDialog fd = new FileDialog(f, "FileDialog");
fd.setDirectory(".");
retValue.add(fd);
retValue.add(f);
Frame choiceFrame = new Frame("Choice");
Choice c = new Choice();
for (int i = 0; i < 50; i++) {
c.add(i + " choice item");
}
choiceFrame.add(c);
choiceFrame.setSize(150, 150);
choiceFrame.setLocationRelativeTo(null);
retValue.add(choiceFrame);
return retValue;
}
public static Frame makeTextFrame(int policy, int textShape) {
Frame f = new Frame("TextArea");
f.add(makeTextArea(policy, textShape));
f.setSize(150, 150);
f.setLocationRelativeTo(null);
return f;
}
public static Frame makeListFrame(int textShape) {
Frame f = new Frame("List");
f.add(makeList(textShape));
f.setSize(150, 150);
f.setLocationRelativeTo(null);
return f;
}
public static TextArea makeTextArea(int policy, int textShape) {
TextArea ta = new TextArea("", 0, 0, policy);
if (textShape == TEXT_TALL) {
for (int i = 0; i < 50 ; i++) {
ta.append(i + "\n");
}
} else if (textShape == TEXT_WIDE) {
for (int i = 0; i < 2; i++) {
ta.append(i + "very, very, very, very, very, very, very, long line of text number\n");
}
} else if (textShape == TEXT_SMALL) {
ta.append("text");
} else if (textShape == TEXT_BIG) {
for (int i = 0; i < 50 ; i++) {
ta.append(i + "very, very, very, very, very, very, very, long line of text number\n");
}
}
return ta;
}
public static List makeList(int textShape) {
java.awt.List l = new java.awt.List();
if (textShape == TEXT_TALL) {
for (int i = 0; i < 50 ; i++) {
l.add(" " + i + " ");
}
} else if (textShape == TEXT_WIDE) {
for (int i = 0; i < 2 ; i++) {
l.add(i + "very, very, very, very, very, very, very, long line of text number");
}
} else if (textShape == TEXT_SMALL) {
l.add("text");
} else if (textShape == TEXT_BIG) {
for (int i = 0; i < 50 ; i++) {
l.add(i + "very, very, very, very, very, very, very, long line of text number");
}
}
return l;
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.logArea(10)
.testUI(HWWheelScroll::initUI)
.build()
.awaitAndCheck();
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2001, 2025, 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.
*/
/*
* @test
* @bug 4492456
* @summary MouseWheelEvent coordinates are wrong
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual WheelEventCoord
*/
import java.awt.Button;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.lang.reflect.InvocationTargetException;
public class WheelEventCoord extends Frame {
static String INSTRUCTIONS = """
This test requires mouse with scrolling wheel or device,
that has capability to simulate scrolling wheel with gestures
such as Apple mouse or a trackpad with gesture control.
If you do not have such device press "Pass".
Move mouse to the top of the button named "Button 1".
While constantly turning mouse wheel up and down slowly move
mouse cursor until it reaches bottom of the button named "Button 3".
While doing so look at the log area.
If despite the wheel direction y coordinate is steadily increases
as you move the mouse down press "Pass".
If y coordinate decreases when cursor is moving down or suddenly jumps
by more than 50 points when crossing to another button press "Fail".
""";
public WheelEventCoord() {
super("Wheel Event Coordinates");
setLayout(new GridLayout(3, 1));
add(new BigButton("Button 1"));
add(new BigButton("Button 2"));
add(new BigButton("Button 3"));
addMouseWheelListener(e -> PassFailJFrame.log("Mouse y coordinate = " + e.getY()));
pack();
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Wheel Event Coordinates Instructions")
.instructions(INSTRUCTIONS)
.logArea(10)
.testUI(WheelEventCoord::new)
.build()
.awaitAndCheck();
}
}
class BigButton extends Button {
public BigButton(String label) {
super(label);
}
public Dimension getPreferredSize() {
return new Dimension(300, 100);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
}

View File

@ -0,0 +1,129 @@
/*
* Copyright (c) 2000, 2025, 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.
*/
/*
* @test
* @bug 4372477
* @summary Test disabling of wheel scrolling
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual WheelScrollEnabled
*/
import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.lang.reflect.InvocationTargetException;
public class WheelScrollEnabled extends Frame {
static String INSTRUCTIONS = """
This test requires mouse with a scrolling wheel or
device that is able to simulate scrolling using gestures.
If you do not have such device press "Pass" to skip testing.
You should see a ScrollPane with some labels in it and two checkboxes.
For each of the four combinations of the two checkboxes,
move the cursor over the ScrollPane and rotate the mouse wheel.
When (and ONLY when) the 'WheelListener added' checkbox is checked,
scrolling the mouse wheel should produce a text message in the log area.
When (and ONLY when) the 'Wheel scrolling enabled' checkbox is checked,
the ScrollPane should scroll when mouse wheel is scrolled on top of it.
If all four checkbox combinations work properly press "Pass",
otherwise press "Fail".
""";
MouseWheelListener mwl;
Checkbox cb;
Checkbox cb2;
ScrollPane sp;
public WheelScrollEnabled() {
setLayout(new BorderLayout());
Panel pnl = new Panel();
pnl.setLayout(new GridLayout(10, 10));
for (int i = 0; i < 100; i++) {
pnl.add(new Label("Label " + i));
}
sp = new ScrollPane();
sp.add(pnl);
sp.setWheelScrollingEnabled(false);
mwl = new MouseWheelListener() {
int i;
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
PassFailJFrame.log("mouseWheelMoved " + i++);
}
};
sp.addMouseWheelListener(mwl);
add(sp, BorderLayout.CENTER);
Panel pl2 = new Panel();
ItemListener il = new ControlListener();
cb = new Checkbox("WheelListener added", true);
cb.addItemListener(il);
pl2.add(cb);
cb2 = new Checkbox("Wheel scrolling enabled", false);
cb2.addItemListener(il);
pl2.add(cb2);
add(pl2, BorderLayout.SOUTH);
setSize(400, 200);
}
class ControlListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == cb) {
boolean state = cb.getState();
if (state) {
sp.addMouseWheelListener(mwl);
}
else {
sp.removeMouseWheelListener(mwl);
}
}
if (e.getSource() == cb2) {
sp.setWheelScrollingEnabled(cb2.getState());
}
}
}
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
PassFailJFrame.builder()
.title("Wheel Scroll Enabled Instructions")
.instructions(INSTRUCTIONS)
.logArea(10)
.testUI(WheelScrollEnabled::new)
.build()
.awaitAndCheck();
}
}