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