Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mouse/EnterExitEvents/DragWindowOutOfFrameTest.java
47311 views
1
/*
2
* Copyright (c) 2005, 2012, 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 7154048
27
* @summary Window created under a mouse does not receive mouse enter event.
28
* Mouse Entered/Exited events should be generated during dragging the window
29
* out of the frame and to the frame.
30
* @library ../../regtesthelpers
31
* @build Util
32
* @author alexandr.scherbatiy area=awt.event
33
* @run main DragWindowOutOfFrameTest
34
*/
35
import java.awt.*;
36
import java.awt.event.*;
37
import javax.swing.*;
38
39
import java.util.concurrent.*;
40
41
import test.java.awt.regtesthelpers.Util;
42
43
public class DragWindowOutOfFrameTest {
44
45
private static volatile int dragWindowMouseEnteredCount = 0;
46
private static volatile int dragWindowMouseExitedCount = 0;
47
private static volatile int dragWindowMouseReleasedCount = 0;
48
private static volatile int buttonMouseEnteredCount = 0;
49
private static volatile int buttonMouseExitedCount = 0;
50
private static volatile int labelMouseEnteredCount = 0;
51
private static volatile int labelMouseExitedCount = 0;
52
private static volatile int labelMouseReleasedCount = 0;
53
private static MyDragWindow dragWindow;
54
private static JLabel label;
55
private static JButton button;
56
57
public static void main(String[] args) throws Exception {
58
59
Robot robot = new Robot();
60
robot.setAutoDelay(50);
61
62
SwingUtilities.invokeAndWait(new Runnable() {
63
64
@Override
65
public void run() {
66
createAndShowGUI();
67
}
68
});
69
70
robot.waitForIdle();
71
72
Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {
73
74
@Override
75
public Point call() throws Exception {
76
return getCenterPoint(label);
77
}
78
});
79
80
81
robot.mouseMove(pointToClick.x, pointToClick.y);
82
robot.mousePress(InputEvent.BUTTON1_MASK);
83
robot.waitForIdle();
84
85
if (dragWindowMouseEnteredCount != 1 && dragWindowMouseExitedCount != 0) {
86
throw new RuntimeException(
87
"Wrong number mouse Entered/Exited events on Drag Window!");
88
}
89
90
Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {
91
92
@Override
93
public Point call() throws Exception {
94
label.addMouseListener(new LabelMouseListener());
95
button.addMouseListener(new ButtonMouseListener());
96
return getCenterPoint(button);
97
}
98
});
99
100
robot.mouseMove(450, pointToClick.y);
101
robot.waitForIdle();
102
103
if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {
104
throw new RuntimeException(
105
"Wrong number Mouse Entered/Exited events on label!");
106
}
107
108
robot.mouseMove(450, pointToDrag.y);
109
robot.waitForIdle();
110
111
if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {
112
throw new RuntimeException(
113
"Wrong number Mouse Entered/Exited events on label!");
114
}
115
116
if (buttonMouseEnteredCount != 0 && buttonMouseExitedCount != 0) {
117
throw new RuntimeException(
118
"Wrong number Mouse Entered/Exited events on button!");
119
}
120
121
robot.mouseMove(pointToDrag.y, pointToDrag.y);
122
robot.waitForIdle();
123
124
if (buttonMouseEnteredCount != 1 && buttonMouseExitedCount != 0) {
125
throw new RuntimeException(
126
"Wrong number Mouse Entered/Exited events on button!");
127
}
128
129
robot.mouseRelease(InputEvent.BUTTON1_MASK);
130
robot.waitForIdle();
131
132
if (labelMouseReleasedCount != 1) {
133
throw new RuntimeException("No MouseReleased event on label!");
134
}
135
}
136
137
private static Point getCenterPoint(Component comp) {
138
Point p = comp.getLocationOnScreen();
139
Rectangle rect = comp.getBounds();
140
return new Point(p.x + rect.width / 2, p.y + rect.height / 2);
141
}
142
143
private static void createAndShowGUI() {
144
145
JFrame frame = new JFrame("Main Frame");
146
frame.setLocation(100, 100);
147
frame.setSize(300, 200);
148
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
149
150
label = new JLabel("Label");
151
152
DragWindowCreationMouseListener listener = new DragWindowCreationMouseListener(frame);
153
label.addMouseListener(listener);
154
label.addMouseMotionListener(listener);
155
156
button = new JButton("Button");
157
Panel panel = new Panel(new BorderLayout());
158
159
panel.add(label, BorderLayout.NORTH);
160
panel.add(button, BorderLayout.CENTER);
161
162
frame.getContentPane().add(panel);
163
frame.setVisible(true);
164
165
}
166
167
private static Point getAbsoluteLocation(MouseEvent e) {
168
return new Point(e.getXOnScreen(), e.getYOnScreen());
169
}
170
171
static class MyDragWindow extends Window {
172
173
public MyDragWindow(Window parent, Point location) {
174
super(parent);
175
setSize(500, 300);
176
setVisible(true);
177
JPanel panel = new JPanel();
178
add(panel);
179
setLocation(location.x - 250, location.y - 150);
180
addMouseListener(new DragWindowMouseListener());
181
}
182
183
void dragTo(Point point) {
184
setLocation(point.x - 250, point.y - 150);
185
}
186
}
187
188
static class DragWindowCreationMouseListener extends MouseAdapter {
189
190
Point origin;
191
Window parent;
192
193
public DragWindowCreationMouseListener(Window parent) {
194
this.parent = parent;
195
}
196
197
@Override
198
public void mousePressed(MouseEvent e) {
199
if (dragWindow == null) {
200
dragWindow = new MyDragWindow(parent, getAbsoluteLocation(e));
201
} else {
202
dragWindow.setVisible(true);
203
dragWindow.dragTo(getAbsoluteLocation(e));
204
}
205
}
206
207
@Override
208
public void mouseReleased(MouseEvent e) {
209
labelMouseReleasedCount++;
210
if (dragWindow != null) {
211
dragWindow.setVisible(false);
212
}
213
}
214
215
public void mouseDragged(MouseEvent e) {
216
if (dragWindow != null) {
217
dragWindow.dragTo(getAbsoluteLocation(e));
218
}
219
}
220
}
221
222
static class DragWindowMouseListener extends MouseAdapter {
223
224
@Override
225
public void mouseEntered(MouseEvent e) {
226
dragWindowMouseEnteredCount++;
227
}
228
229
@Override
230
public void mouseExited(MouseEvent e) {
231
dragWindowMouseExitedCount++;
232
}
233
234
@Override
235
public void mouseReleased(MouseEvent e) {
236
dragWindowMouseReleasedCount++;
237
}
238
}
239
240
static class LabelMouseListener extends MouseAdapter {
241
242
@Override
243
public void mouseEntered(MouseEvent e) {
244
labelMouseEnteredCount++;
245
}
246
247
@Override
248
public void mouseExited(MouseEvent e) {
249
labelMouseExitedCount++;
250
}
251
}
252
253
static class ButtonMouseListener extends MouseAdapter {
254
255
@Override
256
public void mouseEntered(MouseEvent e) {
257
buttonMouseEnteredCount++;
258
}
259
260
@Override
261
public void mouseExited(MouseEvent e) {
262
buttonMouseExitedCount++;
263
}
264
}
265
}
266
267