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