Skip to content

Commit 6cf39a7

Browse files
committed
8341170: Open source several Choice related tests (part 2)
Backport-of: 52eded4a9ce612a978ae15d5b606784bcf671c69
1 parent 94abc6a commit 6cf39a7

File tree

5 files changed

+635
-0
lines changed

5 files changed

+635
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* Copyright (c) 2000, 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+
/*
25+
* @test
26+
* @bug 6251983 6722236
27+
* @summary MouseDragged events not triggered for Choice when dragging it with left mouse button
28+
* @key headful
29+
* @run main ChoiceDragEventsInside
30+
*/
31+
32+
import java.awt.Choice;
33+
import java.awt.Color;
34+
import java.awt.Dimension;
35+
import java.awt.EventQueue;
36+
import java.awt.FlowLayout;
37+
import java.awt.Frame;
38+
import java.awt.Point;
39+
import java.awt.Robot;
40+
import java.awt.event.InputEvent;
41+
import java.awt.event.KeyEvent;
42+
import java.awt.event.MouseEvent;
43+
import java.awt.event.MouseMotionAdapter;
44+
import java.lang.reflect.InvocationTargetException;
45+
46+
public class ChoiceDragEventsInside extends Frame {
47+
Robot robot;
48+
Choice choice1;
49+
Point pt;
50+
Dimension size;
51+
volatile boolean mouseDragged = false;
52+
volatile boolean mouseDraggedOutside = false;
53+
54+
public void setupUI() {
55+
setTitle("Choce Drag Events Inside");
56+
choice1 = new Choice();
57+
for (int i = 1; i < 50; i++) {
58+
choice1.add("item-0" + i);
59+
}
60+
choice1.setForeground(Color.red);
61+
choice1.setBackground(Color.red);
62+
choice1.addMouseMotionListener(new MouseMotionAdapter() {
63+
public void mouseMoved(MouseEvent me) {
64+
System.out.println(me);
65+
}
66+
67+
public void mouseDragged(MouseEvent me) {
68+
System.out.println(me);
69+
mouseDragged = true;
70+
if (me.getY() < 0) {
71+
mouseDraggedOutside = true;
72+
}
73+
}
74+
}
75+
);
76+
add(choice1);
77+
setLayout(new FlowLayout());
78+
setSize(200, 200);
79+
setLocationRelativeTo(null);
80+
setVisible(true);
81+
validate();
82+
}
83+
84+
public void start() {
85+
try {
86+
robot = new Robot();
87+
robot.setAutoWaitForIdle(true);
88+
robot.setAutoDelay(50);
89+
robot.delay(100);
90+
EventQueue.invokeAndWait(() -> {
91+
pt = choice1.getLocationOnScreen();
92+
size = choice1.getSize();
93+
});
94+
testDragInsideChoice(InputEvent.BUTTON1_MASK);
95+
testDragInsideChoiceList(InputEvent.BUTTON1_MASK);
96+
testDragOutsideChoice(InputEvent.BUTTON1_MASK);
97+
} catch (Throwable e) {
98+
throw new RuntimeException("Test failed. Exception thrown: " + e);
99+
}
100+
}
101+
102+
public void testDragInsideChoice(int button) {
103+
robot.mouseMove(pt.x + size.width / 2, pt.y + size.height / 2);
104+
robot.delay(100);
105+
robot.mousePress(button);
106+
robot.mouseRelease(button);
107+
robot.delay(200);
108+
robot.mousePress(button);
109+
robot.mouseRelease(button);
110+
robot.delay(200);
111+
112+
//close opened choice
113+
robot.keyPress(KeyEvent.VK_ESCAPE);
114+
robot.keyRelease(KeyEvent.VK_ESCAPE);
115+
robot.delay(200);
116+
117+
robot.mouseMove(pt.x + size.width / 4, pt.y + size.height / 2);
118+
robot.mousePress(button);
119+
120+
dragMouse(pt.x + size.width / 4, pt.y + size.height / 2,
121+
pt.x + size.width * 3 / 4, pt.y + size.height / 2);
122+
robot.mouseRelease(button);
123+
robot.delay(200);
124+
if (!mouseDragged) {
125+
throw new RuntimeException("Test failed. Choice should generate MouseDragged events inside Choice itself");
126+
} else {
127+
System.out.println("Stage 1 passed. Choice generates MouseDragged events inside Choice itself");
128+
}
129+
mouseDragged = false;
130+
//close opened choice
131+
robot.keyPress(KeyEvent.VK_ESCAPE);
132+
robot.keyRelease(KeyEvent.VK_ESCAPE);
133+
robot.delay(200);
134+
}
135+
136+
public void testDragInsideChoiceList(int button) {
137+
robot.mouseMove(pt.x + size.width / 2, pt.y + size.height / 2);
138+
robot.delay(100);
139+
robot.mousePress(button);
140+
robot.mouseRelease(button);
141+
robot.delay(200);
142+
143+
robot.mouseMove(pt.x + size.width / 2, pt.y + 5 * size.height);
144+
robot.delay(200);
145+
robot.mousePress(button);
146+
147+
dragMouse(pt.x + size.width / 2, pt.y + 5 * size.height,
148+
pt.x + size.width / 2, pt.y + 8 * size.height);
149+
robot.mouseRelease(button);
150+
robot.delay(200);
151+
if (mouseDragged) {
152+
throw new RuntimeException("Test failed. Choice shouldn't generate MouseDragged events inside Choice's list");
153+
} else {
154+
System.out.println("Stage 2 passed. Choice doesn't generate MouseDragged events inside Choice's list");
155+
}
156+
robot.keyPress(KeyEvent.VK_ESCAPE);
157+
robot.keyRelease(KeyEvent.VK_ESCAPE);
158+
robot.delay(200);
159+
mouseDragged = false;
160+
}
161+
162+
public void testDragOutsideChoice(int button) {
163+
pt = choice1.getLocationOnScreen();
164+
robot.mouseMove(pt.x + size.width / 2, pt.y + size.height / 2);
165+
robot.delay(100);
166+
167+
robot.mousePress(button);
168+
//drag mouse outside of Choice
169+
dragMouse(pt.x + size.width / 2, pt.y + size.height / 2,
170+
pt.x + size.width / 2, pt.y - 3 * size.height);
171+
robot.mouseRelease(button);
172+
robot.delay(200);
173+
if (!mouseDragged || !mouseDraggedOutside) {
174+
throw new RuntimeException("Test failed. Choice should generate MouseDragged events outside Choice");
175+
} else {
176+
System.out.println("Stage 3 passed. Choice generates MouseDragged events outside Choice");
177+
}
178+
robot.keyPress(KeyEvent.VK_ESCAPE);
179+
robot.keyRelease(KeyEvent.VK_ESCAPE);
180+
robot.delay(200);
181+
mouseDragged = false;
182+
}
183+
184+
public void dragMouse(int x0, int y0, int x1, int y1) {
185+
int curX = x0;
186+
int curY = y0;
187+
int dx = x0 < x1 ? 1 : -1;
188+
int dy = y0 < y1 ? 1 : -1;
189+
190+
while (curX != x1) {
191+
curX += dx;
192+
robot.mouseMove(curX, curY);
193+
}
194+
while (curY != y1) {
195+
curY += dy;
196+
robot.mouseMove(curX, curY);
197+
}
198+
}
199+
200+
public static void main(final String[] args) throws InterruptedException,
201+
InvocationTargetException {
202+
ChoiceDragEventsInside app = new ChoiceDragEventsInside();
203+
try {
204+
EventQueue.invokeAndWait(app::setupUI);
205+
app.start();
206+
} finally {
207+
EventQueue.invokeAndWait(app::dispose);
208+
}
209+
}
210+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) 2000, 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+
/*
25+
* @test
26+
* @bug 4319246
27+
* @summary Tests that MouseReleased, MouseClicked and MouseDragged are triggered on choice
28+
* @key headful
29+
* @run main ChoiceMouseEventTest
30+
*/
31+
32+
import java.awt.AWTException;
33+
import java.awt.BorderLayout;
34+
import java.awt.Choice;
35+
import java.awt.Dimension;
36+
import java.awt.EventQueue;
37+
import java.awt.Frame;
38+
import java.awt.Point;
39+
import java.awt.Robot;
40+
import java.awt.event.InputEvent;
41+
import java.awt.event.MouseAdapter;
42+
import java.awt.event.MouseEvent;
43+
import java.lang.reflect.InvocationTargetException;
44+
45+
46+
public class ChoiceMouseEventTest extends Frame {
47+
static volatile boolean mousePressed = false;
48+
static volatile boolean mouseReleased = false;
49+
static volatile boolean mouseClicked = false;
50+
Choice choice = new Choice();
51+
static Point location;
52+
static Dimension size;
53+
54+
public void setupGUI() {
55+
setTitle("Choice Mouse Event Test");
56+
this.setLayout(new BorderLayout());
57+
choice.add("item-1");
58+
choice.add("item-2");
59+
choice.add("item-3");
60+
choice.add("item-4");
61+
add("Center", choice);
62+
choice.addMouseListener(new MouseAdapter() {
63+
@Override
64+
public void mouseClicked(MouseEvent e) {
65+
mouseClicked = true;
66+
}
67+
68+
@Override
69+
public void mousePressed(MouseEvent e) {
70+
mousePressed = true;
71+
}
72+
73+
@Override
74+
public void mouseReleased(MouseEvent e) {
75+
mouseReleased = true;
76+
}
77+
});
78+
setLocationRelativeTo(null);
79+
setSize(400, 200);
80+
setVisible(true);
81+
}
82+
83+
public Point _location() {
84+
return choice.getLocationOnScreen();
85+
}
86+
87+
public Dimension _size() {
88+
return choice.getSize();
89+
}
90+
91+
public static void main(String[] args) throws InterruptedException,
92+
InvocationTargetException, AWTException {
93+
ChoiceMouseEventTest test = new ChoiceMouseEventTest();
94+
try {
95+
EventQueue.invokeAndWait(test::setupGUI);
96+
Robot robot = new Robot();
97+
robot.setAutoDelay(50);
98+
robot.delay(1000);
99+
robot.waitForIdle();
100+
EventQueue.invokeAndWait(() -> {
101+
location = test._location();
102+
size = test._size();
103+
});
104+
robot.waitForIdle();
105+
robot.mouseMove(location.x + size.width - 10, location.y + (size.height / 2));
106+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
107+
robot.delay(2000);
108+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
109+
robot.delay(2000);
110+
robot.waitForIdle();
111+
if (!mouseClicked || !mousePressed || !mouseReleased) {
112+
throw new RuntimeException(String.format("One of the events not arrived: " +
113+
"mouseClicked = %b, mousePressed = %b, mouseReleased = %b",
114+
mouseClicked, mousePressed, mouseReleased));
115+
}
116+
} finally {
117+
if (test != null) {
118+
EventQueue.invokeAndWait(test::dispose);
119+
}
120+
}
121+
}
122+
}
123+

0 commit comments

Comments
 (0)