Path: blob/master/test/jdk/java/awt/Component/NoUpdateUponShow/NoUpdateUponShow.java
41152 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@test25@key headful26@bug 677425827@summary api/java_awt/Component/index.html#PaintUpdate fails randomly28@author dmitry.cherepanov@...: area=awt.painting29@run main NoUpdateUponShow30*/3132/**33* NoUpdateUponShow.java34*35* summary: System-level painting operations shouldn't make call to update()36*/3738import java.awt.*;3940public class NoUpdateUponShow41{4243static volatile boolean wasUpdate = false;4445private static void init()46{47// Create the frame and the button48Frame f = new Frame();49f.setBounds(100, 100, 200, 200);50f.setLayout(new FlowLayout());51f.add(new Button() {52@Override53public void update(Graphics g) {54wasUpdate = true;55super.update(g);56}57});58f.setVisible(true);5960try {61Robot robot = new Robot();62robot.waitForIdle();63}catch(Exception ex) {64ex.printStackTrace();65throw new RuntimeException("Unexpected failure");66}6768if (wasUpdate) {69fail(" Unexpected update. ");70} else {71pass();72}73}//End init()7475/*****************************************************76* Standard Test Machinery Section77* DO NOT modify anything in this section -- it's a78* standard chunk of code which has all of the79* synchronisation necessary for the test harness.80* By keeping it the same in all tests, it is easier81* to read and understand someone else's test, as82* well as insuring that all tests behave correctly83* with the test harness.84* There is a section following this for test-85* classes86******************************************************/87private static boolean theTestPassed = false;88private static boolean testGeneratedInterrupt = false;89private static String failureMessage = "";9091private static Thread mainThread = null;9293private static int sleepTime = 300000;9495// Not sure about what happens if multiple of this test are96// instantiated in the same VM. Being static (and using97// static vars), it aint gonna work. Not worrying about98// it for now.99public static void main( String args[] ) throws InterruptedException100{101mainThread = Thread.currentThread();102try103{104init();105}106catch( TestPassedException e )107{108//The test passed, so just return from main and harness will109// interepret this return as a pass110return;111}112//At this point, neither test pass nor test fail has been113// called -- either would have thrown an exception and ended the114// test, so we know we have multiple threads.115116//Test involves other threads, so sleep and wait for them to117// called pass() or fail()118try119{120Thread.sleep( sleepTime );121//Timed out, so fail the test122throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );123}124catch (InterruptedException e)125{126//The test harness may have interrupted the test. If so, rethrow the exception127// so that the harness gets it and deals with it.128if( ! testGeneratedInterrupt ) throw e;129130//reset flag in case hit this code more than once for some reason (just safety)131testGeneratedInterrupt = false;132133if ( theTestPassed == false )134{135throw new RuntimeException( failureMessage );136}137}138139}//main140141public static synchronized void setTimeoutTo( int seconds )142{143sleepTime = seconds * 1000;144}145146public static synchronized void pass()147{148System.out.println( "The test passed." );149System.out.println( "The test is over, hit Ctl-C to stop Java VM" );150//first check if this is executing in main thread151if ( mainThread == Thread.currentThread() )152{153//Still in the main thread, so set the flag just for kicks,154// and throw a test passed exception which will be caught155// and end the test.156theTestPassed = true;157throw new TestPassedException();158}159theTestPassed = true;160testGeneratedInterrupt = true;161mainThread.interrupt();162}//pass()163164public static synchronized void fail()165{166//test writer didn't specify why test failed, so give generic167fail( "it just plain failed! :-)" );168}169170public static synchronized void fail( String whyFailed )171{172System.out.println( "The test failed: " + whyFailed );173System.out.println( "The test is over, hit Ctl-C to stop Java VM" );174//check if this called from main thread175if ( mainThread == Thread.currentThread() )176{177//If main thread, fail now 'cause not sleeping178throw new RuntimeException( whyFailed );179}180theTestPassed = false;181testGeneratedInterrupt = true;182failureMessage = whyFailed;183mainThread.interrupt();184}//fail()185186}// class ValidBounds187188//This exception is used to exit from any level of call nesting189// when it's determined that the test has passed, and immediately190// end the test.191class TestPassedException extends RuntimeException192{193}194195196