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