|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 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 | +/* |
| 25 | + @test |
| 26 | + @bug 4774532 |
| 27 | + @summary tests that DropTargetDragEvent.getDropAction() returns correct value |
| 28 | + after DropTargetDragEvent.rejectDrag() |
| 29 | + @key headful |
| 30 | + @run main RejectDragDropActionTest |
| 31 | +*/ |
| 32 | + |
| 33 | +import java.awt.AWTException; |
| 34 | +import java.awt.EventQueue; |
| 35 | +import java.awt.Frame; |
| 36 | +import java.awt.Point; |
| 37 | +import java.awt.Robot; |
| 38 | +import java.awt.datatransfer.StringSelection; |
| 39 | +import java.awt.dnd.DnDConstants; |
| 40 | +import java.awt.dnd.DragGestureListener; |
| 41 | +import java.awt.dnd.DragGestureRecognizer; |
| 42 | +import java.awt.dnd.DragSource; |
| 43 | +import java.awt.dnd.DropTarget; |
| 44 | +import java.awt.dnd.DropTargetAdapter; |
| 45 | +import java.awt.dnd.DropTargetDragEvent; |
| 46 | +import java.awt.dnd.DropTargetDropEvent; |
| 47 | +import java.awt.dnd.DropTargetListener; |
| 48 | +import java.awt.event.InputEvent; |
| 49 | +import java.lang.reflect.InvocationTargetException; |
| 50 | + |
| 51 | + |
| 52 | +public class RejectDragDropActionTest { |
| 53 | + |
| 54 | + private static volatile boolean incorrectActionDetected = false; |
| 55 | + |
| 56 | + private static final int FRAME_ACTIVATION_TIMEOUT = 3000; |
| 57 | + |
| 58 | + private static Frame frame; |
| 59 | + private static DragSource ds; |
| 60 | + private static DragGestureListener dgl; |
| 61 | + private static DragGestureRecognizer dgr; |
| 62 | + private final DropTargetListener dtl = new DropTargetAdapter() { |
| 63 | + public void dragEnter(DropTargetDragEvent dtde) { |
| 64 | + dtde.rejectDrag(); |
| 65 | + } |
| 66 | + public void dragOver(DropTargetDragEvent dtde) { |
| 67 | + if (dtde.getDropAction() == DnDConstants.ACTION_NONE) { |
| 68 | + incorrectActionDetected = true; |
| 69 | + } |
| 70 | + } |
| 71 | + public void drop(DropTargetDropEvent dtde) { |
| 72 | + dtde.rejectDrop(); |
| 73 | + } |
| 74 | + }; |
| 75 | + private final DropTarget dt = new DropTarget(frame, dtl); |
| 76 | + |
| 77 | + public static void main(String[] args) throws InterruptedException, |
| 78 | + InvocationTargetException, AWTException { |
| 79 | + EventQueue.invokeAndWait(() -> { |
| 80 | + frame = new Frame("RejectDragDropActionTest"); |
| 81 | + ds = DragSource.getDefaultDragSource(); |
| 82 | + dgl = dge -> dge.startDrag(null, new StringSelection("OOKK")); |
| 83 | + dgr = ds.createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_COPY, dgl); |
| 84 | + frame.setBounds(100, 100, 200, 200); |
| 85 | + frame.setVisible(true); |
| 86 | + }); |
| 87 | + |
| 88 | + try { |
| 89 | + Robot robot = new Robot(); |
| 90 | + robot.waitForIdle(); |
| 91 | + robot.delay(FRAME_ACTIVATION_TIMEOUT); |
| 92 | + |
| 93 | + Point startPoint = frame.getLocationOnScreen(); |
| 94 | + Point endPoint = new Point(startPoint); |
| 95 | + startPoint.translate(50, 50); |
| 96 | + endPoint.translate(150, 150); |
| 97 | + |
| 98 | + robot.mouseMove(startPoint.x, startPoint.y); |
| 99 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 100 | + for (Point p = new Point(startPoint); !p.equals(endPoint); |
| 101 | + p.translate(sign(endPoint.x - p.x), |
| 102 | + sign(endPoint.y - p.y))) { |
| 103 | + robot.mouseMove(p.x, p.y); |
| 104 | + robot.delay(50); |
| 105 | + } |
| 106 | + |
| 107 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 108 | + } finally { |
| 109 | + EventQueue.invokeAndWait(() -> { |
| 110 | + if (frame != null) { |
| 111 | + frame.dispose(); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + if (incorrectActionDetected) { |
| 117 | + throw new RuntimeException("User action reported incorrectly."); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static int sign(int n) { |
| 122 | + return n < 0 ? -1 : n == 0 ? 0 : 1; |
| 123 | + } |
| 124 | +} |
0 commit comments