1 /* 2 * Copyright (c) 2006, 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 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 105 public void dragExit(DragSourceEvent dse) { 106 checkLocation(dse); 107 } 108 109 public void dragDropEnd(DragSourceDropEvent dsde) { 110 checkLocation(dsde); 111 dropSuccess = dsde.getDropSuccess(); 112 System.err.println("Drop was successful: " + dropSuccess); 113 } 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 } 158 }