Path: blob/master/test/jdk/java/awt/Mixing/Validating.java
41149 views
/*1* Copyright (c) 2008, 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 6682046 819800227@summary Mixing code does not always recalculate shapes correctly when resizing components28@author anthony.petrov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main Validating32*/3334/**35* Validating.java36*37* summary: Mixing code does not always recalculate shapes correctly when resizing components38*/3940import java.awt.Button;41import java.awt.event.InputEvent;42import java.awt.Frame;43import java.awt.Point;44import java.awt.Robot;45import java.awt.Toolkit;46import test.java.awt.regtesthelpers.Util;4748public class Validating49{50static volatile boolean clickPassed = false;5152private static void init()53{54if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {55System.out.println("The test environment does not support maximization. The test cannot be performed.");56pass();57return;58}5960// Create the frame with a button.61Frame f = new Frame();62Button b = new Button("ok");63b.addActionListener(new java.awt.event.ActionListener() {64public void actionPerformed(java.awt.event.ActionEvent e) {65clickPassed = true;66}67});68f.add(b);69// Make the frame maximized70f.setExtendedState(Frame.MAXIMIZED_BOTH);71f.pack();72f.setVisible(true);7374Robot robot = Util.createRobot();75robot.setAutoDelay(20);7677Util.waitForIdle(robot);7879// Now let's attempt to click in the middle of the button80// (i.e. in the middle of the window).81// If the button doesn't receive the click, it means that the test82// failed: the shape of the button was not enlarged.83Point heavyLoc = b.getLocationOnScreen();84robot.mouseMove(heavyLoc.x + b.getWidth() / 2, heavyLoc.y + b.getHeight() / 2);8586robot.mousePress(InputEvent.BUTTON1_MASK);87robot.mouseRelease(InputEvent.BUTTON1_MASK);88Util.waitForIdle(robot);8990if (clickPassed) {91pass();92} else {93fail("The button cannot be clicked.");94}95}//End init()96979899/*****************************************************100* Standard Test Machinery Section101* DO NOT modify anything in this section -- it's a102* standard chunk of code which has all of the103* synchronisation necessary for the test harness.104* By keeping it the same in all tests, it is easier105* to read and understand someone else's test, as106* well as insuring that all tests behave correctly107* with the test harness.108* There is a section following this for test-109* classes110******************************************************/111private static boolean theTestPassed = false;112private static boolean testGeneratedInterrupt = false;113private static String failureMessage = "";114115private static Thread mainThread = null;116117private static int sleepTime = 300000;118119// Not sure about what happens if multiple of this test are120// instantiated in the same VM. Being static (and using121// static vars), it aint gonna work. Not worrying about122// it for now.123public static void main( String args[] ) throws InterruptedException124{125mainThread = Thread.currentThread();126try127{128init();129}130catch( TestPassedException e )131{132//The test passed, so just return from main and harness will133// interepret this return as a pass134return;135}136//At this point, neither test pass nor test fail has been137// called -- either would have thrown an exception and ended the138// test, so we know we have multiple threads.139140//Test involves other threads, so sleep and wait for them to141// called pass() or fail()142try143{144Thread.sleep( sleepTime );145//Timed out, so fail the test146throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );147}148catch (InterruptedException e)149{150//The test harness may have interrupted the test. If so, rethrow the exception151// so that the harness gets it and deals with it.152if( ! testGeneratedInterrupt ) throw e;153154//reset flag in case hit this code more than once for some reason (just safety)155testGeneratedInterrupt = false;156157if ( theTestPassed == false )158{159throw new RuntimeException( failureMessage );160}161}162163}//main164165public static synchronized void setTimeoutTo( int seconds )166{167sleepTime = seconds * 1000;168}169170public static synchronized void pass()171{172System.out.println( "The test passed." );173System.out.println( "The test is over, hit Ctl-C to stop Java VM" );174//first check if this is executing in main thread175if ( mainThread == Thread.currentThread() )176{177//Still in the main thread, so set the flag just for kicks,178// and throw a test passed exception which will be caught179// and end the test.180theTestPassed = true;181throw new TestPassedException();182}183theTestPassed = true;184testGeneratedInterrupt = true;185mainThread.interrupt();186}//pass()187188public static synchronized void fail()189{190//test writer didn't specify why test failed, so give generic191fail( "it just plain failed! :-)" );192}193194public static synchronized void fail( String whyFailed )195{196System.out.println( "The test failed: " + whyFailed );197System.out.println( "The test is over, hit Ctl-C to stop Java VM" );198//check if this called from main thread199if ( mainThread == Thread.currentThread() )200{201//If main thread, fail now 'cause not sleeping202throw new RuntimeException( whyFailed );203}204theTestPassed = false;205testGeneratedInterrupt = true;206failureMessage = whyFailed;207mainThread.interrupt();208}//fail()209210}// class Validating211212//This exception is used to exit from any level of call nesting213// when it's determined that the test has passed, and immediately214// end the test.215class TestPassedException extends RuntimeException216{217}218219220