Skip to content

Commit 1b154e4

Browse files
author
Alisen Chung
committed
8307083: Open source some drag and drop tests 3
Reviewed-by: prr, serb
1 parent cc5c9b5 commit 1b154e4

4 files changed

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

0 commit comments

Comments
 (0)