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.GraphicsConfiguration;
27 import java.awt.GraphicsDevice;
28 import java.awt.GraphicsEnvironment;
29 import java.awt.Point;
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
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 volatile boolean dropSuccess;
61     private volatile boolean locationValid = true;
62 
63     private static Frame frame;
64 
main(final String[] args)65     public static void main(final String[] args) {
66         var lge = GraphicsEnvironment.getLocalGraphicsEnvironment();
67         for (GraphicsDevice device : lge.getScreenDevices()) {
68             Button2DragTest test = new Button2DragTest();
69             frame = new Frame(device.getDefaultConfiguration());
70             try {
71                 test.run();
72             } finally {
73                 frame.dispose();
74             }
75         }
76     }
77 
run()78     public void run() {
79         final DragSourceListener dragSourceListener = new DragSourceListener() {
80             private void checkLocation(DragSourceEvent dsde) {
81                 if (!frame.getBounds().contains(dsde.getLocation())) {
82                     System.err.println("Expected in" + frame.getBounds());
83                     System.err.println("Actual" + dsde.getLocation());
84                     locationValid = false;
85                 }
86             }
87 
88             @Override
89             public void dragEnter(DragSourceDragEvent dsde) {
90                 checkLocation(dsde);
91             }
92 
93             @Override
94             public void dragOver(DragSourceDragEvent dsde) {
95                 checkLocation(dsde);
96             }
97 
98             @Override
99             public void dropActionChanged(DragSourceDragEvent dsde) {
100                 checkLocation(dsde);
101             }
102 
103             @Override
104             public void dragExit(DragSourceEvent dse) {
105                 checkLocation(dse);
106             }
107 
108             public void dragDropEnd(DragSourceDropEvent dsde) {
109                 checkLocation(dsde);
110                 dropSuccess = dsde.getDropSuccess();
111                 System.err.println("Drop was successful: " + dropSuccess);
112             }
113         };
114         DragGestureListener dragGestureListener = new DragGestureListener() {
115             public void dragGestureRecognized(DragGestureEvent dge) {
116                 dge.startDrag(null, new StringSelection("OK"), dragSourceListener);
117             }
118         };
119         new DragSource().createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_MOVE,
120                                                             dragGestureListener);
121 
122         DropTargetAdapter dropTargetListener = new DropTargetAdapter() {
123             public void drop(DropTargetDropEvent dtde) {
124                 dtde.acceptDrop(DnDConstants.ACTION_MOVE);
125                 dtde.dropComplete(true);
126                 System.err.println("Drop");
127             }
128         };
129         new DropTarget(frame, dropTargetListener);
130 
131         frame.setBackground(Color.GREEN);
132         frame.setUndecorated(true);
133         frame.setSize(200, 200);
134         frame.setLocationRelativeTo(null);
135         frame.setVisible(true);
136 
137         Robot robot = Util.createRobot();
138 
139         Util.waitForIdle(robot);
140 
141         Point startPoint = frame.getLocationOnScreen();
142         Point endPoint = new Point(startPoint);
143         startPoint.translate(50, 50);
144         endPoint.translate(150, 150);
145 
146         Util.drag(robot, startPoint, endPoint, InputEvent.BUTTON2_MASK);
147 
148         Util.waitForIdle(robot);
149         robot.delay(500);
150 
151         if (!dropSuccess || !locationValid) {
152             throw new RuntimeException("test failed: drop was not successful");
153         }
154     }
155 }
156