Path: blob/master/test/jdk/java/awt/Frame/FrameStateTest/FrameStateTest.java
41153 views
/*1* Copyright (c) 2012, 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/*24test25@bug 415727126@summary Checks that when a Frame is created it honors the state it27was set to. The bug was that if setState(Frame.ICONIFIED) was28called before setVisible(true) the Frame would be shown in NORMAL29state instead of ICONIFIED.30@author JTG East Team: area=awt.Frame31@run applet/manual=yesno FrameStateTest.html32*/3334/**35* FrameStateTest.java36*37* summary: Checks that when setState(Frame.ICONIFIED) is called before38* setVisible(true) the Frame is shown in the proper iconified state.39* The problem was that it did not honor the initial iconic state, but40* instead was shown in the NORMAL state.41*/4243import java.awt.event.*;44import java.awt.*;45import java.lang.*;46import java.applet.Applet;474849public class FrameStateTest extends Applet implements ActionListener, ItemListener{5051Button btnCreate = new Button("Create Frame");52Button btnDispose = new Button("Dispose Frame");53CheckboxGroup cbgState = new CheckboxGroup();54CheckboxGroup cbgResize = new CheckboxGroup();55Checkbox cbIconState = new Checkbox("Frame state ICONIFIED",cbgState,false);56Checkbox cbNormState = new Checkbox("Frame state NORMAL",cbgState,true);57Checkbox cbNonResize = new Checkbox("Frame Nonresizable",cbgResize,false);58Checkbox cbResize = new Checkbox("Frame Resizable",cbgResize,true);59int iState = 0;60boolean bResize = true;61CreateFrame icontst;6263public void init() {64this.setLayout (new BorderLayout ());6566String[] instructions =67{68"Steps to try to reproduce this problem:",69"When this test is run an Applet Viewer window will display. In the",70"Applet Viewer window select the different options for the Frame (i.e.",71"{Normal, Non-resizalbe}, {Normal, Resizable}, {Iconified, Resizable},",72"{Iconified, Non-resizalbe}). After chosing the Frame's state click the",73"Create Frame button. After the Frame (Frame State Test (Window2)) comes",74"up make sure the proper behavior occurred (Frame shown in proper state).",75"Click the Dispose button to close the Frame. Do the above steps for all",76"the different Frame state combinations available. If you observe the",77"proper behavior the test has passed, Press the Pass button. Otherwise",78"the test has failed, Press the Fail button.",79"Note: In Frame State Test (Window2) you can also chose the different",80"buttons to see different Frame behavior. An example of a problem that",81"has been seen, With the Frame nonresizable you can not iconify the Frame."82};83Sysout.createDialogWithInstructions( instructions );8485btnDispose.setEnabled(false);86add(btnCreate, BorderLayout.NORTH);87add(btnDispose, BorderLayout.SOUTH);8889Panel p = new Panel(new GridLayout(0,1));90p.add(cbIconState);91p.add(cbResize);92add(p, BorderLayout.WEST);9394p = new Panel(new GridLayout(0,1));95p.add(cbNormState);96p.add(cbNonResize);97add(p, BorderLayout.EAST);9899// Add Listeners100btnDispose.addActionListener(this);101btnCreate.addActionListener(this);102cbNormState.addItemListener(this);103cbResize.addItemListener(this);104cbIconState.addItemListener(this);105cbNonResize.addItemListener(this);106107resize(600, 200);108109}//End init()110111public void actionPerformed(ActionEvent evt) {112113114if (evt.getSource() == btnCreate) {115btnCreate.setEnabled(false);116btnDispose.setEnabled(true);117icontst = new CreateFrame(iState, bResize);118icontst.show();119} else if (evt.getSource() == btnDispose) {120btnCreate.setEnabled(true);121btnDispose.setEnabled(false);122icontst.dispose();123}124}125126public void itemStateChanged(ItemEvent evt) {127128if (cbNormState.getState()) iState = 0;129if (cbIconState.getState()) iState = 1;130if (cbResize.getState()) bResize = true;131if (cbNonResize.getState()) bResize = false;132133}134135}// class FrameStateTest136137138class CreateFrame extends Frame implements ActionListener , WindowListener {139140static int e=0;141static int u=0;142static int p=0;143static int i=0;144static int v=0;145146Button b1, b2, b3, b4, b5, b6, b7;147boolean resizable = true;148boolean iconic = false;149String name = "Frame State Test";150151CreateFrame (int iFrameState, boolean bFrameResizable) {152153setTitle("Frame State Test (Window 2)");154155if (iFrameState == 1) {156iconic = true;157}158159if (!(bFrameResizable)) {160resizable = false;161}162163System.out.println("CREATING FRAME - Initially "+164((iconic) ? "ICONIFIED" : "NORMAL (NON-ICONIFIED)") + " and " +165((resizable) ? "RESIZABLE" : "NON-RESIZABLE") );166167Sysout.println("CREATING FRAME - Initially "+168((iconic) ? "ICONIFIED" : "NORMAL (NON-ICONIFIED)") + " and " +169((resizable) ? "RESIZABLE" : "NON-RESIZABLE") );170171setLayout(new FlowLayout() );172b1 = new Button("resizable");173add(b1);174b2 = new Button("resize");175add(b2);176b3 = new Button("iconify");177add(b3);178b4 = new Button("iconify and restore");179add(b4);180b5 = new Button("hide and show");181add(b5);182b6 = new Button("hide, iconify and show");183add(b6);184b7 = new Button("hide, iconify, show, and restore");185add(b7);186b1.addActionListener(this);187b2.addActionListener(this);188b3.addActionListener(this);189b4.addActionListener(this);190b5.addActionListener(this);191b6.addActionListener(this);192b7.addActionListener(this);193addWindowListener(this);194195setBounds(100,2,200, 200);196setState(iconic ? Frame.ICONIFIED: Frame.NORMAL);197setResizable(resizable);198pack();199setVisible(true);200201}202203public void actionPerformed ( ActionEvent e )204{205if ( e.getSource() == b2 ) {206Rectangle r = this.getBounds();207r.width += 10;208System.out.println(" - button pressed - setting bounds on Frame to: "+r);209setBounds(r);210validate();211} else if ( e.getSource() == b1 ) {212resizable = !resizable;213System.out.println(" - button pressed - setting Resizable to: "+resizable);214((Frame)(b1.getParent())).setResizable(resizable);215} else if ( e.getSource() == b3 ) {216System.out.println(" - button pressed - setting Iconic: ");217dolog();218((Frame)(b1.getParent())).setState(Frame.ICONIFIED);219dolog();220} else if ( e.getSource() == b4 ) {221System.out.println(" - button pressed - setting Iconic: ");222dolog();223((Frame)(b1.getParent())).setState(Frame.ICONIFIED);224dolog();225try {226Thread.sleep(1000);227} catch (Exception ex) {};228System.out.println(" - now restoring: ");229((Frame)(b1.getParent())).setState(Frame.NORMAL);230dolog();231} else if ( e.getSource() == b5 ) {232System.out.println(" - button pressed - hiding : ");233dolog();234((Frame)(b1.getParent())).setVisible(false);235dolog();236try {237Thread.sleep(1000);238} catch (Exception ex) {};239System.out.println(" - now reshowing: ");240((Frame)(b1.getParent())).setVisible(true);241dolog();242} else if ( e.getSource() == b6 ) {243System.out.println(" - button pressed - hiding : ");244dolog();245((Frame)(b1.getParent())).setVisible(false);246dolog();247try {248Thread.sleep(1000);249} catch (Exception ex) {};250System.out.println(" - setting Iconic: ");251dolog();252((Frame)(b1.getParent())).setState(Frame.ICONIFIED);253try {254Thread.sleep(1000);255} catch (Exception ex) {};256System.out.println(" - now reshowing: ");257((Frame)(b1.getParent())).setVisible(true);258dolog();259} else if ( e.getSource() == b7 ) {260System.out.println(" - button pressed - hiding : ");261dolog();262((Frame)(b1.getParent())).setVisible(false);263dolog();264try {265Thread.sleep(1000);266} catch (Exception ex) {};267System.out.println(" - setting Iconic: ");268dolog();269((Frame)(b1.getParent())).setState(Frame.ICONIFIED);270try {271Thread.sleep(1000);272} catch (Exception ex) {};273System.out.println(" - now reshowing: ");274((Frame)(b1.getParent())).setVisible(true);275dolog();276try {277Thread.sleep(1000);278} catch (Exception ex) {};279System.out.println(" - now restoring: ");280((Frame)(b1.getParent())).setState(Frame.NORMAL);281dolog();282}283}284285public void windowActivated(WindowEvent e) {286System.out.println(name + " Activated");287dolog();288}289public void windowClosed(WindowEvent e) {290System.out.println(name + " Closed");291dolog();292}293public void windowClosing(WindowEvent e) {294((Window)(e.getSource())).dispose();295System.out.println(name + " Closing");296dolog();297}298public void windowDeactivated(WindowEvent e) {299System.out.println(name + " Deactivated");300dolog();301}302public void windowDeiconified(WindowEvent e) {303System.out.println(name + " Deiconified");304dolog();305}306public void windowIconified(WindowEvent e) {307System.out.println(name + " Iconified");308dolog();309}310public void windowOpened(WindowEvent e) {311System.out.println(name + " Opened");312dolog();313}314315public void dolog() {316System.out.println(" getState returns: "+getState());317}318}319320// }// class FrameStateTest321322/****************************************************323Standard Test Machinery324DO NOT modify anything below -- it's a standard325chunk of code whose purpose is to make user326interaction uniform, and thereby make it simpler327to read and understand someone else's test.328****************************************************/329330/**331This is part of the standard test machinery.332It creates a dialog (with the instructions), and is the interface333for sending text messages to the user.334To print the instructions, send an array of strings to Sysout.createDialog335WithInstructions method. Put one line of instructions per array entry.336To display a message for the tester to see, simply call Sysout.println337with the string to be displayed.338This mimics System.out.println but works within the test harness as well339as standalone.340*/341342class Sysout343{344private static TestDialog dialog;345346public static void createDialogWithInstructions( String[] instructions )347{348dialog = new TestDialog( new Frame(), "Instructions" );349dialog.printInstructions( instructions );350dialog.show();351println( "Any messages for the tester will display here." );352}353354public static void createDialog( )355{356dialog = new TestDialog( new Frame(), "Instructions" );357String[] defInstr = { "Instructions will appear here. ", "" } ;358dialog.printInstructions( defInstr );359dialog.show();360println( "Any messages for the tester will display here." );361}362363364public static void printInstructions( String[] instructions )365{366dialog.printInstructions( instructions );367}368369370public static void println( String messageIn )371{372dialog.displayMessage( messageIn );373}374375}// Sysout class376377/**378This is part of the standard test machinery. It provides a place for the379test instructions to be displayed, and a place for interactive messages380to the user to be displayed.381To have the test instructions displayed, see Sysout.382To have a message to the user be displayed, see Sysout.383Do not call anything in this dialog directly.384*/385class TestDialog extends Dialog386{387388TextArea instructionsText;389TextArea messageText;390int maxStringLength = 80;391392//DO NOT call this directly, go through Sysout393public TestDialog( Frame frame, String name )394{395super( frame, name );396int scrollBoth = TextArea.SCROLLBARS_BOTH;397int scrollNone = TextArea.SCROLLBARS_NONE;398instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );399add( "North", instructionsText );400401messageText = new TextArea( "", 10, maxStringLength, scrollBoth );402add("South", messageText);403404pack();405406show();407}// TestDialog()408409//DO NOT call this directly, go through Sysout410public void printInstructions( String[] instructions )411{412//Clear out any current instructions413instructionsText.setText( "" );414415//Go down array of instruction strings416417String printStr, remainingStr;418for( int i=0; i < instructions.length; i++ )419{420//chop up each into pieces maxSringLength long421remainingStr = instructions[ i ];422while( remainingStr.length() > 0 )423{424//if longer than max then chop off first max chars to print425if( remainingStr.length() >= maxStringLength )426{427//Try to chop on a word boundary428int posOfSpace = remainingStr.429lastIndexOf( ' ', maxStringLength - 1 );430431if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;432433printStr = remainingStr.substring( 0, posOfSpace + 1 );434remainingStr = remainingStr.substring( posOfSpace + 1 );435}436//else just print437else438{439printStr = remainingStr;440remainingStr = "";441}442443instructionsText.append( printStr + "\n" );444445}// while446447}// for448449}//printInstructions()450451//DO NOT call this directly, go through Sysout452public void displayMessage( String messageIn )453{454messageText.append( messageIn + "\n" );455}456457458}// TestDialog class459460461