3131
3232import java .awt .Point ;
3333import java .awt .Robot ;
34+ import java .awt .event .ActionEvent ;
35+ import java .awt .event .ActionListener ;
3436import java .awt .event .InputEvent ;
37+ import java .awt .event .MouseAdapter ;
38+ import java .awt .event .MouseEvent ;
39+ import java .awt .event .WindowAdapter ;
40+ import java .awt .event .WindowEvent ;
41+ import java .util .concurrent .CountDownLatch ;
42+
3543import javax .swing .JButton ;
3644import javax .swing .JFrame ;
3745import javax .swing .SwingUtilities ;
3846
39- public class bug4490179 {
47+ import static java .util .concurrent .TimeUnit .MILLISECONDS ;
48+ import static java .util .concurrent .TimeUnit .SECONDS ;
49+
50+ public final class bug4490179
51+ extends MouseAdapter
52+ implements ActionListener {
4053 static JFrame frame ;
4154 static JButton button ;
42- static volatile Point pt ;
43- static volatile int buttonW ;
44- static volatile int buttonH ;
45- static volatile boolean passed = true ;
55+
56+ private static volatile Point buttonCenter ;
57+
58+ private static final CountDownLatch windowGainedFocus = new CountDownLatch (1 );
59+
60+ private static final CountDownLatch mouseButton1Released = new CountDownLatch (1 );
61+ private static final CountDownLatch mouseButton3Released = new CountDownLatch (2 );
62+
63+ private static final CountDownLatch actionPerformed = new CountDownLatch (1 );
4664
4765 public static void main (String [] args ) throws Exception {
4866 Robot robot = new Robot ();
4967 robot .setAutoDelay (100 );
50- robot .setAutoWaitForIdle (true );
68+
69+ final bug4490179 eventHandler = new bug4490179 ();
5170 try {
5271 SwingUtilities .invokeAndWait (() -> {
53- frame = new JFrame ("bug4490179" );
5472 button = new JButton ("Button" );
73+ button .addActionListener (eventHandler );
74+ button .addMouseListener (eventHandler );
75+
76+ frame = new JFrame ("bug4490179" );
5577 frame .getContentPane ().add (button );
56- button . addActionListener ( e -> {
57- if (( e . getModifiers () & InputEvent . BUTTON1_MASK )
58- != InputEvent . BUTTON1_MASK ) {
59- System . out . println ( "Status: Failed" );
60- passed = false ;
78+
79+ frame . addWindowFocusListener ( new WindowAdapter () {
80+ @ Override
81+ public void windowGainedFocus ( WindowEvent e ) {
82+ windowGainedFocus . countDown () ;
6183 }
6284 });
85+
6386 frame .pack ();
6487 frame .setLocationRelativeTo (null );
6588 frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
6689 frame .setVisible (true );
6790 });
91+
92+ if (!windowGainedFocus .await (1 , SECONDS )) {
93+ throw new RuntimeException ("Window didn't gain focus" );
94+ }
6895 robot .waitForIdle ();
69- robot . delay ( 1000 );
96+
7097 SwingUtilities .invokeAndWait (() -> {
71- pt = button .getLocationOnScreen ();
72- buttonW = button .getSize (). width ;
73- buttonH = button .getSize (). height ;
98+ Point location = button .getLocationOnScreen ();
99+ buttonCenter = new Point ( location . x + button .getWidth () / 2 ,
100+ location . y + button .getHeight () / 2 ) ;
74101 });
75102
76- robot .mouseMove (pt . x + buttonW / 2 , pt . y + buttonH / 2 );
77- robot . waitForIdle ( );
103+ robot .mouseMove (buttonCenter . x , buttonCenter . y );
104+ System . out . println ( "Press / Release button 3" );
78105 robot .mousePress (InputEvent .BUTTON3_DOWN_MASK );
79106 robot .mouseRelease (InputEvent .BUTTON3_DOWN_MASK );
80107
108+ System .out .println ("Press button 1" );
81109 robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
110+ System .out .println ("Press button 3" );
82111 robot .mousePress (InputEvent .BUTTON3_DOWN_MASK );
112+ System .out .println ("Release button 3" );
83113 robot .mouseRelease (InputEvent .BUTTON3_DOWN_MASK );
84- robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
85- robot .delay (500 );
86114
87- if (!passed ) {
88- throw new RuntimeException ("Test Failed" );
115+ try {
116+ if (!mouseButton3Released .await (1 , SECONDS )) {
117+ throw new RuntimeException ("Mouse button 3 isn't released" );
118+ }
119+
120+ robot .waitForIdle ();
121+
122+ if (actionPerformed .await (100 , MILLISECONDS )) {
123+ throw new RuntimeException ("Action event triggered by releasing button 3" );
124+ }
125+ } finally {
126+ System .out .println ("Release button 1" );
127+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
128+ }
129+
130+ if (!mouseButton1Released .await (1 , SECONDS )) {
131+ throw new RuntimeException ("Mouse button 1 isn't released" );
132+ }
133+ if (!actionPerformed .await (100 , MILLISECONDS )) {
134+ throw new RuntimeException ("Action event isn't triggered by releasing button 1" );
89135 }
90136 } finally {
91137 SwingUtilities .invokeAndWait (() -> {
@@ -95,4 +141,21 @@ public static void main(String[] args) throws Exception {
95141 });
96142 }
97143 }
144+
145+ @ Override
146+ public void actionPerformed (ActionEvent e ) {
147+ System .out .println (" actionPerformed" );
148+ actionPerformed .countDown ();
149+ }
150+
151+ @ Override
152+ public void mouseReleased (MouseEvent e ) {
153+ if (e .getButton () == MouseEvent .BUTTON1 ) {
154+ System .out .println (" mouseReleased: button 1" );
155+ mouseButton1Released .countDown ();
156+ } else if (e .getButton () == MouseEvent .BUTTON3 ) {
157+ System .out .println (" mouseReleased: button 3" );
158+ mouseButton3Released .countDown ();
159+ }
160+ }
98161}
0 commit comments