Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/swing/JTextField/4532513/DefaultCaretRequestsFocusTest.java
66646 views
1
/*
2
* Copyright (c) 2002, 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.GridLayout;
25
import java.awt.Point;
26
import java.awt.Robot;
27
import java.awt.event.InputEvent;
28
import java.awt.event.KeyEvent;
29
import java.util.Arrays;
30
import java.util.List;
31
import java.util.concurrent.atomic.AtomicBoolean;
32
import java.util.concurrent.atomic.AtomicReference;
33
import java.util.stream.Collectors;
34
import javax.swing.InputVerifier;
35
import javax.swing.JComponent;
36
import javax.swing.JFrame;
37
import javax.swing.JPanel;
38
import javax.swing.JTextField;
39
import javax.swing.SwingUtilities;
40
import javax.swing.UIManager;
41
import javax.swing.UIManager.LookAndFeelInfo;
42
import javax.swing.UnsupportedLookAndFeelException;
43
44
import static javax.swing.UIManager.getInstalledLookAndFeels;
45
46
/*
47
* @test
48
* @key headful
49
* @bug 4532513
50
* @summary Verifies that DefaultCaret doesn't requests focus in mouseClick and mousePressed
51
* causing the associated input verifier to fire twice.
52
* @run main DefaultCaretRequestsFocusTest
53
*/
54
public class DefaultCaretRequestsFocusTest {
55
56
private static JTextField jTextField1;
57
private static JTextField jTextField2;
58
private static JTextField jTextField3;
59
private static JFrame frame;
60
private static Robot robot;
61
private static volatile int shouldYieldFocusCount;
62
63
public static void main(String[] args) throws Exception {
64
runTest();
65
}
66
67
public static void runTest() throws Exception {
68
robot = new Robot();
69
robot.setAutoWaitForIdle(true);
70
robot.setAutoDelay(200);
71
72
List<String> lafs = Arrays.stream(getInstalledLookAndFeels())
73
.map(LookAndFeelInfo::getClassName)
74
.collect(Collectors.toList());
75
for (final String laf : lafs) {
76
try {
77
AtomicBoolean lafSetSuccess = new AtomicBoolean(false);
78
SwingUtilities.invokeAndWait(() -> {
79
lafSetSuccess.set(setLookAndFeel(laf));
80
if (lafSetSuccess.get()) {
81
createUI();
82
}
83
});
84
if (!lafSetSuccess.get()) {
85
continue;
86
}
87
robot.waitForIdle();
88
89
AtomicReference<Point> jTextField1LocRef = new AtomicReference<>();
90
AtomicReference<Point> jTextField2LocRef = new AtomicReference<>();
91
SwingUtilities.invokeAndWait(() -> {
92
jTextField1LocRef.set(jTextField1.getLocationOnScreen());
93
jTextField2LocRef.set(jTextField2.getLocationOnScreen());
94
});
95
final Point jTextField1Loc = jTextField1LocRef.get();
96
final Point jTextField2Loc = jTextField2LocRef.get();
97
98
shouldYieldFocusCount = 0;
99
100
// Click on TextField2
101
robot.mouseMove(jTextField2Loc.x + 5, jTextField2Loc.y + 5);
102
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
103
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
104
105
typeSomeText();
106
107
// Click on TextField1
108
robot.mouseMove(jTextField1Loc.x + 5, jTextField1Loc.y + 5);
109
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
110
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
111
112
typeSomeText();
113
114
if (shouldYieldFocusCount == 1) {
115
System.out.println("Test passed for " + laf);
116
} else {
117
throw new RuntimeException("Test failed for " + laf
118
+ " as InputVerifier.shouldYieldFocus() was called " + shouldYieldFocusCount
119
+ " times on jTextField2, but it is expected to be called only once.");
120
}
121
122
} finally {
123
SwingUtilities.invokeAndWait(DefaultCaretRequestsFocusTest::disposeFrame);
124
}
125
}
126
}
127
128
private static void typeSomeText() {
129
robot.keyPress(KeyEvent.VK_T);
130
robot.keyRelease(KeyEvent.VK_T);
131
robot.keyPress(KeyEvent.VK_E);
132
robot.keyRelease(KeyEvent.VK_E);
133
robot.keyPress(KeyEvent.VK_X);
134
robot.keyRelease(KeyEvent.VK_X);
135
robot.keyPress(KeyEvent.VK_T);
136
robot.keyRelease(KeyEvent.VK_T);
137
}
138
139
private static void createUI() {
140
frame = new JFrame();
141
jTextField1 = new JTextField(6);
142
jTextField2 = new JTextField(6);
143
jTextField3 = new JTextField(6);
144
JPanel panel = new JPanel();
145
panel.setLayout(new GridLayout(3, 1));
146
panel.add(jTextField1);
147
panel.add(jTextField2);
148
panel.add(jTextField3);
149
150
InputVerifier iv = new InputVerifier() {
151
public boolean verify(JComponent input) {
152
System.out.println("InputVerifier.verify() called");
153
return false;
154
}
155
156
public boolean shouldYieldFocus(JComponent input) {
157
++shouldYieldFocusCount;
158
System.out.println("InputVerifier.shouldYieldFocus() called " + shouldYieldFocusCount);
159
return false;
160
}
161
};
162
163
jTextField2.setInputVerifier(iv);
164
165
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
166
frame.add(panel);
167
frame.pack();
168
frame.setAlwaysOnTop(true);
169
frame.setLocationRelativeTo(null);
170
frame.setVisible(true);
171
}
172
173
private static boolean setLookAndFeel(String lafName) {
174
try {
175
UIManager.setLookAndFeel(lafName);
176
} catch (UnsupportedLookAndFeelException ignored) {
177
System.out.println("Ignoring Unsupported laf : " + lafName);
178
return false;
179
} catch (ClassNotFoundException | InstantiationException
180
| IllegalAccessException e) {
181
throw new RuntimeException(e);
182
}
183
return true;
184
}
185
186
private static void disposeFrame() {
187
if (frame != null) {
188
frame.dispose();
189
frame = null;
190
}
191
}
192
193
}
194
195