Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/awt/dnd/8024061/bug8024061.java
38855 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 802406125* @summary Checks that no exception is thrown if dragGestureRecognized26* takes a while to complete.27* @library ../../../../lib/testlibrary28* @build jdk.testlibrary.OSInfo29* @run main bug802406130*/31import java.awt.*;32import java.awt.datatransfer.DataFlavor;33import java.awt.datatransfer.Transferable;34import java.awt.datatransfer.UnsupportedFlavorException;35import java.awt.dnd.DnDConstants;36import java.awt.dnd.DragGestureEvent;37import java.awt.dnd.DragGestureListener;38import java.awt.dnd.DragSource;39import java.awt.dnd.DragSourceDragEvent;40import java.awt.dnd.DragSourceDropEvent;41import java.awt.dnd.DragSourceEvent;42import java.awt.dnd.DragSourceListener;43import java.awt.dnd.DropTarget;44import java.awt.dnd.DropTargetDragEvent;45import java.awt.dnd.DropTargetDropEvent;46import java.awt.dnd.DropTargetEvent;47import java.awt.dnd.DropTargetListener;48import java.awt.event.InputEvent;4950import java.io.IOException;51import java.lang.reflect.InvocationTargetException;52import java.util.concurrent.CountDownLatch;53import java.util.concurrent.TimeUnit;5455import javax.swing.*;56import jdk.testlibrary.OSInfo;575859/**60* If dragGestureRecognized() takes a while to complete and if user performs a drag quickly,61* an exception is thrown from DropTargetListener.dragEnter when it calls62* DropTargetDragEvent.getTransferable().63* <p>64* This class introduces a delay in dragGestureRecognized() to cause the exception.65*/66public class bug8024061 {67private static final DataFlavor DropObjectFlavor;68private static final int DELAY = 1000;6970private final DnDPanel panel1 = new DnDPanel(Color.yellow);71private final DnDPanel panel2 = new DnDPanel(Color.pink);72private final JFrame frame;7374private static final CountDownLatch lock = new CountDownLatch(1);75private static volatile Exception dragEnterException = null;7677static {78DataFlavor flavor = null;79try {80flavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);81} catch (ClassNotFoundException e) {82e.printStackTrace();83}84DropObjectFlavor = flavor;85}8687bug8024061() {88frame = new JFrame("DnDWithRobot");89frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);9091Dimension d = new Dimension(100, 100);9293panel1.setPreferredSize(d);94panel2.setPreferredSize(d);9596Container content = frame.getContentPane();97content.setLayout(new GridLayout(1, 2, 5, 5));98content.add(panel1);99content.add(panel2);100101frame.pack();102103DropObject drop = new DropObject();104drop.place(panel1, new Point(10, 10));105frame.setVisible(true);106}107108public static void main(String[] args) throws AWTException, InvocationTargetException, InterruptedException {109OSInfo.OSType type = OSInfo.getOSType();110if (type != OSInfo.OSType.LINUX && type != OSInfo.OSType.SOLARIS) {111System.out.println("This test is for Linux and Solaris only... " +112"skipping!");113return;114}115116final bug8024061[] dnd = {null};117SwingUtilities.invokeAndWait(new Runnable() {118@Override119public void run() {120dnd[0] = new bug8024061();121}122});123final Robot robot = new Robot();124robot.setAutoDelay(10);125robot.waitForIdle();126127JFrame frame = dnd[0].frame;128Point point = frame.getLocationOnScreen();129Point here = new Point(point.x + 35, point.y + 45);130Point there = new Point(point.x + 120, point.y + 45);131here.x += 25;132robot.mouseMove(here.x, here.y);133robot.mousePress(InputEvent.BUTTON1_MASK);134while (here.x < there.x) {135here.x += 20;136robot.mouseMove(here.x, here.y);137System.out.println("x = " + here.x);138}139robot.mouseRelease(InputEvent.BUTTON1_MASK);140robot.waitForIdle();141robot.mousePress(InputEvent.BUTTON1_MASK);142robot.mouseRelease(InputEvent.BUTTON1_MASK);143System.out.println("finished");144145try {146if (lock.await(5, TimeUnit.SECONDS)) {147if (dragEnterException == null) {148System.out.println("Test passed.");149} else {150System.out.println("Test failed.");151dragEnterException.printStackTrace();152throw new RuntimeException(dragEnterException);153}154} else {155System.out.println("Test failed. Timeout reached");156throw new RuntimeException("Timed out waiting for dragEnter()");157}158} finally {159frame.dispose();160}161}162163class DropObject implements Transferable {164DnDPanel panel;165Color color = Color.CYAN;166int width = 50;167int height = 50;168int x;169int y;170171void draw(Graphics2D g) {172Color savedColor = g.getColor();173g.setColor(color);174g.fillRect(x, y, width, height);175g.setColor(Color.lightGray);176g.drawRect(x, y, width, height);177g.setColor(savedColor);178}179180boolean contains(int x, int y) {181return (x > this.x && x < this.x + width)182&& (y > this.y && y < this.y + height);183}184185@Override186public DataFlavor[] getTransferDataFlavors() {187return new DataFlavor[]{DropObjectFlavor};188}189190void place(DnDPanel panel, Point location) {191if (panel != this.panel) {192x = location.x;193y = location.y;194if (this.panel != null) {195this.panel.setDropObject(null);196this.panel.repaint();197}198this.panel = panel;199this.panel.setDropObject(this);200this.panel.repaint();201}202}203204@Override205public boolean isDataFlavorSupported(DataFlavor flavor) {206return DropObjectFlavor.equals(flavor);207}208209@Override210public Object getTransferData(DataFlavor flavor)211throws UnsupportedFlavorException, IOException {212if (isDataFlavorSupported(flavor)) {213return this;214} else {215throw new UnsupportedFlavorException(flavor);216}217}218}219220class DnDPanel extends JPanel {221DropObject dropObject;222final DragSource dragSource;223final DropTarget dropTarget;224final Color color;225final DragGestureListener dgListener;226final DragSourceListener dsListener;227final DropTargetListener dtListener;228229DnDPanel(Color color) {230this.color = color;231this.dragSource = DragSource.getDefaultDragSource();232dgListener = new DragGestureListener() {233@Override234public void dragGestureRecognized(DragGestureEvent dge) {235Point location = dge.getDragOrigin();236if (dropObject != null && dropObject.contains(location.x, location.y)) {237dragSource.startDrag(dge, DragSource.DefaultCopyNoDrop, dropObject, dsListener);238try {239Thread.sleep(DELAY);240} catch (InterruptedException e) {241}242}243}244};245246dsListener = new DragSourceListener() {247@Override248public void dragEnter(DragSourceDragEvent dsde) {249}250251@Override252public void dragOver(DragSourceDragEvent dsde) {253}254255@Override256public void dropActionChanged(DragSourceDragEvent dsde) {257}258259@Override260public void dragExit(DragSourceEvent dse) {261}262263@Override264public void dragDropEnd(DragSourceDropEvent dsde) {265}266};267268dtListener = new DropTargetListener() {269@Override270public void dragEnter(DropTargetDragEvent dtde) {271if (dropObject != null) {272dtde.rejectDrag();273return;274}275dtde.acceptDrag(DnDConstants.ACTION_MOVE);276try {277Transferable t = dtde.getTransferable();278Object data = t.getTransferData(DropObjectFlavor);279} catch (Exception e) {280dragEnterException = e;281e.printStackTrace();282} finally {283lock.countDown();284}285}286287@Override288public void dragOver(DropTargetDragEvent dtde) {289if (dropObject != null) {290dtde.rejectDrag();291return;292}293dtde.acceptDrag(DnDConstants.ACTION_MOVE);294}295296@Override297public void dropActionChanged(DropTargetDragEvent dtde) {298}299300@Override301public void dragExit(DropTargetEvent dte) {302}303304@Override305public void drop(DropTargetDropEvent dtde) {306if (dropObject != null) {307dtde.rejectDrop();308return;309}310try {311dtde.acceptDrop(DnDConstants.ACTION_MOVE);312Transferable t = dtde.getTransferable();313DropObject dropObject = (DropObject) t.getTransferData(DropObjectFlavor);314Point location = dtde.getLocation();315dropObject.place(DnDPanel.this, location);316dtde.dropComplete(true);317} catch (Exception e) {318e.printStackTrace();319}320321}322};323324dragSource.createDefaultDragGestureRecognizer(this,325DnDConstants.ACTION_MOVE, dgListener);326327dropTarget = new DropTarget(this, DnDConstants.ACTION_MOVE, dtListener, true);328329}330331public void paintComponent(Graphics g) {332super.paintComponent(g);333Color savedColor = g.getColor();334g.setColor(color);335g.fillRect(0, 0, getWidth(), getHeight());336g.setColor(savedColor);337if (dropObject != null) {338dropObject.draw((Graphics2D) g);339}340}341342void setDropObject(DropObject dropObject) {343this.dropObject = dropObject;344}345346DropObject findDropObject(int x, int y) {347if (dropObject != null && dropObject.contains(x, y)) {348return dropObject;349}350return null;351}352}353}354355356