Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/swing/JComboBox/JComboBoxPopupMenuEventTest.java
66644 views
1
/*
2
* Copyright (c) 2001, 2022, 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.Point;
25
import java.awt.Robot;
26
import java.awt.event.InputEvent;
27
import java.awt.event.KeyEvent;
28
import java.util.Arrays;
29
import java.util.List;
30
import java.util.concurrent.CountDownLatch;
31
import java.util.concurrent.TimeUnit;
32
import java.util.concurrent.atomic.AtomicBoolean;
33
import java.util.concurrent.atomic.AtomicReference;
34
import java.util.stream.Collectors;
35
import javax.swing.JComboBox;
36
import javax.swing.JComponent;
37
import javax.swing.JFrame;
38
import javax.swing.JPanel;
39
import javax.swing.JTextField;
40
import javax.swing.SwingUtilities;
41
import javax.swing.UIManager;
42
import javax.swing.UIManager.LookAndFeelInfo;
43
import javax.swing.UnsupportedLookAndFeelException;
44
import javax.swing.event.PopupMenuEvent;
45
import javax.swing.event.PopupMenuListener;
46
47
import static javax.swing.UIManager.getInstalledLookAndFeels;
48
49
/*
50
* @test
51
* @key headful
52
* @bug 4287690 4331058
53
* @summary This testcase tests RFE-4287690 and RFE-4331058 requests,
54
* JComboBox should send drop down visible as well as invisible events.
55
* @run main JComboBoxPopupMenuEventTest
56
*/
57
public class JComboBoxPopupMenuEventTest {
58
59
private static final String[] compStrs =
60
{"Apple", "Citibank", "Cisco", "Cienna", "Oracle", "IBM"};
61
private static Robot robot;
62
private static JComboBox comboBox;
63
private static JTextField searchTextField;
64
private static CountDownLatch popupMenuVisibleLatch;
65
private static CountDownLatch popupMenuInvisibleLatch;
66
private static JFrame frame;
67
68
public static void main(String[] args) throws Exception {
69
robot = new Robot();
70
robot.setAutoWaitForIdle(true);
71
robot.setAutoDelay(200);
72
List<String> lafs = Arrays.stream(getInstalledLookAndFeels())
73
.map(LookAndFeelInfo::getClassName)
74
.collect(Collectors.toList());
75
for (final String laf : lafs) {
76
try {
77
popupMenuVisibleLatch = new CountDownLatch(1);
78
popupMenuInvisibleLatch = new CountDownLatch(1);
79
AtomicBoolean lafSetSuccess = new AtomicBoolean(false);
80
SwingUtilities.invokeAndWait(() -> {
81
lafSetSuccess.set(setLookAndFeel(laf));
82
if (lafSetSuccess.get()) {
83
createUI();
84
}
85
});
86
if (!lafSetSuccess.get()) {
87
continue;
88
}
89
robot.waitForIdle();
90
91
mouseClick(searchTextField);
92
hitKeys(KeyEvent.VK_C, KeyEvent.VK_I);
93
mouseClick(comboBox);
94
95
// Verifying whether popupMenuWillBecomeVisible method of
96
// PopupMenuListener gets called when popup menu appears.
97
if (!popupMenuVisibleLatch.await(3, TimeUnit.SECONDS)) {
98
throw new RuntimeException(
99
"Waited too long, but popupMenuWillBecomeVisible " +
100
"not yet got called for " + laf);
101
}
102
103
hitKeys(KeyEvent.VK_ENTER);
104
105
// Verifying whether popupMenuWillBecomeInvisible method of
106
// PopupMenuListener gets called when popup menu disappears.
107
if (!popupMenuInvisibleLatch.await(3, TimeUnit.SECONDS)) {
108
throw new RuntimeException(
109
"Waited too long, but popupMenuWillBecomeInvisible " +
110
"not yet got called for " + laf);
111
}
112
113
System.out.println("Test passed for " + laf);
114
} finally {
115
SwingUtilities.invokeAndWait(
116
JComboBoxPopupMenuEventTest::disposeFrame);
117
}
118
}
119
}
120
121
private static void mouseClick(JComponent jComponent) throws Exception {
122
final Point location = getLocationOnScreen(jComponent);
123
robot.mouseMove(location.x + 8, location.y + 8);
124
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
125
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
126
}
127
128
private static Point getLocationOnScreen(JComponent jComponent)
129
throws Exception {
130
final AtomicReference<Point> loc = new AtomicReference<>();
131
SwingUtilities.invokeAndWait(
132
() -> loc.set(jComponent.getLocationOnScreen()));
133
return loc.get();
134
}
135
136
private static void hitKeys(int... keys) {
137
for (int key : keys) {
138
robot.keyPress(key);
139
}
140
141
for (int i = keys.length - 1; i >= 0; i--) {
142
robot.keyRelease(keys[i]);
143
}
144
}
145
146
public static void createUI() {
147
frame = new JFrame();
148
JPanel panel = new JPanel();
149
searchTextField = new JTextField(6);
150
panel.add(searchTextField);
151
comboBox = new JComboBox(compStrs);
152
panel.add(comboBox);
153
comboBox.addPopupMenuListener(new PopupMenuListener() {
154
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
155
System.out.println("popupMenuWillBecomeVisible() got called");
156
popupMenuVisibleLatch.countDown();
157
comboBox.removeAllItems();
158
String text = searchTextField.getText().trim();
159
Arrays.stream(compStrs)
160
.filter(str -> str.toLowerCase().startsWith(text))
161
.forEach(str -> comboBox.addItem(str));
162
}
163
164
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
165
System.out.println("popupMenuWillBecomeInvisible() got called");
166
popupMenuInvisibleLatch.countDown();
167
}
168
169
public void popupMenuCanceled(PopupMenuEvent e) {
170
}
171
});
172
173
frame.setContentPane(panel);
174
frame.setSize(250, 100);
175
frame.setLocationRelativeTo(null);
176
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
177
frame.setVisible(true);
178
}
179
180
private static boolean setLookAndFeel(String lafName) {
181
try {
182
UIManager.setLookAndFeel(lafName);
183
} catch (UnsupportedLookAndFeelException ignored) {
184
System.out.println("Ignoring Unsupported L&F: " + lafName);
185
return false;
186
} catch (ClassNotFoundException | InstantiationException
187
| IllegalAccessException e) {
188
throw new RuntimeException(e);
189
}
190
return true;
191
}
192
193
private static void disposeFrame() {
194
if (frame != null) {
195
frame.dispose();
196
frame = null;
197
}
198
}
199
200
}
201
202