|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2024, 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.Button; |
| 25 | +import java.awt.Component; |
| 26 | +import java.awt.Dimension; |
| 27 | +import java.awt.EventQueue; |
| 28 | +import java.awt.Frame; |
| 29 | +import java.awt.GridLayout; |
| 30 | +import java.awt.Panel; |
| 31 | +import java.awt.Point; |
| 32 | +import java.awt.Robot; |
| 33 | +import java.awt.datatransfer.DataFlavor; |
| 34 | +import java.awt.datatransfer.Transferable; |
| 35 | +import java.awt.datatransfer.UnsupportedFlavorException; |
| 36 | +import java.awt.dnd.DnDConstants; |
| 37 | +import java.awt.dnd.DragGestureEvent; |
| 38 | +import java.awt.dnd.DragGestureListener; |
| 39 | +import java.awt.dnd.DragSource; |
| 40 | +import java.awt.dnd.DragSourceDragEvent; |
| 41 | +import java.awt.dnd.DragSourceDropEvent; |
| 42 | +import java.awt.dnd.DragSourceEvent; |
| 43 | +import java.awt.dnd.DragSourceListener; |
| 44 | +import java.awt.dnd.DropTarget; |
| 45 | +import java.awt.dnd.DropTargetContext; |
| 46 | +import java.awt.dnd.DropTargetDragEvent; |
| 47 | +import java.awt.dnd.DropTargetDropEvent; |
| 48 | +import java.awt.dnd.DropTargetEvent; |
| 49 | +import java.awt.dnd.DropTargetListener; |
| 50 | +import java.awt.event.InputEvent; |
| 51 | +import java.awt.event.KeyEvent; |
| 52 | +import java.io.ByteArrayInputStream; |
| 53 | +import java.io.ByteArrayOutputStream; |
| 54 | +import java.io.IOException; |
| 55 | +import java.io.ObjectInputStream; |
| 56 | +import java.io.ObjectOutputStream; |
| 57 | +import java.io.Serializable; |
| 58 | + |
| 59 | +/* |
| 60 | + * @test |
| 61 | + * @bug 4395290 |
| 62 | + * @key headful |
| 63 | + * @summary tests that dragExit() is not called before drop() |
| 64 | + */ |
| 65 | + |
| 66 | +public class DragExitBeforeDropTest { |
| 67 | + private static Frame frame; |
| 68 | + private static final DragSourceButton dragSourceButton = new DragSourceButton(); |
| 69 | + private static final DropTargetPanel dropTargetPanel = new DropTargetPanel(); |
| 70 | + private static volatile Point srcPoint; |
| 71 | + private static volatile Point dstPoint; |
| 72 | + |
| 73 | + public static void main(String[] args) throws Exception { |
| 74 | + try { |
| 75 | + Robot robot = new Robot(); |
| 76 | + EventQueue.invokeAndWait(DragExitBeforeDropTest::createAndShowUI); |
| 77 | + robot.waitForIdle(); |
| 78 | + robot.delay(1000); |
| 79 | + |
| 80 | + EventQueue.invokeAndWait(() -> { |
| 81 | + Point p = dragSourceButton.getLocationOnScreen(); |
| 82 | + Dimension d = dragSourceButton.getSize(); |
| 83 | + p.translate(d.width / 2, d.height / 2); |
| 84 | + srcPoint = p; |
| 85 | + |
| 86 | + p = dropTargetPanel.getLocationOnScreen(); |
| 87 | + d = dropTargetPanel.getSize(); |
| 88 | + p.translate(d.width / 2, d.height / 2); |
| 89 | + dstPoint = p; |
| 90 | + }); |
| 91 | + |
| 92 | + robot.mouseMove(srcPoint.x, srcPoint.y); |
| 93 | + robot.keyPress(KeyEvent.VK_CONTROL); |
| 94 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 95 | + for (; !srcPoint.equals(dstPoint); |
| 96 | + srcPoint.translate(sign(dstPoint.x - srcPoint.x), |
| 97 | + sign(dstPoint.y - srcPoint.y))) { |
| 98 | + robot.mouseMove(srcPoint.x, srcPoint.y); |
| 99 | + robot.delay(10); |
| 100 | + } |
| 101 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 102 | + robot.keyRelease(KeyEvent.VK_CONTROL); |
| 103 | + robot.waitForIdle(); |
| 104 | + robot.delay(1000); |
| 105 | + |
| 106 | + if (!dropTargetPanel.getStatus()) { |
| 107 | + throw new RuntimeException("The test failed: dragExit()" |
| 108 | + + " is called before drop()"); |
| 109 | + } |
| 110 | + } finally { |
| 111 | + EventQueue.invokeAndWait(() -> { |
| 112 | + if (frame != null) { |
| 113 | + frame.dispose(); |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static void createAndShowUI() { |
| 120 | + frame = new Frame("DragExitBeforeDropTest"); |
| 121 | + frame.setLayout(new GridLayout(2, 1)); |
| 122 | + frame.add(dragSourceButton); |
| 123 | + frame.add(dropTargetPanel); |
| 124 | + frame.setLocationRelativeTo(null); |
| 125 | + frame.setSize(300, 400); |
| 126 | + frame.setVisible(true); |
| 127 | + } |
| 128 | + |
| 129 | + public static int sign(int n) { |
| 130 | + return Integer.compare(n, 0); |
| 131 | + } |
| 132 | + |
| 133 | + private static class DragSourceButton extends Button implements Serializable, |
| 134 | + Transferable, |
| 135 | + DragGestureListener, |
| 136 | + DragSourceListener { |
| 137 | + private final DataFlavor dataflavor = |
| 138 | + new DataFlavor(Button.class, "DragSourceButton"); |
| 139 | + |
| 140 | + public DragSourceButton() { |
| 141 | + this("DragSourceButton"); |
| 142 | + } |
| 143 | + |
| 144 | + public DragSourceButton(String str) { |
| 145 | + super(str); |
| 146 | + |
| 147 | + DragSource ds = DragSource.getDefaultDragSource(); |
| 148 | + ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, |
| 149 | + this); |
| 150 | + } |
| 151 | + |
| 152 | + public void dragGestureRecognized(DragGestureEvent dge) { |
| 153 | + dge.startDrag(null, this, this); |
| 154 | + } |
| 155 | + |
| 156 | + public void dragEnter(DragSourceDragEvent dsde) {} |
| 157 | + |
| 158 | + public void dragExit(DragSourceEvent dse) {} |
| 159 | + |
| 160 | + public void dragOver(DragSourceDragEvent dsde) {} |
| 161 | + |
| 162 | + public void dragDropEnd(DragSourceDropEvent dsde) {} |
| 163 | + |
| 164 | + public void dropActionChanged(DragSourceDragEvent dsde) {} |
| 165 | + |
| 166 | + public Object getTransferData(DataFlavor flavor) |
| 167 | + throws UnsupportedFlavorException, IOException { |
| 168 | + |
| 169 | + if (!isDataFlavorSupported(flavor)) { |
| 170 | + throw new UnsupportedFlavorException(flavor); |
| 171 | + } |
| 172 | + |
| 173 | + Object retObj; |
| 174 | + |
| 175 | + ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); |
| 176 | + ObjectOutputStream ooStream = new ObjectOutputStream(baoStream); |
| 177 | + ooStream.writeObject(this); |
| 178 | + |
| 179 | + ByteArrayInputStream baiStream = |
| 180 | + new ByteArrayInputStream(baoStream.toByteArray()); |
| 181 | + ObjectInputStream ois = new ObjectInputStream(baiStream); |
| 182 | + try { |
| 183 | + retObj = ois.readObject(); |
| 184 | + } catch (ClassNotFoundException e) { |
| 185 | + e.printStackTrace(); |
| 186 | + throw new RuntimeException(e.toString()); |
| 187 | + } |
| 188 | + |
| 189 | + return retObj; |
| 190 | + } |
| 191 | + |
| 192 | + public DataFlavor[] getTransferDataFlavors() { |
| 193 | + return new DataFlavor[] { dataflavor }; |
| 194 | + } |
| 195 | + |
| 196 | + public boolean isDataFlavorSupported(DataFlavor dflavor) { |
| 197 | + return dataflavor.equals(dflavor); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private static class DropTargetPanel extends Panel implements DropTargetListener { |
| 202 | + |
| 203 | + final Dimension preferredDimension = new Dimension(200, 100); |
| 204 | + volatile boolean testPassed = true; |
| 205 | + |
| 206 | + public DropTargetPanel() { |
| 207 | + setDropTarget(new DropTarget(this, this)); |
| 208 | + } |
| 209 | + |
| 210 | + public boolean getStatus() { |
| 211 | + return testPassed; |
| 212 | + } |
| 213 | + |
| 214 | + public Dimension getPreferredSize() { |
| 215 | + return preferredDimension; |
| 216 | + } |
| 217 | + |
| 218 | + public void dragEnter(DropTargetDragEvent dtde) {} |
| 219 | + |
| 220 | + public void dragExit(DropTargetEvent dte) { |
| 221 | + testPassed = false; |
| 222 | + } |
| 223 | + |
| 224 | + public void dragOver(DropTargetDragEvent dtde) {} |
| 225 | + |
| 226 | + public void dropActionChanged(DropTargetDragEvent dtde) {} |
| 227 | + |
| 228 | + public void drop(DropTargetDropEvent dtde) { |
| 229 | + DropTargetContext dtc = dtde.getDropTargetContext(); |
| 230 | + |
| 231 | + if ((dtde.getSourceActions() & DnDConstants.ACTION_COPY) != 0) { |
| 232 | + dtde.acceptDrop(DnDConstants.ACTION_COPY); |
| 233 | + } else { |
| 234 | + dtde.rejectDrop(); |
| 235 | + } |
| 236 | + |
| 237 | + DataFlavor[] dfs = dtde.getCurrentDataFlavors(); |
| 238 | + Component comp = null; |
| 239 | + |
| 240 | + if(dfs != null && dfs.length >= 1) { |
| 241 | + Transferable transfer = dtde.getTransferable(); |
| 242 | + |
| 243 | + try { |
| 244 | + comp = (Component)transfer.getTransferData(dfs[0]); |
| 245 | + } catch (Throwable e) { |
| 246 | + e.printStackTrace(); |
| 247 | + dtc.dropComplete(false); |
| 248 | + } |
| 249 | + } |
| 250 | + dtc.dropComplete(true); |
| 251 | + add(comp); |
| 252 | + } |
| 253 | + } |
| 254 | +} |
| 255 | + |
| 256 | + |
| 257 | + |
0 commit comments