Skip to content

Commit 1b8f760

Browse files
author
Alexander Zuev
committed
8354928: Clean up and open source some miscellaneous AWT tests
Reviewed-by: prr, dnguyen
1 parent 239760a commit 1b8f760

File tree

4 files changed

+499
-0
lines changed

4 files changed

+499
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4176525
27+
* @summary InputEvent.getWhen() returns the wrong event time.
28+
* @key headful
29+
* @run main InputEventTimeTest
30+
*/
31+
32+
import java.awt.AWTEvent;
33+
import java.awt.AWTException;
34+
import java.awt.Dimension;
35+
import java.awt.EventQueue;
36+
import java.awt.Frame;
37+
import java.awt.Point;
38+
import java.awt.Robot;
39+
import java.awt.event.InputEvent;
40+
import java.awt.event.KeyEvent;
41+
import java.lang.reflect.InvocationTargetException;
42+
import java.util.Date;
43+
44+
public class InputEventTimeTest extends Frame {
45+
public void initUI() {
46+
setTitle("Input Event Time Test");
47+
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
48+
enableEvents(AWTEvent.KEY_EVENT_MASK);
49+
setSize(200, 200);
50+
setLocationRelativeTo(null);
51+
setVisible(true);
52+
}
53+
54+
public void center(Point point) {
55+
Point loc = getLocationOnScreen();
56+
Dimension size = getSize();
57+
point.setLocation(loc.x + (size.width / 2), loc.y + (size.height / 2));
58+
}
59+
60+
public void processEvent(AWTEvent e) {
61+
long currentTime;
62+
long eventTime;
63+
long difference;
64+
65+
if (!(e instanceof InputEvent)) {
66+
return;
67+
}
68+
69+
currentTime = (new Date()).getTime();
70+
eventTime = ((InputEvent) e).getWhen();
71+
difference = currentTime - eventTime;
72+
73+
if ((difference > 5000) || (difference < -5000)) {
74+
throw new RuntimeException("The difference between current time" +
75+
" and event creation time is " + difference + "ms");
76+
}
77+
}
78+
79+
public static void main(String[] args) throws InterruptedException,
80+
InvocationTargetException, AWTException {
81+
InputEventTimeTest test = new InputEventTimeTest();
82+
try {
83+
EventQueue.invokeAndWait(test::initUI);
84+
Robot robot = new Robot();
85+
robot.setAutoDelay(50);
86+
robot.waitForIdle();
87+
robot.delay(1000);
88+
Point center = new Point();
89+
EventQueue.invokeAndWait(() -> test.center(center));
90+
robot.mouseMove(center.x, center.y);
91+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
92+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
93+
robot.waitForIdle();
94+
robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
95+
robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
96+
robot.waitForIdle();
97+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
98+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
99+
robot.waitForIdle();
100+
for (int i = 0; i < 6; i++) {
101+
robot.keyPress(KeyEvent.VK_A + i);
102+
robot.keyRelease(KeyEvent.VK_A + i);
103+
robot.waitForIdle();
104+
}
105+
for (int i = 0; i < 150; i += 5) {
106+
robot.mouseMove(center.x - i, center.y - i);
107+
}
108+
for (int i = 150; i > 0; i -= 5) {
109+
robot.mouseMove(center.x - i, center.y - i);
110+
}
111+
} finally {
112+
EventQueue.invokeAndWait(test::dispose);
113+
}
114+
}
115+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4425654
27+
* @summary Test wheel scrolling of heavyweight components
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual HWWheelScroll
31+
*/
32+
33+
import java.awt.Choice;
34+
import java.awt.FileDialog;
35+
import java.awt.Frame;
36+
import java.awt.List;
37+
import java.awt.TextArea;
38+
import java.awt.Window;
39+
import java.lang.reflect.InvocationTargetException;
40+
import java.util.ArrayList;
41+
42+
public class HWWheelScroll {
43+
public static final int TEXT_TALL = 0;
44+
public static final int TEXT_WIDE = 1;
45+
public static final int TEXT_SMALL = 2;
46+
public static final int TEXT_BIG = 3;
47+
static String INSTRUCTIONS = """
48+
Test for mouse wheel scrolling of heavyweight components with built-in
49+
scrollbars or similar functionality that is controlled by guestures
50+
such as Apple Magic Mouse or trackpad scrolling guesture.
51+
Several windows containing either a TextArea, List, Choice, or a
52+
FileDialog will appear. For each window, use the mouse wheel to
53+
scroll its content, and then minimize it or move away
54+
and continue with the next window.
55+
Do not close any of the opened windows except the FileDialog.
56+
For the FileDialog, first change to a directory with enough items that a
57+
scrollbar appears.
58+
Some of the other windows don't have enough text to warrant scrollbars,
59+
but should be tested anyway to make sure no crash or hang occurs.
60+
If all scrollbars scroll correctly, press "Pass", otherwise press "Fail".
61+
""";
62+
63+
public static ArrayList<Window> initUI() {
64+
ArrayList<Window> retValue = new ArrayList<>();
65+
retValue.add(makeTextFrame(TextArea.SCROLLBARS_BOTH, TEXT_BIG));
66+
retValue.add(makeTextFrame(TextArea.SCROLLBARS_BOTH, TEXT_TALL));
67+
retValue.add(makeTextFrame(TextArea.SCROLLBARS_BOTH, TEXT_SMALL));
68+
retValue.add(makeTextFrame(TextArea.SCROLLBARS_BOTH, TEXT_WIDE));
69+
retValue.add(makeTextFrame(TextArea.SCROLLBARS_VERTICAL_ONLY, TEXT_TALL));
70+
retValue.add(makeTextFrame(TextArea.SCROLLBARS_VERTICAL_ONLY, TEXT_SMALL));
71+
retValue.add(makeTextFrame(TextArea.SCROLLBARS_HORIZONTAL_ONLY, TEXT_SMALL));
72+
retValue.add(makeTextFrame(TextArea.SCROLLBARS_HORIZONTAL_ONLY, TEXT_WIDE));
73+
retValue.add(makeListFrame(TEXT_TALL));
74+
retValue.add(makeListFrame(TEXT_WIDE));
75+
retValue.add(makeListFrame(TEXT_SMALL));
76+
Frame f = new Frame("File Dialog Owner");
77+
f.setSize(150, 150);
78+
f.setLocationRelativeTo(null);
79+
FileDialog fd = new FileDialog(f, "FileDialog");
80+
fd.setDirectory(".");
81+
retValue.add(fd);
82+
retValue.add(f);
83+
Frame choiceFrame = new Frame("Choice");
84+
Choice c = new Choice();
85+
for (int i = 0; i < 50; i++) {
86+
c.add(i + " choice item");
87+
}
88+
choiceFrame.add(c);
89+
choiceFrame.setSize(150, 150);
90+
choiceFrame.setLocationRelativeTo(null);
91+
retValue.add(choiceFrame);
92+
return retValue;
93+
}
94+
95+
public static Frame makeTextFrame(int policy, int textShape) {
96+
Frame f = new Frame("TextArea");
97+
f.add(makeTextArea(policy, textShape));
98+
f.setSize(150, 150);
99+
f.setLocationRelativeTo(null);
100+
return f;
101+
}
102+
103+
public static Frame makeListFrame(int textShape) {
104+
Frame f = new Frame("List");
105+
f.add(makeList(textShape));
106+
f.setSize(150, 150);
107+
f.setLocationRelativeTo(null);
108+
return f;
109+
}
110+
111+
public static TextArea makeTextArea(int policy, int textShape) {
112+
TextArea ta = new TextArea("", 0, 0, policy);
113+
if (textShape == TEXT_TALL) {
114+
for (int i = 0; i < 50 ; i++) {
115+
ta.append(i + "\n");
116+
}
117+
} else if (textShape == TEXT_WIDE) {
118+
for (int i = 0; i < 2; i++) {
119+
ta.append(i + "very, very, very, very, very, very, very, long line of text number\n");
120+
}
121+
} else if (textShape == TEXT_SMALL) {
122+
ta.append("text");
123+
} else if (textShape == TEXT_BIG) {
124+
for (int i = 0; i < 50 ; i++) {
125+
ta.append(i + "very, very, very, very, very, very, very, long line of text number\n");
126+
}
127+
}
128+
return ta;
129+
}
130+
131+
public static List makeList(int textShape) {
132+
java.awt.List l = new java.awt.List();
133+
if (textShape == TEXT_TALL) {
134+
for (int i = 0; i < 50 ; i++) {
135+
l.add(" " + i + " ");
136+
}
137+
} else if (textShape == TEXT_WIDE) {
138+
for (int i = 0; i < 2 ; i++) {
139+
l.add(i + "very, very, very, very, very, very, very, long line of text number");
140+
}
141+
} else if (textShape == TEXT_SMALL) {
142+
l.add("text");
143+
} else if (textShape == TEXT_BIG) {
144+
for (int i = 0; i < 50 ; i++) {
145+
l.add(i + "very, very, very, very, very, very, very, long line of text number");
146+
}
147+
}
148+
return l;
149+
}
150+
151+
public static void main(String[] args) throws InterruptedException,
152+
InvocationTargetException {
153+
PassFailJFrame.builder()
154+
.instructions(INSTRUCTIONS)
155+
.logArea(10)
156+
.testUI(HWWheelScroll::initUI)
157+
.build()
158+
.awaitAndCheck();
159+
}
160+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 4492456
27+
* @summary MouseWheelEvent coordinates are wrong
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual WheelEventCoord
31+
*/
32+
33+
import java.awt.Button;
34+
import java.awt.Dimension;
35+
import java.awt.Frame;
36+
import java.awt.GridLayout;
37+
import java.lang.reflect.InvocationTargetException;
38+
39+
public class WheelEventCoord extends Frame {
40+
static String INSTRUCTIONS = """
41+
This test requires mouse with scrolling wheel or device,
42+
that has capability to simulate scrolling wheel with gestures
43+
such as Apple mouse or a trackpad with gesture control.
44+
If you do not have such device press "Pass".
45+
Move mouse to the top of the button named "Button 1".
46+
While constantly turning mouse wheel up and down slowly move
47+
mouse cursor until it reaches bottom of the button named "Button 3".
48+
While doing so look at the log area.
49+
If despite the wheel direction y coordinate is steadily increases
50+
as you move the mouse down press "Pass".
51+
If y coordinate decreases when cursor is moving down or suddenly jumps
52+
by more than 50 points when crossing to another button press "Fail".
53+
""";
54+
55+
public WheelEventCoord() {
56+
super("Wheel Event Coordinates");
57+
setLayout(new GridLayout(3, 1));
58+
59+
add(new BigButton("Button 1"));
60+
add(new BigButton("Button 2"));
61+
add(new BigButton("Button 3"));
62+
63+
addMouseWheelListener(e -> PassFailJFrame.log("Mouse y coordinate = " + e.getY()));
64+
pack();
65+
}
66+
67+
public static void main(String[] args) throws InterruptedException,
68+
InvocationTargetException {
69+
PassFailJFrame.builder()
70+
.title("Wheel Event Coordinates Instructions")
71+
.instructions(INSTRUCTIONS)
72+
.logArea(10)
73+
.testUI(WheelEventCoord::new)
74+
.build()
75+
.awaitAndCheck();
76+
}
77+
}
78+
79+
class BigButton extends Button {
80+
public BigButton(String label) {
81+
super(label);
82+
}
83+
84+
public Dimension getPreferredSize() {
85+
return new Dimension(300, 100);
86+
}
87+
88+
public Dimension getMinimumSize() {
89+
return getPreferredSize();
90+
}
91+
92+
public Dimension getMaximumSize() {
93+
return getPreferredSize();
94+
}
95+
}

0 commit comments

Comments
 (0)