FrameStateTest.java 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
  test
  @bug 4157271
  @summary Checks that when a Frame is created it honors the state it
        was set to. The bug was that if setState(Frame.ICONIFIED) was
        called before setVisible(true) the Frame would be shown in NORMAL
        state instead of ICONIFIED.
  @author JTG East Team: area=awt.Frame
  @run applet/manual=yesno FrameStateTest.html
*/

/**
 * FrameStateTest.java
 *
 * summary: Checks that when setState(Frame.ICONIFIED) is called before
 *      setVisible(true) the Frame is shown in the proper iconified state.
 *      The problem was that it did not honor the initial iconic state, but
 *      instead was shown in the NORMAL state.
 */

import java.awt.event.*;
import java.awt.*;
import java.lang.*;
import java.applet.Applet;


public class FrameStateTest extends Applet implements ActionListener, ItemListener{

   Button btnCreate = new Button("Create Frame");
   Button btnDispose = new Button("Dispose Frame");
   CheckboxGroup cbgState = new CheckboxGroup();
   CheckboxGroup cbgResize = new CheckboxGroup();
   Checkbox cbIconState = new Checkbox("Frame state ICONIFIED",cbgState,false);
   Checkbox cbNormState = new Checkbox("Frame state NORMAL",cbgState,true);
   Checkbox cbNonResize = new Checkbox("Frame Nonresizable",cbgResize,false);
   Checkbox cbResize = new Checkbox("Frame Resizable",cbgResize,true);
   int iState = 0;
   boolean bResize = true;
   CreateFrame icontst;

   public void init() {
      this.setLayout (new BorderLayout ());

      String[] instructions =
       {
        "Steps to try to reproduce this problem:",
        "When this test is run an Applet Viewer window will display. In the",
        "Applet Viewer window select the different options for the Frame (i.e.",
        "{Normal, Non-resizalbe}, {Normal, Resizable}, {Iconified, Resizable},",
        "{Iconified, Non-resizalbe}). After chosing the Frame's state click the",
        "Create Frame button. After the Frame (Frame State Test (Window2)) comes",
        "up make sure the proper behavior occurred (Frame shown in proper state).",
        "Click the Dispose button to close the Frame. Do the above steps for all",
        "the different Frame state combinations available. If you observe the",
        "proper behavior the test has passed, Press the Pass button. Otherwise",
        "the test has failed, Press the Fail button.",
        "Note: In Frame State Test (Window2) you can also chose the different",
        "buttons to see different Frame behavior. An example of a problem that",
        "has been seen, With the Frame nonresizable you can not iconify the Frame."
       };
      Sysout.createDialogWithInstructions( instructions );

      btnDispose.setEnabled(false);
      add(btnCreate, BorderLayout.NORTH);
      add(btnDispose, BorderLayout.SOUTH);

      Panel p = new Panel(new GridLayout(0,1));
      p.add(cbIconState);
      p.add(cbResize);
      add(p, BorderLayout.WEST);

      p = new Panel(new GridLayout(0,1));
      p.add(cbNormState);
      p.add(cbNonResize);
      add(p, BorderLayout.EAST);

      // Add Listeners
      btnDispose.addActionListener(this);
      btnCreate.addActionListener(this);
      cbNormState.addItemListener(this);
      cbResize.addItemListener(this);
      cbIconState.addItemListener(this);
      cbNonResize.addItemListener(this);

      resize(600, 200);

   }//End  init()

   public void actionPerformed(ActionEvent evt) {


        if (evt.getSource() == btnCreate) {
            btnCreate.setEnabled(false);
            btnDispose.setEnabled(true);
            icontst = new CreateFrame(iState, bResize);
            icontst.show();
        } else if (evt.getSource() == btnDispose) {
            btnCreate.setEnabled(true);
            btnDispose.setEnabled(false);
            icontst.dispose();
        }
    }

    public void itemStateChanged(ItemEvent evt) {

        if (cbNormState.getState()) iState = 0;
        if (cbIconState.getState()) iState = 1;
        if (cbResize.getState()) bResize = true;
        if (cbNonResize.getState()) bResize = false;

    }

}// class FrameStateTest


class CreateFrame extends Frame implements ActionListener , WindowListener {

  static int e=0;
  static int u=0;
  static int p=0;
  static int i=0;
  static int v=0;

  Button b1, b2, b3, b4, b5, b6, b7;
  boolean resizable = true;
  boolean iconic = false;
  String name = "Frame State Test";

  CreateFrame (int iFrameState, boolean bFrameResizable) {

    setTitle("Frame State Test (Window 2)");

    if (iFrameState == 1) {
        iconic = true;
    }

    if (!(bFrameResizable)) {
        resizable = false;
    }

    System.out.println("CREATING FRAME - Initially "+
        ((iconic) ? "ICONIFIED" : "NORMAL (NON-ICONIFIED)") + " and " +
        ((resizable) ? "RESIZABLE" : "NON-RESIZABLE") );

    Sysout.println("CREATING FRAME - Initially "+
        ((iconic) ? "ICONIFIED" : "NORMAL (NON-ICONIFIED)") + " and " +
        ((resizable) ? "RESIZABLE" : "NON-RESIZABLE") );

    setLayout(new FlowLayout() );
    b1 = new Button("resizable");
    add(b1);
    b2 = new Button("resize");
    add(b2);
    b3 = new Button("iconify");
    add(b3);
    b4 = new Button("iconify and restore");
    add(b4);
    b5 = new Button("hide and show");
    add(b5);
    b6 = new Button("hide, iconify and show");
    add(b6);
    b7 = new Button("hide, iconify, show, and restore");
    add(b7);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    addWindowListener(this);

    setBounds(100,2,200, 200);
    setState(iconic ? Frame.ICONIFIED: Frame.NORMAL);
    setResizable(resizable);
    pack();
    setVisible(true);

  }

  public void actionPerformed ( ActionEvent e )
  {
    if ( e.getSource() == b2 ) {
        Rectangle r = this.getBounds();
        r.width += 10;
        System.out.println(" - button pressed - setting bounds on Frame to: "+r);
        setBounds(r);
        validate();
    } else if ( e.getSource() == b1 ) {
        resizable = !resizable;
        System.out.println(" - button pressed - setting Resizable to: "+resizable);
        ((Frame)(b1.getParent())).setResizable(resizable);
    } else if ( e.getSource() == b3 ) {
        System.out.println(" - button pressed - setting Iconic: ");
        dolog();
        ((Frame)(b1.getParent())).setState(Frame.ICONIFIED);
        dolog();
    } else if ( e.getSource() == b4 ) {
        System.out.println(" - button pressed - setting Iconic: ");
        dolog();
        ((Frame)(b1.getParent())).setState(Frame.ICONIFIED);
        dolog();
        try {
                Thread.sleep(1000);
        } catch (Exception ex) {};
        System.out.println(" - now restoring: ");
        ((Frame)(b1.getParent())).setState(Frame.NORMAL);
        dolog();
    } else if ( e.getSource() == b5 ) {
        System.out.println(" - button pressed - hiding : ");
        dolog();
        ((Frame)(b1.getParent())).setVisible(false);
        dolog();
        try {
                Thread.sleep(1000);
        } catch (Exception ex) {};
        System.out.println(" - now reshowing: ");
        ((Frame)(b1.getParent())).setVisible(true);
        dolog();
    } else if ( e.getSource() == b6 ) {
        System.out.println(" - button pressed - hiding : ");
        dolog();
        ((Frame)(b1.getParent())).setVisible(false);
        dolog();
        try {
                Thread.sleep(1000);
        } catch (Exception ex) {};
        System.out.println(" - setting Iconic: ");
        dolog();
        ((Frame)(b1.getParent())).setState(Frame.ICONIFIED);
        try {
                Thread.sleep(1000);
        } catch (Exception ex) {};
        System.out.println(" - now reshowing: ");
        ((Frame)(b1.getParent())).setVisible(true);
        dolog();
    } else if ( e.getSource() == b7 ) {
        System.out.println(" - button pressed - hiding : ");
        dolog();
        ((Frame)(b1.getParent())).setVisible(false);
        dolog();
        try {
                Thread.sleep(1000);
        } catch (Exception ex) {};
        System.out.println(" - setting Iconic: ");
        dolog();
        ((Frame)(b1.getParent())).setState(Frame.ICONIFIED);
        try {
                Thread.sleep(1000);
        } catch (Exception ex) {};
        System.out.println(" - now reshowing: ");
        ((Frame)(b1.getParent())).setVisible(true);
        dolog();
        try {
                Thread.sleep(1000);
        } catch (Exception ex) {};
        System.out.println(" - now restoring: ");
        ((Frame)(b1.getParent())).setState(Frame.NORMAL);
        dolog();
    }
  }

    public void windowActivated(WindowEvent e) {
        System.out.println(name + " Activated");
        dolog();
    }
    public void windowClosed(WindowEvent e) {
        System.out.println(name + " Closed");
        dolog();
    }
    public void windowClosing(WindowEvent e) {
        ((Window)(e.getSource())).dispose();
        System.out.println(name + " Closing");
        dolog();
    }
    public void windowDeactivated(WindowEvent e) {
        System.out.println(name + " Deactivated");
        dolog();
    }
    public void windowDeiconified(WindowEvent e) {
        System.out.println(name + " Deiconified");
        dolog();
    }
    public void windowIconified(WindowEvent e) {
        System.out.println(name + " Iconified");
        dolog();
    }
    public void windowOpened(WindowEvent e) {
        System.out.println(name + " Opened");
        dolog();
    }

    public void dolog() {
        System.out.println(" getState returns: "+getState());
    }
}

// }// class FrameStateTest

/****************************************************
 Standard Test Machinery
 DO NOT modify anything below -- it's a standard
  chunk of code whose purpose is to make user
  interaction uniform, and thereby make it simpler
  to read and understand someone else's test.
 ****************************************************/

/**
 This is part of the standard test machinery.
 It creates a dialog (with the instructions), and is the interface
  for sending text messages to the user.
 To print the instructions, send an array of strings to Sysout.createDialog
  WithInstructions method.  Put one line of instructions per array entry.
 To display a message for the tester to see, simply call Sysout.println
  with the string to be displayed.
 This mimics System.out.println but works within the test harness as well
  as standalone.
 */

class Sysout
 {
   private static TestDialog dialog;

   public static void createDialogWithInstructions( String[] instructions )
    {
      dialog = new TestDialog( new Frame(), "Instructions" );
      dialog.printInstructions( instructions );
      dialog.show();
      println( "Any messages for the tester will display here." );
    }

   public static void createDialog( )
    {
      dialog = new TestDialog( new Frame(), "Instructions" );
      String[] defInstr = { "Instructions will appear here. ", "" } ;
      dialog.printInstructions( defInstr );
      dialog.show();
      println( "Any messages for the tester will display here." );
    }


   public static void printInstructions( String[] instructions )
    {
      dialog.printInstructions( instructions );
    }


   public static void println( String messageIn )
    {
      dialog.displayMessage( messageIn );
    }

 }// Sysout  class

/**
  This is part of the standard test machinery.  It provides a place for the
   test instructions to be displayed, and a place for interactive messages
   to the user to be displayed.
  To have the test instructions displayed, see Sysout.
  To have a message to the user be displayed, see Sysout.
  Do not call anything in this dialog directly.
  */
class TestDialog extends Dialog
 {

   TextArea instructionsText;
   TextArea messageText;
   int maxStringLength = 80;

   //DO NOT call this directly, go through Sysout
   public TestDialog( Frame frame, String name )
    {
      super( frame, name );
      int scrollBoth = TextArea.SCROLLBARS_BOTH;
      int scrollNone = TextArea.SCROLLBARS_NONE;
      instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
      add( "North", instructionsText );

      messageText = new TextArea( "", 10, maxStringLength, scrollBoth );
      add("South", messageText);

      pack();

      show();
    }// TestDialog()

   //DO NOT call this directly, go through Sysout
   public void printInstructions( String[] instructions )
    {
      //Clear out any current instructions
      instructionsText.setText( "" );

      //Go down array of instruction strings

      String printStr, remainingStr;
      for( int i=0; i < instructions.length; i++ )
       {
         //chop up each into pieces maxSringLength long
         remainingStr = instructions[ i ];
         while( remainingStr.length() > 0 )
          {
            //if longer than max then chop off first max chars to print
            if( remainingStr.length() >= maxStringLength )
             {
               //Try to chop on a word boundary
               int posOfSpace = remainingStr.
                  lastIndexOf( ' ', maxStringLength - 1 );

               if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;

               printStr = remainingStr.substring( 0, posOfSpace + 1 );
               remainingStr = remainingStr.substring( posOfSpace + 1 );
             }
            //else just print
            else
             {
               printStr = remainingStr;
               remainingStr = "";
             }

            instructionsText.append( printStr + "\n" );

          }// while

       }// for

    }//printInstructions()

   //DO NOT call this directly, go through Sysout
   public void displayMessage( String messageIn )
    {
      messageText.append( messageIn + "\n" );
    }


 }// TestDialog  class