Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JComboBox/8032878/bug8032878.java
41154 views
1
/*
2
* Copyright (c) 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
24
/*
25
* @test
26
* @key headful
27
* @bug 8032878 8078855
28
* @summary Checks that JComboBox as JTable cell editor processes key events
29
* even where setSurrendersFocusOnKeystroke flag in JTable is false and
30
* that it does not lose the first key press where the flag is true.
31
* @run main bug8032878
32
*/
33
34
import java.awt.*;
35
import java.awt.event.KeyEvent;
36
import javax.swing.*;
37
import javax.swing.text.JTextComponent;
38
import javax.swing.plaf.metal.MetalLookAndFeel;
39
40
public class bug8032878 implements Runnable {
41
private static final String ONE = "one";
42
private static final String TWO = "two";
43
private static final String THREE = "three";
44
45
private static final String EXPECTED = "one123";
46
47
private final Robot robot;
48
49
private JFrame frame;
50
private JComboBox cb;
51
52
private volatile boolean surrender;
53
private volatile String text;
54
55
public static void main(String[] args) throws Exception {
56
UIManager.setLookAndFeel(new MetalLookAndFeel());
57
58
final bug8032878 test = new bug8032878();
59
60
test.test(false);
61
test.test(true);
62
}
63
64
public bug8032878() throws AWTException {
65
robot = new Robot();
66
robot.setAutoDelay(100);
67
}
68
69
private void setupUI() {
70
frame = new JFrame();
71
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
72
73
JTable table = new JTable(new String[][] {{ONE}, {TWO}, {THREE}},
74
new String[] { "#"});
75
table.setSurrendersFocusOnKeystroke(surrender);
76
77
cb = new JComboBox(new String[]{ONE, TWO, THREE});
78
cb.setEditable(true);
79
DefaultCellEditor comboEditor = new DefaultCellEditor(cb);
80
comboEditor.setClickCountToStart(1);
81
table.getColumnModel().getColumn(0).setCellEditor(comboEditor);
82
frame.add(table);
83
84
frame.pack();
85
frame.setVisible(true);
86
frame.setLocationRelativeTo(null);
87
}
88
89
private void test(final boolean flag) throws Exception {
90
try {
91
surrender = flag;
92
SwingUtilities.invokeAndWait(this);
93
94
robot.waitForIdle();
95
robot.delay(1000);
96
runTest();
97
checkResult();
98
} finally {
99
if (frame != null) {
100
SwingUtilities.invokeAndWait(() -> frame.dispose());
101
}
102
}
103
}
104
105
private void runTest() throws Exception {
106
robot.waitForIdle();
107
// Select 'one'
108
robot.keyPress(KeyEvent.VK_TAB);
109
robot.keyRelease(KeyEvent.VK_TAB);
110
robot.waitForIdle();
111
robot.keyPress(KeyEvent.VK_1);
112
robot.keyRelease(KeyEvent.VK_1);
113
robot.waitForIdle();
114
robot.keyPress(KeyEvent.VK_2);
115
robot.keyRelease(KeyEvent.VK_2);
116
robot.waitForIdle();
117
robot.keyPress(KeyEvent.VK_3);
118
robot.keyRelease(KeyEvent.VK_3);
119
robot.waitForIdle();
120
robot.keyPress(KeyEvent.VK_ENTER);
121
robot.keyRelease(KeyEvent.VK_ENTER);
122
robot.waitForIdle();
123
}
124
125
private void checkResult() throws Exception {
126
SwingUtilities.invokeAndWait(new Runnable() {
127
public void run() {
128
text = ((JTextComponent) cb.getEditor().getEditorComponent()).getText();
129
}
130
});
131
if (text.equals(EXPECTED)) {
132
System.out.println("Test with surrender = " + surrender + " passed");
133
} else {
134
System.out.println("Test with surrender = " + surrender + " failed");
135
throw new RuntimeException("Expected value in JComboBox editor '" +
136
EXPECTED + "' but found '" + text + "'.");
137
}
138
}
139
140
141
@Override
142
public void run() {
143
setupUI();
144
}
145
}
146
147