Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Focus/6382144/EndlessLoopTest.java
41152 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 6382144
28
@summary REGRESSION: InputVerifier and JOptionPane
29
@run main EndlessLoopTest
30
*/
31
32
/**
33
* EndlessLoopTest.java
34
*
35
* summary: REGRESSION: InputVerifier and JOptionPane
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
import java.awt.Toolkit;
47
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.awt.event.InputEvent;
51
import java.awt.event.KeyEvent;
52
53
import javax.swing.InputVerifier;
54
import javax.swing.JButton;
55
import javax.swing.JComponent;
56
import javax.swing.JDialog;
57
import javax.swing.JFrame;
58
import javax.swing.JTextField;
59
import javax.swing.SwingUtilities;
60
61
public class EndlessLoopTest
62
{
63
64
//*** test-writer defined static variables go here ***
65
static volatile int n_iv_calls;
66
static JFrame frame;
67
static JTextField t1;
68
static JButton button;
69
70
private static void init() throws Exception
71
{
72
//*** Create instructions for the user here ***
73
74
try {
75
SwingUtilities.invokeAndWait(() -> {
76
frame = new JFrame();
77
final JDialog dialog = new JDialog(frame, true);
78
button = new JButton("press me");
79
button.addActionListener(new ActionListener() {
80
public void actionPerformed(ActionEvent ae) {
81
dialog.dispose();
82
}
83
});
84
dialog.getContentPane().add(button);
85
dialog.setLocationRelativeTo(null);
86
dialog.pack();
87
88
t1 = new JTextField();
89
t1.setInputVerifier(new InputVerifier() {
90
public boolean verify(JComponent input) {
91
n_iv_calls++;
92
if (n_iv_calls == 1) {
93
dialog.setVisible(true);
94
}
95
return true;
96
}
97
});
98
JTextField t2 = new JTextField();
99
100
frame.getContentPane().add(t1, BorderLayout.NORTH);
101
frame.getContentPane().add(t2, BorderLayout.SOUTH);
102
frame.setLocationRelativeTo(null);
103
frame.setSize(200, 200);
104
frame.setVisible(true);
105
});
106
107
Robot r = null;
108
try {
109
r = new Robot();
110
} catch (AWTException e) {
111
EndlessLoopTest.fail(e);
112
}
113
114
try {
115
r.setAutoDelay(100);
116
r.waitForIdle();
117
r.delay(1000);
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
n_iv_calls = 0;
126
r.keyPress(KeyEvent.VK_TAB);
127
r.keyRelease(KeyEvent.VK_TAB);
128
r.waitForIdle();
129
130
mouseClickOnComp(r, button);
131
r.waitForIdle();
132
} catch (Exception e) {
133
EndlessLoopTest.fail(e);
134
}
135
136
if (n_iv_calls != 1) {
137
EndlessLoopTest.fail(new RuntimeException("InputVerifier was called " + n_iv_calls + " times"));
138
}
139
140
EndlessLoopTest.pass();
141
} finally {
142
SwingUtilities.invokeAndWait(() -> {
143
if (frame != null) {
144
frame.dispose();
145
}
146
});
147
}
148
}//End init()
149
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( Exception whyFailed )
251
{
252
System.out.println( "The test failed: " + whyFailed );
253
System.out.println( "The test is over, hit Ctl-C to stop Java VM" );
254
//check if this called from main thread
255
if ( mainThread == Thread.currentThread() )
256
{
257
//If main thread, fail now 'cause not sleeping
258
throw new RuntimeException( whyFailed );
259
}
260
theTestPassed = false;
261
testGeneratedInterrupt = true;
262
failureMessage = whyFailed.toString();
263
mainThread.interrupt();
264
}//fail()
265
266
}// class EndlessLoopTest
267
268
//This exception is used to exit from any level of call nesting
269
// when it's determined that the test has passed, and immediately
270
// end the test.
271
class TestPassedException extends RuntimeException
272
{
273
}
274
275