Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Component/NoUpdateUponShow/NoUpdateUponShow.java
47311 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@test25@bug 677425826@summary api/java_awt/Component/index.html#PaintUpdate fails randomly27@author dmitry.cherepanov@...: area=awt.painting28@run main NoUpdateUponShow29*/3031/**32* NoUpdateUponShow.java33*34* summary: System-level painting operations shouldn't make call to update()35*/3637import java.awt.*;3839public class NoUpdateUponShow40{4142static volatile boolean wasUpdate = false;4344private static void init()45{46//*** Create instructions for the user here ***4748String[] instructions =49{50"This is an AUTOMATIC test, simply wait until it is done.",51"The result (passed or failed) will be shown in the",52"message window below."53};54Sysout.createDialog( );55Sysout.printInstructions( instructions );565758// Create the frame and the button59Frame f = new Frame();60f.setBounds(100, 100, 200, 200);61f.setLayout(new FlowLayout());62f.add(new Button() {63@Override64public void update(Graphics g) {65wasUpdate = true;66super.update(g);67}68});69f.setVisible(true);7071try {72Robot robot = new Robot();73robot.waitForIdle();74}catch(Exception ex) {75ex.printStackTrace();76throw new RuntimeException("Unexpected failure");77}7879if (wasUpdate) {80fail(" Unexpected update. ");81} else {82pass();83}84}//End init()8586/*****************************************************87* Standard Test Machinery Section88* DO NOT modify anything in this section -- it's a89* standard chunk of code which has all of the90* synchronisation necessary for the test harness.91* By keeping it the same in all tests, it is easier92* to read and understand someone else's test, as93* well as insuring that all tests behave correctly94* with the test harness.95* There is a section following this for test-96* classes97******************************************************/98private static boolean theTestPassed = false;99private static boolean testGeneratedInterrupt = false;100private static String failureMessage = "";101102private static Thread mainThread = null;103104private static int sleepTime = 300000;105106// Not sure about what happens if multiple of this test are107// instantiated in the same VM. Being static (and using108// static vars), it aint gonna work. Not worrying about109// it for now.110public static void main( String args[] ) throws InterruptedException111{112mainThread = Thread.currentThread();113try114{115init();116}117catch( TestPassedException e )118{119//The test passed, so just return from main and harness will120// interepret this return as a pass121return;122}123//At this point, neither test pass nor test fail has been124// called -- either would have thrown an exception and ended the125// test, so we know we have multiple threads.126127//Test involves other threads, so sleep and wait for them to128// called pass() or fail()129try130{131Thread.sleep( sleepTime );132//Timed out, so fail the test133throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );134}135catch (InterruptedException e)136{137//The test harness may have interrupted the test. If so, rethrow the exception138// so that the harness gets it and deals with it.139if( ! testGeneratedInterrupt ) throw e;140141//reset flag in case hit this code more than once for some reason (just safety)142testGeneratedInterrupt = false;143144if ( theTestPassed == false )145{146throw new RuntimeException( failureMessage );147}148}149150}//main151152public static synchronized void setTimeoutTo( int seconds )153{154sleepTime = seconds * 1000;155}156157public static synchronized void pass()158{159Sysout.println( "The test passed." );160Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );161//first check if this is executing in main thread162if ( mainThread == Thread.currentThread() )163{164//Still in the main thread, so set the flag just for kicks,165// and throw a test passed exception which will be caught166// and end the test.167theTestPassed = true;168throw new TestPassedException();169}170theTestPassed = true;171testGeneratedInterrupt = true;172mainThread.interrupt();173}//pass()174175public static synchronized void fail()176{177//test writer didn't specify why test failed, so give generic178fail( "it just plain failed! :-)" );179}180181public static synchronized void fail( String whyFailed )182{183Sysout.println( "The test failed: " + whyFailed );184Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );185//check if this called from main thread186if ( mainThread == Thread.currentThread() )187{188//If main thread, fail now 'cause not sleeping189throw new RuntimeException( whyFailed );190}191theTestPassed = false;192testGeneratedInterrupt = true;193failureMessage = whyFailed;194mainThread.interrupt();195}//fail()196197}// class ValidBounds198199//This exception is used to exit from any level of call nesting200// when it's determined that the test has passed, and immediately201// end the test.202class TestPassedException extends RuntimeException203{204}205206//*********** End Standard Test Machinery Section **********207208209//************ Begin classes defined for the test ****************210211// if want to make listeners, here is the recommended place for them, then instantiate212// them in init()213214/* Example of a class which may be written as part of a test215class NewClass implements anInterface216{217static int newVar = 0;218219public void eventDispatched(AWTEvent e)220{221//Counting events to see if we get enough222eventCount++;223224if( eventCount == 20 )225{226//got enough events, so pass227228ValidBounds.pass();229}230else if( tries == 20 )231{232//tried too many times without getting enough events so fail233234ValidBounds.fail();235}236237}// eventDispatched()238239}// NewClass class240241*/242243244//************** End classes defined for the test *******************245246247248249/****************************************************250Standard Test Machinery251DO NOT modify anything below -- it's a standard252chunk of code whose purpose is to make user253interaction uniform, and thereby make it simpler254to read and understand someone else's test.255****************************************************/256257/**258This is part of the standard test machinery.259It creates a dialog (with the instructions), and is the interface260for sending text messages to the user.261To print the instructions, send an array of strings to Sysout.createDialog262WithInstructions method. Put one line of instructions per array entry.263To display a message for the tester to see, simply call Sysout.println264with the string to be displayed.265This mimics System.out.println but works within the test harness as well266as standalone.267*/268269class Sysout270{271private static TestDialog dialog;272273public static void createDialogWithInstructions( String[] instructions )274{275dialog = new TestDialog( new Frame(), "Instructions" );276dialog.printInstructions( instructions );277dialog.setVisible(true);278println( "Any messages for the tester will display here." );279}280281public static void createDialog( )282{283dialog = new TestDialog( new Frame(), "Instructions" );284String[] defInstr = { "Instructions will appear here. ", "" } ;285dialog.printInstructions( defInstr );286dialog.setVisible(true);287println( "Any messages for the tester will display here." );288}289290291public static void printInstructions( String[] instructions )292{293dialog.printInstructions( instructions );294}295296297public static void println( String messageIn )298{299dialog.displayMessage( messageIn );300System.out.println(messageIn);301}302303}// Sysout class304305/**306This is part of the standard test machinery. It provides a place for the307test instructions to be displayed, and a place for interactive messages308to the user to be displayed.309To have the test instructions displayed, see Sysout.310To have a message to the user be displayed, see Sysout.311Do not call anything in this dialog directly.312*/313class TestDialog extends Dialog314{315316TextArea instructionsText;317TextArea messageText;318int maxStringLength = 80;319320//DO NOT call this directly, go through Sysout321public TestDialog( Frame frame, String name )322{323super( frame, name );324int scrollBoth = TextArea.SCROLLBARS_BOTH;325instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );326add( "North", instructionsText );327328messageText = new TextArea( "", 5, maxStringLength, scrollBoth );329add("Center", messageText);330331pack();332333setVisible(true);334}// TestDialog()335336//DO NOT call this directly, go through Sysout337public void printInstructions( String[] instructions )338{339//Clear out any current instructions340instructionsText.setText( "" );341342//Go down array of instruction strings343344String printStr, remainingStr;345for( int i=0; i < instructions.length; i++ )346{347//chop up each into pieces maxSringLength long348remainingStr = instructions[ i ];349while( remainingStr.length() > 0 )350{351//if longer than max then chop off first max chars to print352if( remainingStr.length() >= maxStringLength )353{354//Try to chop on a word boundary355int posOfSpace = remainingStr.356lastIndexOf( ' ', maxStringLength - 1 );357358if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;359360printStr = remainingStr.substring( 0, posOfSpace + 1 );361remainingStr = remainingStr.substring( posOfSpace + 1 );362}363//else just print364else365{366printStr = remainingStr;367remainingStr = "";368}369370instructionsText.append( printStr + "\n" );371372}// while373374}// for375376}//printInstructions()377378//DO NOT call this directly, go through Sysout379public void displayMessage( String messageIn )380{381messageText.append( messageIn + "\n" );382System.out.println(messageIn);383}384385}// TestDialog class386387388