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