Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/MixingOnShrinkingHWButton.java
47661 views
/*1* Copyright (c) 2009, 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@bug 677732026@summary PIT : Canvas is not fully painted on the internal frame & internal frame goes behind the canvas27@author dmitry.cherepanov@...: area=awt.mixing28@library ../regtesthelpers29@build Util30@run main MixingOnShrinkingHWButton31*/323334/**35* MixingOnDialog.java36*37* summary: Tests whether awt.Button and swing.JButton mix correctly38* when awt.Button's width got shrinked39*/4041import java.awt.*;42import java.awt.event.*;43import javax.swing.*;44import test.java.awt.regtesthelpers.Util;45464748public class MixingOnShrinkingHWButton49{50static volatile boolean heavyClicked = false;51static volatile boolean lightClicked = false;5253private static void init()54{55//*** Create instructions for the user here ***5657String[] instructions =58{59"This is an AUTOMATIC test, simply wait until it is done.",60"The result (passed or failed) will be shown in the",61"message window below."62};63Sysout.createDialog( );64Sysout.printInstructions( instructions );656667// Create components68final Dialog d = new Dialog((Frame)null, "Button-JButton mix test");69final Button heavy = new Button(" Heavyweight Button ");70final JButton light = new JButton(" LW Button ");7172// Actions for the buttons add appropriate number to the test sequence73heavy.addActionListener(new java.awt.event.ActionListener()74{75public void actionPerformed(java.awt.event.ActionEvent e) {76heavyClicked = true;77}78}79);8081light.addActionListener(new java.awt.event.ActionListener()82{83public void actionPerformed(java.awt.event.ActionEvent e) {84lightClicked = true;85}86}87);8889// Shrink the HW button under LW button90heavy.setBounds(30, 30, 100, 100);91light.setBounds(40, 30, 100, 100);9293// Put the components into the frame94d.setLayout(null);95d.add(light);96d.add(heavy);97d.setBounds(50, 50, 400, 400);98d.setVisible(true);99100101Robot robot = Util.createRobot();102robot.setAutoDelay(20);103104Util.waitForIdle(robot);105106// Move the mouse pointer to the position where both107// buttons overlap108Point heavyLoc = heavy.getLocationOnScreen();109robot.mouseMove(heavyLoc.x + 20, heavyLoc.y + 20);110111// Now perform the click at this point112robot.mousePress(InputEvent.BUTTON1_MASK);113robot.mouseRelease(InputEvent.BUTTON1_MASK);114Util.waitForIdle(robot);115116// If the buttons are correctly mixed, the test sequence117// is equal to the check sequence.118if (lightClicked == true) {119MixingOnShrinkingHWButton.pass();120} else {121MixingOnShrinkingHWButton.fail("The lightweight component left behind the heavyweight one.");122}123}//End init()124125126127/*****************************************************128* Standard Test Machinery Section129* DO NOT modify anything in this section -- it's a130* standard chunk of code which has all of the131* synchronisation necessary for the test harness.132* By keeping it the same in all tests, it is easier133* to read and understand someone else's test, as134* well as insuring that all tests behave correctly135* with the test harness.136* There is a section following this for test-137* classes138******************************************************/139private static boolean theTestPassed = false;140private static boolean testGeneratedInterrupt = false;141private static String failureMessage = "";142143private static Thread mainThread = null;144145private static int sleepTime = 300000;146147// Not sure about what happens if multiple of this test are148// instantiated in the same VM. Being static (and using149// static vars), it aint gonna work. Not worrying about150// it for now.151public static void main( String args[] ) throws InterruptedException152{153mainThread = Thread.currentThread();154try155{156init();157}158catch( TestPassedException e )159{160//The test passed, so just return from main and harness will161// interepret this return as a pass162return;163}164//At this point, neither test pass nor test fail has been165// called -- either would have thrown an exception and ended the166// test, so we know we have multiple threads.167168//Test involves other threads, so sleep and wait for them to169// called pass() or fail()170try171{172Thread.sleep( sleepTime );173//Timed out, so fail the test174throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );175}176catch (InterruptedException e)177{178//The test harness may have interrupted the test. If so, rethrow the exception179// so that the harness gets it and deals with it.180if( ! testGeneratedInterrupt ) throw e;181182//reset flag in case hit this code more than once for some reason (just safety)183testGeneratedInterrupt = false;184185if ( theTestPassed == false )186{187throw new RuntimeException( failureMessage );188}189}190191}//main192193public static synchronized void setTimeoutTo( int seconds )194{195sleepTime = seconds * 1000;196}197198public static synchronized void pass()199{200Sysout.println( "The test passed." );201Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );202//first check if this is executing in main thread203if ( mainThread == Thread.currentThread() )204{205//Still in the main thread, so set the flag just for kicks,206// and throw a test passed exception which will be caught207// and end the test.208theTestPassed = true;209throw new TestPassedException();210}211theTestPassed = true;212testGeneratedInterrupt = true;213mainThread.interrupt();214}//pass()215216public static synchronized void fail()217{218//test writer didn't specify why test failed, so give generic219fail( "it just plain failed! :-)" );220}221222public static synchronized void fail( String whyFailed )223{224Sysout.println( "The test failed: " + whyFailed );225Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );226//check if this called from main thread227if ( mainThread == Thread.currentThread() )228{229//If main thread, fail now 'cause not sleeping230throw new RuntimeException( whyFailed );231}232theTestPassed = false;233testGeneratedInterrupt = true;234failureMessage = whyFailed;235mainThread.interrupt();236}//fail()237238}// class MixingOnDialog239240//This exception is used to exit from any level of call nesting241// when it's determined that the test has passed, and immediately242// end the test.243class TestPassedException extends RuntimeException244{245}246247//*********** End Standard Test Machinery Section **********248249250//************ Begin classes defined for the test ****************251252// if want to make listeners, here is the recommended place for them, then instantiate253// them in init()254255/* Example of a class which may be written as part of a test256class NewClass implements anInterface257{258static int newVar = 0;259260public void eventDispatched(AWTEvent e)261{262//Counting events to see if we get enough263eventCount++;264265if( eventCount == 20 )266{267//got enough events, so pass268269MixingOnDialog.pass();270}271else if( tries == 20 )272{273//tried too many times without getting enough events so fail274275MixingOnDialog.fail();276}277278}// eventDispatched()279280}// NewClass class281282*/283284285//************** End classes defined for the test *******************286287288289290/****************************************************291Standard Test Machinery292DO NOT modify anything below -- it's a standard293chunk of code whose purpose is to make user294interaction uniform, and thereby make it simpler295to read and understand someone else's test.296****************************************************/297298/**299This is part of the standard test machinery.300It creates a dialog (with the instructions), and is the interface301for sending text messages to the user.302To print the instructions, send an array of strings to Sysout.createDialog303WithInstructions method. Put one line of instructions per array entry.304To display a message for the tester to see, simply call Sysout.println305with the string to be displayed.306This mimics System.out.println but works within the test harness as well307as standalone.308*/309310class Sysout311{312private static TestDialog dialog;313314public static void createDialogWithInstructions( String[] instructions )315{316dialog = new TestDialog( new Frame(), "Instructions" );317dialog.printInstructions( instructions );318dialog.setVisible(true);319println( "Any messages for the tester will display here." );320}321322public static void createDialog( )323{324dialog = new TestDialog( new Frame(), "Instructions" );325String[] defInstr = { "Instructions will appear here. ", "" } ;326dialog.printInstructions( defInstr );327dialog.setVisible(true);328println( "Any messages for the tester will display here." );329}330331332public static void printInstructions( String[] instructions )333{334dialog.printInstructions( instructions );335}336337338public static void println( String messageIn )339{340dialog.displayMessage( messageIn );341System.out.println(messageIn);342}343344}// Sysout class345346/**347This is part of the standard test machinery. It provides a place for the348test instructions to be displayed, and a place for interactive messages349to the user to be displayed.350To have the test instructions displayed, see Sysout.351To have a message to the user be displayed, see Sysout.352Do not call anything in this dialog directly.353*/354class TestDialog extends Dialog355{356357TextArea instructionsText;358TextArea messageText;359int maxStringLength = 80;360361//DO NOT call this directly, go through Sysout362public TestDialog( Frame frame, String name )363{364super( frame, name );365int scrollBoth = TextArea.SCROLLBARS_BOTH;366instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );367add( "North", instructionsText );368369messageText = new TextArea( "", 5, maxStringLength, scrollBoth );370add("Center", messageText);371372pack();373374setVisible(true);375}// TestDialog()376377//DO NOT call this directly, go through Sysout378public void printInstructions( String[] instructions )379{380//Clear out any current instructions381instructionsText.setText( "" );382383//Go down array of instruction strings384385String printStr, remainingStr;386for( int i=0; i < instructions.length; i++ )387{388//chop up each into pieces maxSringLength long389remainingStr = instructions[ i ];390while( remainingStr.length() > 0 )391{392//if longer than max then chop off first max chars to print393if( remainingStr.length() >= maxStringLength )394{395//Try to chop on a word boundary396int posOfSpace = remainingStr.397lastIndexOf( ' ', maxStringLength - 1 );398399if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;400401printStr = remainingStr.substring( 0, posOfSpace + 1 );402remainingStr = remainingStr.substring( posOfSpace + 1 );403}404//else just print405else406{407printStr = remainingStr;408remainingStr = "";409}410411instructionsText.append( printStr + "\n" );412413}// while414415}// for416417}//printInstructions()418419//DO NOT call this directly, go through Sysout420public void displayMessage( String messageIn )421{422messageText.append( messageIn + "\n" );423System.out.println(messageIn);424}425426}// TestDialog class427428429430431