Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Modal/WsDisabledStyle/CloseBlocker/CloseBlocker.java
41159 views
1
/*
2
* Copyright (c) 2007, 2008, 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 %I% %E%
26
@bug 4080029
27
@summary Modal Dialog block input to all frame windows not just its parent.
28
@author dmitry.cherepanov: area=awt.modal
29
@run main/manual CloseBlocker
30
*/
31
32
/**
33
* ManualMainTest.java
34
*
35
* summary: The test opens and closes blocker dialog, the test verifies
36
* that active window is correct when the dialog is closed.
37
*/
38
39
import java.awt.*;
40
import java.awt.event.*;
41
42
public class CloseBlocker
43
{
44
45
private static void init()
46
{
47
//*** Create instructions for the user here ***
48
49
String[] instructions =
50
{
51
" the test will be run 6 times, to start next test just close all ",
52
" windows of previous; the instructions are the same for all tests: ",
53
" 1) there are two frames (one the frames has 'show modal' button), ",
54
" 2) press the button to show a dialog, ",
55
" 3) close the dialog (an alternative scenario - activate another",
56
" native window before closing the dialog), ",
57
" 4) the frame with button should become next active window, ",
58
" if it's true, then the test passed, otherwise, it failed. ",
59
" Press 'pass' button only after all of the 6 tests are completed, ",
60
" the number of the currently executed test is displayed on the ",
61
" output window. "
62
};
63
Sysout.createDialog( );
64
Sysout.printInstructions( instructions );
65
66
test(true, true, false);
67
test(true, true, true);
68
test(false, true, false); // 3rd parameter has no affect for ownerless
69
70
test(true, false, false);
71
test(true, false, true);
72
test(false, false, false); // 3rd parameter has no affect for ownerless
73
74
}//End init()
75
76
private static final Object obj = new Object();
77
private static int counter = 0;
78
79
/*
80
* The ownerless parameter indicates whether the blocker dialog
81
* has owner. The usual parameter indicates whether the blocker
82
* dialog is a Java dialog (non-native dialog like file dialog).
83
*/
84
private static void test(final boolean ownerless, final boolean usual, final boolean initiallyOwnerIsActive) {
85
86
Sysout.print(" * test #" + (++counter) + " is running ... ");
87
88
final Frame active = new Frame();
89
final Frame nonactive = new Frame();
90
Button button = new Button("show modal");
91
button.addActionListener(new ActionListener() {
92
public void actionPerformed(ActionEvent ae) {
93
Dialog dialog = null;
94
Frame parent = ownerless ? null : (initiallyOwnerIsActive? active : nonactive);
95
if (usual) {
96
dialog = new Dialog(parent, "Sample", true);
97
} else {
98
dialog = new FileDialog(parent, "Sample", FileDialog.LOAD);
99
}
100
dialog.addWindowListener(new WindowAdapter(){
101
public void windowClosing(WindowEvent e){
102
e.getWindow().dispose();
103
}
104
});
105
dialog.setBounds(200, 200, 200, 200);
106
dialog.setVisible(true);
107
}
108
});
109
110
active.add(button);
111
active.setBounds(200, 400, 200, 200);
112
WindowAdapter adapter = new WindowAdapter(){
113
public void windowClosing(WindowEvent e){
114
active.dispose();
115
nonactive.dispose();
116
synchronized(obj) {
117
obj.notify();
118
}
119
}
120
};
121
active.addWindowListener(adapter);
122
active.setVisible(true);
123
124
nonactive.setBounds(400, 400, 200, 200);
125
nonactive.addWindowListener(adapter);
126
nonactive.setVisible(true);
127
128
synchronized(obj) {
129
try{
130
obj.wait();
131
} catch(Exception e) {
132
throw new RuntimeException(e);
133
}
134
}
135
136
Sysout.println(" completed. ");
137
138
}
139
140
/*****************************************************
141
* Standard Test Machinery Section
142
* DO NOT modify anything in this section -- it's a
143
* standard chunk of code which has all of the
144
* synchronisation necessary for the test harness.
145
* By keeping it the same in all tests, it is easier
146
* to read and understand someone else's test, as
147
* well as insuring that all tests behave correctly
148
* with the test harness.
149
* There is a section following this for test-defined
150
* classes
151
******************************************************/
152
private static boolean theTestPassed = false;
153
private static boolean testGeneratedInterrupt = false;
154
private static String failureMessage = "";
155
156
private static Thread mainThread = null;
157
158
private static int sleepTime = 300000;
159
160
public static void main( String args[] ) throws InterruptedException
161
{
162
mainThread = Thread.currentThread();
163
try
164
{
165
init();
166
}
167
catch( TestPassedException e )
168
{
169
//The test passed, so just return from main and harness will
170
// interepret this return as a pass
171
return;
172
}
173
//At this point, neither test passed nor test failed has been
174
// called -- either would have thrown an exception and ended the
175
// test, so we know we have multiple threads.
176
177
//Test involves other threads, so sleep and wait for them to
178
// called pass() or fail()
179
try
180
{
181
Thread.sleep( sleepTime );
182
//Timed out, so fail the test
183
throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
184
}
185
catch (InterruptedException e)
186
{
187
if( ! testGeneratedInterrupt ) throw e;
188
189
//reset flag in case hit this code more than once for some reason (just safety)
190
testGeneratedInterrupt = false;
191
if ( theTestPassed == false )
192
{
193
throw new RuntimeException( failureMessage );
194
}
195
}
196
197
}//main
198
199
public static synchronized void setTimeoutTo( int seconds )
200
{
201
sleepTime = seconds * 1000;
202
}
203
204
public static synchronized void pass()
205
{
206
Sysout.println( "The test passed." );
207
Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
208
//first check if this is executing in main thread
209
if ( mainThread == Thread.currentThread() )
210
{
211
//Still in the main thread, so set the flag just for kicks,
212
// and throw a test passed exception which will be caught
213
// and end the test.
214
theTestPassed = true;
215
throw new TestPassedException();
216
}
217
//pass was called from a different thread, so set the flag and interrupt
218
// the main thead.
219
theTestPassed = true;
220
testGeneratedInterrupt = true;
221
mainThread.interrupt();
222
}//pass()
223
224
public static synchronized void fail()
225
{
226
//test writer didn't specify why test failed, so give generic
227
fail( "it just plain failed! :-)" );
228
}
229
230
public static synchronized void fail( String whyFailed )
231
{
232
Sysout.println( "The test failed: " + whyFailed );
233
Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
234
//check if this called from main thread
235
if ( mainThread == Thread.currentThread() )
236
{
237
//If main thread, fail now 'cause not sleeping
238
throw new RuntimeException( whyFailed );
239
}
240
theTestPassed = false;
241
testGeneratedInterrupt = true;
242
failureMessage = whyFailed;
243
mainThread.interrupt();
244
}//fail()
245
246
}// class ManualMainTest
247
248
//This exception is used to exit from any level of call nesting
249
// when it's determined that the test has passed, and immediately
250
// end the test.
251
class TestPassedException extends RuntimeException
252
{
253
}
254
255
//*********** End Standard Test Machinery Section **********
256
257
258
//************ Begin classes defined for the test ****************
259
260
// make listeners in a class defined here, and instantiate them in init()
261
262
/* Example of a class which may be written as part of a test
263
class NewClass implements anInterface
264
{
265
static int newVar = 0;
266
267
public void eventDispatched(AWTEvent e)
268
{
269
//Counting events to see if we get enough
270
eventCount++;
271
272
if( eventCount == 20 )
273
{
274
//got enough events, so pass
275
276
ManualMainTest.pass();
277
}
278
else if( tries == 20 )
279
{
280
//tried too many times without getting enough events so fail
281
282
ManualMainTest.fail();
283
}
284
285
}// eventDispatched()
286
287
}// NewClass class
288
289
*/
290
291
292
//************** End classes defined for the test *******************
293
294
295
296
297
/****************************************************
298
Standard Test Machinery
299
DO NOT modify anything below -- it's a standard
300
chunk of code whose purpose is to make user
301
interaction uniform, and thereby make it simpler
302
to read and understand someone else's test.
303
****************************************************/
304
305
/**
306
This is part of the standard test machinery.
307
It creates a dialog (with the instructions), and is the interface
308
for sending text messages to the user.
309
To print the instructions, send an array of strings to Sysout.createDialog
310
WithInstructions method. Put one line of instructions per array entry.
311
To display a message for the tester to see, simply call Sysout.println
312
with the string to be displayed.
313
This mimics System.out.println but works within the test harness as well
314
as standalone.
315
*/
316
317
class Sysout
318
{
319
private static TestDialog dialog;
320
321
public static void createDialogWithInstructions( String[] instructions )
322
{
323
dialog = new TestDialog( new Frame(), "Instructions" );
324
dialog.printInstructions( instructions );
325
dialog.setVisible(true);
326
println( "Any messages for the tester will display here." );
327
}
328
329
public static void createDialog( )
330
{
331
dialog = new TestDialog( new Frame(), "Instructions" );
332
String[] defInstr = { "Instructions will appear here. ", "" } ;
333
dialog.printInstructions( defInstr );
334
dialog.setVisible(true);
335
println( "Any messages for the tester will display here." );
336
}
337
338
339
public static void printInstructions( String[] instructions )
340
{
341
dialog.printInstructions( instructions );
342
}
343
344
345
public static void println( String messageIn )
346
{
347
dialog.displayMessage( messageIn, true );
348
}
349
350
public static void print( String messageIn )
351
{
352
dialog.displayMessage( messageIn, false );
353
}
354
355
}// Sysout class
356
357
/**
358
This is part of the standard test machinery. It provides a place for the
359
test instructions to be displayed, and a place for interactive messages
360
to the user to be displayed.
361
To have the test instructions displayed, see Sysout.
362
To have a message to the user be displayed, see Sysout.
363
Do not call anything in this dialog directly.
364
*/
365
class TestDialog extends Dialog implements ActionListener
366
{
367
368
TextArea instructionsText;
369
TextArea messageText;
370
int maxStringLength = 80;
371
Panel buttonP = new Panel();
372
Button passB = new Button( "pass" );
373
Button failB = new Button( "fail" );
374
375
//DO NOT call this directly, go through Sysout
376
public TestDialog( Frame frame, String name )
377
{
378
super( frame, name );
379
int scrollBoth = TextArea.SCROLLBARS_BOTH;
380
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
381
add( "North", instructionsText );
382
383
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
384
add("Center", messageText);
385
386
passB = new Button( "pass" );
387
passB.setActionCommand( "pass" );
388
passB.addActionListener( this );
389
buttonP.add( "East", passB );
390
391
failB = new Button( "fail" );
392
failB.setActionCommand( "fail" );
393
failB.addActionListener( this );
394
buttonP.add( "West", failB );
395
396
add( "South", buttonP );
397
pack();
398
399
setVisible(true);
400
}// TestDialog()
401
402
//DO NOT call this directly, go through Sysout
403
public void printInstructions( String[] instructions )
404
{
405
//Clear out any current instructions
406
instructionsText.setText( "" );
407
408
//Go down array of instruction strings
409
410
String printStr, remainingStr;
411
for( int i=0; i < instructions.length; i++ )
412
{
413
//chop up each into pieces maxSringLength long
414
remainingStr = instructions[ i ];
415
while( remainingStr.length() > 0 )
416
{
417
//if longer than max then chop off first max chars to print
418
if( remainingStr.length() >= maxStringLength )
419
{
420
//Try to chop on a word boundary
421
int posOfSpace = remainingStr.
422
lastIndexOf( ' ', maxStringLength - 1 );
423
424
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
425
426
printStr = remainingStr.substring( 0, posOfSpace + 1 );
427
remainingStr = remainingStr.substring( posOfSpace + 1 );
428
}
429
//else just print
430
else
431
{
432
printStr = remainingStr;
433
remainingStr = "";
434
}
435
436
instructionsText.append( printStr + "\n" );
437
438
}// while
439
440
}// for
441
442
}//printInstructions()
443
444
//DO NOT call this directly, go through Sysout
445
public void displayMessage( String messageIn, boolean nextLine )
446
{
447
messageText.append( messageIn + (nextLine? "\n" : "") );
448
System.out.println(messageIn);
449
}
450
451
//catch presses of the passed and failed buttons.
452
//simply call the standard pass() or fail() static methods of
453
//ManualMainTest
454
public void actionPerformed( ActionEvent e )
455
{
456
if( e.getActionCommand() == "pass" )
457
{
458
CloseBlocker.pass();
459
}
460
else
461
{
462
CloseBlocker.fail();
463
}
464
}
465
466
}// TestDialog class
467
468