Skip to content

Commit 00ef9f9

Browse files
Ravi Guptaaivanov-jdk
authored andcommitted
8316947: Write a test to check textArea triggers MouseEntered/MouseExited events properly
Reviewed-by: tr, honkar, aivanov
1 parent 77dc891 commit 00ef9f9

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (c) 2007, 2023, 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+
import java.awt.Dimension;
25+
import java.awt.EventQueue;
26+
import java.awt.FlowLayout;
27+
import java.awt.Frame;
28+
import java.awt.List;
29+
import java.awt.Point;
30+
import java.awt.Robot;
31+
import java.awt.TextArea;
32+
import java.awt.event.MouseAdapter;
33+
import java.awt.event.MouseEvent;
34+
import java.awt.event.MouseListener;
35+
36+
/*
37+
* @test
38+
* @key headful
39+
* @bug 4454304
40+
* @summary On Solaris, TextArea triggers MouseEntered when the mouse is inside the component
41+
* @run main MouseEnterExitTest
42+
*/
43+
public class MouseEnterExitTest {
44+
45+
private static Frame frame;
46+
47+
private volatile static boolean entered = false;
48+
private volatile static boolean exited = false;
49+
private volatile static boolean passed = true;
50+
51+
private volatile static Point compAt;
52+
private volatile static Dimension compSize;
53+
54+
private static final MouseListener mouseListener = new MouseAdapter() {
55+
@Override
56+
public void mouseEntered(MouseEvent e) {
57+
System.out.println(
58+
"MouseEntered component " + e.getSource().getClass().getName());
59+
if (entered) {
60+
passed = false;
61+
}
62+
entered = true;
63+
exited = false;
64+
}
65+
66+
@Override
67+
public void mouseExited(MouseEvent e) {
68+
System.out.println(
69+
"MouseExited component " + e.getSource().getClass().getName());
70+
if (exited) {
71+
passed = false;
72+
}
73+
entered = false;
74+
exited = true;
75+
}
76+
};
77+
78+
private static void initializeGUI() {
79+
frame = new Frame("MouseEnterExitTest");
80+
frame.setLayout(new FlowLayout());
81+
List list = new List(4);
82+
for (int i = 0; i < 10; i++) {
83+
list.add("item " + i);
84+
}
85+
list.addMouseListener(mouseListener);
86+
frame.add(list);
87+
88+
TextArea textArea = new TextArea("TextArea", 10, 20);
89+
textArea.addMouseListener(mouseListener);
90+
frame.add(textArea);
91+
92+
frame.pack();
93+
frame.setLocationRelativeTo(null);
94+
frame.setVisible(true);
95+
}
96+
97+
public static void main(String[] args) throws Exception {
98+
try {
99+
Robot robot = new Robot();
100+
robot.setAutoDelay(100);
101+
robot.setAutoWaitForIdle(true);
102+
103+
EventQueue.invokeAndWait(MouseEnterExitTest::initializeGUI);
104+
robot.waitForIdle();
105+
106+
EventQueue.invokeAndWait(() -> {
107+
compAt = frame.getLocationOnScreen();
108+
compSize = frame.getSize();
109+
});
110+
compAt.y += compSize.getHeight() / 2;
111+
int xr = compAt.x + compSize.width + 1;
112+
for (int i = compAt.x - 5; (i < xr) && passed; i++) {
113+
robot.mouseMove(i, compAt.y);
114+
}
115+
116+
if (!passed || entered || !exited) {
117+
throw new RuntimeException(
118+
"MouseEnterExitTest FAILED. MouseEntered/MouseExited "
119+
+ "not properly triggered. Please see the log");
120+
}
121+
System.out.println("Test PASSED");
122+
} finally {
123+
EventQueue.invokeAndWait(MouseEnterExitTest::disposeFrame);
124+
}
125+
}
126+
127+
private static void disposeFrame() {
128+
if (frame != null) {
129+
frame.dispose();
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)