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