21
21
* questions.
22
22
*/
23
23
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
-
31
24
import java .awt .AWTEvent ;
32
25
import java .awt .Dimension ;
33
26
import java .awt .FlowLayout ;
34
27
import java .awt .Point ;
35
28
import java .awt .Robot ;
36
29
import java .awt .Toolkit ;
37
30
import java .awt .event .AWTEventListener ;
31
+ import java .awt .event .FocusAdapter ;
32
+ import java .awt .event .FocusEvent ;
38
33
import java .awt .event .MouseAdapter ;
39
34
import java .awt .event .MouseEvent ;
40
35
import java .util .concurrent .CountDownLatch ;
36
+ import java .util .concurrent .TimeUnit ;
41
37
42
38
import javax .swing .JButton ;
43
39
import javax .swing .JFrame ;
44
40
import javax .swing .SwingUtilities ;
45
41
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
+ */
46
48
public class WheelModifier {
47
49
48
50
JFrame f ;
49
51
JButton fb ;
50
52
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 );
54
57
volatile CountDownLatch wheelSema ;
55
58
56
59
private volatile Point sLoc ;
@@ -59,6 +62,14 @@ public class WheelModifier {
59
62
void createGui () {
60
63
f = new JFrame ("frame" );
61
64
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
+
62
73
fb .addMouseListener (new MouseAdapter () {
63
74
@ Override
64
75
public void mouseReleased (MouseEvent e ) {
@@ -69,7 +80,6 @@ public void mouseReleased(MouseEvent e) {
69
80
@ Override
70
81
public void mouseEntered (MouseEvent e ) {
71
82
System .out .println ("WheelModifier.mouseEntered: " + e );
72
-
73
83
}
74
84
75
85
@ Override
@@ -106,9 +116,13 @@ public void eventDispatched(AWTEvent event) {
106
116
}
107
117
108
118
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
+
109
124
Robot r = new Robot ();
110
125
r .waitForIdle ();
111
- System .out .println ("# Started" );
112
126
113
127
SwingUtilities .invokeAndWait (() -> {
114
128
sLoc = fb .getLocationOnScreen ();
@@ -117,33 +131,51 @@ void run() throws Exception {
117
131
118
132
r .mouseMove (sLoc .x + bSize .width / 2 , sLoc .y + bSize .height / 2 );
119
133
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
+ }
121
137
System .out .println ("# Pressed" );
122
138
123
139
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
+ }
125
143
System .out .println ("# Exited" );
126
144
127
145
wheelSema = new CountDownLatch (1 );
128
146
r .mouseWheel (1 );
129
- wheelSema .await ();
147
+ if (!wheelSema .await (1 , TimeUnit .SECONDS )) {
148
+ throw new RuntimeException ("Mouse is not wheeled 1" );
149
+ }
130
150
System .out .println ("# Wheeled 1" );
131
151
132
152
wheelSema = new CountDownLatch (1 );
133
153
r .mouseWheel (-1 );
134
- wheelSema .await ();
154
+ if (!wheelSema .await (1 , TimeUnit .SECONDS )) {
155
+ throw new RuntimeException ("Mouse is not wheeled 2" );
156
+ }
135
157
System .out .println ("# Wheeled 2" );
136
158
137
159
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
+ }
139
163
System .out .println ("# Released!" );
140
164
}
141
165
142
166
public static void main (String [] args ) throws Exception {
143
167
WheelModifier test = new WheelModifier ();
144
168
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
+ }
147
179
148
180
System .out .println ("Done." );
149
181
}
1 commit comments
openjdk-notifier[bot] commentedon Jan 22, 2024
Review
Issues