< prev index next >

test/jdk/java/awt/dnd/InterJVMGetDropSuccessTest/InterJVMGetDropSuccessTest.java

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

@@ -27,20 +27,41 @@
    @bug 4658741
    @summary verifies that getDropSuccess() returns correct value for inter-JVM DnD
    @run main InterJVMGetDropSuccessTest
  */
  
- import java.awt.*;
- import java.awt.datatransfer.*;
- import java.awt.dnd.*;
- import java.awt.event.*;
- import java.io.*;
+ import java.awt.AWTEvent;
+ import java.awt.Component;
+ import java.awt.Dimension;
+ import java.awt.EventQueue;
+ import java.awt.Frame;
+ import java.awt.Point;
+ import java.awt.Robot;
+ import java.awt.Toolkit;
+ import java.awt.datatransfer.StringSelection;
+ import java.awt.datatransfer.Transferable;
+ import java.awt.dnd.DnDConstants;
+ import java.awt.dnd.DragGestureEvent;
+ import java.awt.dnd.DragGestureListener;
+ import java.awt.dnd.DragGestureRecognizer;
+ import java.awt.dnd.DragSource;
+ import java.awt.dnd.DragSourceAdapter;
+ import java.awt.dnd.DragSourceDropEvent;
+ import java.awt.dnd.DropTarget;
+ import java.awt.dnd.DropTargetAdapter;
+ import java.awt.dnd.DropTargetDropEvent;
+ import java.awt.dnd.DropTargetListener;
+ import java.awt.event.AWTEventListener;
+ import java.awt.event.InputEvent;
+ import java.awt.event.MouseEvent;
+ import java.io.File;
+ import java.io.InputStream;
  
  public class InterJVMGetDropSuccessTest {
  
      private int returnCode = Util.CODE_NOT_RETURNED;
-     private boolean successCodes[] = { true, false };
+     private final boolean[] successCodes = { true, false };
      private int dropCount = 0;
  
      final Frame frame = new Frame("Target Frame");
  
      final DropTargetListener dropTargetListener = new DropTargetAdapter() {

@@ -66,11 +87,13 @@
      public void start() {
  
          frame.setVisible(true);
  
          try {
-             Thread.sleep(Util.FRAME_ACTIVATION_TIMEOUT);
+             Robot robot = new Robot();
+             robot.waitForIdle();
+             robot.delay(Util.FRAME_ACTIVATION_TIMEOUT);
  
              Point p = frame.getLocationOnScreen();
              Dimension d = frame.getSize();
  
              String javaPath = System.getProperty("java.home", "");

@@ -134,14 +157,13 @@
      public static final int CODE_NOT_RETURNED = -1;
      public static final int CODE_FIRST_SUCCESS = 0x2;
      public static final int CODE_SECOND_SUCCESS = 0x2;
      public static final int CODE_FAILURE = 0x1;
  
-     public static final int FRAME_ACTIVATION_TIMEOUT = 3000;
+     public static final int FRAME_ACTIVATION_TIMEOUT = 1000;
  
      static final Object SYNC_LOCK = new Object();
-     static final int MOUSE_RELEASE_TIMEOUT = 1000;
  
      static final Util theInstance = new Util();
  
      static {
          Toolkit.getDefaultToolkit().addAWTEventListener(theInstance, AWTEvent.MOUSE_EVENT_MASK);

@@ -156,49 +178,17 @@
  
      public static int sign(int n) {
          return n < 0 ? -1 : n == 0 ? 0 : 1;
      }
  
-     private Component clickedComponent = null;
- 
-     private void reset() {
-         clickedComponent = null;
-     }
- 
      public void eventDispatched(AWTEvent e) {
          if (e.getID() == MouseEvent.MOUSE_RELEASED) {
-             clickedComponent = (Component)e.getSource();
              synchronized (SYNC_LOCK) {
                  SYNC_LOCK.notifyAll();
              }
          }
      }
- 
-     public static boolean pointInComponent(Robot robot, Point p, Component comp)
-       throws InterruptedException {
-         return theInstance.pointInComponentImpl(robot, p, comp);
-     }
- 
-     private boolean pointInComponentImpl(Robot robot, Point p, Component comp)
-       throws InterruptedException {
-         robot.waitForIdle();
-         reset();
-         robot.mouseMove(p.x, p.y);
-         robot.mousePress(InputEvent.BUTTON1_MASK);
-         synchronized (SYNC_LOCK) {
-             robot.mouseRelease(InputEvent.BUTTON1_MASK);
-             SYNC_LOCK.wait(MOUSE_RELEASE_TIMEOUT);
-         }
- 
-         Component c = clickedComponent;
- 
-         while (c != null && c != comp) {
-             c = c.getParent();
-         }
- 
-         return c == comp;
-     }
  }
  
  class Child {
      static class DragSourceDropListener extends DragSourceAdapter {
          private boolean finished = false;

@@ -224,10 +214,13 @@
                  Util.SYNC_LOCK.notifyAll();
              }
          }
      }
  
+     private volatile boolean success1 = false;
+     private volatile boolean success2 = false;
+ 
      final Frame frame = new Frame("Source Frame");
      final DragSource dragSource = DragSource.getDefaultDragSource();
      final DragSourceDropListener dragSourceListener = new DragSourceDropListener();
      final Transferable transferable = new StringSelection("TEXT");
      final DragGestureListener dragGestureListener = new DragGestureListener() {

@@ -256,57 +249,66 @@
              int h = Integer.parseInt(args[3]);
  
              frame.setBounds(300, 200, 150, 150);
              frame.setVisible(true);
  
-             Thread.sleep(Util.FRAME_ACTIVATION_TIMEOUT);
+             Robot robot = new Robot();
+             robot.waitForIdle();
+             robot.delay(Util.FRAME_ACTIVATION_TIMEOUT);
  
              Point sourcePoint = Util.getCenterLocationOnScreen(frame);
- 
              Point targetPoint = new Point(x + w / 2, y + h / 2);
  
-             Robot robot = new Robot();
              robot.mouseMove(sourcePoint.x, sourcePoint.y);
-             robot.mousePress(InputEvent.BUTTON1_MASK);
+             robot.waitForIdle();
+             robot.delay(50);
+             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
              for (Point p = new Point(sourcePoint); !p.equals(targetPoint);
-                  p.translate(Util.sign(targetPoint.x - p.x),
-                              Util.sign(targetPoint.y - p.y))) {
+                 p.translate(Util.sign(targetPoint.x - p.x),
+                             Util.sign(targetPoint.y - p.y))) {
                  robot.mouseMove(p.x, p.y);
-                 Thread.sleep(50);
+                 robot.delay(5);
              }
  
              synchronized (Util.SYNC_LOCK) {
-                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
+                 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                  Util.SYNC_LOCK.wait(Util.FRAME_ACTIVATION_TIMEOUT);
              }
  
-             if (!dragSourceListener.isDropFinished()) {
-                 throw new RuntimeException("Drop not finished");
-             }
+             EventQueue.invokeAndWait(() -> {
+                 if (!dragSourceListener.isDropFinished()) {
+                     throw new RuntimeException("Drop not finished");
+                 }
+                 success1 = dragSourceListener.getDropSuccess();
+                 dragSourceListener.reset();
+             });
  
-             boolean success1 = dragSourceListener.getDropSuccess();
  
-             dragSourceListener.reset();
              robot.mouseMove(sourcePoint.x, sourcePoint.y);
-             robot.mousePress(InputEvent.BUTTON1_MASK);
+             robot.waitForIdle();
+             robot.delay(50);
+             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
              for (Point p = new Point(sourcePoint); !p.equals(targetPoint);
-                  p.translate(Util.sign(targetPoint.x - p.x),
-                              Util.sign(targetPoint.y - p.y))) {
+                 p.translate(Util.sign(targetPoint.x - p.x),
+                             Util.sign(targetPoint.y - p.y))) {
                  robot.mouseMove(p.x, p.y);
-                 Thread.sleep(50);
+                 robot.delay(5);
              }
  
              synchronized (Util.SYNC_LOCK) {
-                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
+                 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                  Util.SYNC_LOCK.wait(Util.FRAME_ACTIVATION_TIMEOUT);
              }
  
-             if (!dragSourceListener.isDropFinished()) {
-                 throw new RuntimeException("Drop not finished");
-             }
+             EventQueue.invokeAndWait(() -> {
+                 if (!dragSourceListener.isDropFinished()) {
+                     throw new RuntimeException("Drop not finished");
+                 }
+                 success2 = dragSourceListener.getDropSuccess();
+                 dragSourceListener.reset();
+             });
  
-             boolean success2 = dragSourceListener.getDropSuccess();
              int retCode = 0;
  
              if (success1) {
                  retCode |= Util.CODE_FIRST_SUCCESS;
              }
< prev index next >