Skip to content

Commit fe770e3

Browse files
Victor RudometovPaul Hohensee
authored andcommitted
8299296: Write a test to verify the components selection sends ItemEvent
Backport-of: 5ae6de859d472d107cdf642c417c6d2f1c74e5db
1 parent 38eb0d6 commit fe770e3

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (c) 2007, 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+
import java.awt.Checkbox;
25+
import java.awt.Choice;
26+
import java.awt.Dimension;
27+
import java.awt.EventQueue;
28+
import java.awt.FlowLayout;
29+
import java.awt.Frame;
30+
import java.awt.Point;
31+
import java.awt.Robot;
32+
import java.awt.event.InputEvent;
33+
34+
/*
35+
* @test
36+
* @key headful
37+
* @bug 8299296
38+
* @summary Verify that Component selection via mouse generates ItemEvent.
39+
* @run main ComponentItemEventTest
40+
*/
41+
public class ComponentItemEventTest {
42+
43+
private static Frame frame;
44+
private volatile static Choice choice;
45+
private volatile static Checkbox cb;
46+
private static Robot robot;
47+
private volatile static boolean cbStateChanged = false;
48+
private volatile static boolean choiceStateChanged = false;
49+
private volatile static Point compAt;
50+
private volatile static Dimension compSize;
51+
52+
private static void initializeGUI() {
53+
frame = new Frame("Test Frame");
54+
frame.setLayout(new FlowLayout());
55+
choice = new Choice();
56+
for (int i = 0; i < 8; i++) {
57+
choice.add("Choice "+i);
58+
}
59+
choice.select(3);
60+
choice.addItemListener((event) -> {
61+
System.out.println("Choice got an ItemEvent: " + event);
62+
choiceStateChanged = true;
63+
});
64+
65+
cb = new Checkbox("CB");
66+
cb.addItemListener((event) -> {
67+
System.out.println("Checkbox got an ItemEvent: " + event);
68+
cbStateChanged = true;
69+
});
70+
frame.add(choice);
71+
frame.add(cb);
72+
frame.pack();
73+
frame.setLocationRelativeTo(null);
74+
frame.setVisible(true);
75+
}
76+
77+
public static void main(String[] args) throws Exception {
78+
try {
79+
EventQueue.invokeAndWait(ComponentItemEventTest::initializeGUI);
80+
robot = new Robot();
81+
robot.setAutoDelay(1000);
82+
robot.setAutoWaitForIdle(true);
83+
84+
robot.waitForIdle();
85+
EventQueue.invokeAndWait(() -> {
86+
compAt = choice.getLocationOnScreen();
87+
compSize = choice.getSize();
88+
});
89+
robot.mouseMove(compAt.x + choice.getSize().width - 10,
90+
compAt.y + compSize.height / 2);
91+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
92+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
93+
robot.mouseMove(compAt.x + compSize.width / 2,
94+
compAt.y + compSize.height + 15);
95+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
96+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
97+
98+
robot.waitForIdle();
99+
if (!choiceStateChanged) {
100+
throw new RuntimeException(
101+
"FAIL: Choice did not trigger ItemEvent when item selected!");
102+
}
103+
104+
EventQueue.invokeAndWait(() -> {
105+
compAt = cb.getLocationOnScreen();
106+
compSize = cb.getSize();
107+
});
108+
robot.mouseMove(compAt.x + compSize.width / 2,
109+
compAt.y + compSize.height / 2);
110+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
111+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
112+
113+
robot.waitForIdle();
114+
if (!cbStateChanged) {
115+
throw new RuntimeException(
116+
"FAIL: Checkbox did not trigger ItemEvent when item selected!");
117+
}
118+
System.out.println("Test passed!");
119+
} finally {
120+
EventQueue.invokeAndWait(ComponentItemEventTest::disposeFrame);
121+
}
122+
}
123+
124+
public static void disposeFrame() {
125+
if (frame != null) {
126+
frame.dispose();
127+
frame = null;
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)