Skip to content

Commit cc42090

Browse files
vieiroTheRealMDoerr
authored andcommitted
8306466: Open source more AWT Drag & Drop related tests
Backport-of: 418a82551a2c58e43963beb5aa242a58bbd30e2f
1 parent 8489b52 commit cc42090

8 files changed

+848
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2002, 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 4530216
27+
@summary tests that DragSourceListeners are properly removed
28+
@key headful
29+
@run main RemoveDragSourceListenerTest
30+
*/
31+
32+
import java.awt.dnd.DragSource;
33+
import java.awt.dnd.DragSourceAdapter;
34+
import java.awt.dnd.DragSourceListener;
35+
import java.awt.dnd.DragSourceMotionListener;
36+
37+
38+
public class RemoveDragSourceListenerTest {
39+
public static void main(String[] args) {
40+
class TestDragSourceAdapter extends DragSourceAdapter {}
41+
42+
final DragSource dragSource = DragSource.getDefaultDragSource();
43+
44+
final DragSourceAdapter listeners[] = {
45+
new TestDragSourceAdapter(),
46+
new TestDragSourceAdapter(),
47+
new TestDragSourceAdapter() // should be three or more listeners
48+
};
49+
50+
for (int i = 0; i < listeners.length; i++) {
51+
dragSource.addDragSourceListener(listeners[i]);
52+
}
53+
54+
DragSourceListener[] dragSourceListeners =
55+
dragSource.getDragSourceListeners();
56+
57+
if (dragSourceListeners.length != listeners.length) {
58+
throw new RuntimeException("Unexpected length: " +
59+
dragSourceListeners.length);
60+
}
61+
62+
for (int i = 0; i < listeners.length; i++) {
63+
dragSource.removeDragSourceListener(listeners[i]);
64+
}
65+
66+
for (int i = 0; i < listeners.length; i++) {
67+
dragSource.addDragSourceMotionListener(listeners[i]);
68+
}
69+
70+
DragSourceMotionListener[] dragSourceMotionListeners =
71+
dragSource.getDragSourceMotionListeners();
72+
73+
if (dragSourceMotionListeners.length != listeners.length) {
74+
throw new RuntimeException("Unexpected length: " +
75+
dragSourceMotionListeners.length);
76+
}
77+
78+
for (int i = 0; i < listeners.length; i++) {
79+
dragSource.removeDragSourceMotionListener(listeners[i]);
80+
}
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2001, 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 4411368
27+
@summary tests the app doesn't crash if the child drop target is removed
28+
after the parent drop target is removed
29+
@key headful
30+
@run main RemoveParentChildDropTargetTest
31+
*/
32+
33+
import java.awt.EventQueue;
34+
import java.awt.Frame;
35+
import java.awt.Label;
36+
import java.awt.Panel;
37+
import java.awt.dnd.DropTarget;
38+
import java.awt.dnd.DropTargetAdapter;
39+
import java.awt.dnd.DropTargetDropEvent;
40+
import java.lang.reflect.InvocationTargetException;
41+
42+
43+
public class RemoveParentChildDropTargetTest {
44+
45+
static Frame frame;
46+
static Panel panel;
47+
static Label label;
48+
49+
public static void main(String[] args) throws InterruptedException,
50+
InvocationTargetException {
51+
EventQueue.invokeAndWait(() -> {
52+
frame = new Frame("RemoveParentChildDropTargetTest");
53+
panel = new Panel();
54+
label = new Label("Label");
55+
panel.add(label);
56+
frame.add(panel);
57+
frame.pack();
58+
59+
panel.setDropTarget(new DropTarget(panel, new DropTargetAdapter() {
60+
public void drop(DropTargetDropEvent dtde) {}
61+
}));
62+
label.setDropTarget(new DropTarget(label, new DropTargetAdapter() {
63+
public void drop(DropTargetDropEvent dtde) {}
64+
}));
65+
panel.setDropTarget(null);
66+
frame.setVisible(true);
67+
68+
label.setDropTarget(null);
69+
});
70+
71+
EventQueue.invokeAndWait(() -> {
72+
if (frame != null) {
73+
frame.dispose();
74+
}
75+
});
76+
}
77+
}

0 commit comments

Comments
 (0)