|
| 1 | +/* |
| 2 | + * Copyright (c) 2013, 2023, 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.EventQueue; |
| 25 | +import java.awt.GridLayout; |
| 26 | +import java.awt.Robot; |
| 27 | +import java.awt.Point; |
| 28 | +import java.awt.Rectangle; |
| 29 | +import java.awt.Dimension; |
| 30 | +import java.awt.dnd.DropTarget; |
| 31 | +import java.awt.dnd.DropTargetDragEvent; |
| 32 | +import java.awt.dnd.DropTargetDropEvent; |
| 33 | +import java.awt.dnd.DropTargetEvent; |
| 34 | +import java.awt.dnd.DropTargetListener; |
| 35 | +import java.awt.event.InputEvent; |
| 36 | +import java.awt.event.KeyEvent; |
| 37 | +import javax.swing.JFrame; |
| 38 | +import javax.swing.JPanel; |
| 39 | +import javax.swing.JList; |
| 40 | + |
| 41 | +/* |
| 42 | + @test |
| 43 | + @bug 4388802 |
| 44 | + @summary tests that a drag can be initiated with MOUSE_MOVED event |
| 45 | + @key headful |
| 46 | + @run main DragTriggerEventTest |
| 47 | +*/ |
| 48 | + |
| 49 | +public class DragTriggerEventTest { |
| 50 | + |
| 51 | + volatile JFrame frame; |
| 52 | + volatile JList list; |
| 53 | + volatile DropTargetPanel panel; |
| 54 | + volatile Point srcPoint; |
| 55 | + volatile Rectangle cellBounds; |
| 56 | + volatile Point dstPoint; |
| 57 | + volatile Dimension d; |
| 58 | + static final int FRAME_ACTIVATION_TIMEOUT = 3000; |
| 59 | + volatile boolean mouse1Pressed = false; |
| 60 | + volatile boolean ctrlPressed = false; |
| 61 | + |
| 62 | + public static void main(String[] args) throws Exception { |
| 63 | + DragTriggerEventTest test = new DragTriggerEventTest(); |
| 64 | + EventQueue.invokeAndWait(test::init); |
| 65 | + try { |
| 66 | + test.start(); |
| 67 | + } finally { |
| 68 | + EventQueue.invokeAndWait(() -> { |
| 69 | + if (test.frame != null) { |
| 70 | + test.frame.dispose(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void init() { |
| 77 | + list = new JList(new String[] {"one", "two", "three", "four"}); |
| 78 | + list.setDragEnabled(true); |
| 79 | + panel = new DropTargetPanel(); |
| 80 | + |
| 81 | + frame = new JFrame(); |
| 82 | + frame.setTitle("DragTriggerEventTest"); |
| 83 | + frame.setLocation(200, 200); |
| 84 | + frame.getContentPane().setLayout(new GridLayout(2, 1)); |
| 85 | + frame.getContentPane().add(list); |
| 86 | + frame.getContentPane().add(panel); |
| 87 | + |
| 88 | + frame.pack(); |
| 89 | + frame.setVisible(true); |
| 90 | + } |
| 91 | + |
| 92 | + public void start() throws Exception { |
| 93 | + Robot robot; |
| 94 | + robot = new Robot(); |
| 95 | + |
| 96 | + EventQueue.invokeAndWait(() -> { |
| 97 | + srcPoint = list.getLocationOnScreen(); |
| 98 | + cellBounds = list.getCellBounds(0, 0); |
| 99 | + }); |
| 100 | + |
| 101 | + srcPoint.translate(cellBounds.x + cellBounds.width / 2, |
| 102 | + cellBounds.y + cellBounds.height / 2); |
| 103 | + |
| 104 | + EventQueue.invokeAndWait(() -> { |
| 105 | + dstPoint = panel.getLocationOnScreen(); |
| 106 | + d = panel.getSize(); |
| 107 | + }); |
| 108 | + dstPoint.translate(d.width / 2, d.height / 2); |
| 109 | + |
| 110 | + for (int delay = 32; delay < 10000 && !panel.getResult(); delay *= 2) { |
| 111 | + System.err.println("attempt to drag with delay " + delay); |
| 112 | + robot.mouseMove(srcPoint.x, srcPoint.y); |
| 113 | + robot.mousePress(InputEvent.BUTTON1_MASK); |
| 114 | + mouse1Pressed = true; |
| 115 | + robot.mouseRelease(InputEvent.BUTTON1_MASK); |
| 116 | + mouse1Pressed = false; |
| 117 | + |
| 118 | + robot.keyPress(KeyEvent.VK_CONTROL); |
| 119 | + ctrlPressed = true; |
| 120 | + robot.mousePress(InputEvent.BUTTON1_MASK); |
| 121 | + mouse1Pressed = true; |
| 122 | + |
| 123 | + Point p = new Point(srcPoint); |
| 124 | + while (!p.equals(dstPoint)) { |
| 125 | + p.translate(sign(dstPoint.x - p.x), |
| 126 | + sign(dstPoint.y - p.y)); |
| 127 | + robot.mouseMove(p.x, p.y); |
| 128 | + robot.delay(delay); |
| 129 | + } |
| 130 | + } |
| 131 | + if (mouse1Pressed) { |
| 132 | + robot.mouseRelease(InputEvent.BUTTON1_MASK); |
| 133 | + } |
| 134 | + if (ctrlPressed) { |
| 135 | + robot.keyRelease(KeyEvent.VK_CONTROL); |
| 136 | + } |
| 137 | + |
| 138 | + EventQueue.invokeAndWait(() -> { |
| 139 | + if (!panel.getResult()) { |
| 140 | + throw new RuntimeException("The test failed."); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + public static int sign(int n) { |
| 146 | + return n < 0 ? -1 : n == 0 ? 0 : 1; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +class DropTargetPanel extends JPanel implements DropTargetListener { |
| 151 | + |
| 152 | + private boolean passed = false; |
| 153 | + final Dimension preferredDimension = new Dimension(200, 100); |
| 154 | + |
| 155 | + public DropTargetPanel() { |
| 156 | + setDropTarget(new DropTarget(this, this)); |
| 157 | + } |
| 158 | + |
| 159 | + public Dimension getPreferredSize() { |
| 160 | + return preferredDimension; |
| 161 | + } |
| 162 | + |
| 163 | + public void dragEnter(DropTargetDragEvent dtde) { |
| 164 | + passed = true; |
| 165 | + } |
| 166 | + |
| 167 | + public void dragExit(DropTargetEvent dte) { |
| 168 | + passed = true; |
| 169 | + } |
| 170 | + |
| 171 | + public void dragOver(DropTargetDragEvent dtde) { |
| 172 | + passed = true; |
| 173 | + } |
| 174 | + |
| 175 | + public void dropActionChanged(DropTargetDragEvent dtde) { |
| 176 | + passed = true; |
| 177 | + } |
| 178 | + |
| 179 | + public void drop(DropTargetDropEvent dtde) { |
| 180 | + passed = true; |
| 181 | + dtde.rejectDrop(); |
| 182 | + } |
| 183 | + |
| 184 | + public boolean getResult() { |
| 185 | + return passed; |
| 186 | + } |
| 187 | +} |
0 commit comments