Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Frame/MaximizedToIconified/MaximizedToIconified.java
38828 views
/*1* Copyright (c) 2007, 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 497749126@summary State changes should always be reported as events27@author anthony.petrov@...: area=awt.toplevel28@library ../../regtesthelpers29@build Util30@run main MaximizedToIconified31*/3233/**34* MaximizedToIconified.java35*36* summary: Invoking setExtendedState(ICONIFIED) on a maximized37* frame should not combine the maximized and iconified38* states in the newState of the state change event.39*/4041import java.awt.*;42import java.awt.event.*;43import java.util.*;44import test.java.awt.regtesthelpers.Util;454647public class MaximizedToIconified48{49static volatile int lastFrameState = Frame.NORMAL;50static volatile boolean failed = false;51static volatile Toolkit myKit;5253private static void checkState(Frame f, int state) {54f.setExtendedState(state);55Util.waitForIdle(null);5657System.out.println("state = " + state + "; getExtendedState() = " + f.getExtendedState());5859if (failed) {60MaximizedToIconified.fail("getOldState() != previous getNewState() in WINDOW_STATE_CHANGED event.");61}62if (lastFrameState != f.getExtendedState()) {63MaximizedToIconified.fail("getExtendedState() != last getNewState() in WINDOW_STATE_CHANGED event.");64}65if (f.getExtendedState() != state) {66MaximizedToIconified.fail("getExtendedState() != " + state + " as expected.");67}68// Plain return means the check passed69}7071private static void examineStates(Frame f_arg, int states[]) {72Frame f = f_arg;7374if (f == null) {75f = new Frame("test");76f.setSize(200, 200);77f.setVisible(true);78}7980Util.waitForIdle(null);8182f.addWindowStateListener(new WindowStateListener() {83public void windowStateChanged(WindowEvent e) {84System.out.println("last = " + lastFrameState + "; getOldState() = " + e.getOldState() + "; getNewState() = " + e.getNewState());85if (e.getOldState() == lastFrameState) {86lastFrameState = e.getNewState();87} else {88System.out.println("Wrong getOldState(): expected = " + lastFrameState + "; received = " + e.getOldState());89failed = true;90}91}92});9394for (int state: states) {95if (myKit.isFrameStateSupported(state)) {96checkState(f, state);97} else {98System.out.println("Frame state = " + state + " is NOT supported by the native system. The state is skipped.");99}100}101102if (f_arg == null) {103f.dispose();104}105}106107private static void init()108{109String[] instructions =110{111"This is an AUTOMATIC test, simply wait until it is done.",112"The result (passed or failed) will be shown in the",113"message window below."114};115Sysout.createDialog( );116Sysout.printInstructions( instructions );117118myKit = Toolkit.getDefaultToolkit();119120// NOTE! Compound states (like MAXIMIZED_BOTH | ICONIFIED) CANNOT be used,121// because Toolkit.isFrameStateSupported() method reports these states122// as not supported. And such states will simply be skipped.123examineStates(null, new int[] {Frame.MAXIMIZED_BOTH, Frame.ICONIFIED, Frame.NORMAL});124examineStates(null, new int[] {Frame.ICONIFIED, Frame.MAXIMIZED_BOTH, Frame.NORMAL});125126127MaximizedToIconified.pass();128129}//End init()130131132133/*****************************************************134* Standard Test Machinery Section135* DO NOT modify anything in this section -- it's a136* standard chunk of code which has all of the137* synchronisation necessary for the test harness.138* By keeping it the same in all tests, it is easier139* to read and understand someone else's test, as140* well as insuring that all tests behave correctly141* with the test harness.142* There is a section following this for test-143* classes144******************************************************/145private static boolean theTestPassed = false;146private static boolean testGeneratedInterrupt = false;147private static String failureMessage = "";148149private static Thread mainThread = null;150151private static int sleepTime = 300000;152153// Not sure about what happens if multiple of this test are154// instantiated in the same VM. Being static (and using155// static vars), it aint gonna work. Not worrying about156// it for now.157public static void main( String args[] ) throws InterruptedException158{159mainThread = Thread.currentThread();160try161{162init();163}164catch( TestPassedException e )165{166//The test passed, so just return from main and harness will167// interepret this return as a pass168return;169}170//At this point, neither test pass nor test fail has been171// called -- either would have thrown an exception and ended the172// test, so we know we have multiple threads.173174//Test involves other threads, so sleep and wait for them to175// called pass() or fail()176try177{178Thread.sleep( sleepTime );179//Timed out, so fail the test180throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );181}182catch (InterruptedException e)183{184//The test harness may have interrupted the test. If so, rethrow the exception185// so that the harness gets it and deals with it.186if( ! testGeneratedInterrupt ) throw e;187188//reset flag in case hit this code more than once for some reason (just safety)189testGeneratedInterrupt = false;190191if ( theTestPassed == false )192{193throw new RuntimeException( failureMessage );194}195}196197}//main198199public static synchronized void setTimeoutTo( int seconds )200{201sleepTime = seconds * 1000;202}203204public static synchronized void pass()205{206Sysout.println( "The test passed." );207Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );208//first check if this is executing in main thread209if ( mainThread == Thread.currentThread() )210{211//Still in the main thread, so set the flag just for kicks,212// and throw a test passed exception which will be caught213// and end the test.214theTestPassed = true;215throw new TestPassedException();216}217theTestPassed = true;218testGeneratedInterrupt = true;219mainThread.interrupt();220}//pass()221222public static synchronized void fail()223{224//test writer didn't specify why test failed, so give generic225fail( "it just plain failed! :-)" );226}227228public static synchronized void fail( String whyFailed )229{230Sysout.println( "The test failed: " + whyFailed );231Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );232//check if this called from main thread233if ( mainThread == Thread.currentThread() )234{235//If main thread, fail now 'cause not sleeping236throw new RuntimeException( whyFailed );237}238theTestPassed = false;239testGeneratedInterrupt = true;240failureMessage = whyFailed;241mainThread.interrupt();242}//fail()243244}// class MaximizedToIconified245246//This exception is used to exit from any level of call nesting247// when it's determined that the test has passed, and immediately248// end the test.249class TestPassedException extends RuntimeException250{251}252253//*********** End Standard Test Machinery Section **********254255256//************ Begin classes defined for the test ****************257258// if want to make listeners, here is the recommended place for them, then instantiate259// them in init()260261/* Example of a class which may be written as part of a test262class NewClass implements anInterface263{264static int newVar = 0;265266public void eventDispatched(AWTEvent e)267{268//Counting events to see if we get enough269eventCount++;270271if( eventCount == 20 )272{273//got enough events, so pass274275MaximizedToIconified.pass();276}277else if( tries == 20 )278{279//tried too many times without getting enough events so fail280281MaximizedToIconified.fail();282}283284}// eventDispatched()285286}// NewClass class287288*/289290291//************** End classes defined for the test *******************292293294295296/****************************************************297Standard Test Machinery298DO NOT modify anything below -- it's a standard299chunk of code whose purpose is to make user300interaction uniform, and thereby make it simpler301to read and understand someone else's test.302****************************************************/303304/**305This is part of the standard test machinery.306It creates a dialog (with the instructions), and is the interface307for sending text messages to the user.308To print the instructions, send an array of strings to Sysout.createDialog309WithInstructions method. Put one line of instructions per array entry.310To display a message for the tester to see, simply call Sysout.println311with the string to be displayed.312This mimics System.out.println but works within the test harness as well313as standalone.314*/315316class Sysout317{318private static TestDialog dialog;319320public static void createDialogWithInstructions( String[] instructions )321{322dialog = new TestDialog( new Frame(), "Instructions" );323dialog.printInstructions( instructions );324dialog.setVisible(true);325println( "Any messages for the tester will display here." );326}327328public static void createDialog( )329{330dialog = new TestDialog( new Frame(), "Instructions" );331String[] defInstr = { "Instructions will appear here. ", "" } ;332dialog.printInstructions( defInstr );333dialog.setVisible(true);334println( "Any messages for the tester will display here." );335}336337338public static void printInstructions( String[] instructions )339{340dialog.printInstructions( instructions );341}342343344public static void println( String messageIn )345{346dialog.displayMessage( messageIn );347System.out.println(messageIn);348}349350}// Sysout class351352/**353This is part of the standard test machinery. It provides a place for the354test instructions to be displayed, and a place for interactive messages355to the user to be displayed.356To have the test instructions displayed, see Sysout.357To have a message to the user be displayed, see Sysout.358Do not call anything in this dialog directly.359*/360class TestDialog extends Dialog361{362363TextArea instructionsText;364TextArea messageText;365int maxStringLength = 80;366367//DO NOT call this directly, go through Sysout368public TestDialog( Frame frame, String name )369{370super( frame, name );371int scrollBoth = TextArea.SCROLLBARS_BOTH;372instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );373add( "North", instructionsText );374375messageText = new TextArea( "", 5, maxStringLength, scrollBoth );376add("Center", messageText);377378pack();379380setVisible(true);381}// TestDialog()382383//DO NOT call this directly, go through Sysout384public void printInstructions( String[] instructions )385{386//Clear out any current instructions387instructionsText.setText( "" );388389//Go down array of instruction strings390391String printStr, remainingStr;392for( int i=0; i < instructions.length; i++ )393{394//chop up each into pieces maxSringLength long395remainingStr = instructions[ i ];396while( remainingStr.length() > 0 )397{398//if longer than max then chop off first max chars to print399if( remainingStr.length() >= maxStringLength )400{401//Try to chop on a word boundary402int posOfSpace = remainingStr.403lastIndexOf( ' ', maxStringLength - 1 );404405if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;406407printStr = remainingStr.substring( 0, posOfSpace + 1 );408remainingStr = remainingStr.substring( posOfSpace + 1 );409}410//else just print411else412{413printStr = remainingStr;414remainingStr = "";415}416417instructionsText.append( printStr + "\n" );418419}// while420421}// for422423}//printInstructions()424425//DO NOT call this directly, go through Sysout426public void displayMessage( String messageIn )427{428messageText.append( messageIn + "\n" );429System.out.println(messageIn);430}431432}// TestDialog class433434435