|
| 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.AWTEvent; |
| 25 | +import java.awt.Component; |
| 26 | +import java.awt.Container; |
| 27 | +import java.awt.Dimension; |
| 28 | +import java.awt.EventQueue; |
| 29 | +import java.awt.Frame; |
| 30 | +import java.awt.GridLayout; |
| 31 | +import java.awt.Panel; |
| 32 | +import java.awt.Point; |
| 33 | +import java.awt.Robot; |
| 34 | +import java.awt.Toolkit; |
| 35 | +import java.awt.datatransfer.DataFlavor; |
| 36 | +import java.awt.datatransfer.StringSelection; |
| 37 | +import java.awt.datatransfer.Transferable; |
| 38 | +import java.awt.dnd.DnDConstants; |
| 39 | +import java.awt.dnd.DragGestureListener; |
| 40 | +import java.awt.dnd.DragSource; |
| 41 | +import java.awt.dnd.DragSourceAdapter; |
| 42 | +import java.awt.dnd.DragSourceDragEvent; |
| 43 | +import java.awt.dnd.DragSourceDropEvent; |
| 44 | +import java.awt.dnd.DropTarget; |
| 45 | +import java.awt.dnd.DropTargetAdapter; |
| 46 | +import java.awt.dnd.DropTargetDropEvent; |
| 47 | +import java.awt.dnd.DropTargetListener; |
| 48 | +import java.awt.event.AWTEventListener; |
| 49 | +import java.awt.event.InputEvent; |
| 50 | +import java.awt.event.KeyEvent; |
| 51 | +import java.awt.event.MouseEvent; |
| 52 | +import java.util.concurrent.CountDownLatch; |
| 53 | +import java.util.concurrent.TimeUnit; |
| 54 | + |
| 55 | +/* |
| 56 | + * @test |
| 57 | + * @key headful |
| 58 | + * @bug 4422345 |
| 59 | + * @summary tests that DragSourceMotionListeners work correctly and |
| 60 | + DragSourceEvents position is correct |
| 61 | + */ |
| 62 | + |
| 63 | +public class DragSourceMotionListenerTest implements AWTEventListener { |
| 64 | + static class TestPanel extends Panel { |
| 65 | + final Dimension preferredDimension = new Dimension(200, 200); |
| 66 | + public Dimension getPreferredSize() { |
| 67 | + return preferredDimension; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static Frame frame; |
| 72 | + private static final Panel source = new TestPanel(); |
| 73 | + private static final Panel target = new TestPanel(); |
| 74 | + private static final DragSource ds = DragSource.getDefaultDragSource(); |
| 75 | + private static volatile CountDownLatch mouseReleaseEvent; |
| 76 | + |
| 77 | + static volatile boolean passedTest1 = false; |
| 78 | + static volatile boolean passedTest2 = false; |
| 79 | + |
| 80 | + private static final Point testPoint1 = new Point(); |
| 81 | + private static final Point testPoint2 = new Point(); |
| 82 | + private static volatile Point srcPoint; |
| 83 | + private static volatile Point dstOutsidePoint; |
| 84 | + private static volatile Point dstInsidePoint; |
| 85 | + |
| 86 | + private static final Transferable t = new StringSelection("TEXT"); |
| 87 | + private static final DragGestureListener gestureListener = e -> e.startDrag(null, t); |
| 88 | + |
| 89 | + private static final DragSourceAdapter sourceAdapter = new DragSourceAdapter() { |
| 90 | + public void dragMouseMoved(DragSourceDragEvent dsde) { |
| 91 | + if (Math.abs(dsde.getX() - testPoint1.getX()) < 5) { |
| 92 | + passedTest1 = true; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public void dragDropEnd(DragSourceDropEvent dsde) { |
| 97 | + if (Math.abs(dsde.getX() - testPoint2.getX()) < 5) { |
| 98 | + passedTest2 = true; |
| 99 | + } |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + private static final DropTargetListener targetAdapter = new DropTargetAdapter() { |
| 104 | + public void drop(DropTargetDropEvent e) { |
| 105 | + e.acceptDrop(DnDConstants.ACTION_COPY); |
| 106 | + try { |
| 107 | + final Transferable t = e.getTransferable(); |
| 108 | + final String str = |
| 109 | + (String) t.getTransferData(DataFlavor.stringFlavor); |
| 110 | + e.dropComplete(true); |
| 111 | + } catch (Exception ex) { |
| 112 | + ex.printStackTrace(); |
| 113 | + e.dropComplete(false); |
| 114 | + } |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + private static final DropTarget dropTarget = new DropTarget(target, targetAdapter); |
| 119 | + Component clickedComponent = null; |
| 120 | + |
| 121 | + private void createAndShowUI() { |
| 122 | + frame = new Frame("DragSourceMotionListenerTest"); |
| 123 | + ds.addDragSourceListener(sourceAdapter); |
| 124 | + ds.addDragSourceMotionListener(sourceAdapter); |
| 125 | + ds.createDefaultDragGestureRecognizer(source, DnDConstants.ACTION_COPY, gestureListener); |
| 126 | + target.setDropTarget(dropTarget); |
| 127 | + |
| 128 | + frame.setLayout(new GridLayout(1, 2)); |
| 129 | + |
| 130 | + frame.add(source); |
| 131 | + frame.add(target); |
| 132 | + |
| 133 | + Toolkit.getDefaultToolkit() |
| 134 | + .addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK); |
| 135 | + frame.pack(); |
| 136 | + frame.setVisible(true); |
| 137 | + } |
| 138 | + |
| 139 | + public static void main(String[] args) throws Exception { |
| 140 | + try { |
| 141 | + Robot robot = new Robot(); |
| 142 | + robot.setAutoDelay(10); |
| 143 | + |
| 144 | + DragSourceMotionListenerTest dsmObj = new DragSourceMotionListenerTest(); |
| 145 | + EventQueue.invokeAndWait(dsmObj::createAndShowUI); |
| 146 | + robot.waitForIdle(); |
| 147 | + robot.delay(1000); |
| 148 | + |
| 149 | + EventQueue.invokeAndWait(() -> { |
| 150 | + srcPoint = getPoint(source, 1); |
| 151 | + |
| 152 | + dstOutsidePoint = getPoint(frame, 3); |
| 153 | + testPoint1.setLocation(dstOutsidePoint); |
| 154 | + |
| 155 | + dstInsidePoint = getPoint(target, 1); |
| 156 | + testPoint2.setLocation(dstInsidePoint); |
| 157 | + }); |
| 158 | + robot.waitForIdle(); |
| 159 | + |
| 160 | + if (!dsmObj.pointInComponent(robot, srcPoint, source)) { |
| 161 | + throw new RuntimeException("WARNING: Couldn't locate source panel."); |
| 162 | + } |
| 163 | + |
| 164 | + if (!dsmObj.pointInComponent(robot, dstInsidePoint, target)) { |
| 165 | + throw new RuntimeException("WARNING: Couldn't locate target panel."); |
| 166 | + } |
| 167 | + |
| 168 | + robot.mouseMove(srcPoint.x, srcPoint.y); |
| 169 | + robot.keyPress(KeyEvent.VK_CONTROL); |
| 170 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 171 | + for (; !srcPoint.equals(dstOutsidePoint); |
| 172 | + srcPoint.translate(sign(dstOutsidePoint.x - srcPoint.x), |
| 173 | + sign(dstOutsidePoint.y - srcPoint.y))) { |
| 174 | + robot.mouseMove(srcPoint.x, srcPoint.y); |
| 175 | + } |
| 176 | + |
| 177 | + for (int i = 0; i < 10; i++) { |
| 178 | + robot.mouseMove(srcPoint.x, srcPoint.y++); |
| 179 | + } |
| 180 | + |
| 181 | + for (;!srcPoint.equals(dstInsidePoint); |
| 182 | + srcPoint.translate(sign(dstInsidePoint.x - srcPoint.x), |
| 183 | + sign(dstInsidePoint.y - srcPoint.y))) { |
| 184 | + robot.mouseMove(srcPoint.x, srcPoint.y); |
| 185 | + } |
| 186 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 187 | + robot.keyRelease(KeyEvent.VK_CONTROL); |
| 188 | + robot.waitForIdle(); |
| 189 | + robot.delay(1000); |
| 190 | + |
| 191 | + if (!passedTest1) { |
| 192 | + throw new RuntimeException("Failed first test."); |
| 193 | + } |
| 194 | + |
| 195 | + if (!passedTest2) { |
| 196 | + throw new RuntimeException("Failed second test."); |
| 197 | + } |
| 198 | + } finally { |
| 199 | + EventQueue.invokeAndWait(() -> { |
| 200 | + if (frame != null) { |
| 201 | + frame.dispose(); |
| 202 | + } |
| 203 | + }); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private static Point getPoint(Container container, int multiple) { |
| 208 | + Point p = container.getLocationOnScreen(); |
| 209 | + Dimension d = container.getSize(); |
| 210 | + p.translate(multiple * d.width / 2, d.height / 2); |
| 211 | + return p; |
| 212 | + } |
| 213 | + |
| 214 | + public static int sign(int n) { |
| 215 | + return Integer.compare(n, 0); |
| 216 | + } |
| 217 | + |
| 218 | + public void eventDispatched(AWTEvent e) { |
| 219 | + if (e.getID() == MouseEvent.MOUSE_RELEASED) { |
| 220 | + clickedComponent = (Component)e.getSource(); |
| 221 | + mouseReleaseEvent.countDown(); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + boolean pointInComponent(Robot robot, Point p, Component comp) throws Exception { |
| 226 | + robot.waitForIdle(); |
| 227 | + clickedComponent = null; |
| 228 | + mouseReleaseEvent = new CountDownLatch(1); |
| 229 | + robot.mouseMove(p.x, p.y); |
| 230 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 231 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 232 | + if (!mouseReleaseEvent.await(2, TimeUnit.SECONDS)) { |
| 233 | + throw new RuntimeException("Mouse Release Event not received"); |
| 234 | + } |
| 235 | + |
| 236 | + Component c = clickedComponent; |
| 237 | + while (c != null && c != comp) { |
| 238 | + c = c.getParent(); |
| 239 | + } |
| 240 | + return c == comp; |
| 241 | + } |
| 242 | +} |
0 commit comments