Skip to content

Commit 671cdc0

Browse files
author
duke
committedOct 4, 2024
Automatic merge of jdk:master into master
2 parents 5a46d9f + 0dd4997 commit 671cdc0

7 files changed

+837
-0
lines changed
 

‎test/jdk/ProblemList.txt

+3
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ java/awt/KeyboardFocusmanager/ConsumeNextMnemonicKeyTypedTest/ConsumeNextMnemoni
467467
java/awt/Window/GetScreenLocation/GetScreenLocationTest.java 8225787 linux-x64
468468
java/awt/Dialog/MakeWindowAlwaysOnTop/MakeWindowAlwaysOnTop.java 8266243 macosx-aarch64
469469
java/awt/dnd/BadSerializationTest/BadSerializationTest.java 8277817 linux-x64,windows-x64
470+
java/awt/dnd/DragSourceMotionListenerTest.java 8225131 windows-all
471+
java/awt/dnd/RejectDragTest.java 7124259 macosx-all
472+
java/awt/dnd/DnDHTMLToOutlookTest/DnDHTMLToOutlookTest.java 8027424 generic-all
470473
java/awt/GraphicsDevice/DisplayModes/UnknownRefrshRateTest.java 8286436 macosx-aarch64
471474
java/awt/image/multiresolution/MultiresolutionIconTest.java 8291979 linux-x64,windows-all
472475
java/awt/event/SequencedEvent/MultipleContextsFunctionalTest.java 8305061 macosx-x64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2011, 2024, 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.BorderLayout;
25+
import java.awt.Color;
26+
import java.awt.Component;
27+
import java.awt.Frame;
28+
import java.awt.Panel;
29+
30+
31+
/*
32+
* @test
33+
* @bug 6392086
34+
* @summary Tests dnd to another screen
35+
* @library /java/awt/regtesthelpers
36+
* @build PassFailJFrame
37+
* @run main/manual DnDHTMLToOutlookTest
38+
*/
39+
40+
public class DnDHTMLToOutlookTest {
41+
42+
private static final String INSTRUCTIONS = """
43+
The window contains a yellow button. Click on the button
44+
to copy HTML from DnDSource.html file into the clipboard or drag
45+
HTML context. Paste into or drop over the HTML capable editor in
46+
external application such as Outlook, Word.
47+
48+
When the mouse enters the editor, cursor should change to indicate
49+
that copy operation is about to happen and then release the mouse
50+
button. HTML text without tags should appear inside the document.
51+
52+
You should be able to repeat this operation multiple times.
53+
If the above is true Press PASS else FAIL.
54+
""";
55+
56+
public static void main(String[] args) throws Exception {
57+
PassFailJFrame.builder()
58+
.title("Test Instructions")
59+
.instructions(INSTRUCTIONS)
60+
.columns(40)
61+
.testUI(DnDHTMLToOutlookTest::createAndShowUI)
62+
.build()
63+
.awaitAndCheck();
64+
}
65+
66+
private static Frame createAndShowUI() {
67+
Frame frame = new Frame("DnDHTMLToOutlookTest");
68+
Panel mainPanel;
69+
Component dragSource;
70+
71+
mainPanel = new Panel();
72+
mainPanel.setLayout(new BorderLayout());
73+
74+
mainPanel.setBackground(Color.YELLOW);
75+
dragSource = new DnDSource("Drag ME (HTML)!");
76+
77+
mainPanel.add(dragSource, BorderLayout.CENTER);
78+
frame.add(mainPanel);
79+
frame.setSize(200, 200);
80+
return frame;
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!--
2+
Copyright (c) 2011, 2024, 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+
<h1>DnDHTMLToOutlookTest<br>HTML Drag & Paste problem</h1>
25+
<p>if you see the bold header above without HTML tags and without StartHTML as the first word, press PASS</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright (c) 2011, 2024, 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.Color;
26+
import java.awt.Toolkit;
27+
import java.awt.datatransfer.DataFlavor;
28+
import java.awt.datatransfer.Transferable;
29+
import java.awt.datatransfer.UnsupportedFlavorException;
30+
import java.awt.dnd.DnDConstants;
31+
import java.awt.dnd.DragGestureEvent;
32+
import java.awt.dnd.DragGestureListener;
33+
import java.awt.dnd.DragSource;
34+
import java.awt.dnd.DragSourceDragEvent;
35+
import java.awt.dnd.DragSourceDropEvent;
36+
import java.awt.dnd.DragSourceEvent;
37+
import java.awt.dnd.DragSourceListener;
38+
import java.awt.dnd.InvalidDnDOperationException;
39+
import java.awt.event.ActionEvent;
40+
import java.awt.event.ActionListener;
41+
import java.io.ByteArrayInputStream;
42+
import java.io.InputStream;
43+
import java.nio.file.Files;
44+
import java.nio.file.Paths;
45+
46+
class DnDSource extends Button implements Transferable,
47+
DragGestureListener,
48+
DragSourceListener {
49+
private DataFlavor m_df;
50+
private transient int m_dropAction;
51+
private ByteArrayInputStream m_data = null;
52+
53+
DnDSource(String label) {
54+
super(label);
55+
setBackground(Color.yellow);
56+
setForeground(Color.blue);
57+
setSize(200, 120);
58+
59+
try {
60+
m_df = new DataFlavor("text/html; Class=" + InputStream.class.getName() + "; charset=UTF-8");
61+
} catch (Exception e) {
62+
e.printStackTrace();
63+
}
64+
65+
DragSource dragSource = new DragSource();
66+
dragSource.createDefaultDragGestureRecognizer(
67+
this,
68+
DnDConstants.ACTION_COPY_OR_MOVE,
69+
this
70+
);
71+
dragSource.addDragSourceListener(this);
72+
73+
String dir = System.getProperty("test.src", ".");
74+
75+
try {
76+
m_data = new ByteArrayInputStream(Files.readAllBytes(
77+
Paths.get(dir, "DnDSource.html")));
78+
m_data.mark(m_data.available());
79+
addActionListener(
80+
new ActionListener(){
81+
public void actionPerformed(ActionEvent ae){
82+
Toolkit.getDefaultToolkit().getSystemClipboard()
83+
.setContents( DnDSource.this, null);
84+
}
85+
}
86+
);
87+
} catch (Exception e) {
88+
e.printStackTrace();
89+
}
90+
}
91+
92+
public void dragGestureRecognized(DragGestureEvent dge) {
93+
System.err.println("starting Drag");
94+
try {
95+
dge.startDrag(null, this, this);
96+
} catch (InvalidDnDOperationException e) {
97+
e.printStackTrace();
98+
}
99+
}
100+
101+
public void dragEnter(DragSourceDragEvent dsde) {
102+
System.err.println("[Source] dragEnter");
103+
}
104+
105+
public void dragOver(DragSourceDragEvent dsde) {
106+
System.err.println("[Source] dragOver");
107+
m_dropAction = dsde.getDropAction();
108+
System.out.println("m_dropAction = " + m_dropAction);
109+
}
110+
111+
public void dragExit(DragSourceEvent dsde) {
112+
System.err.println("[Source] dragExit");
113+
}
114+
115+
public void dragDropEnd(DragSourceDropEvent dsde) {
116+
System.err.println("[Source] dragDropEnd");
117+
}
118+
119+
public void dropActionChanged(DragSourceDragEvent dsde) {
120+
System.err.println("[Source] dropActionChanged");
121+
m_dropAction = dsde.getDropAction();
122+
System.out.println("m_dropAction = " + m_dropAction);
123+
}
124+
125+
public DataFlavor[] getTransferDataFlavors() {
126+
return new DataFlavor[] {m_df};
127+
}
128+
129+
public boolean isDataFlavorSupported(DataFlavor sdf) {
130+
System.err.println("[Source] isDataFlavorSupported" + m_df.equals(sdf));
131+
return m_df.equals(sdf);
132+
}
133+
134+
public Object getTransferData(DataFlavor tdf) throws UnsupportedFlavorException {
135+
if (!m_df.equals(tdf)) {
136+
throw new UnsupportedFlavorException(tdf);
137+
}
138+
System.err.println("[Source] Ok");
139+
m_data.reset();
140+
return m_data;
141+
}
142+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
/*
2+
* Copyright (c) 2001, 2024, 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.AWTEvent;
25+
import java.awt.Component;
26+
import java.awt.Container;
27+
import java.awt.Dimension;
28+
import java.awt.EventQueue;
29+
import java.awt.Frame;
30+
import java.awt.GridLayout;
31+
import java.awt.Panel;
32+
import java.awt.Point;
33+
import java.awt.Robot;
34+
import java.awt.Toolkit;
35+
import java.awt.datatransfer.DataFlavor;
36+
import java.awt.datatransfer.StringSelection;
37+
import java.awt.datatransfer.Transferable;
38+
import java.awt.dnd.DnDConstants;
39+
import java.awt.dnd.DragGestureListener;
40+
import java.awt.dnd.DragSource;
41+
import java.awt.dnd.DragSourceAdapter;
42+
import java.awt.dnd.DragSourceDragEvent;
43+
import java.awt.dnd.DragSourceDropEvent;
44+
import java.awt.dnd.DropTarget;
45+
import java.awt.dnd.DropTargetAdapter;
46+
import java.awt.dnd.DropTargetDropEvent;
47+
import java.awt.dnd.DropTargetListener;
48+
import java.awt.event.AWTEventListener;
49+
import java.awt.event.InputEvent;
50+
import java.awt.event.KeyEvent;
51+
import java.awt.event.MouseEvent;
52+
import java.util.concurrent.CountDownLatch;
53+
import java.util.concurrent.TimeUnit;
54+
55+
/*
56+
* @test
57+
* @key headful
58+
* @bug 4422345
59+
* @summary tests that DragSourceMotionListeners work correctly and
60+
DragSourceEvents position is correct
61+
*/
62+
63+
public class DragSourceMotionListenerTest implements AWTEventListener {
64+
static class TestPanel extends Panel {
65+
final Dimension preferredDimension = new Dimension(200, 200);
66+
public Dimension getPreferredSize() {
67+
return preferredDimension;
68+
}
69+
}
70+
71+
private static Frame frame;
72+
private static final Panel source = new TestPanel();
73+
private static final Panel target = new TestPanel();
74+
private static final DragSource ds = DragSource.getDefaultDragSource();
75+
private static volatile CountDownLatch mouseReleaseEvent;
76+
77+
static volatile boolean passedTest1 = false;
78+
static volatile boolean passedTest2 = false;
79+
80+
private static final Point testPoint1 = new Point();
81+
private static final Point testPoint2 = new Point();
82+
private static volatile Point srcPoint;
83+
private static volatile Point dstOutsidePoint;
84+
private static volatile Point dstInsidePoint;
85+
86+
private static final Transferable t = new StringSelection("TEXT");
87+
private static final DragGestureListener gestureListener = e -> e.startDrag(null, t);
88+
89+
private static final DragSourceAdapter sourceAdapter = new DragSourceAdapter() {
90+
public void dragMouseMoved(DragSourceDragEvent dsde) {
91+
if (Math.abs(dsde.getX() - testPoint1.getX()) < 5) {
92+
passedTest1 = true;
93+
}
94+
}
95+
96+
public void dragDropEnd(DragSourceDropEvent dsde) {
97+
if (Math.abs(dsde.getX() - testPoint2.getX()) < 5) {
98+
passedTest2 = true;
99+
}
100+
}
101+
};
102+
103+
private static final DropTargetListener targetAdapter = new DropTargetAdapter() {
104+
public void drop(DropTargetDropEvent e) {
105+
e.acceptDrop(DnDConstants.ACTION_COPY);
106+
try {
107+
final Transferable t = e.getTransferable();
108+
final String str =
109+
(String) t.getTransferData(DataFlavor.stringFlavor);
110+
e.dropComplete(true);
111+
} catch (Exception ex) {
112+
ex.printStackTrace();
113+
e.dropComplete(false);
114+
}
115+
}
116+
};
117+
118+
private static final DropTarget dropTarget = new DropTarget(target, targetAdapter);
119+
Component clickedComponent = null;
120+
121+
private void createAndShowUI() {
122+
frame = new Frame("DragSourceMotionListenerTest");
123+
ds.addDragSourceListener(sourceAdapter);
124+
ds.addDragSourceMotionListener(sourceAdapter);
125+
ds.createDefaultDragGestureRecognizer(source, DnDConstants.ACTION_COPY, gestureListener);
126+
target.setDropTarget(dropTarget);
127+
128+
frame.setLayout(new GridLayout(1, 2));
129+
130+
frame.add(source);
131+
frame.add(target);
132+
133+
Toolkit.getDefaultToolkit()
134+
.addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK);
135+
frame.pack();
136+
frame.setVisible(true);
137+
}
138+
139+
public static void main(String[] args) throws Exception {
140+
try {
141+
Robot robot = new Robot();
142+
robot.setAutoDelay(10);
143+
144+
DragSourceMotionListenerTest dsmObj = new DragSourceMotionListenerTest();
145+
EventQueue.invokeAndWait(dsmObj::createAndShowUI);
146+
robot.waitForIdle();
147+
robot.delay(1000);
148+
149+
EventQueue.invokeAndWait(() -> {
150+
srcPoint = getPoint(source, 1);
151+
152+
dstOutsidePoint = getPoint(frame, 3);
153+
testPoint1.setLocation(dstOutsidePoint);
154+
155+
dstInsidePoint = getPoint(target, 1);
156+
testPoint2.setLocation(dstInsidePoint);
157+
});
158+
robot.waitForIdle();
159+
160+
if (!dsmObj.pointInComponent(robot, srcPoint, source)) {
161+
throw new RuntimeException("WARNING: Couldn't locate source panel.");
162+
}
163+
164+
if (!dsmObj.pointInComponent(robot, dstInsidePoint, target)) {
165+
throw new RuntimeException("WARNING: Couldn't locate target panel.");
166+
}
167+
168+
robot.mouseMove(srcPoint.x, srcPoint.y);
169+
robot.keyPress(KeyEvent.VK_CONTROL);
170+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
171+
for (; !srcPoint.equals(dstOutsidePoint);
172+
srcPoint.translate(sign(dstOutsidePoint.x - srcPoint.x),
173+
sign(dstOutsidePoint.y - srcPoint.y))) {
174+
robot.mouseMove(srcPoint.x, srcPoint.y);
175+
}
176+
177+
for (int i = 0; i < 10; i++) {
178+
robot.mouseMove(srcPoint.x, srcPoint.y++);
179+
}
180+
181+
for (;!srcPoint.equals(dstInsidePoint);
182+
srcPoint.translate(sign(dstInsidePoint.x - srcPoint.x),
183+
sign(dstInsidePoint.y - srcPoint.y))) {
184+
robot.mouseMove(srcPoint.x, srcPoint.y);
185+
}
186+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
187+
robot.keyRelease(KeyEvent.VK_CONTROL);
188+
robot.waitForIdle();
189+
robot.delay(1000);
190+
191+
if (!passedTest1) {
192+
throw new RuntimeException("Failed first test.");
193+
}
194+
195+
if (!passedTest2) {
196+
throw new RuntimeException("Failed second test.");
197+
}
198+
} finally {
199+
EventQueue.invokeAndWait(() -> {
200+
if (frame != null) {
201+
frame.dispose();
202+
}
203+
});
204+
}
205+
}
206+
207+
private static Point getPoint(Container container, int multiple) {
208+
Point p = container.getLocationOnScreen();
209+
Dimension d = container.getSize();
210+
p.translate(multiple * d.width / 2, d.height / 2);
211+
return p;
212+
}
213+
214+
public static int sign(int n) {
215+
return Integer.compare(n, 0);
216+
}
217+
218+
public void eventDispatched(AWTEvent e) {
219+
if (e.getID() == MouseEvent.MOUSE_RELEASED) {
220+
clickedComponent = (Component)e.getSource();
221+
mouseReleaseEvent.countDown();
222+
}
223+
}
224+
225+
boolean pointInComponent(Robot robot, Point p, Component comp) throws Exception {
226+
robot.waitForIdle();
227+
clickedComponent = null;
228+
mouseReleaseEvent = new CountDownLatch(1);
229+
robot.mouseMove(p.x, p.y);
230+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
231+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
232+
if (!mouseReleaseEvent.await(2, TimeUnit.SECONDS)) {
233+
throw new RuntimeException("Mouse Release Event not received");
234+
}
235+
236+
Component c = clickedComponent;
237+
while (c != null && c != comp) {
238+
c = c.getParent();
239+
}
240+
return c == comp;
241+
}
242+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright (c) 2005, 2024, 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.Frame;
25+
import java.awt.GraphicsDevice;
26+
import java.awt.GraphicsEnvironment;
27+
import java.awt.Label;
28+
import java.awt.Toolkit;
29+
import java.awt.Window;
30+
import java.awt.datatransfer.DataFlavor;
31+
import java.awt.datatransfer.StringSelection;
32+
import java.awt.datatransfer.Transferable;
33+
import java.awt.dnd.DnDConstants;
34+
import java.awt.dnd.DragGestureListener;
35+
import java.awt.dnd.DragSource;
36+
import java.awt.dnd.DropTarget;
37+
import java.awt.dnd.DropTargetAdapter;
38+
import java.awt.dnd.DropTargetDropEvent;
39+
import java.util.List;
40+
41+
import javax.swing.JOptionPane;
42+
43+
/*
44+
* @test
45+
* @bug 6179157
46+
* @key multimon
47+
* @summary Tests dnd to another screen
48+
* @library /java/awt/regtesthelpers
49+
* @build PassFailJFrame
50+
* @run main/manual DragToAnotherScreenTest
51+
*/
52+
53+
public class DragToAnotherScreenTest {
54+
private static Label label0;
55+
private static Label label1;
56+
private static final int HGAP = 20;
57+
58+
private static final String INSTRUCTIONS = """
59+
The following test is applicable for Single as well
60+
as Multi-monitor screens.
61+
62+
It is a semi-automated test, the test will prompt
63+
the user whether the drag and drop action was successful or not
64+
and automatically PASS/FAIL the test.
65+
66+
If on multi-monitor screens then please position
67+
the drag and drop windows on different screens.
68+
69+
If you can not move the mouse from the frame "Drag Source"
70+
to the frame "Drop Target" press PASS,
71+
else proceed to the next step.
72+
73+
Drag the label "Drag me" and drop it on the
74+
label "Drop on me".
75+
76+
If you can not drag to the second label (for example
77+
if you can not drag across screens) press FAIL.
78+
79+
After the drag and drop action, the test displays
80+
Success/Failure msg in JOptionPane.
81+
Click on OK button and the test is configured to
82+
automatically PASS/FAIL.
83+
""";
84+
85+
public static void main(String[] args) throws Exception {
86+
PassFailJFrame.builder()
87+
.title("Test Instructions")
88+
.instructions(INSTRUCTIONS)
89+
.columns(35)
90+
.testUI(DragToAnotherScreenTest::createAndShowUI)
91+
.positionTestUI(DragToAnotherScreenTest::positionMultiTestUI)
92+
.logArea(10)
93+
.build()
94+
.awaitAndCheck();
95+
}
96+
97+
private static List<Frame> createAndShowUI() {
98+
PassFailJFrame.log("----- System Configuration ----");
99+
PassFailJFrame.log("Toolkit:" + Toolkit.getDefaultToolkit()
100+
.getClass()
101+
.getName());
102+
103+
GraphicsDevice[] gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
104+
.getScreenDevices();
105+
if (gd.length == 1) {
106+
PassFailJFrame.log("Single Monitor");
107+
} else {
108+
PassFailJFrame.log("Multi-Monitor");
109+
}
110+
PassFailJFrame.log("--------------");
111+
PassFailJFrame.log("Test logs:\n");
112+
Frame frame0 = new Frame("Drag Source", gd[0].getDefaultConfiguration());
113+
frame0.setSize(300, 300);
114+
label0 = new Label("Drag me");
115+
frame0.add(label0);
116+
117+
Frame frame1 = new Frame("Drop Target", gd[(gd.length > 1 ? 1 : 0)].getDefaultConfiguration());
118+
frame1.setSize(300, 300);
119+
label1 = new Label("Drop on me");
120+
frame1.add(label1);
121+
122+
DragGestureListener dragGestureListener = dge -> dge.startDrag(null, new StringSelection(label0.getText()), null);
123+
new DragSource().createDefaultDragGestureRecognizer(label0,
124+
DnDConstants.ACTION_COPY, dragGestureListener);
125+
126+
DropTargetAdapter dropTargetAdapter = new DropTargetAdapter() {
127+
public void drop(DropTargetDropEvent dtde) {
128+
Transferable t = dtde.getTransferable();
129+
if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
130+
dtde.acceptDrop(DnDConstants.ACTION_COPY);
131+
try {
132+
String str = (String) t.getTransferData(DataFlavor.stringFlavor);
133+
label1.setText(str);
134+
JOptionPane.showMessageDialog(frame0,
135+
"getTransferData was successful",
136+
"Test Passed", JOptionPane.PLAIN_MESSAGE);
137+
} catch (Exception e) {
138+
dtde.dropComplete(false);
139+
e.printStackTrace();
140+
PassFailJFrame.log("getTransferData() Failed");
141+
JOptionPane.showMessageDialog(frame0,
142+
"getTransferData() Failed",
143+
"Test Failed", JOptionPane.ERROR_MESSAGE);
144+
PassFailJFrame.forceFail("getTransferData() Failed");
145+
}
146+
dtde.dropComplete(true);
147+
} else {
148+
dtde.rejectDrop();
149+
PassFailJFrame.log("stringFlavor is not supported by Transferable");
150+
JOptionPane.showMessageDialog(frame0,
151+
"stringFlavor is not supported by Transferable",
152+
"Test Failed", JOptionPane.ERROR_MESSAGE);
153+
PassFailJFrame.forceFail("stringFlavor is not supported by Transferable");
154+
}
155+
}
156+
};
157+
new DropTarget(label1, dropTargetAdapter);
158+
return List.of(frame0, frame1);
159+
}
160+
161+
private static void positionMultiTestUI(List<? extends Window> windows,
162+
PassFailJFrame.InstructionUI instructionUI) {
163+
int x = instructionUI.getLocation().x + instructionUI.getSize().width + HGAP;
164+
for (Window w : windows) {
165+
w.setLocation(x, instructionUI.getLocation().y);
166+
x += w.getWidth() + HGAP;
167+
}
168+
}
169+
}
+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright (c) 2002, 2024, 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.EventQueue;
25+
import java.awt.Frame;
26+
import java.awt.Point;
27+
import java.awt.Robot;
28+
import java.awt.datatransfer.StringSelection;
29+
import java.awt.dnd.DnDConstants;
30+
import java.awt.dnd.DragGestureListener;
31+
import java.awt.dnd.DragSource;
32+
import java.awt.dnd.DragSourceAdapter;
33+
import java.awt.dnd.DragSourceDragEvent;
34+
import java.awt.dnd.DragSourceDropEvent;
35+
import java.awt.dnd.DragSourceEvent;
36+
import java.awt.dnd.DragSourceListener;
37+
import java.awt.dnd.DropTarget;
38+
import java.awt.dnd.DropTargetAdapter;
39+
import java.awt.dnd.DropTargetDragEvent;
40+
import java.awt.dnd.DropTargetDropEvent;
41+
import java.awt.event.InputEvent;
42+
43+
/*
44+
* @test
45+
* @key headful
46+
* @bug 4407521
47+
* @summary Tests that DragSourceListener.dragEnter() and
48+
DragSourceListener.dragOver() are not called after
49+
drag rejecting, but DragSourceListener.dragExit() is.
50+
*/
51+
52+
public class RejectDragTest {
53+
private static Frame frame;
54+
private static Robot robot;
55+
private static volatile boolean dragEnterCalled;
56+
private static volatile boolean dragOverCalled;
57+
private static volatile boolean dragExitCalledAtFirst;
58+
private static volatile Point startPoint;
59+
private static volatile Point endPoint;
60+
61+
public static void main(String[] args) throws Exception {
62+
try {
63+
robot = new Robot();
64+
65+
EventQueue.invokeAndWait(RejectDragTest::createAndShowUI);
66+
robot.waitForIdle();
67+
robot.delay(1000);
68+
69+
EventQueue.invokeAndWait(RejectDragTest::addDnDListeners);
70+
robot.waitForIdle();
71+
72+
testDnD();
73+
robot.waitForIdle();
74+
robot.delay(200);
75+
} finally {
76+
EventQueue.invokeAndWait(() -> {
77+
if (frame != null) {
78+
frame.dispose();
79+
}
80+
});
81+
}
82+
}
83+
84+
private static void addDnDListeners() {
85+
final DragSourceListener dragSourceListener = new DragSourceAdapter() {
86+
private boolean first = true;
87+
88+
public void dragEnter(DragSourceDragEvent dsde) {
89+
first = false;
90+
dragEnterCalled = true;
91+
}
92+
93+
public void dragExit(DragSourceEvent dse) {
94+
if (first) {
95+
dragExitCalledAtFirst = true;
96+
first = false;
97+
}
98+
}
99+
100+
public void dragDropEnd(DragSourceDropEvent dsde) {
101+
first = false;
102+
}
103+
104+
public void dragOver(DragSourceDragEvent dsde) {
105+
first = false;
106+
dragOverCalled = true;
107+
}
108+
109+
public void dropActionChanged(DragSourceDragEvent dsde) {
110+
first = false;
111+
}
112+
};
113+
114+
DragGestureListener dragGestureListener =
115+
dge -> dge.startDrag(null, new StringSelection("OKAY"),
116+
dragSourceListener);
117+
new DragSource().createDefaultDragGestureRecognizer(frame,
118+
DnDConstants.ACTION_COPY,
119+
dragGestureListener);
120+
121+
DropTargetAdapter dropTargetListener = new DropTargetAdapter() {
122+
public void dragEnter(DropTargetDragEvent dtde) {
123+
dtde.rejectDrag();
124+
}
125+
126+
public void drop(DropTargetDropEvent dtde) {
127+
dtde.rejectDrop();
128+
}
129+
};
130+
131+
new DropTarget(frame, dropTargetListener);
132+
}
133+
134+
private static void createAndShowUI() {
135+
frame = new Frame("RejectDragTest");
136+
frame.setSize(200, 200);
137+
frame.setLocationRelativeTo(null);
138+
frame.setVisible(true);
139+
}
140+
141+
private static void testDnD() throws Exception {
142+
EventQueue.invokeAndWait(() -> {
143+
Point start = frame.getLocationOnScreen();
144+
start.translate(50, 50);
145+
startPoint = start;
146+
147+
Point end = new Point(start);
148+
end.translate(150, 150);
149+
endPoint = end;
150+
});
151+
152+
robot.mouseMove(startPoint.x, startPoint.y);
153+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
154+
for (Point p = new Point(startPoint); !p.equals(endPoint);
155+
p.translate(sign(endPoint.x - p.x),
156+
sign(endPoint.y - p.y))) {
157+
robot.mouseMove(p.x, p.y);
158+
robot.delay(30);
159+
}
160+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
161+
162+
if (dragEnterCalled || dragOverCalled) {
163+
throw new RuntimeException("Test failed: " +
164+
(dragEnterCalled ? "DragSourceListener.dragEnter() was called; " : "") +
165+
(dragOverCalled ? "DragSourceListener.dragOver() was called; " : "") +
166+
(!dragExitCalledAtFirst ? "DragSourceListener.dragExit() was not " +
167+
"called immediately after rejectDrag() " : ""));
168+
}
169+
}
170+
171+
public static int sign(int n) {
172+
return Integer.compare(n, 0);
173+
}
174+
}

0 commit comments

Comments
 (0)
Please sign in to comment.