1
1
/*
2
- * Copyright (c) 2014, 2018 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2014, 2024 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
29
29
@run main InterJVMGetDropSuccessTest
30
30
*/
31
31
32
- import java .awt .*;
33
- import java .awt .datatransfer .*;
34
- import java .awt .dnd .*;
35
- import java .awt .event .*;
36
- import java .io .*;
32
+ import java .awt .AWTEvent ;
33
+ import java .awt .Component ;
34
+ import java .awt .Dimension ;
35
+ import java .awt .EventQueue ;
36
+ import java .awt .Frame ;
37
+ import java .awt .Point ;
38
+ import java .awt .Robot ;
39
+ import java .awt .Toolkit ;
40
+ import java .awt .datatransfer .StringSelection ;
41
+ import java .awt .datatransfer .Transferable ;
42
+ import java .awt .dnd .DnDConstants ;
43
+ import java .awt .dnd .DragGestureEvent ;
44
+ import java .awt .dnd .DragGestureListener ;
45
+ import java .awt .dnd .DragGestureRecognizer ;
46
+ import java .awt .dnd .DragSource ;
47
+ import java .awt .dnd .DragSourceAdapter ;
48
+ import java .awt .dnd .DragSourceDropEvent ;
49
+ import java .awt .dnd .DropTarget ;
50
+ import java .awt .dnd .DropTargetAdapter ;
51
+ import java .awt .dnd .DropTargetDropEvent ;
52
+ import java .awt .dnd .DropTargetListener ;
53
+ import java .awt .event .AWTEventListener ;
54
+ import java .awt .event .InputEvent ;
55
+ import java .awt .event .MouseEvent ;
56
+ import java .io .File ;
57
+ import java .io .InputStream ;
37
58
38
59
public class InterJVMGetDropSuccessTest {
39
60
40
61
private int returnCode = Util .CODE_NOT_RETURNED ;
41
- private boolean successCodes [] = { true , false };
62
+ private final boolean [] successCodes = { true , false };
42
63
private int dropCount = 0 ;
43
64
44
65
final Frame frame = new Frame ("Target Frame" );
@@ -68,7 +89,9 @@ public void start() {
68
89
frame .setVisible (true );
69
90
70
91
try {
71
- Thread .sleep (Util .FRAME_ACTIVATION_TIMEOUT );
92
+ Robot robot = new Robot ();
93
+ robot .waitForIdle ();
94
+ robot .delay (Util .FRAME_ACTIVATION_TIMEOUT );
72
95
73
96
Point p = frame .getLocationOnScreen ();
74
97
Dimension d = frame .getSize ();
@@ -136,10 +159,9 @@ final class Util implements AWTEventListener {
136
159
public static final int CODE_SECOND_SUCCESS = 0x2 ;
137
160
public static final int CODE_FAILURE = 0x1 ;
138
161
139
- public static final int FRAME_ACTIVATION_TIMEOUT = 3000 ;
162
+ public static final int FRAME_ACTIVATION_TIMEOUT = 1000 ;
140
163
141
164
static final Object SYNC_LOCK = new Object ();
142
- static final int MOUSE_RELEASE_TIMEOUT = 1000 ;
143
165
144
166
static final Util theInstance = new Util ();
145
167
@@ -158,45 +180,13 @@ public static int sign(int n) {
158
180
return n < 0 ? -1 : n == 0 ? 0 : 1 ;
159
181
}
160
182
161
- private Component clickedComponent = null ;
162
-
163
- private void reset () {
164
- clickedComponent = null ;
165
- }
166
-
167
183
public void eventDispatched (AWTEvent e ) {
168
184
if (e .getID () == MouseEvent .MOUSE_RELEASED ) {
169
- clickedComponent = (Component )e .getSource ();
170
185
synchronized (SYNC_LOCK ) {
171
186
SYNC_LOCK .notifyAll ();
172
187
}
173
188
}
174
189
}
175
-
176
- public static boolean pointInComponent (Robot robot , Point p , Component comp )
177
- throws InterruptedException {
178
- return theInstance .pointInComponentImpl (robot , p , comp );
179
- }
180
-
181
- private boolean pointInComponentImpl (Robot robot , Point p , Component comp )
182
- throws InterruptedException {
183
- robot .waitForIdle ();
184
- reset ();
185
- robot .mouseMove (p .x , p .y );
186
- robot .mousePress (InputEvent .BUTTON1_MASK );
187
- synchronized (SYNC_LOCK ) {
188
- robot .mouseRelease (InputEvent .BUTTON1_MASK );
189
- SYNC_LOCK .wait (MOUSE_RELEASE_TIMEOUT );
190
- }
191
-
192
- Component c = clickedComponent ;
193
-
194
- while (c != null && c != comp ) {
195
- c = c .getParent ();
196
- }
197
-
198
- return c == comp ;
199
- }
200
190
}
201
191
202
192
class Child {
@@ -226,6 +216,9 @@ public void dragDropEnd(DragSourceDropEvent dsde) {
226
216
}
227
217
}
228
218
219
+ private volatile boolean success1 = false ;
220
+ private volatile boolean success2 = false ;
221
+
229
222
final Frame frame = new Frame ("Source Frame" );
230
223
final DragSource dragSource = DragSource .getDefaultDragSource ();
231
224
final DragSourceDropListener dragSourceListener = new DragSourceDropListener ();
@@ -258,53 +251,62 @@ public void run(String[] args) {
258
251
frame .setBounds (300 , 200 , 150 , 150 );
259
252
frame .setVisible (true );
260
253
261
- Thread .sleep (Util .FRAME_ACTIVATION_TIMEOUT );
254
+ Robot robot = new Robot ();
255
+ robot .waitForIdle ();
256
+ robot .delay (Util .FRAME_ACTIVATION_TIMEOUT );
262
257
263
258
Point sourcePoint = Util .getCenterLocationOnScreen (frame );
264
-
265
259
Point targetPoint = new Point (x + w / 2 , y + h / 2 );
266
260
267
- Robot robot = new Robot ();
268
261
robot .mouseMove (sourcePoint .x , sourcePoint .y );
269
- robot .mousePress (InputEvent .BUTTON1_MASK );
262
+ robot .waitForIdle ();
263
+ robot .delay (50 );
264
+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
270
265
for (Point p = new Point (sourcePoint ); !p .equals (targetPoint );
271
- p .translate (Util .sign (targetPoint .x - p .x ),
272
- Util .sign (targetPoint .y - p .y ))) {
266
+ p .translate (Util .sign (targetPoint .x - p .x ),
267
+ Util .sign (targetPoint .y - p .y ))) {
273
268
robot .mouseMove (p .x , p .y );
274
- Thread . sleep ( 50 );
269
+ robot . delay ( 5 );
275
270
}
276
271
277
272
synchronized (Util .SYNC_LOCK ) {
278
- robot .mouseRelease (InputEvent .BUTTON1_MASK );
273
+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
279
274
Util .SYNC_LOCK .wait (Util .FRAME_ACTIVATION_TIMEOUT );
280
275
}
281
276
282
- if (!dragSourceListener .isDropFinished ()) {
283
- throw new RuntimeException ("Drop not finished" );
284
- }
277
+ EventQueue .invokeAndWait (() -> {
278
+ if (!dragSourceListener .isDropFinished ()) {
279
+ throw new RuntimeException ("Drop not finished" );
280
+ }
281
+ success1 = dragSourceListener .getDropSuccess ();
282
+ dragSourceListener .reset ();
283
+ });
285
284
286
- boolean success1 = dragSourceListener .getDropSuccess ();
287
285
288
- dragSourceListener .reset ();
289
286
robot .mouseMove (sourcePoint .x , sourcePoint .y );
290
- robot .mousePress (InputEvent .BUTTON1_MASK );
287
+ robot .waitForIdle ();
288
+ robot .delay (50 );
289
+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
291
290
for (Point p = new Point (sourcePoint ); !p .equals (targetPoint );
292
- p .translate (Util .sign (targetPoint .x - p .x ),
293
- Util .sign (targetPoint .y - p .y ))) {
291
+ p .translate (Util .sign (targetPoint .x - p .x ),
292
+ Util .sign (targetPoint .y - p .y ))) {
294
293
robot .mouseMove (p .x , p .y );
295
- Thread . sleep ( 50 );
294
+ robot . delay ( 5 );
296
295
}
297
296
298
297
synchronized (Util .SYNC_LOCK ) {
299
- robot .mouseRelease (InputEvent .BUTTON1_MASK );
298
+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
300
299
Util .SYNC_LOCK .wait (Util .FRAME_ACTIVATION_TIMEOUT );
301
300
}
302
301
303
- if (!dragSourceListener .isDropFinished ()) {
304
- throw new RuntimeException ("Drop not finished" );
305
- }
302
+ EventQueue .invokeAndWait (() -> {
303
+ if (!dragSourceListener .isDropFinished ()) {
304
+ throw new RuntimeException ("Drop not finished" );
305
+ }
306
+ success2 = dragSourceListener .getDropSuccess ();
307
+ dragSourceListener .reset ();
308
+ });
306
309
307
- boolean success2 = dragSourceListener .getDropSuccess ();
308
310
int retCode = 0 ;
309
311
310
312
if (success1 ) {
0 commit comments