Path: blob/master/test/jdk/java/awt/Mixing/MixingOnShrinkingHWButton.java
41149 views
/*1* Copyright (c) 2009, 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@test %W% %E%25@key headful26@bug 677732027@summary PIT : Canvas is not fully painted on the internal frame & internal frame goes behind the canvas28@author dmitry.cherepanov@...: area=awt.mixing29@library ../regtesthelpers30@build Util31@run main MixingOnShrinkingHWButton32*/333435/**36* MixingOnDialog.java37*38* summary: Tests whether awt.Button and swing.JButton mix correctly39* when awt.Button's width got shrinked40*/4142import java.awt.*;43import java.awt.event.*;44import javax.swing.*;45import test.java.awt.regtesthelpers.Util;46474849public class MixingOnShrinkingHWButton50{51static volatile boolean heavyClicked = false;52static volatile boolean lightClicked = false;5354private static void init()55{56// Create components57final Dialog d = new Dialog((Frame)null, "Button-JButton mix test");58final Button heavy = new Button(" Heavyweight Button ");59final JButton light = new JButton(" LW Button ");6061// Actions for the buttons add appropriate number to the test sequence62heavy.addActionListener(new java.awt.event.ActionListener()63{64public void actionPerformed(java.awt.event.ActionEvent e) {65heavyClicked = true;66}67}68);6970light.addActionListener(new java.awt.event.ActionListener()71{72public void actionPerformed(java.awt.event.ActionEvent e) {73lightClicked = true;74}75}76);7778// Shrink the HW button under LW button79heavy.setBounds(30, 30, 100, 100);80light.setBounds(40, 30, 100, 100);8182// Put the components into the frame83d.setLayout(null);84d.add(light);85d.add(heavy);86d.setBounds(50, 50, 400, 400);87d.setVisible(true);888990Robot robot = Util.createRobot();91robot.setAutoDelay(20);9293Util.waitForIdle(robot);9495// Move the mouse pointer to the position where both96// buttons overlap97Point heavyLoc = heavy.getLocationOnScreen();98robot.mouseMove(heavyLoc.x + 20, heavyLoc.y + 20);99100// Now perform the click at this point101robot.mousePress(InputEvent.BUTTON1_MASK);102robot.mouseRelease(InputEvent.BUTTON1_MASK);103Util.waitForIdle(robot);104105// If the buttons are correctly mixed, the test sequence106// is equal to the check sequence.107if (lightClicked == true) {108MixingOnShrinkingHWButton.pass();109} else {110MixingOnShrinkingHWButton.fail("The lightweight component left behind the heavyweight one.");111}112}//End init()113114115116/*****************************************************117* Standard Test Machinery Section118* DO NOT modify anything in this section -- it's a119* standard chunk of code which has all of the120* synchronisation necessary for the test harness.121* By keeping it the same in all tests, it is easier122* to read and understand someone else's test, as123* well as insuring that all tests behave correctly124* with the test harness.125* There is a section following this for test-126* classes127******************************************************/128private static boolean theTestPassed = false;129private static boolean testGeneratedInterrupt = false;130private static String failureMessage = "";131132private static Thread mainThread = null;133134private static int sleepTime = 300000;135136// Not sure about what happens if multiple of this test are137// instantiated in the same VM. Being static (and using138// static vars), it aint gonna work. Not worrying about139// it for now.140public static void main( String args[] ) throws InterruptedException141{142mainThread = Thread.currentThread();143try144{145init();146}147catch( TestPassedException e )148{149//The test passed, so just return from main and harness will150// interepret this return as a pass151return;152}153//At this point, neither test pass nor test fail has been154// called -- either would have thrown an exception and ended the155// test, so we know we have multiple threads.156157//Test involves other threads, so sleep and wait for them to158// called pass() or fail()159try160{161Thread.sleep( sleepTime );162//Timed out, so fail the test163throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );164}165catch (InterruptedException e)166{167//The test harness may have interrupted the test. If so, rethrow the exception168// so that the harness gets it and deals with it.169if( ! testGeneratedInterrupt ) throw e;170171//reset flag in case hit this code more than once for some reason (just safety)172testGeneratedInterrupt = false;173174if ( theTestPassed == false )175{176throw new RuntimeException( failureMessage );177}178}179180}//main181182public static synchronized void setTimeoutTo( int seconds )183{184sleepTime = seconds * 1000;185}186187public static synchronized void pass()188{189System.out.println( "The test passed." );190System.out.println( "The test is over, hit Ctl-C to stop Java VM" );191//first check if this is executing in main thread192if ( 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 caught196// and end the test.197theTestPassed = true;198throw new TestPassedException();199}200theTestPassed = true;201testGeneratedInterrupt = true;202mainThread.interrupt();203}//pass()204205public static synchronized void fail()206{207//test writer didn't specify why test failed, so give generic208fail( "it just plain failed! :-)" );209}210211public static synchronized void fail( String whyFailed )212{213System.out.println( "The test failed: " + whyFailed );214System.out.println( "The test is over, hit Ctl-C to stop Java VM" );215//check if this called from main thread216if ( mainThread == Thread.currentThread() )217{218//If main thread, fail now 'cause not sleeping219throw new RuntimeException( whyFailed );220}221theTestPassed = false;222testGeneratedInterrupt = true;223failureMessage = whyFailed;224mainThread.interrupt();225}//fail()226227}// class MixingOnDialog228229//This exception is used to exit from any level of call nesting230// when it's determined that the test has passed, and immediately231// end the test.232class TestPassedException extends RuntimeException233{234}235236237