Skip to content

Commit 813ca6d

Browse files
author
Andrew Lu
committedJul 1, 2024
8260633: [macos] java/awt/dnd/MouseEventAfterStartDragTest/MouseEventAfterStartDragTest.html test failed
Backport-of: 6f7ddbec7d0bc459d44b6518fe1d982eaba7f37b
1 parent eff4452 commit 813ca6d

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
* Copyright (c) 2014, 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.Dimension;
27+
import java.awt.Frame;
28+
import java.awt.Point;
29+
import java.awt.Robot;
30+
import java.awt.datatransfer.StringSelection;
31+
import java.awt.datatransfer.Transferable;
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.DragSourceAdapter;
37+
import java.awt.dnd.DragSourceDragEvent;
38+
import java.awt.dnd.DragSourceListener;
39+
import java.awt.event.AWTEventListener;
40+
import java.awt.event.InputEvent;
41+
import java.awt.event.MouseEvent;
42+
import java.awt.event.MouseMotionAdapter;
43+
import java.awt.event.MouseMotionListener;
44+
import javax.swing.SwingUtilities;
45+
46+
/*
47+
* @test
48+
* @bug 4613903
49+
* @summary verifies that mouse events are not dispatched during drag
50+
* @key headful
51+
* @run main MouseEventAfterStartDragTest
52+
*/
53+
54+
public final class MouseEventAfterStartDragTest implements AWTEventListener {
55+
final Frame frame = new Frame();
56+
volatile Point srcPoint;
57+
volatile Dimension d;
58+
volatile MouseEvent lastMouseEvent = null;
59+
volatile boolean passed = true;
60+
final DragSource dragSource = DragSource.getDefaultDragSource();
61+
final Transferable transferable = new StringSelection("TEXT");
62+
63+
final MouseMotionListener mouseMotionListener = new MouseMotionAdapter() {
64+
public void mouseDragged(MouseEvent e) {
65+
System.out.println("mouseDragged: " + e
66+
+ ", hash:" + e.hashCode());
67+
if (lastMouseEvent != null && !e.equals(lastMouseEvent)) {
68+
System.out.println("Unexpected: " + e
69+
+ ", hash:" + e.hashCode());
70+
passed = false;
71+
}
72+
}
73+
};
74+
75+
final DragSourceListener dragSourceListener = new DragSourceAdapter() {
76+
public void dragDropEnd(DragSourceDragEvent dsde) {
77+
System.out.println("dragDropEnd: " + dsde);
78+
lastMouseEvent = null;
79+
}
80+
};
81+
82+
final DragGestureListener dragGestureListener = new DragGestureListener() {
83+
public void dragGestureRecognized(DragGestureEvent dge) {
84+
System.out.println("dragGestureRecognized: " + dge);
85+
Object[] events = dge.toArray();
86+
Object lastEvent = events[events.length - 1];
87+
if (lastEvent instanceof MouseEvent) {
88+
lastMouseEvent = (MouseEvent) lastEvent;
89+
}
90+
System.out.println("The last mouse event: " + lastMouseEvent
91+
+ ", hash:" + lastMouseEvent.hashCode());
92+
dge.startDrag(null, transferable, dragSourceListener);
93+
}
94+
};
95+
96+
static final Object SYNC_LOCK = new Object();
97+
static final int MOUSE_RELEASE_TIMEOUT = 1000;
98+
volatile Component clickedComponent = null;
99+
100+
public static void main(String[] args) throws Exception {
101+
System.setProperty("awt.dnd.drag.threshold", "0");
102+
MouseEventAfterStartDragTest app = new MouseEventAfterStartDragTest();
103+
try {
104+
app.createAndShowGUI();
105+
app.test();
106+
} finally {
107+
app.dispose();
108+
}
109+
}
110+
111+
public void createAndShowGUI() throws Exception {
112+
SwingUtilities.invokeAndWait(() -> {
113+
frame.setTitle("Test frame");
114+
frame.setBounds(100, 100, 200, 200);
115+
frame.setLocationRelativeTo(null);
116+
frame.addMouseMotionListener(mouseMotionListener);
117+
dragSource.createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_COPY_OR_MOVE,
118+
dragGestureListener);
119+
120+
frame.getToolkit().addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK);
121+
frame.setVisible(true);
122+
});
123+
}
124+
125+
public static int sign(int n) {
126+
return n < 0 ? -1 : n == 0 ? 0 : 1;
127+
}
128+
129+
public void test() throws Exception {
130+
final Robot robot = new Robot();
131+
robot.setAutoDelay(45);
132+
robot.waitForIdle();
133+
134+
SwingUtilities.invokeAndWait(() -> {
135+
srcPoint = frame.getLocationOnScreen();
136+
d = frame.getSize();
137+
});
138+
srcPoint.translate(d.width / 2, d.height / 2);
139+
140+
if (!pointInComponent(robot, srcPoint, frame)) {
141+
System.err.println("WARNING: Couldn't locate source frame.");
142+
return;
143+
}
144+
145+
final Point dstPoint = new Point(srcPoint);
146+
dstPoint.translate(d.width / 4, d.height / 4);
147+
148+
if (!pointInComponent(robot, dstPoint, frame)) {
149+
System.err.println("WARNING: Couldn't locate target frame.");
150+
return;
151+
}
152+
153+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
154+
robot.mouseMove(srcPoint.x, srcPoint.y);
155+
robot.delay(250);
156+
System.out.println("srcPoint = " + srcPoint);
157+
for (; !srcPoint.equals(dstPoint);
158+
srcPoint.translate(sign(dstPoint.x - srcPoint.x),
159+
sign(dstPoint.y - srcPoint.y))) {
160+
robot.mouseMove(srcPoint.x, srcPoint.y);
161+
System.out.println("srcPoint = " + srcPoint);
162+
}
163+
164+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
165+
System.out.println("done");
166+
robot.waitForIdle();
167+
robot.delay(MOUSE_RELEASE_TIMEOUT);
168+
169+
if (!passed) {
170+
throw new RuntimeException("Test failed");
171+
}
172+
}
173+
174+
public void dispose() throws Exception {
175+
SwingUtilities.invokeAndWait(() -> {
176+
if (frame != null) {
177+
frame.dispose();
178+
}
179+
});
180+
}
181+
182+
public void reset() {
183+
clickedComponent = null;
184+
}
185+
186+
public void eventDispatched(AWTEvent e) {
187+
if (e.getID() == MouseEvent.MOUSE_RELEASED) {
188+
clickedComponent = (Component) e.getSource();
189+
synchronized (SYNC_LOCK) {
190+
SYNC_LOCK.notifyAll();
191+
}
192+
}
193+
}
194+
195+
boolean pointInComponent(Robot robot, Point p, Component comp)
196+
throws InterruptedException {
197+
robot.waitForIdle();
198+
reset();
199+
robot.mouseMove(p.x, p.y);
200+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
201+
synchronized (SYNC_LOCK) {
202+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
203+
SYNC_LOCK.wait(MOUSE_RELEASE_TIMEOUT);
204+
}
205+
206+
Component c = clickedComponent;
207+
208+
while (c != null && c != comp) {
209+
c = c.getParent();
210+
}
211+
212+
return c == comp;
213+
}
214+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jul 1, 2024

@openjdk-notifier[bot]
Please sign in to comment.