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