< prev index next >

test/jdk/java/awt/dnd/Button2DragTest/Button2DragTest.java

Print this page
rev 60071 : 8211999: Window positioning bugs due to overlapping GraphicsDevice bounds (Windows/HiDPI)
Reviewed-by: XXX


   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 import java.awt.Color;
  25 import java.awt.Frame;
  26 import java.awt.GraphicsConfiguration;
  27 import java.awt.GraphicsDevice;
  28 import java.awt.GraphicsEnvironment;
  29 import java.awt.Point;

  30 import java.awt.Robot;
  31 import java.awt.datatransfer.StringSelection;
  32 import java.awt.dnd.DnDConstants;
  33 import java.awt.dnd.DragGestureEvent;
  34 import java.awt.dnd.DragGestureListener;
  35 import java.awt.dnd.DragSource;
  36 import java.awt.dnd.DragSourceDragEvent;
  37 import java.awt.dnd.DragSourceDropEvent;
  38 import java.awt.dnd.DragSourceEvent;
  39 import java.awt.dnd.DragSourceListener;
  40 import java.awt.dnd.DropTarget;
  41 import java.awt.dnd.DropTargetAdapter;
  42 import java.awt.dnd.DropTargetDropEvent;
  43 import java.awt.event.InputEvent;
  44 
  45 import test.java.awt.regtesthelpers.Util;
  46 
  47 /**
  48  * @test
  49  * @key headful
  50  * @bug 4955110 8238575
  51  * @summary tests that DragSourceDragEvent.getDropAction() accords to its new
  52  *          spec (does not depend on the user drop action)
  53  * @library ../../regtesthelpers
  54  * @build Util
  55  * @run main/othervm Button2DragTest
  56  * @author Alexander.Gerasimov area=dnd
  57  */
  58 public final class Button2DragTest {
  59 

  60     private volatile boolean dropSuccess;
  61     private volatile boolean locationValid = true;
  62 
  63     private static Frame frame;
  64 
  65     public static void main(final String[] args) {
  66         var lge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  67         for (GraphicsDevice device : lge.getScreenDevices()) {
  68             Button2DragTest test = new Button2DragTest();
  69             frame = new Frame(device.getDefaultConfiguration());
  70             try {
  71                 test.run();
  72             } finally {
  73                 frame.dispose();
  74             }
  75         }
  76     }
  77 
  78     public void run() {
  79         final DragSourceListener dragSourceListener = new DragSourceListener() {
  80             private void checkLocation(DragSourceEvent dsde) {
  81                 if (!frame.getBounds().contains(dsde.getLocation())) {
  82                     System.err.println("Expected in" + frame.getBounds());
  83                     System.err.println("Actual" + dsde.getLocation());
  84                     locationValid = false;
  85                 }
  86             }
  87 
  88             @Override
  89             public void dragEnter(DragSourceDragEvent dsde) {
  90                 checkLocation(dsde);
  91             }
  92 
  93             @Override
  94             public void dragOver(DragSourceDragEvent dsde) {
  95                 checkLocation(dsde);
  96             }
  97 
  98             @Override
  99             public void dropActionChanged(DragSourceDragEvent dsde) {
 100                 checkLocation(dsde);
 101             }
 102 
 103             @Override


 113         };
 114         DragGestureListener dragGestureListener = new DragGestureListener() {
 115             public void dragGestureRecognized(DragGestureEvent dge) {
 116                 dge.startDrag(null, new StringSelection("OK"), dragSourceListener);
 117             }
 118         };
 119         new DragSource().createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_MOVE,
 120                                                             dragGestureListener);
 121 
 122         DropTargetAdapter dropTargetListener = new DropTargetAdapter() {
 123             public void drop(DropTargetDropEvent dtde) {
 124                 dtde.acceptDrop(DnDConstants.ACTION_MOVE);
 125                 dtde.dropComplete(true);
 126                 System.err.println("Drop");
 127             }
 128         };
 129         new DropTarget(frame, dropTargetListener);
 130 
 131         frame.setBackground(Color.GREEN);
 132         frame.setUndecorated(true);
 133         frame.setSize(200, 200);
 134         frame.setLocationRelativeTo(null);


 135         frame.setVisible(true);
 136 
 137         Robot robot = Util.createRobot();
 138 
 139         Util.waitForIdle(robot);
 140 
 141         Point startPoint = frame.getLocationOnScreen();
 142         Point endPoint = new Point(startPoint);
 143         startPoint.translate(50, 50);
 144         endPoint.translate(150, 150);
 145 
 146         Util.drag(robot, startPoint, endPoint, InputEvent.BUTTON2_MASK);
 147 
 148         Util.waitForIdle(robot);
 149         robot.delay(500);
 150 
 151         if (!dropSuccess || !locationValid) {
 152             throw new RuntimeException("test failed: drop was not successful");
 153         }
 154     }


   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 import java.awt.Color;
  25 import java.awt.Frame;

  26 import java.awt.GraphicsDevice;
  27 import java.awt.GraphicsEnvironment;
  28 import java.awt.Point;
  29 import java.awt.Rectangle;
  30 import java.awt.Robot;
  31 import java.awt.datatransfer.StringSelection;
  32 import java.awt.dnd.DnDConstants;
  33 import java.awt.dnd.DragGestureEvent;
  34 import java.awt.dnd.DragGestureListener;
  35 import java.awt.dnd.DragSource;
  36 import java.awt.dnd.DragSourceDragEvent;
  37 import java.awt.dnd.DragSourceDropEvent;
  38 import java.awt.dnd.DragSourceEvent;
  39 import java.awt.dnd.DragSourceListener;
  40 import java.awt.dnd.DropTarget;
  41 import java.awt.dnd.DropTargetAdapter;
  42 import java.awt.dnd.DropTargetDropEvent;
  43 import java.awt.event.InputEvent;
  44 
  45 import test.java.awt.regtesthelpers.Util;
  46 
  47 /**
  48  * @test
  49  * @key headful
  50  * @bug 4955110 8238575 8211999
  51  * @summary tests that DragSourceDragEvent.getDropAction() accords to its new
  52  *          spec (does not depend on the user drop action)
  53  * @library ../../regtesthelpers
  54  * @build Util
  55  * @run main/othervm Button2DragTest
  56  * @author Alexander.Gerasimov area=dnd
  57  */
  58 public final class Button2DragTest {
  59 
  60     private static final int SIZE = 200;
  61     private volatile boolean dropSuccess;
  62     private volatile boolean locationValid = true;
  63 
  64     private static Frame frame;
  65 
  66     public static void main(final String[] args) {
  67         var lge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  68         for (GraphicsDevice device : lge.getScreenDevices()) {
  69             Button2DragTest test = new Button2DragTest();
  70             frame = new Frame(device.getDefaultConfiguration());
  71             try {
  72                 test.run();
  73             } finally {
  74                 frame.dispose();
  75             }
  76         }
  77     }
  78 
  79     public void run() {
  80         final DragSourceListener dragSourceListener = new DragSourceListener() {
  81             private void checkLocation(DragSourceEvent dsde) {
  82                 if (!frame.getBounds().contains(dsde.getLocation())) {
  83                     System.err.println("Expected in: " + frame.getBounds());
  84                     System.err.println("Actual: " + dsde.getLocation());
  85                     locationValid = false;
  86                 }
  87             }
  88 
  89             @Override
  90             public void dragEnter(DragSourceDragEvent dsde) {
  91                 checkLocation(dsde);
  92             }
  93 
  94             @Override
  95             public void dragOver(DragSourceDragEvent dsde) {
  96                 checkLocation(dsde);
  97             }
  98 
  99             @Override
 100             public void dropActionChanged(DragSourceDragEvent dsde) {
 101                 checkLocation(dsde);
 102             }
 103 
 104             @Override


 114         };
 115         DragGestureListener dragGestureListener = new DragGestureListener() {
 116             public void dragGestureRecognized(DragGestureEvent dge) {
 117                 dge.startDrag(null, new StringSelection("OK"), dragSourceListener);
 118             }
 119         };
 120         new DragSource().createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_MOVE,
 121                                                             dragGestureListener);
 122 
 123         DropTargetAdapter dropTargetListener = new DropTargetAdapter() {
 124             public void drop(DropTargetDropEvent dtde) {
 125                 dtde.acceptDrop(DnDConstants.ACTION_MOVE);
 126                 dtde.dropComplete(true);
 127                 System.err.println("Drop");
 128             }
 129         };
 130         new DropTarget(frame, dropTargetListener);
 131 
 132         frame.setBackground(Color.GREEN);
 133         frame.setUndecorated(true);
 134         Rectangle screen = frame.getGraphicsConfiguration().getBounds();
 135         int x = (int) (screen.getCenterX() - SIZE / 2);
 136         int y = (int) (screen.getCenterY() - SIZE / 2);
 137         frame.setBounds(x, y, SIZE, SIZE);
 138         frame.setVisible(true);
 139 
 140         Robot robot = Util.createRobot();
 141 
 142         Util.waitForIdle(robot);
 143 
 144         Point startPoint = frame.getLocationOnScreen();
 145         Point endPoint = new Point(startPoint);
 146         startPoint.translate(50, 50);
 147         endPoint.translate(150, 150);
 148 
 149         Util.drag(robot, startPoint, endPoint, InputEvent.BUTTON2_MASK);
 150 
 151         Util.waitForIdle(robot);
 152         robot.delay(500);
 153 
 154         if (!dropSuccess || !locationValid) {
 155             throw new RuntimeException("test failed: drop was not successful");
 156         }
 157     }
< prev index next >