Skip to content

Commit 03cd885

Browse files
author
Andrew Lu
committedJan 22, 2024
8169475: WheelModifier.java fails by timeout
Backport-of: 613d32c2822a443fdcb131a7d67c528410e9f7e6
1 parent ef24634 commit 03cd885

File tree

1 file changed

+51
-19
lines changed

1 file changed

+51
-19
lines changed
 

‎test/jdk/java/awt/event/MouseWheelEvent/WheelModifier/WheelModifier.java

+51-19
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,39 @@
2121
* questions.
2222
*/
2323

24-
/*
25-
@test
26-
@key headful
27-
@bug 8041470
28-
@summary JButtons stay pressed after they have lost focus if you use the mouse wheel
29-
*/
30-
3124
import java.awt.AWTEvent;
3225
import java.awt.Dimension;
3326
import java.awt.FlowLayout;
3427
import java.awt.Point;
3528
import java.awt.Robot;
3629
import java.awt.Toolkit;
3730
import java.awt.event.AWTEventListener;
31+
import java.awt.event.FocusAdapter;
32+
import java.awt.event.FocusEvent;
3833
import java.awt.event.MouseAdapter;
3934
import java.awt.event.MouseEvent;
4035
import java.util.concurrent.CountDownLatch;
36+
import java.util.concurrent.TimeUnit;
4137

4238
import javax.swing.JButton;
4339
import javax.swing.JFrame;
4440
import javax.swing.SwingUtilities;
4541

42+
/*
43+
* @test
44+
* @key headful
45+
* @bug 8041470
46+
* @summary JButtons stay pressed after they have lost focus if you use the mouse wheel
47+
*/
4648
public class WheelModifier {
4749

4850
JFrame f;
4951
JButton fb;
5052

51-
CountDownLatch pressSema = new CountDownLatch(1);
52-
CountDownLatch exitSema = new CountDownLatch(1);
53-
CountDownLatch releaseSema = new CountDownLatch(1);
53+
final CountDownLatch focusSema = new CountDownLatch(1);
54+
final CountDownLatch pressSema = new CountDownLatch(1);
55+
final CountDownLatch exitSema = new CountDownLatch(1);
56+
final CountDownLatch releaseSema = new CountDownLatch(1);
5457
volatile CountDownLatch wheelSema;
5558

5659
private volatile Point sLoc;
@@ -59,6 +62,14 @@ public class WheelModifier {
5962
void createGui() {
6063
f = new JFrame("frame");
6164
fb = new JButton("frame_button");
65+
66+
fb.addFocusListener(new FocusAdapter() {
67+
@Override
68+
public void focusGained(FocusEvent focusEvent) {
69+
focusSema.countDown();
70+
}
71+
});
72+
6273
fb.addMouseListener(new MouseAdapter() {
6374
@Override
6475
public void mouseReleased(MouseEvent e) {
@@ -69,7 +80,6 @@ public void mouseReleased(MouseEvent e) {
6980
@Override
7081
public void mouseEntered(MouseEvent e) {
7182
System.out.println("WheelModifier.mouseEntered: " + e);
72-
7383
}
7484

7585
@Override
@@ -106,9 +116,13 @@ public void eventDispatched(AWTEvent event) {
106116
}
107117

108118
void run() throws Exception {
119+
System.out.println("# Started");
120+
if (!focusSema.await(2, TimeUnit.SECONDS)) {
121+
throw new RuntimeException("Didn't receive focus in time");
122+
}
123+
109124
Robot r = new Robot();
110125
r.waitForIdle();
111-
System.out.println("# Started");
112126

113127
SwingUtilities.invokeAndWait(() -> {
114128
sLoc = fb.getLocationOnScreen();
@@ -117,33 +131,51 @@ void run() throws Exception {
117131

118132
r.mouseMove(sLoc.x + bSize.width / 2, sLoc.y + bSize.height / 2);
119133
r.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
120-
pressSema.await();
134+
if (!pressSema.await(2, TimeUnit.SECONDS)) {
135+
throw new RuntimeException("Mouse is not pressed");
136+
}
121137
System.out.println("# Pressed");
122138

123139
r.mouseMove(sLoc.x + bSize.width / 2, sLoc.y + bSize.height * 2);
124-
exitSema.await();
140+
if (!exitSema.await(1, TimeUnit.SECONDS)) {
141+
throw new RuntimeException("Mouse did not exit");
142+
}
125143
System.out.println("# Exited");
126144

127145
wheelSema = new CountDownLatch(1);
128146
r.mouseWheel(1);
129-
wheelSema.await();
147+
if (!wheelSema.await(1, TimeUnit.SECONDS)) {
148+
throw new RuntimeException("Mouse is not wheeled 1");
149+
}
130150
System.out.println("# Wheeled 1");
131151

132152
wheelSema = new CountDownLatch(1);
133153
r.mouseWheel(-1);
134-
wheelSema.await();
154+
if (!wheelSema.await(1, TimeUnit.SECONDS)) {
155+
throw new RuntimeException("Mouse is not wheeled 2");
156+
}
135157
System.out.println("# Wheeled 2");
136158

137159
r.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
138-
releaseSema.await();
160+
if (!releaseSema.await(1, TimeUnit.SECONDS)) {
161+
throw new RuntimeException("Mouse is not released");
162+
}
139163
System.out.println("# Released!");
140164
}
141165

142166
public static void main(String[] args) throws Exception {
143167
WheelModifier test = new WheelModifier();
144168

145-
SwingUtilities.invokeAndWait(() -> test.createGui());
146-
test.run();
169+
try {
170+
SwingUtilities.invokeAndWait(test::createGui);
171+
test.run();
172+
} finally {
173+
SwingUtilities.invokeAndWait(() -> {
174+
if (test.f != null) {
175+
test.f.dispose();
176+
}
177+
});
178+
}
147179

148180
System.out.println("Done.");
149181
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jan 22, 2024

@openjdk-notifier[bot]
Please sign in to comment.