Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/4515752/DefaultButtonTest.java
38853 views
1
/*
2
* Copyright (c) 2002, 2014, 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
import java.awt.*;
24
import java.awt.event.*;
25
26
import javax.swing.*;
27
28
/**
29
* @test
30
* @bug 4515752 4337071
31
* @author Mark Davidson
32
* @summary Tests the invocation of the default button within the JComboBox.
33
*/
34
public class DefaultButtonTest extends JFrame implements ActionListener {
35
36
private static boolean defaultButtonPressed = false;
37
private static boolean editChanged = false;
38
39
private static String[] strData = {
40
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
41
};
42
43
private static String[] strData2 = {
44
"One", "Two", "Three", "Four", "Five", "Six", "Seven"
45
};
46
47
public static void main(String[] args) throws Throwable {
48
SwingUtilities.invokeAndWait(new Runnable(){
49
public void run() {
50
new DefaultButtonTest();
51
}
52
});
53
test();
54
System.out.println("Test Passed");
55
}
56
57
public DefaultButtonTest() {
58
getContentPane().add(new DefaultPanel(this));
59
pack();
60
setVisible(true);
61
}
62
63
public static void test() {
64
// Use Robot to automate the test
65
Robot robot = null;
66
try {
67
robot = new Robot();
68
} catch (Exception ex) {
69
ex.printStackTrace();
70
}
71
robot.setAutoDelay(125);
72
73
for (int i = 0; i < 3; i++) {
74
// Test ENTER press on the non editable combo.
75
robot.waitForIdle();
76
robot.keyPress(KeyEvent.VK_ENTER);
77
robot.waitForIdle();
78
robot.keyRelease(KeyEvent.VK_ENTER);
79
robot.waitForIdle();
80
testDefaultButton(true);
81
82
// Test the ENTER press on the editable combo box
83
robot.keyPress(KeyEvent.VK_TAB);
84
robot.waitForIdle();
85
robot.keyRelease(KeyEvent.VK_TAB);
86
robot.waitForIdle();
87
robot.keyPress(KeyEvent.VK_ENTER);
88
robot.waitForIdle();
89
robot.keyRelease(KeyEvent.VK_ENTER);
90
robot.waitForIdle();
91
testDefaultButton(true);
92
93
// Change the value, should generate a change but not a Default Button press.
94
robot.waitForIdle();
95
robot.keyPress(KeyEvent.VK_D);
96
robot.waitForIdle();
97
robot.keyRelease(KeyEvent.VK_D);
98
robot.waitForIdle();
99
robot.keyPress(KeyEvent.VK_ENTER);
100
robot.waitForIdle();
101
robot.keyRelease(KeyEvent.VK_ENTER);
102
robot.waitForIdle();
103
testEditChange(true);
104
robot.waitForIdle();
105
testDefaultButton(true);
106
107
// Change value, changing focus should fire an ActionEvent.
108
robot.waitForIdle();
109
robot.keyPress(KeyEvent.VK_BACK_SPACE);
110
robot.waitForIdle();
111
robot.keyRelease(KeyEvent.VK_BACK_SPACE);
112
robot.waitForIdle();
113
robot.keyPress(KeyEvent.VK_SHIFT);
114
robot.waitForIdle();
115
robot.keyPress(KeyEvent.VK_TAB);
116
robot.waitForIdle();
117
robot.keyRelease(KeyEvent.VK_SHIFT);
118
robot.waitForIdle();
119
robot.keyRelease(KeyEvent.VK_TAB);
120
robot.waitForIdle();
121
testEditChange(true);
122
robot.waitForIdle();
123
testDefaultButton(false);
124
}
125
}
126
127
public void actionPerformed(ActionEvent evt) {
128
String cmd = evt.getActionCommand();
129
System.out.println("ActionEvent: " + cmd);
130
131
if (cmd.equals("OK")) {
132
defaultButtonPressed = true;
133
}
134
135
if (cmd.equals("comboBoxChanged")) {
136
editChanged = true;
137
}
138
}
139
140
public static void testDefaultButton(boolean flag) {
141
if (defaultButtonPressed != flag) {
142
new RuntimeException("defaultButtonPressed unexpectedly = " + defaultButtonPressed);
143
}
144
// reset
145
defaultButtonPressed = false;
146
}
147
148
public static void testEditChange(boolean flag) {
149
if (editChanged != flag) {
150
new RuntimeException("editChanged unexpectedly = " + editChanged);
151
}
152
// reset
153
editChanged = false;
154
}
155
156
class DefaultPanel extends JPanel {
157
158
public JComboBox combo;
159
public JComboBox combo2;
160
161
private JButton okButton = new JButton("OK");
162
private JButton cancelButton = new JButton("Cancel");
163
164
public DefaultPanel(JFrame root) {
165
setLayout(new BorderLayout());
166
add(createPanel(), BorderLayout.NORTH);
167
add(createInfoPanel(), BorderLayout.CENTER);
168
add(createButtonPanel(root), BorderLayout.SOUTH);
169
}
170
171
private JPanel createPanel() {
172
combo = new JComboBox(strData);
173
combo.addActionListener(DefaultButtonTest.this);
174
combo2 = new JComboBox(strData2);
175
combo2.setEditable(true);
176
combo2.addActionListener(DefaultButtonTest.this);
177
178
JPanel panel = new JPanel();
179
180
panel.add(combo);
181
panel.add(combo2);
182
183
return panel;
184
}
185
186
private JScrollPane createInfoPanel() {
187
StringBuffer txt = new StringBuffer("Test for 4337071:\n");
188
txt.append("ENTER pressed in NON-EDITABLE combo box should be passed to the OK button.\n");
189
txt.append("For an EDITABLE combo box, the combo box should fire an action event.");
190
txt.append("\n\nTest for 4515752:\n");
191
txt.append("ENTER on an EDITABLE combo box in which the contents has not changed\n");
192
txt.append("should be passed to the default button");
193
194
JTextArea text = new JTextArea(txt.toString());
195
text.setEditable(false);
196
197
return new JScrollPane(text);
198
}
199
200
201
private JPanel createButtonPanel(JFrame frame) {
202
frame.getRootPane().setDefaultButton(okButton);
203
204
// This is just to check when the OK Button was pressed.
205
okButton.addActionListener(DefaultButtonTest.this);
206
207
JPanel panel = new JPanel();
208
panel.add(okButton);
209
panel.add(cancelButton);
210
return panel;
211
}
212
}
213
214
}
215
216