Path: blob/master/test/jdk/java/awt/Mixing/ValidBounds.java
41149 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 663779627@summary Shape should be correctly updated on invalid components28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main ValidBounds32*/3334/**35* ValidBounds.java36*37* summary: Shape should be correctly updated on invalid components38*/3940import java.awt.*;41import java.awt.event.*;42import test.java.awt.regtesthelpers.Util;4344public class ValidBounds45{4647static volatile boolean clickPassed = false;4849private static void init()50{51// Create the frame and the button52Frame f = new Frame();53f.setBounds(100, 100, 400, 300);5455Button b = new Button("OK");5657f.setLayout(null);58f.add(b);59b.setBounds(50, 50, 200, 50);6061b.addActionListener(new java.awt.event.ActionListener() {62public void actionPerformed(java.awt.event.ActionEvent e) {63clickPassed = true;64}65});6667f.setVisible(true);6869// Let's make the button much smaller first...70Robot robot = Util.createRobot();71robot.setAutoDelay(20);7273Util.waitForIdle(robot);7475b.setBounds(50, 50, 5, 5);76Util.waitForIdle(robot);7778// ... and now let's enlarge it.79b.setBounds(50, 50, 200, 50);80Util.waitForIdle(robot);8182// If the button doesn't receive the click, it means that the test83// failed: the shape of the button was not enlarged.84Point heavyLoc = b.getLocationOnScreen();85robot.mouseMove(heavyLoc.x + 20, heavyLoc.y + 20);8687robot.mousePress(InputEvent.BUTTON1_MASK);88robot.mouseRelease(InputEvent.BUTTON1_MASK);89Util.waitForIdle(robot);9091if (clickPassed) {92pass();93} else {94fail("The button cannot be clicked.");95}96}//End init()979899100/*****************************************************101* Standard Test Machinery Section102* DO NOT modify anything in this section -- it's a103* standard chunk of code which has all of the104* synchronisation necessary for the test harness.105* By keeping it the same in all tests, it is easier106* to read and understand someone else's test, as107* well as insuring that all tests behave correctly108* with the test harness.109* There is a section following this for test-110* classes111******************************************************/112private static boolean theTestPassed = false;113private static boolean testGeneratedInterrupt = false;114private static String failureMessage = "";115116private static Thread mainThread = null;117118private static int sleepTime = 300000;119120// Not sure about what happens if multiple of this test are121// instantiated in the same VM. Being static (and using122// static vars), it aint gonna work. Not worrying about123// it for now.124public static void main( String args[] ) throws InterruptedException125{126mainThread = Thread.currentThread();127try128{129init();130}131catch( TestPassedException e )132{133//The test passed, so just return from main and harness will134// interepret this return as a pass135return;136}137//At this point, neither test pass nor test fail has been138// called -- either would have thrown an exception and ended the139// test, so we know we have multiple threads.140141//Test involves other threads, so sleep and wait for them to142// called pass() or fail()143try144{145Thread.sleep( sleepTime );146//Timed out, so fail the test147throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );148}149catch (InterruptedException e)150{151//The test harness may have interrupted the test. If so, rethrow the exception152// so that the harness gets it and deals with it.153if( ! testGeneratedInterrupt ) throw e;154155//reset flag in case hit this code more than once for some reason (just safety)156testGeneratedInterrupt = false;157158if ( theTestPassed == false )159{160throw new RuntimeException( failureMessage );161}162}163164}//main165166public static synchronized void setTimeoutTo( int seconds )167{168sleepTime = seconds * 1000;169}170171public static synchronized void pass()172{173System.out.println( "The test passed." );174System.out.println( "The test is over, hit Ctl-C to stop Java VM" );175//first check if this is executing in main thread176if ( mainThread == Thread.currentThread() )177{178//Still in the main thread, so set the flag just for kicks,179// and throw a test passed exception which will be caught180// and end the test.181theTestPassed = true;182throw new TestPassedException();183}184theTestPassed = true;185testGeneratedInterrupt = true;186mainThread.interrupt();187}//pass()188189public static synchronized void fail()190{191//test writer didn't specify why test failed, so give generic192fail( "it just plain failed! :-)" );193}194195public static synchronized void fail( String whyFailed )196{197System.out.println( "The test failed: " + whyFailed );198System.out.println( "The test is over, hit Ctl-C to stop Java VM" );199//check if this called from main thread200if ( mainThread == Thread.currentThread() )201{202//If main thread, fail now 'cause not sleeping203throw new RuntimeException( whyFailed );204}205theTestPassed = false;206testGeneratedInterrupt = true;207failureMessage = whyFailed;208mainThread.interrupt();209}//fail()210211}// class ValidBounds212213//This exception is used to exit from any level of call nesting214// when it's determined that the test has passed, and immediately215// end the test.216class TestPassedException extends RuntimeException217{218}219220221