blob: 6d00f4393be2ca3c6cbaa9f9e248bfe7843bc45c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 @test
26 @bug 4811096
27 @summary Tests whether opaque and non-opaque components mix correctly
28 @author anthony.petrov@...: area=awt.mixing
29 @library ../regtesthelpers
30 @build Util
31 @run main OpaqueTest
32*/
33
34
35/**
36 * OpaqueTest.java
37 *
38 * summary: OpaqueTest
39 */
40
41import java.awt.*;
42import java.awt.event.*;
43import javax.swing.*;
44import test.java.awt.regtesthelpers.Util;
45
46
47
48public class OpaqueTest
49{
50
51 //*** test-writer defined static variables go here ***
52
53 static String testSeq = new String("");
54 final static String checkSeq = new String("010000101");
55
56 private static void init()
57 {
58 //*** Create instructions for the user here ***
59
60 String[] instructions =
61 {
62 "This is an AUTOMATIC test, simply wait until it is done.",
63 "The result (passed or failed) will be shown in the",
64 "message window below."
65 };
66 Sysout.createDialog( );
67 Sysout.printInstructions( instructions );
68
69
70 // Create components
71 final Frame f = new Frame("Button-JButton mix test");
72 final Panel p = new Panel();
73 final Button heavy = new Button(" Heavyweight Button ");
74 final JButton light = new JButton(" LW Button ");
75
76 // Actions for the buttons add appropriate number to the test sequence
77 heavy.addActionListener(new java.awt.event.ActionListener()
78 {
79 public void actionPerformed(java.awt.event.ActionEvent e) {
80 p.setComponentZOrder(light, 0);
81 testSeq = testSeq + "0";
82 }
83 }
84 );
85
86 light.addActionListener(new java.awt.event.ActionListener()
87 {
88 public void actionPerformed(java.awt.event.ActionEvent e) {
89 p.setComponentZOrder(heavy, 0);
90 testSeq = testSeq + "1";
91 }
92 }
93 );
94
95 // Overlap the buttons
96 heavy.setBounds(30, 30, 200, 200);
97 light.setBounds(10, 10, 50, 50);
98
99 // Put the components into the frame
100 p.setLayout(null);
101 p.add(heavy);
102 p.add(light);
103 f.add(p);
104 f.setBounds(50, 50, 400, 400);
105 f.show();
106
107
108 Robot robot = Util.createRobot();
109 robot.setAutoDelay(20);
110
111 Util.waitForIdle(robot);
112
113 // Move the mouse pointer to the position where both
114 // buttons overlap
115 Point heavyLoc = heavy.getLocationOnScreen();
116 robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);
117
118 // Now perform the click at this point for 9 times
119 // In the middle of the process toggle the opaque
120 // flag value.
121 for (int i = 0; i < 9; ++i) {
122 if (i == 3) {
123 light.setOpaque(false);
124 }
125 if (i == 6) {
126 light.setOpaque(true);
127 }
128
129 robot.mousePress(InputEvent.BUTTON1_MASK);
130 robot.mouseRelease(InputEvent.BUTTON1_MASK);
131 Util.waitForIdle(robot);
132 }
133
134 Util.waitForIdle(robot);
135
136 // If the buttons are correctly mixed, the test sequence
137 // is equal to the check sequence.
138 if (testSeq.equals(checkSeq)) {
139 OpaqueTest.pass();
140 } else {
141 OpaqueTest.fail("The components changed their visible Z-order in a wrong sequence: '" + testSeq + "' instead of '" + checkSeq + "'");
142 }
143 }//End init()
144
145
146
147 /*****************************************************
148 * Standard Test Machinery Section
149 * DO NOT modify anything in this section -- it's a
150 * standard chunk of code which has all of the
151 * synchronisation necessary for the test harness.
152 * By keeping it the same in all tests, it is easier
153 * to read and understand someone else's test, as
154 * well as insuring that all tests behave correctly
155 * with the test harness.
156 * There is a section following this for test-
157 * classes
158 ******************************************************/
159 private static boolean theTestPassed = false;
160 private static boolean testGeneratedInterrupt = false;
161 private static String failureMessage = "";
162
163 private static Thread mainThread = null;
164
165 private static int sleepTime = 300000;
166
167 // Not sure about what happens if multiple of this test are
168 // instantiated in the same VM. Being static (and using
169 // static vars), it aint gonna work. Not worrying about
170 // it for now.
171 public static void main( String args[] ) throws InterruptedException
172 {
173 mainThread = Thread.currentThread();
174 try
175 {
176 init();
177 }
178 catch( TestPassedException e )
179 {
180 //The test passed, so just return from main and harness will
181 // interepret this return as a pass
182 return;
183 }
184 //At this point, neither test pass nor test fail has been
185 // called -- either would have thrown an exception and ended the
186 // test, so we know we have multiple threads.
187
188 //Test involves other threads, so sleep and wait for them to
189 // called pass() or fail()
190 try
191 {
192 Thread.sleep( sleepTime );
193 //Timed out, so fail the test
194 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
195 }
196 catch (InterruptedException e)
197 {
198 //The test harness may have interrupted the test. If so, rethrow the exception
199 // so that the harness gets it and deals with it.
200 if( ! testGeneratedInterrupt ) throw e;
201
202 //reset flag in case hit this code more than once for some reason (just safety)
203 testGeneratedInterrupt = false;
204
205 if ( theTestPassed == false )
206 {
207 throw new RuntimeException( failureMessage );
208 }
209 }
210
211 }//main
212
213 public static synchronized void setTimeoutTo( int seconds )
214 {
215 sleepTime = seconds * 1000;
216 }
217
218 public static synchronized void pass()
219 {
220 Sysout.println( "The test passed." );
221 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
222 //first check if this is executing in main thread
223 if ( mainThread == Thread.currentThread() )
224 {
225 //Still in the main thread, so set the flag just for kicks,
226 // and throw a test passed exception which will be caught
227 // and end the test.
228 theTestPassed = true;
229 throw new TestPassedException();
230 }
231 theTestPassed = true;
232 testGeneratedInterrupt = true;
233 mainThread.interrupt();
234 }//pass()
235
236 public static synchronized void fail()
237 {
238 //test writer didn't specify why test failed, so give generic
239 fail( "it just plain failed! :-)" );
240 }
241
242 public static synchronized void fail( String whyFailed )
243 {
244 Sysout.println( "The test failed: " + whyFailed );
245 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
246 //check if this called from main thread
247 if ( mainThread == Thread.currentThread() )
248 {
249 //If main thread, fail now 'cause not sleeping
250 throw new RuntimeException( whyFailed );
251 }
252 theTestPassed = false;
253 testGeneratedInterrupt = true;
254 failureMessage = whyFailed;
255 mainThread.interrupt();
256 }//fail()
257
258}// class OpaqueTest
259
260//This exception is used to exit from any level of call nesting
261// when it's determined that the test has passed, and immediately
262// end the test.
263class TestPassedException extends RuntimeException
264{
265}
266
267//*********** End Standard Test Machinery Section **********
268
269
270//************ Begin classes defined for the test ****************
271
272// if want to make listeners, here is the recommended place for them, then instantiate
273// them in init()
274
275/* Example of a class which may be written as part of a test
276class NewClass implements anInterface
277 {
278 static int newVar = 0;
279
280 public void eventDispatched(AWTEvent e)
281 {
282 //Counting events to see if we get enough
283 eventCount++;
284
285 if( eventCount == 20 )
286 {
287 //got enough events, so pass
288
289 OpaqueTest.pass();
290 }
291 else if( tries == 20 )
292 {
293 //tried too many times without getting enough events so fail
294
295 OpaqueTest.fail();
296 }
297
298 }// eventDispatched()
299
300 }// NewClass class
301
302*/
303
304
305//************** End classes defined for the test *******************
306
307
308
309
310/****************************************************
311 Standard Test Machinery
312 DO NOT modify anything below -- it's a standard
313 chunk of code whose purpose is to make user
314 interaction uniform, and thereby make it simpler
315 to read and understand someone else's test.
316 ****************************************************/
317
318/**
319 This is part of the standard test machinery.
320 It creates a dialog (with the instructions), and is the interface
321 for sending text messages to the user.
322 To print the instructions, send an array of strings to Sysout.createDialog
323 WithInstructions method. Put one line of instructions per array entry.
324 To display a message for the tester to see, simply call Sysout.println
325 with the string to be displayed.
326 This mimics System.out.println but works within the test harness as well
327 as standalone.
328 */
329
330class Sysout
331{
332 private static TestDialog dialog;
333
334 public static void createDialogWithInstructions( String[] instructions )
335 {
336 dialog = new TestDialog( new Frame(), "Instructions" );
337 dialog.printInstructions( instructions );
338 dialog.setVisible(true);
339 println( "Any messages for the tester will display here." );
340 }
341
342 public static void createDialog( )
343 {
344 dialog = new TestDialog( new Frame(), "Instructions" );
345 String[] defInstr = { "Instructions will appear here. ", "" } ;
346 dialog.printInstructions( defInstr );
347 dialog.setVisible(true);
348 println( "Any messages for the tester will display here." );
349 }
350
351
352 public static void printInstructions( String[] instructions )
353 {
354 dialog.printInstructions( instructions );
355 }
356
357
358 public static void println( String messageIn )
359 {
360 dialog.displayMessage( messageIn );
361 System.out.println(messageIn);
362 }
363
364}// Sysout class
365
366/**
367 This is part of the standard test machinery. It provides a place for the
368 test instructions to be displayed, and a place for interactive messages
369 to the user to be displayed.
370 To have the test instructions displayed, see Sysout.
371 To have a message to the user be displayed, see Sysout.
372 Do not call anything in this dialog directly.
373 */
374class TestDialog extends Dialog
375{
376
377 TextArea instructionsText;
378 TextArea messageText;
379 int maxStringLength = 80;
380
381 //DO NOT call this directly, go through Sysout
382 public TestDialog( Frame frame, String name )
383 {
384 super( frame, name );
385 int scrollBoth = TextArea.SCROLLBARS_BOTH;
386 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
387 add( "North", instructionsText );
388
389 messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
390 add("Center", messageText);
391
392 pack();
393
394 setVisible(true);
395 }// TestDialog()
396
397 //DO NOT call this directly, go through Sysout
398 public void printInstructions( String[] instructions )
399 {
400 //Clear out any current instructions
401 instructionsText.setText( "" );
402
403 //Go down array of instruction strings
404
405 String printStr, remainingStr;
406 for( int i=0; i < instructions.length; i++ )
407 {
408 //chop up each into pieces maxSringLength long
409 remainingStr = instructions[ i ];
410 while( remainingStr.length() > 0 )
411 {
412 //if longer than max then chop off first max chars to print
413 if( remainingStr.length() >= maxStringLength )
414 {
415 //Try to chop on a word boundary
416 int posOfSpace = remainingStr.
417 lastIndexOf( ' ', maxStringLength - 1 );
418
419 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
420
421 printStr = remainingStr.substring( 0, posOfSpace + 1 );
422 remainingStr = remainingStr.substring( posOfSpace + 1 );
423 }
424 //else just print
425 else
426 {
427 printStr = remainingStr;
428 remainingStr = "";
429 }
430
431 instructionsText.append( printStr + "\n" );
432
433 }// while
434
435 }// for
436
437 }//printInstructions()
438
439 //DO NOT call this directly, go through Sysout
440 public void displayMessage( String messageIn )
441 {
442 messageText.append( messageIn + "\n" );
443 System.out.println(messageIn);
444 }
445
446}// TestDialog class