Skip to content

8354928: Clean up and open source some miscellaneous AWT tests #24755

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions test/jdk/java/awt/event/InputEvent/InputEventTimeTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
160 changes: 160 additions & 0 deletions test/jdk/java/awt/event/MouseWheelEvent/HWWheelScroll.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
95 changes: 95 additions & 0 deletions test/jdk/java/awt/event/MouseWheelEvent/WheelEventCoord.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading