2121 * questions.
2222 */
2323
24- import javax .swing .JFrame ;
2524import java .awt .AWTEvent ;
2625import java .awt .Component ;
2726import java .awt .EventQueue ;
2827import java .awt .Frame ;
2928import java .awt .Panel ;
3029import java .awt .Point ;
30+ import java .awt .Rectangle ;
3131import java .awt .Robot ;
32+ import java .awt .Toolkit ;
3233import java .awt .datatransfer .StringSelection ;
3334import java .awt .dnd .DnDConstants ;
34- import java .awt .dnd .DragGestureEvent ;
3535import java .awt .dnd .DragGestureListener ;
3636import java .awt .dnd .DragSource ;
3737import java .awt .dnd .DragSourceAdapter ;
4343import java .awt .dnd .DropTargetDropEvent ;
4444import java .awt .dnd .DropTargetListener ;
4545import java .awt .event .AWTEventListener ;
46- import java .awt .event .MouseEvent ;
4746import java .awt .event .InputEvent ;
4847import java .awt .event .KeyEvent ;
48+ import java .awt .event .MouseEvent ;
49+ import java .io .File ;
50+ import java .util .concurrent .atomic .AtomicReference ;
51+ import javax .imageio .ImageIO ;
52+ import javax .swing .JFrame ;
4953
5054/*
5155 @test
5660*/
5761
5862public class DropActionChangeTest extends JFrame implements AWTEventListener {
59- Robot robot ;
60- Frame frame ;
63+ private static Robot robot ;
64+ private static Frame frame ;
65+ private static volatile DropActionChangeTest test ;
6166 Panel panel ;
6267 private volatile boolean failed ;
6368 private volatile boolean dropEnd ;
6469 private volatile Component clickedComponent ;
6570 private final Object LOCK = new Object ();
66- static final int FRAME_ACTIVATION_TIMEOUT = 3000 ;
67- static final int DROP_COMPLETION_TIMEOUT = 5000 ;
71+ static final int DROP_COMPLETION_TIMEOUT = 8000 ;
6872 static final int MOUSE_RELEASE_TIMEOUT = 2000 ;
6973
7074 public static void main (String [] args ) throws Exception {
71- DropActionChangeTest test = new DropActionChangeTest ();
75+ EventQueue . invokeAndWait (() -> test = new DropActionChangeTest () );
7276 EventQueue .invokeAndWait (test ::init );
7377 try {
7478 test .start ();
7579 } finally {
76- EventQueue .invokeAndWait (() -> {
77- if (test .frame != null ) {
78- test .frame .dispose ();
79- }
80- });
80+ EventQueue .invokeAndWait (DropActionChangeTest ::disposeFrame );
8181 }
8282 }
8383
@@ -97,10 +97,12 @@ public void init() {
9797
9898 final DragSourceListener dsl = new DragSourceAdapter () {
9999 public void dragDropEnd (DragSourceDropEvent e ) {
100- System .err .println ("DragSourseListener .dragDropEnd(): " +
100+ System .err .println ("DragSourceListener .dragDropEnd(): " +
101101 "drop action=" + e .getDropAction ());
102102 if (e .getDropAction () != DnDConstants .ACTION_MOVE ) {
103- System .err .println ("FAILURE: wrong drop action:" + e .getDropAction ());
103+ System .err .println ("FAILURE: wrong drop action:"
104+ + e .getDropAction () + ", It should be "
105+ + DnDConstants .ACTION_MOVE );
104106 failed = true ;
105107 }
106108 synchronized (LOCK ) {
@@ -110,11 +112,7 @@ public void dragDropEnd(DragSourceDropEvent e) {
110112 }
111113 };
112114
113- DragGestureListener dgl = new DragGestureListener () {
114- public void dragGestureRecognized (DragGestureEvent dge ) {
115- dge .startDrag (null , new StringSelection ("test" ), dsl );
116- }
117- };
115+ DragGestureListener dgl = dge -> dge .startDrag (null , new StringSelection ("test" ), dsl );
118116
119117 new DragSource ().createDefaultDragGestureRecognizer (panel ,
120118 DnDConstants .ACTION_COPY_OR_MOVE , dgl );
@@ -142,11 +140,25 @@ public void drop(DropTargetDropEvent e) {
142140 frame .setVisible (true );
143141 }
144142
143+ private static void disposeFrame () {
144+ if (frame != null ) {
145+ frame .dispose ();
146+ }
147+ if (test != null ) {
148+ test .dispose ();
149+ }
150+ }
151+
145152 public void start () {
146153 try {
147154 robot = new Robot ();
155+ robot .setAutoDelay (100 );
156+ robot .waitForIdle ();
157+ robot .delay (500 );
148158
149- Point startPoint = panel .getLocationOnScreen ();
159+ AtomicReference <Point > startPointRef = new AtomicReference <>();
160+ EventQueue .invokeAndWait (()-> startPointRef .set (panel .getLocationOnScreen ()));
161+ Point startPoint = startPointRef .get ();
150162 startPoint .translate (50 , 50 );
151163
152164 if (!pointInComponent (robot , startPoint , panel )) {
@@ -163,26 +175,41 @@ public void start() {
163175 synchronized (LOCK ) {
164176 robot .keyPress (KeyEvent .VK_CONTROL );
165177 robot .mouseMove (startPoint .x , startPoint .y );
166- robot .mousePress (InputEvent .BUTTON1_MASK );
178+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
167179 Util .doDrag (robot , startPoint , medPoint );
180+ robot .delay (500 );
168181 robot .keyRelease (KeyEvent .VK_CONTROL );
169182 Util .doDrag (robot , medPoint , endPoint );
170- robot .mouseRelease (InputEvent .BUTTON1_MASK );
183+ robot .delay (500 );
184+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
171185 LOCK .wait (DROP_COMPLETION_TIMEOUT );
172186 }
173187 if (!dropEnd ) {
174- System .err .println ("DragSourseListener.dragDropEnd() was not called, returning" );
188+ captureScreen ("No_Drop_End_" );
189+ System .err .println ("DragSourceListener.dragDropEnd() was not called, returning" );
175190 return ;
176191 }
177192 } catch (Throwable e ) {
178193 e .printStackTrace ();
179194 }
180195
181196 if (failed ) {
182- throw new RuntimeException ("wrong drop action!" );
197+ captureScreen ("Wrong_Drop_Action_" );
198+ throw new RuntimeException ("Wrong drop action!" );
183199 }
184200
185- System .err .println ("test passed!" );
201+ System .err .println ("Test passed!" );
202+ }
203+
204+ private static void captureScreen (String str ) {
205+ try {
206+ final Rectangle screenBounds = new Rectangle (
207+ Toolkit .getDefaultToolkit ().getScreenSize ());
208+ ImageIO .write (robot .createScreenCapture (screenBounds ), "png" ,
209+ new File (str + "Failure_Screen.png" ));
210+ } catch (Exception e ) {
211+ e .printStackTrace ();
212+ }
186213 }
187214
188215 public void reset () {
@@ -203,9 +230,9 @@ boolean pointInComponent(Robot robot, Point p, Component comp)
203230 robot .waitForIdle ();
204231 reset ();
205232 robot .mouseMove (p .x , p .y );
206- robot .mousePress (InputEvent .BUTTON1_MASK );
233+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
207234 synchronized (LOCK ) {
208- robot .mouseRelease (InputEvent .BUTTON1_MASK );
235+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
209236 LOCK .wait (MOUSE_RELEASE_TIMEOUT );
210237 }
211238
@@ -227,15 +254,11 @@ public static int sign(int n) {
227254 }
228255
229256 public static void doDrag (Robot robot , Point startPoint , Point endPoint ) {
257+ robot .waitForIdle ();
230258 for (Point p = new Point (startPoint ); !p .equals (endPoint );
231259 p .translate (Util .sign (endPoint .x - p .x ),
232260 Util .sign (endPoint .y - p .y ))) {
233261 robot .mouseMove (p .x , p .y );
234- try {
235- Thread .sleep (100 );
236- } catch (InterruptedException e ) {
237- e .printStackTrace ();
238- }
239262 }
240263 }
241- }
264+ }
0 commit comments