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