1 /*
   2  * Copyright (c) 2007, 2018, 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   @key headful
  27   @bug 4811096
  28   @summary Tests whether mixing works on Dialogs
  29   @author anthony.petrov@...: area=awt.mixing
  30   @library ../regtesthelpers
  31   @build Util
  32   @run main MixingOnDialog
  33 */
  34 
  35 
  36 /**
  37  * MixingOnDialog.java
  38  *
  39  * summary:  Tests whether awt.Button and swing.JButton mix correctly
  40  */
  41 
  42 import java.awt.*;
  43 import java.awt.event.*;
  44 import javax.swing.*;
  45 import test.java.awt.regtesthelpers.Util;
  46 
  47 
  48 
  49 public class MixingOnDialog
  50 {
  51     static volatile boolean heavyClicked = false;
  52     static volatile boolean lightClicked = false;
  53 
  54     private static void init()
  55     {
  56         // Create components
  57         final Dialog d = new Dialog((Frame)null, "Button-JButton mix test");
  58         final Button heavy = new Button("  Heavyweight Button  ");
  59         final JButton light = new JButton("  LW Button  ");
  60 
  61         // Actions for the buttons add appropriate number to the test sequence
  62         heavy.addActionListener(new java.awt.event.ActionListener()
  63                 {
  64                     public void actionPerformed(java.awt.event.ActionEvent e) {
  65                         heavyClicked = true;
  66                     }
  67                 }
  68                 );
  69 
  70         light.addActionListener(new java.awt.event.ActionListener()
  71                 {
  72                     public void actionPerformed(java.awt.event.ActionEvent e) {
  73                         lightClicked = true;
  74                     }
  75                 }
  76                 );
  77 
  78         // Overlap the buttons
  79         heavy.setBounds(30, 30, 200, 200);
  80         light.setBounds(10, 10, 50, 50);
  81 
  82         // Put the components into the frame
  83         d.setLayout(null);
  84         d.add(light);
  85         d.add(heavy);
  86         d.setBounds(50, 50, 400, 400);
  87         d.setVisible(true);
  88 
  89 
  90         Robot robot = Util.createRobot();
  91         robot.setAutoDelay(20);
  92 
  93         Util.waitForIdle(robot);
  94 
  95         // Move the mouse pointer to the position where both
  96         //    buttons overlap
  97         Point heavyLoc = heavy.getLocationOnScreen();
  98         robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);
  99 
 100         // Now perform the click at this point
 101         robot.mousePress(InputEvent.BUTTON1_MASK);
 102         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 103         Util.waitForIdle(robot);
 104 
 105         // If the buttons are correctly mixed, the test sequence
 106         // is equal to the check sequence.
 107         if (lightClicked == true) {
 108             MixingOnDialog.pass();
 109         } else {
 110             MixingOnDialog.fail("The lightweight component left behind the heavyweight one.");
 111         }
 112     }//End  init()
 113 
 114 
 115 
 116     /*****************************************************
 117      * Standard Test Machinery Section
 118      * DO NOT modify anything in this section -- it's a
 119      * standard chunk of code which has all of the
 120      * synchronisation necessary for the test harness.
 121      * By keeping it the same in all tests, it is easier
 122      * to read and understand someone else's test, as
 123      * well as insuring that all tests behave correctly
 124      * with the test harness.
 125      * There is a section following this for test-
 126      * classes
 127      ******************************************************/
 128     private static boolean theTestPassed = false;
 129     private static boolean testGeneratedInterrupt = false;
 130     private static String failureMessage = "";
 131 
 132     private static Thread mainThread = null;
 133 
 134     private static int sleepTime = 300000;
 135 
 136     // Not sure about what happens if multiple of this test are
 137     //  instantiated in the same VM.  Being static (and using
 138     //  static vars), it aint gonna work.  Not worrying about
 139     //  it for now.
 140     public static void main( String args[] ) throws InterruptedException
 141     {
 142         mainThread = Thread.currentThread();
 143         try
 144         {
 145             init();
 146         }
 147         catch( TestPassedException e )
 148         {
 149             //The test passed, so just return from main and harness will
 150             // interepret this return as a pass
 151             return;
 152         }
 153         //At this point, neither test pass nor test fail has been
 154         // called -- either would have thrown an exception and ended the
 155         // test, so we know we have multiple threads.
 156 
 157         //Test involves other threads, so sleep and wait for them to
 158         // called pass() or fail()
 159         try
 160         {
 161             Thread.sleep( sleepTime );
 162             //Timed out, so fail the test
 163             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 164         }
 165         catch (InterruptedException e)
 166         {
 167             //The test harness may have interrupted the test.  If so, rethrow the exception
 168             // so that the harness gets it and deals with it.
 169             if( ! testGeneratedInterrupt ) throw e;
 170 
 171             //reset flag in case hit this code more than once for some reason (just safety)
 172             testGeneratedInterrupt = false;
 173 
 174             if ( theTestPassed == false )
 175             {
 176                 throw new RuntimeException( failureMessage );
 177             }
 178         }
 179 
 180     }//main
 181 
 182     public static synchronized void setTimeoutTo( int seconds )
 183     {
 184         sleepTime = seconds * 1000;
 185     }
 186 
 187     public static synchronized void pass()
 188     {
 189         System.out.println( "The test passed." );
 190         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 191         //first check if this is executing in main thread
 192         if ( mainThread == Thread.currentThread() )
 193         {
 194             //Still in the main thread, so set the flag just for kicks,
 195             // and throw a test passed exception which will be caught
 196             // and end the test.
 197             theTestPassed = true;
 198             throw new TestPassedException();
 199         }
 200         theTestPassed = true;
 201         testGeneratedInterrupt = true;
 202         mainThread.interrupt();
 203     }//pass()
 204 
 205     public static synchronized void fail()
 206     {
 207         //test writer didn't specify why test failed, so give generic
 208         fail( "it just plain failed! :-)" );
 209     }
 210 
 211     public static synchronized void fail( String whyFailed )
 212     {
 213         System.out.println( "The test failed: " + whyFailed );
 214         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 215         //check if this called from main thread
 216         if ( mainThread == Thread.currentThread() )
 217         {
 218             //If main thread, fail now 'cause not sleeping
 219             throw new RuntimeException( whyFailed );
 220         }
 221         theTestPassed = false;
 222         testGeneratedInterrupt = true;
 223         failureMessage = whyFailed;
 224         mainThread.interrupt();
 225     }//fail()
 226 
 227 }// class MixingOnDialog
 228 
 229 //This exception is used to exit from any level of call nesting
 230 // when it's determined that the test has passed, and immediately
 231 // end the test.
 232 class TestPassedException extends RuntimeException
 233 {
 234 }