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