3636import java .awt .Point ;
3737import java .awt .Robot ;
3838
39- import java .awt .event .InputEvent ;
4039import java .awt .event .MouseAdapter ;
4140import java .awt .event .MouseEvent ;
4241
42+ import java .util .concurrent .CountDownLatch ;
43+ import java .util .concurrent .TimeUnit ;
44+
4345public class ListEnterExitTest {
4446 final List list = new List ();
45- final MouseEnterExitListener mouseEnterExitListener = new MouseEnterExitListener ();
4647 Frame frame ;
4748 volatile Point p ;
4849
50+ private static final int X_OFFSET = 30 ;
51+ private static final int Y_OFFSET = 40 ;
52+ private static final int LATCH_TIMEOUT = 3 ;
53+
54+ private final CountDownLatch mouseEnterLatch = new CountDownLatch (1 );
55+ private final CountDownLatch mouseExitLatch = new CountDownLatch (1 );
56+
4957 public static void main (String [] args ) throws Exception {
5058 ListEnterExitTest test = new ListEnterExitTest ();
5159 test .start ();
@@ -57,7 +65,11 @@ public void start() throws Exception {
5765 frame = new Frame ("ListEnterExitTest" );
5866 list .add ("Item 1" );
5967 list .add ("Item 2" );
60- list .addMouseListener (mouseEnterExitListener );
68+ list .add ("Item 3" );
69+ list .add ("Item 4" );
70+ list .add ("Item 5" );
71+ list .add ("Item 6" );
72+ list .addMouseListener (new MouseEnterExitListener ());
6173 frame .add (list );
6274 frame .setLayout (new FlowLayout ());
6375 frame .setSize (300 , 200 );
@@ -66,25 +78,28 @@ public void start() throws Exception {
6678 });
6779
6880 final Robot robot = new Robot ();
69- robot .delay ( 1000 );
81+ robot .setAutoDelay ( 100 );
7082 robot .waitForIdle ();
83+ robot .delay (1000 );
7184
7285 EventQueue .invokeAndWait (() -> {
7386 p = list .getLocationOnScreen ();
7487 });
75- robot .mouseMove (p .x + 10 , p .y + 10 );
76- robot .delay (100 );
88+ robot .mouseMove (p .x + X_OFFSET , p .y + Y_OFFSET );
7789 robot .waitForIdle ();
78- robot . mouseMove ( p . x - 10 , p . y - 10 );
79- robot .delay ( 100 );
90+
91+ robot .mouseMove ( p . x - X_OFFSET , p . y + Y_OFFSET );
8092 robot .waitForIdle ();
81- robot .mouseMove (p .x + 10 , p .y + 10 );
8293
83- robot .mousePress (InputEvent .BUTTON1_MASK );
84- robot .mouseRelease (InputEvent .BUTTON1_MASK );
94+ robot .mouseMove (p .x + X_OFFSET , p .y + Y_OFFSET );
95+ robot .waitForIdle ();
96+
97+ if (!mouseEnterLatch .await (LATCH_TIMEOUT , TimeUnit .SECONDS )) {
98+ throw new RuntimeException ("Mouse enter event timeout" );
99+ }
85100
86- synchronized ( mouseEnterExitListener ) {
87- mouseEnterExitListener . wait ( 2000 );
101+ if (! mouseExitLatch . await ( LATCH_TIMEOUT , TimeUnit . SECONDS ) ) {
102+ throw new RuntimeException ( "Mouse exit event timeout" );
88103 }
89104 } finally {
90105 EventQueue .invokeAndWait (() -> {
@@ -93,38 +108,19 @@ public void start() throws Exception {
93108 }
94109 });
95110 }
96- System .out .println ("mouseEnterExitListener.isPassed() : " + mouseEnterExitListener .isPassed ());
97- if (!mouseEnterExitListener .isPassed ()) {
98- throw new RuntimeException ("Haven't receive mouse enter/exit events" );
99- }
100-
101111 }
102112
103- }
104-
105- class MouseEnterExitListener extends MouseAdapter {
106-
107- volatile boolean passed_1 = false ;
108- volatile boolean passed_2 = false ;
109-
110- public void mouseEntered (MouseEvent e ) {
111- passed_1 = true ;
112- System .out .println ("passed_1 is: " + passed_1 );
113- }
114-
115- public void mouseExited (MouseEvent e ) {
116- passed_2 = true ;
117- System .out .println ("passed_2 is: " + passed_2 );
118- }
119-
120- public void mousePressed (MouseEvent e ) {
121- synchronized (this ) {
122- System .out .println ("mouse pressed" );
123- this .notifyAll ();
113+ private class MouseEnterExitListener extends MouseAdapter {
114+ @ Override
115+ public void mouseEntered (MouseEvent e ) {
116+ System .out .println ("Mouse Entered Event" );
117+ mouseEnterLatch .countDown ();
124118 }
125- }
126119
127- public boolean isPassed () {
128- return passed_1 & passed_2 ;
120+ @ Override
121+ public void mouseExited (MouseEvent e ) {
122+ System .out .println ("Mouse Exited Event" );
123+ mouseExitLatch .countDown ();
124+ }
129125 }
130126}
0 commit comments