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