Skip to content

Commit f49ffca

Browse files
committedSep 28, 2022
8286772: java/awt/dnd/DropTargetInInternalFrameTest/DropTargetInInternalFrameTest.html times out and fails in Windows
Backport-of: d76c1089efc8816c6f804b78371e62e697fc62c5
1 parent ad9e9f2 commit f49ffca

File tree

1 file changed

+380
-0
lines changed

1 file changed

+380
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
/*
2+
* Copyright (c) 2000, 2022, 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.Button;
25+
import java.awt.Component;
26+
import java.awt.Cursor;
27+
import java.awt.Dimension;
28+
import java.awt.GridLayout;
29+
import java.awt.Point;
30+
import java.awt.Rectangle;
31+
import java.awt.Robot;
32+
import java.awt.Toolkit;
33+
import java.awt.datatransfer.DataFlavor;
34+
import java.awt.datatransfer.Transferable;
35+
import java.awt.datatransfer.UnsupportedFlavorException;
36+
import java.awt.dnd.DnDConstants;
37+
import java.awt.dnd.DragGestureEvent;
38+
import java.awt.dnd.DragGestureListener;
39+
import java.awt.dnd.DragSource;
40+
import java.awt.dnd.DragSourceDragEvent;
41+
import java.awt.dnd.DragSourceDropEvent;
42+
import java.awt.dnd.DragSourceEvent;
43+
import java.awt.dnd.DragSourceListener;
44+
import java.awt.dnd.DropTarget;
45+
import java.awt.dnd.DropTargetContext;
46+
import java.awt.dnd.DropTargetDragEvent;
47+
import java.awt.dnd.DropTargetDropEvent;
48+
import java.awt.dnd.DropTargetEvent;
49+
import java.awt.dnd.DropTargetListener;
50+
import java.awt.event.FocusAdapter;
51+
import java.awt.event.FocusEvent;
52+
import java.awt.event.InputEvent;
53+
import java.io.ByteArrayInputStream;
54+
import java.io.ByteArrayOutputStream;
55+
import java.io.File;
56+
import java.io.IOException;
57+
import java.io.ObjectInputStream;
58+
import java.io.ObjectOutputStream;
59+
import java.io.Serializable;
60+
import java.util.concurrent.CountDownLatch;
61+
import java.util.concurrent.TimeUnit;
62+
import java.util.concurrent.atomic.AtomicReference;
63+
import javax.imageio.ImageIO;
64+
import javax.swing.JButton;
65+
import javax.swing.JDesktopPane;
66+
import javax.swing.JFrame;
67+
import javax.swing.JInternalFrame;
68+
import javax.swing.JPanel;
69+
import javax.swing.SwingUtilities;
70+
71+
/*
72+
@test
73+
@bug 4395279
74+
@summary Tests that a drop target in InternalFrame functions properly
75+
@key headful
76+
@run main DropTargetInInternalFrameTest
77+
*/
78+
public class DropTargetInInternalFrameTest implements Serializable {
79+
private static final CountDownLatch dropLatch = new CountDownLatch(1);
80+
private static final CountDownLatch focusLatch = new CountDownLatch(1);
81+
private static JFrame frame;
82+
private static JInternalFrame sourceFrame;
83+
private static JInternalFrame targetFrame;
84+
private static DragSourcePanel dragSourcePanel;
85+
private static DropTargetPanel dropTargetPanel;
86+
private static Robot robot;
87+
88+
private static void createUI() {
89+
frame = new JFrame("Test frame");
90+
sourceFrame = new JInternalFrame("Source");
91+
targetFrame = new JInternalFrame("Destination");
92+
dragSourcePanel = new DragSourcePanel();
93+
dropTargetPanel = new DropTargetPanel(dropLatch);
94+
JDesktopPane desktopPane = new JDesktopPane();
95+
96+
sourceFrame.getContentPane().setLayout(new GridLayout(3, 1));
97+
98+
// add panels to content panes
99+
sourceFrame.getContentPane().add(dragSourcePanel);
100+
targetFrame.getContentPane().add(dropTargetPanel);
101+
102+
sourceFrame.setSize(200, 200);
103+
targetFrame.setSize(200, 200);
104+
targetFrame
105+
.setLocation(sourceFrame.getX() + sourceFrame.getWidth() + 10,
106+
sourceFrame.getY());
107+
108+
desktopPane.add(sourceFrame);
109+
desktopPane.add(targetFrame);
110+
111+
frame.setTitle("Test frame");
112+
frame.setBounds(200, 200, 450, 250);
113+
frame.getContentPane().add(desktopPane);
114+
frame.setAlwaysOnTop(true);
115+
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
116+
117+
sourceFrame.setVisible(true);
118+
targetFrame.setVisible(true);
119+
frame.setVisible(true);
120+
dragSourcePanel.dragSourceButton.requestFocusInWindow();
121+
}
122+
123+
public static void main(String[] argv) throws Exception {
124+
SwingUtilities.invokeAndWait(DropTargetInInternalFrameTest::createUI);
125+
126+
robot = new Robot();
127+
robot.setAutoDelay(50);
128+
robot.setAutoWaitForIdle(true);
129+
robot.waitForIdle();
130+
if (!focusLatch.await(5, TimeUnit.SECONDS)) {
131+
captureScreen();
132+
SwingUtilities
133+
.invokeAndWait(DropTargetInInternalFrameTest::disposeFrame);
134+
System.out.println(
135+
"Test failed, waited too long for the drag button to gain focus");
136+
}
137+
final AtomicReference<Point> p1Ref = new AtomicReference<>();
138+
final AtomicReference<Point> p2Ref = new AtomicReference<>();
139+
SwingUtilities.invokeAndWait(() -> {
140+
final Point dragLocation =
141+
dragSourcePanel.dragSourceButton.getLocationOnScreen();
142+
Dimension d1 = dragSourcePanel.dragSourceButton.getSize();
143+
dragLocation.translate(d1.width / 2, d1.height / 2);
144+
p1Ref.set(dragLocation);
145+
final Point dropLocation = dropTargetPanel.getLocationOnScreen();
146+
dropLocation.translate(d1.width / 2, d1.height / 2);
147+
p2Ref.set(dropLocation);
148+
});
149+
Point p1 = p1Ref.get();
150+
Point p2 = p2Ref.get();
151+
152+
dragAndDrop(p1, p2);
153+
154+
if (!dropLatch.await(5, TimeUnit.SECONDS)) {
155+
captureScreen();
156+
System.out.println("Test Failed, Waited too long for the Drop to complete");
157+
}
158+
int calledMethods = dropTargetPanel.getCalledMethods();
159+
SwingUtilities
160+
.invokeAndWait(DropTargetInInternalFrameTest::disposeFrame);
161+
System.out.println("CalledMethods = " + calledMethods);
162+
if ((calledMethods & DropTargetPanel.ENTER_CALLED) == 0) {
163+
throw new RuntimeException(
164+
"Test Failed, DropTargetListener.dragEnter() not called");
165+
}
166+
if ((calledMethods & DropTargetPanel.OVER_CALLED) == 0) {
167+
throw new RuntimeException(
168+
"Test Failed, DropTargetListener.dragOver() not called");
169+
}
170+
if ((calledMethods & DropTargetPanel.DROP_CALLED) == 0) {
171+
throw new RuntimeException(
172+
"Test Failed, DropTargetListener.drop() not called.");
173+
}
174+
175+
System.out.println("Test Passed");
176+
}
177+
178+
private static void dragAndDrop(final Point p1, final Point p2) {
179+
robot.mouseMove(p1.x, p1.y);
180+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
181+
int dx = 1;
182+
while (p1.x < p2.x) {
183+
p1.translate(dx, 0);
184+
robot.mouseMove(p1.x, p1.y);
185+
dx++;
186+
}
187+
robot.mouseMove(p2.x, p2.y);
188+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
189+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
190+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
191+
}
192+
193+
private static void captureScreen() {
194+
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
195+
try {
196+
ImageIO.write(robot.createScreenCapture(
197+
new Rectangle(0, 0, screenSize.width, screenSize.height)),
198+
"png", new File("screenImage.png"));
199+
} catch (IOException ignore) {
200+
}
201+
}
202+
203+
private static void disposeFrame() {
204+
sourceFrame.dispose();
205+
targetFrame.dispose();
206+
frame.dispose();
207+
}
208+
209+
private static class DragSourcePanel extends JPanel {
210+
211+
final Dimension preferredDimension = new Dimension(200, 100);
212+
final DragSourceButton dragSourceButton = new DragSourceButton();
213+
214+
public DragSourcePanel() {
215+
setLayout(new GridLayout(1, 1));
216+
dragSourceButton.addFocusListener(new FocusAdapter() {
217+
@Override
218+
public void focusGained(final FocusEvent e) {
219+
super.focusGained(e);
220+
focusLatch.countDown();
221+
}
222+
});
223+
add(dragSourceButton);
224+
}
225+
226+
public Dimension getPreferredSize() {
227+
return preferredDimension;
228+
}
229+
230+
}
231+
232+
private static class DropTargetPanel extends JPanel
233+
implements DropTargetListener {
234+
235+
public static final int ENTER_CALLED = 0x1;
236+
public static final int OVER_CALLED = 0x2;
237+
public static final int DROP_CALLED = 0x4;
238+
private final Dimension preferredDimension = new Dimension(200, 100);
239+
private final CountDownLatch dropLatch;
240+
private volatile int calledMethods = 0;
241+
242+
public DropTargetPanel(final CountDownLatch dropLatch) {
243+
this.dropLatch = dropLatch;
244+
setDropTarget(new DropTarget(this, this));
245+
}
246+
247+
public Dimension getPreferredSize() {
248+
return preferredDimension;
249+
}
250+
251+
public void dragEnter(DropTargetDragEvent dtde) {
252+
calledMethods |= ENTER_CALLED;
253+
}
254+
255+
public void dragOver(DropTargetDragEvent dtde) {
256+
calledMethods |= OVER_CALLED;
257+
}
258+
259+
public void dropActionChanged(DropTargetDragEvent dtde) {
260+
}
261+
262+
public void dragExit(DropTargetEvent dte) {
263+
}
264+
265+
public void drop(DropTargetDropEvent dtde) {
266+
System.out.println("Drop!!!!!!!!!!!! ");
267+
calledMethods |= DROP_CALLED;
268+
DropTargetContext dtc = dtde.getDropTargetContext();
269+
270+
if ((dtde.getSourceActions() & DnDConstants.ACTION_COPY) != 0) {
271+
dtde.acceptDrop(DnDConstants.ACTION_COPY);
272+
} else {
273+
dtde.rejectDrop();
274+
}
275+
276+
DataFlavor[] dfs = dtde.getCurrentDataFlavors();
277+
Component comp = null;
278+
279+
if (dfs != null && dfs.length >= 1) {
280+
Transferable transfer = dtde.getTransferable();
281+
282+
try {
283+
comp = (Component) transfer.getTransferData(dfs[0]);
284+
} catch (Throwable e) {
285+
e.printStackTrace();
286+
dtc.dropComplete(false);
287+
}
288+
}
289+
dtc.dropComplete(true);
290+
add(comp);
291+
dropLatch.countDown();
292+
}
293+
294+
public int getCalledMethods() {
295+
return calledMethods;
296+
}
297+
298+
}
299+
300+
private static class DragSourceButton extends JButton
301+
implements Serializable, Transferable, DragGestureListener,
302+
DragSourceListener {
303+
private final DataFlavor dataflavor =
304+
new DataFlavor(Button.class, "DragSourceButton");
305+
306+
public DragSourceButton() {
307+
this("DragSourceButton");
308+
}
309+
310+
public DragSourceButton(String str) {
311+
super(str);
312+
DragSource ds = DragSource.getDefaultDragSource();
313+
ds.createDefaultDragGestureRecognizer(this,
314+
DnDConstants.ACTION_COPY,
315+
this);
316+
}
317+
318+
public void dragGestureRecognized(DragGestureEvent dge) {
319+
dge.startDrag(new Cursor(Cursor.HAND_CURSOR), this, this);
320+
}
321+
322+
public DataFlavor[] getTransferDataFlavors() {
323+
return new DataFlavor[]{dataflavor};
324+
}
325+
326+
public boolean isDataFlavorSupported(DataFlavor dflavor) {
327+
return dataflavor.equals(dflavor);
328+
}
329+
330+
public Object getTransferData(DataFlavor flavor)
331+
throws UnsupportedFlavorException, IOException {
332+
333+
if (!isDataFlavorSupported(flavor)) {
334+
throw new UnsupportedFlavorException(flavor);
335+
}
336+
Object retObj;
337+
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
338+
ObjectOutputStream ooStream = new ObjectOutputStream(baoStream);
339+
ooStream.writeObject(this);
340+
341+
ByteArrayInputStream baiStream =
342+
new ByteArrayInputStream(baoStream.toByteArray());
343+
ObjectInputStream ois = new ObjectInputStream(baiStream);
344+
try {
345+
retObj = ois.readObject();
346+
} catch (ClassNotFoundException e) {
347+
e.printStackTrace();
348+
throw new RuntimeException(e.toString());
349+
}
350+
return retObj;
351+
}
352+
353+
@Override
354+
public void dragEnter(final DragSourceDragEvent dsde) {
355+
356+
}
357+
358+
@Override
359+
public void dragOver(final DragSourceDragEvent dsde) {
360+
361+
}
362+
363+
@Override
364+
public void dropActionChanged(final DragSourceDragEvent dsde) {
365+
366+
}
367+
368+
@Override
369+
public void dragExit(final DragSourceEvent dse) {
370+
371+
}
372+
373+
@Override
374+
public void dragDropEnd(final DragSourceDropEvent dsde) {
375+
376+
}
377+
378+
}
379+
380+
}

0 commit comments

Comments
 (0)