Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Focus/6378278/InputVerifierTest.java
41153 views
1
/*
2
* Copyright (c) 2006, 2021, 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 6378278
28
@summary Apparent missing key events causing Bugster to break
29
@run main InputVerifierTest
30
*/
31
32
/**
33
* InputVerifierTest.java
34
*
35
* summary: Apparent missing key events causing Bugster to break
36
*/
37
38
import java.awt.AWTException;
39
import java.awt.BorderLayout;
40
import java.awt.Component;
41
import java.awt.Dialog;
42
import java.awt.Frame;
43
import java.awt.Point;
44
import java.awt.Robot;
45
import java.awt.TextArea;
46
47
import java.awt.event.InputEvent;
48
import java.awt.event.KeyEvent;
49
50
import javax.swing.InputVerifier;
51
import javax.swing.JComponent;
52
import javax.swing.JFrame;
53
import javax.swing.JTextField;
54
import javax.swing.SwingUtilities;
55
56
public class InputVerifierTest
57
{
58
59
//*** test-writer defined static variables go here ***
60
static volatile boolean ivWasCalled = false;
61
static JFrame frame;
62
static JTextField t1;
63
static JTextField t2;
64
65
private static void init() throws Exception
66
{
67
try {
68
SwingUtilities.invokeAndWait(() -> {
69
frame = new JFrame();
70
t1 = new JTextField();
71
t1.setInputVerifier(new InputVerifier() {
72
public boolean verify(JComponent input) {
73
System.out.println("verify(" + input + ")");
74
ivWasCalled = true;
75
return true;
76
}
77
});
78
t2 = new JTextField();
79
80
frame.getContentPane().add(t1, BorderLayout.NORTH);
81
frame.getContentPane().add(t2, BorderLayout.SOUTH);
82
frame.setLocationRelativeTo(null);
83
frame.setSize(200, 200);
84
frame.setVisible(true);
85
});
86
87
Robot r = null;
88
try {
89
r = new Robot();
90
} catch (AWTException e) {
91
e.printStackTrace();
92
InputVerifierTest.fail(e.toString());
93
}
94
95
try {
96
r.setAutoDelay(100);
97
r.waitForIdle();
98
r.delay(1000);
99
100
mouseClickOnComp(r, t1);
101
r.waitForIdle();
102
103
if (!t1.isFocusOwner()) {
104
throw new RuntimeException("t1 is not a focus owner");
105
}
106
ivWasCalled = false;
107
r.keyPress(KeyEvent.VK_TAB);
108
r.keyRelease(KeyEvent.VK_TAB);
109
r.waitForIdle();
110
r.delay(500);
111
112
if (!t2.isFocusOwner()) {
113
throw new RuntimeException("t2 is not a focus owner 1");
114
}
115
if (!ivWasCalled) {
116
throw new RuntimeException("InputVerifier was not called after tabbing");
117
}
118
119
mouseClickOnComp(r, t1);
120
r.waitForIdle();
121
122
if (!t1.isFocusOwner()) {
123
throw new RuntimeException("t1 is not a focus owner");
124
}
125
126
ivWasCalled = false;
127
mouseClickOnComp(r, t2);
128
r.waitForIdle();
129
r.delay(500);
130
if (!t2.isFocusOwner()) {
131
throw new RuntimeException("t2 is not a focus owner 2");
132
}
133
if (!ivWasCalled) {
134
throw new RuntimeException("InputVErifier was not called after mouse press");
135
}
136
} catch (Exception e) {
137
e.printStackTrace();
138
InputVerifierTest.fail(e.toString());
139
}
140
141
InputVerifierTest.pass();
142
} finally {
143
SwingUtilities.invokeAndWait(() -> {
144
if (frame != null) {
145
frame.dispose();
146
}
147
});
148
}
149
}//End init()
150
151
static void mouseClickOnComp(Robot r, Component comp) {
152
Point loc = comp.getLocationOnScreen();
153
loc.x += comp.getWidth() / 2;
154
loc.y += comp.getHeight() / 2;
155
r.mouseMove(loc.x, loc.y);
156
r.waitForIdle();
157
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
158
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
159
}
160
161
/*****************************************************
162
* Standard Test Machinery Section
163
* DO NOT modify anything in this section -- it's a
164
* standard chunk of code which has all of the
165
* synchronisation necessary for the test harness.
166
* By keeping it the same in all tests, it is easier
167
* to read and understand someone else's test, as
168
* well as insuring that all tests behave correctly
169
* with the test harness.
170
* There is a section following this for test-
171
* classes
172
******************************************************/
173
private static boolean theTestPassed = false;
174
private static boolean testGeneratedInterrupt = false;
175
private static String failureMessage = "";
176
177
private static Thread mainThread = null;
178
179
private static int sleepTime = 300000;
180
181
// Not sure about what happens if multiple of this test are
182
// instantiated in the same VM. Being static (and using
183
// static vars), it aint gonna work. Not worrying about
184
// it for now.
185
public static void main( String args[] ) throws Exception
186
{
187
mainThread = Thread.currentThread();
188
try
189
{
190
init();
191
}
192
catch( TestPassedException e )
193
{
194
//The test passed, so just return from main and harness will
195
// interepret this return as a pass
196
return;
197
}
198
//At this point, neither test pass nor test fail has been
199
// called -- either would have thrown an exception and ended the
200
// test, so we know we have multiple threads.
201
202
//Test involves other threads, so sleep and wait for them to
203
// called pass() or fail()
204
try
205
{
206
Thread.sleep( sleepTime );
207
//Timed out, so fail the test
208
throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
209
}
210
catch (InterruptedException e)
211
{
212
//The test harness may have interrupted the test. If so, rethrow the exception
213
// so that the harness gets it and deals with it.
214
if( ! testGeneratedInterrupt ) throw e;
215
216
//reset flag in case hit this code more than once for some reason (just safety)
217
testGeneratedInterrupt = false;
218
219
if ( theTestPassed == false )
220
{
221
throw new RuntimeException( failureMessage );
222
}
223
}
224
225
}//main
226
227
public static synchronized void setTimeoutTo( int seconds )
228
{
229
sleepTime = seconds * 1000;
230
}
231
232
public static synchronized void pass()
233
{
234
System.out.println( "The test passed." );
235
System.out.println( "The test is over, hit Ctl-C to stop Java VM" );
236
//first check if this is executing in main thread
237
if ( mainThread == Thread.currentThread() )
238
{
239
//Still in the main thread, so set the flag just for kicks,
240
// and throw a test passed exception which will be caught
241
// and end the test.
242
theTestPassed = true;
243
throw new TestPassedException();
244
}
245
theTestPassed = true;
246
testGeneratedInterrupt = true;
247
mainThread.interrupt();
248
}//pass()
249
250
public static synchronized void fail()
251
{
252
//test writer didn't specify why test failed, so give generic
253
fail( "it just plain failed! :-)" );
254
}
255
256
public static synchronized void fail( String whyFailed )
257
{
258
System.out.println( "The test failed: " + whyFailed );
259
System.out.println( "The test is over, hit Ctl-C to stop Java VM" );
260
//check if this called from main thread
261
if ( mainThread == Thread.currentThread() )
262
{
263
//If main thread, fail now 'cause not sleeping
264
throw new RuntimeException( whyFailed );
265
}
266
theTestPassed = false;
267
testGeneratedInterrupt = true;
268
failureMessage = whyFailed;
269
mainThread.interrupt();
270
}//fail()
271
272
}// class InputVerifierTest
273
274
//This exception is used to exit from any level of call nesting
275
// when it's determined that the test has passed, and immediately
276
// end the test.
277
class TestPassedException extends RuntimeException
278
{
279
}
280
281