1 /*
  2  * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25   @test
 26   @key headful
 27   @bug 4658741
 28   @summary verifies that getDropSuccess() returns correct value for inter-JVM DnD
 29   @run main InterJVMGetDropSuccessTest
 30 */
 31 
 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;
 58 
 59 public class InterJVMGetDropSuccessTest {
 60 
 61     private int returnCode = Util.CODE_NOT_RETURNED;
 62     private final boolean[] successCodes = { true, false };
 63     private int dropCount = 0;
 64 
 65     final Frame frame = new Frame("Target Frame");
 66 
 67     final DropTargetListener dropTargetListener = new DropTargetAdapter() {
 68             public void drop(DropTargetDropEvent dtde) {
 69                 dtde.acceptDrop(DnDConstants.ACTION_COPY);
 70                 dtde.dropComplete(successCodes[dropCount]);
 71                 dropCount++;
 72             }
 73         };
 74     final DropTarget dropTarget = new DropTarget(frame, dropTargetListener);
 75 
 76     public static void main(final String[] args) {
 77         InterJVMGetDropSuccessTest app = new InterJVMGetDropSuccessTest();
 78         app.init();
 79         app.start();
 80     }
 81 
 82     public void init() {
 83         frame.setTitle("Test frame");
 84         frame.setBounds(100, 100, 150, 150);
 85     } // init()
 86 
 87     public void start() {
 88 
 89         frame.setVisible(true);
 90 
 91         try {
 92             Robot robot = new Robot();
 93             robot.waitForIdle();
 94             robot.delay(Util.FRAME_ACTIVATION_TIMEOUT);
 95 
 96             Point p = frame.getLocationOnScreen();
 97             Dimension d = frame.getSize();
 98 
 99             String javaPath = System.getProperty("java.home", "");
100             String command = javaPath + File.separator + "bin" +
101                 File.separator + "java -cp " + System.getProperty("test.classes", ".") +
102                 " Child " +
103                 p.x + " " + p.y + " " + d.width + " " + d.height;
104 
105             Process process = Runtime.getRuntime().exec(command);
106             returnCode = process.waitFor();
107 
108             InputStream errorStream = process.getErrorStream();
109             int count = errorStream.available();
110             if (count > 0) {
111                 byte[] b = new byte[count];
112                 errorStream.read(b);
113                 System.err.println("========= Child VM System.err ========");
114                 System.err.print(new String(b));
115                 System.err.println("======================================");
116             }
117 
118             InputStream outputStream = process.getInputStream();
119             count = outputStream.available();
120             if (count > 0) {
121                 byte[] b = new byte[count];
122                 outputStream.read(b);
123                 System.err.println("========= Child VM System.out ========");
124                 System.err.print(new String(b));
125                 System.err.println("======================================");
126             }
127         } catch (Throwable e) {
128             e.printStackTrace();
129             throw new RuntimeException(e);
130         }
131         switch (returnCode) {
132         case Util.CODE_NOT_RETURNED:
133             throw new RuntimeException("Child VM: failed to start");
134         case Util.CODE_FAILURE:
135             throw new RuntimeException("Child VM: abnormal termination");
136         default:
137             if (dropCount == 2) {
138                 int expectedRetCode = 0;
139                 if (successCodes[0]) {
140                     expectedRetCode |= Util.CODE_FIRST_SUCCESS;
141                 }
142                 if (successCodes[1]) {
143                     expectedRetCode |= Util.CODE_SECOND_SUCCESS;
144                 }
145                 if (expectedRetCode != returnCode) {
146                     throw new RuntimeException("The test failed. Expected:" +
147                                                expectedRetCode + ". Returned:" +
148                                                returnCode);
149                 }
150             }
151             break;
152         }
153     } // start()
154 } // class InterJVMGetDropSuccessTest
155 
156 final class Util implements AWTEventListener {
157     public static final int CODE_NOT_RETURNED = -1;
158     public static final int CODE_FIRST_SUCCESS = 0x2;
159     public static final int CODE_SECOND_SUCCESS = 0x2;
160     public static final int CODE_FAILURE = 0x1;
161 
162     public static final int FRAME_ACTIVATION_TIMEOUT = 1000;
163 
164     static final Object SYNC_LOCK = new Object();
165 
166     static final Util theInstance = new Util();
167 
168     static {
169         Toolkit.getDefaultToolkit().addAWTEventListener(theInstance, AWTEvent.MOUSE_EVENT_MASK);
170     }
171 
172     public static Point getCenterLocationOnScreen(Component c) {
173         Point p = c.getLocationOnScreen();
174         Dimension d = c.getSize();
175         p.translate(d.width / 2, d.height / 2);
176         return p;
177     }
178 
179     public static int sign(int n) {
180         return n < 0 ? -1 : n == 0 ? 0 : 1;
181     }
182 
183     public void eventDispatched(AWTEvent e) {
184         if (e.getID() == MouseEvent.MOUSE_RELEASED) {
185             synchronized (SYNC_LOCK) {
186                 SYNC_LOCK.notifyAll();
187             }
188         }
189     }
190 }
191 
192 class Child {
193     static class DragSourceDropListener extends DragSourceAdapter {
194         private boolean finished = false;
195         private boolean dropSuccess = false;
196 
197         public void reset() {
198             finished = false;
199             dropSuccess = false;
200         }
201 
202         public boolean isDropFinished() {
203             return finished;
204         }
205 
206         public boolean getDropSuccess() {
207             return dropSuccess;
208         }
209 
210         public void dragDropEnd(DragSourceDropEvent dsde) {
211             finished = true;
212             dropSuccess = dsde.getDropSuccess();
213             synchronized (Util.SYNC_LOCK) {
214                 Util.SYNC_LOCK.notifyAll();
215             }
216         }
217     }
218 
219     private volatile boolean success1 = false;
220     private volatile boolean success2 = false;
221 
222     final Frame frame = new Frame("Source Frame");
223     final DragSource dragSource = DragSource.getDefaultDragSource();
224     final DragSourceDropListener dragSourceListener = new DragSourceDropListener();
225     final Transferable transferable = new StringSelection("TEXT");
226     final DragGestureListener dragGestureListener = new DragGestureListener() {
227             public void dragGestureRecognized(DragGestureEvent dge) {
228                 dge.startDrag(null, transferable, dragSourceListener);
229             }
230         };
231     final DragGestureRecognizer dragGestureRecognizer =
232         dragSource.createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_COPY,
233                                                       dragGestureListener);
234 
235     public static void main(String[] args) {
236         Child child = new Child();
237         child.run(args);
238     }
239 
240     public void run(String[] args) {
241         try {
242             if (args.length != 4) {
243                 throw new RuntimeException("Incorrect command line arguments.");
244             }
245 
246             int x = Integer.parseInt(args[0]);
247             int y = Integer.parseInt(args[1]);
248             int w = Integer.parseInt(args[2]);
249             int h = Integer.parseInt(args[3]);
250 
251             frame.setBounds(300, 200, 150, 150);
252             frame.setVisible(true);
253 
254             Robot robot = new Robot();
255             robot.waitForIdle();
256             robot.delay(Util.FRAME_ACTIVATION_TIMEOUT);
257 
258             Point sourcePoint = Util.getCenterLocationOnScreen(frame);
259             Point targetPoint = new Point(x + w / 2, y + h / 2);
260 
261             robot.mouseMove(sourcePoint.x, sourcePoint.y);
262             robot.waitForIdle();
263             robot.delay(50);
264             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
265             for (Point p = new Point(sourcePoint); !p.equals(targetPoint);
266                 p.translate(Util.sign(targetPoint.x - p.x),
267                             Util.sign(targetPoint.y - p.y))) {
268                 robot.mouseMove(p.x, p.y);
269                 robot.delay(5);
270             }
271 
272             synchronized (Util.SYNC_LOCK) {
273                 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
274                 Util.SYNC_LOCK.wait(Util.FRAME_ACTIVATION_TIMEOUT);
275             }
276 
277             EventQueue.invokeAndWait(() -> {
278                 if (!dragSourceListener.isDropFinished()) {
279                     throw new RuntimeException("Drop not finished");
280                 }
281                 success1 = dragSourceListener.getDropSuccess();
282                 dragSourceListener.reset();
283             });
284 
285 
286             robot.mouseMove(sourcePoint.x, sourcePoint.y);
287             robot.waitForIdle();
288             robot.delay(50);
289             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
290             for (Point p = new Point(sourcePoint); !p.equals(targetPoint);
291                 p.translate(Util.sign(targetPoint.x - p.x),
292                             Util.sign(targetPoint.y - p.y))) {
293                 robot.mouseMove(p.x, p.y);
294                 robot.delay(5);
295             }
296 
297             synchronized (Util.SYNC_LOCK) {
298                 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
299                 Util.SYNC_LOCK.wait(Util.FRAME_ACTIVATION_TIMEOUT);
300             }
301 
302             EventQueue.invokeAndWait(() -> {
303                 if (!dragSourceListener.isDropFinished()) {
304                     throw new RuntimeException("Drop not finished");
305                 }
306                 success2 = dragSourceListener.getDropSuccess();
307                 dragSourceListener.reset();
308             });
309 
310             int retCode = 0;
311 
312             if (success1) {
313                 retCode |= Util.CODE_FIRST_SUCCESS;
314             }
315             if (success2) {
316                 retCode |= Util.CODE_SECOND_SUCCESS;
317             }
318             // This returns the diagnostic code from the child VM
319             System.exit(retCode);
320         } catch (Throwable e) {
321             e.printStackTrace();
322             // This returns the diagnostic code from the child VM
323             System.exit(Util.CODE_FAILURE);
324         }
325     } // run()
326 } // class child