< prev index next >

test/jdk/sun/awt/dnd/8024061/bug8024061.java

Print this page
rev 58018 : 8238575: DragSourceEvent.getLocation() returns wrong value on HiDPI screens (Windows)
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 2014, 2018, 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 8024061
  28  * @summary Checks that no exception is thrown if dragGestureRecognized
  29  *          takes a while to complete.
  30  * @library /test/lib
  31  * @build jdk.test.lib.Platform
  32  * @run main bug8024061
  33  */
  34 import java.awt.*;









  35 import java.awt.datatransfer.DataFlavor;
  36 import java.awt.datatransfer.Transferable;
  37 import java.awt.datatransfer.UnsupportedFlavorException;
  38 import java.awt.dnd.DnDConstants;
  39 import java.awt.dnd.DragGestureEvent;
  40 import java.awt.dnd.DragGestureListener;
  41 import java.awt.dnd.DragSource;
  42 import java.awt.dnd.DragSourceDragEvent;
  43 import java.awt.dnd.DragSourceDropEvent;
  44 import java.awt.dnd.DragSourceEvent;
  45 import java.awt.dnd.DragSourceListener;
  46 import java.awt.dnd.DropTarget;
  47 import java.awt.dnd.DropTargetDragEvent;
  48 import java.awt.dnd.DropTargetDropEvent;
  49 import java.awt.dnd.DropTargetEvent;
  50 import java.awt.dnd.DropTargetListener;
  51 import java.awt.event.InputEvent;
  52 
  53 import java.io.IOException;
  54 import java.lang.reflect.InvocationTargetException;
  55 import java.util.concurrent.CountDownLatch;
  56 import java.util.concurrent.TimeUnit;
  57 
  58 import javax.swing.*;
  59 
  60 import jdk.test.lib.Platform;

  61 
  62 /**
  63  * If dragGestureRecognized() takes a while to complete and if user performs a drag quickly,
  64  * an exception is thrown from DropTargetListener.dragEnter when it calls
  65  * DropTargetDragEvent.getTransferable().
  66  * <p>
  67  * This class introduces a delay in dragGestureRecognized() to cause the exception.
  68  */
  69 public class bug8024061 {
  70     private static final DataFlavor DropObjectFlavor;
  71     private static final int DELAY = 1000;
  72 
  73     static final DnDPanel panel1 = new DnDPanel(Color.yellow);
  74     static final DnDPanel panel2 = new DnDPanel(Color.pink);
  75     private final JFrame frame;
  76     static Point here;
  77     static Point there;
  78     static Dimension d;
  79 
  80 


  90             e.printStackTrace();
  91         }
  92         DropObjectFlavor = flavor;
  93     }
  94 
  95     bug8024061() {
  96         frame = new JFrame("DnDWithRobot");
  97         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  98 
  99         d = new Dimension(100, 100);
 100 
 101         panel1.setPreferredSize(d);
 102         panel2.setPreferredSize(d);
 103 
 104         Container content = frame.getContentPane();
 105         content.setLayout(new GridLayout(1, 2, 5, 5));
 106         content.add(panel1);
 107         content.add(panel2);
 108 
 109         frame.pack();
 110 
 111         DropObject drop = new DropObject();
 112         drop.place(panel1, new Point(10, 10));
 113         frame.setVisible(true);
 114     }
 115 
 116     public static void main(String[] args) throws AWTException, InvocationTargetException, InterruptedException {
 117         if (!Platform.isLinux() && !Platform.isSolaris()) {
 118             System.out.println("This test is for Linux and Solaris only... " +
 119                                "skipping!");
 120             return;
 121         }
 122 
 123         final bug8024061[] dnd = {null};
 124         SwingUtilities.invokeAndWait(new Runnable() {
 125             @Override
 126             public void run() {
 127                 dnd[0] = new bug8024061();
 128             }
 129         });
 130         final Robot robot = new Robot();
 131         robot.setAutoDelay(10);
 132         robot.waitForIdle();
 133         robot.delay(200);
 134 
 135         JFrame frame = dnd[0].frame;
 136         SwingUtilities.invokeAndWait(() -> {
 137             here = panel1.getLocationOnScreen();
 138             there = panel2.getLocationOnScreen();
 139         });
 140         here.translate(d.width / 2, d.height / 2);
 141         there.translate(d.width / 2, d.height / 2);
 142         robot.mouseMove(here.x, here.y);


 149         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 150         robot.waitForIdle();
 151         robot.mousePress(InputEvent.BUTTON1_MASK);
 152         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 153         System.out.println("finished");
 154 
 155         try {
 156             if (lock.await(5, TimeUnit.SECONDS)) {
 157                 if (dragEnterException == null) {
 158                     System.out.println("Test passed.");
 159                 } else {
 160                     System.out.println("Test failed.");
 161                     dragEnterException.printStackTrace();
 162                     throw new RuntimeException(dragEnterException);
 163                 }
 164             } else {
 165                 System.out.println("Test failed. Timeout reached");
 166                 throw new RuntimeException("Timed out waiting for dragEnter()");
 167             }
 168         } finally {
 169             SwingUtilities.invokeLater(frame::dispose);
 170         }
 171     }
 172 
 173     class DropObject implements Transferable {
 174         DnDPanel panel;
 175         Color color = Color.CYAN;
 176         int width = 50;
 177         int height = 50;
 178         int x;
 179         int y;
 180 
 181         void draw(Graphics2D g) {
 182             Color savedColor = g.getColor();
 183             g.setColor(color);
 184             g.fillRect(x, y, width, height);
 185             g.setColor(Color.lightGray);
 186             g.drawRect(x, y, width, height);
 187             g.setColor(savedColor);
 188         }
 189 


   1 /*
   2  * Copyright (c) 2014, 2020, 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 8024061
  28  * @summary Checks that no exception is thrown if dragGestureRecognized
  29  *          takes a while to complete.



  30  */
  31 
  32 import java.awt.AWTException;
  33 import java.awt.Color;
  34 import java.awt.Container;
  35 import java.awt.Dimension;
  36 import java.awt.Graphics;
  37 import java.awt.Graphics2D;
  38 import java.awt.GridLayout;
  39 import java.awt.Point;
  40 import java.awt.Robot;
  41 import java.awt.datatransfer.DataFlavor;
  42 import java.awt.datatransfer.Transferable;
  43 import java.awt.datatransfer.UnsupportedFlavorException;
  44 import java.awt.dnd.DnDConstants;
  45 import java.awt.dnd.DragGestureEvent;
  46 import java.awt.dnd.DragGestureListener;
  47 import java.awt.dnd.DragSource;
  48 import java.awt.dnd.DragSourceDragEvent;
  49 import java.awt.dnd.DragSourceDropEvent;
  50 import java.awt.dnd.DragSourceEvent;
  51 import java.awt.dnd.DragSourceListener;
  52 import java.awt.dnd.DropTarget;
  53 import java.awt.dnd.DropTargetDragEvent;
  54 import java.awt.dnd.DropTargetDropEvent;
  55 import java.awt.dnd.DropTargetEvent;
  56 import java.awt.dnd.DropTargetListener;
  57 import java.awt.event.InputEvent;

  58 import java.io.IOException;
  59 import java.lang.reflect.InvocationTargetException;
  60 import java.util.concurrent.CountDownLatch;
  61 import java.util.concurrent.TimeUnit;
  62 
  63 import javax.swing.JFrame;
  64 import javax.swing.JPanel;
  65 import javax.swing.SwingUtilities;
  66 import javax.swing.WindowConstants;
  67 
  68 /**
  69  * If dragGestureRecognized() takes a while to complete and if user performs a drag quickly,
  70  * an exception is thrown from DropTargetListener.dragEnter when it calls
  71  * DropTargetDragEvent.getTransferable().
  72  * <p>
  73  * This class introduces a delay in dragGestureRecognized() to cause the exception.
  74  */
  75 public class bug8024061 {
  76     private static final DataFlavor DropObjectFlavor;
  77     private static final int DELAY = 1000;
  78 
  79     static final DnDPanel panel1 = new DnDPanel(Color.yellow);
  80     static final DnDPanel panel2 = new DnDPanel(Color.pink);
  81     private final JFrame frame;
  82     static Point here;
  83     static Point there;
  84     static Dimension d;
  85 
  86 


  96             e.printStackTrace();
  97         }
  98         DropObjectFlavor = flavor;
  99     }
 100 
 101     bug8024061() {
 102         frame = new JFrame("DnDWithRobot");
 103         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 104 
 105         d = new Dimension(100, 100);
 106 
 107         panel1.setPreferredSize(d);
 108         panel2.setPreferredSize(d);
 109 
 110         Container content = frame.getContentPane();
 111         content.setLayout(new GridLayout(1, 2, 5, 5));
 112         content.add(panel1);
 113         content.add(panel2);
 114 
 115         frame.pack();
 116         frame.setLocationRelativeTo(null);
 117         DropObject drop = new DropObject();
 118         drop.place(panel1, new Point(10, 10));
 119         frame.setVisible(true);
 120     }
 121 
 122     public static void main(String[] args) throws AWTException, InvocationTargetException, InterruptedException {






 123         final bug8024061[] dnd = {null};
 124         SwingUtilities.invokeAndWait(new Runnable() {
 125             @Override
 126             public void run() {
 127                 dnd[0] = new bug8024061();
 128             }
 129         });
 130         final Robot robot = new Robot();
 131         robot.setAutoDelay(10);
 132         robot.waitForIdle();
 133         robot.delay(200);
 134 
 135         JFrame frame = dnd[0].frame;
 136         SwingUtilities.invokeAndWait(() -> {
 137             here = panel1.getLocationOnScreen();
 138             there = panel2.getLocationOnScreen();
 139         });
 140         here.translate(d.width / 2, d.height / 2);
 141         there.translate(d.width / 2, d.height / 2);
 142         robot.mouseMove(here.x, here.y);


 149         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 150         robot.waitForIdle();
 151         robot.mousePress(InputEvent.BUTTON1_MASK);
 152         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 153         System.out.println("finished");
 154 
 155         try {
 156             if (lock.await(5, TimeUnit.SECONDS)) {
 157                 if (dragEnterException == null) {
 158                     System.out.println("Test passed.");
 159                 } else {
 160                     System.out.println("Test failed.");
 161                     dragEnterException.printStackTrace();
 162                     throw new RuntimeException(dragEnterException);
 163                 }
 164             } else {
 165                 System.out.println("Test failed. Timeout reached");
 166                 throw new RuntimeException("Timed out waiting for dragEnter()");
 167             }
 168         } finally {
 169             SwingUtilities.invokeAndWait(frame::dispose);
 170         }
 171     }
 172 
 173     class DropObject implements Transferable {
 174         DnDPanel panel;
 175         Color color = Color.CYAN;
 176         int width = 50;
 177         int height = 50;
 178         int x;
 179         int y;
 180 
 181         void draw(Graphics2D g) {
 182             Color savedColor = g.getColor();
 183             g.setColor(color);
 184             g.fillRect(x, y, width, height);
 185             g.setColor(Color.lightGray);
 186             g.drawRect(x, y, width, height);
 187             g.setColor(savedColor);
 188         }
 189 


< prev index next >