1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  @test
  26  @summary Simple test of various awt controls
  27  @summary com.apple.junit.java.awt;
  28  @library ../../regtesthelpers
  29  @build VisibilityValidator
  30  @run main MixedItemsTest01
  31  */
  32 
  33 // classes necessary for this test
  34 
  35 import junit.framework.*;
  36 
  37 import java.awt.*;
  38 
  39 import test.java.awt.regtesthelpers.VisibilityValidator;
  40 
  41 public class MixedItemsTest01 extends TestCase {
  42 
  43     // A panel with a little bit of everything...
  44     class BusyPanel extends Panel {
  45         class ExtendedButton extends Button {
  46             public ExtendedButton( String name ) {
  47                 super();
  48                 setLabel(  name + " " + this.getClass().getName() );
  49             }
  50         }
  51 
  52         class ExtendedCanvas extends Canvas {
  53             protected String name;
  54 
  55             public ExtendedCanvas( String name ) {
  56                 super();
  57                 this.name = name;
  58             }
  59 
  60             public void paint( Graphics g ) {
  61                 super.paint( g );
  62                 Dimension dim = getSize();
  63                 g.setColor( Color.red );
  64                 g.drawRect( 0, 0, dim.width - 1, dim.height - 1 );
  65                 FontMetrics fm = g.getFontMetrics();
  66                 int ascent = fm.getAscent();
  67                 g.drawString(name + " " + this.getClass().getName(), 4, ascent+4);
  68             }
  69         }
  70 
  71         class ExtendedCheckBox extends Checkbox {
  72             public ExtendedCheckBox( String name ) {
  73                 super();
  74                 setLabel(  name + " " + this.getClass().getName() );
  75             }
  76         }
  77 
  78         class ExtendedChoice extends Choice {
  79             public ExtendedChoice( String name ) {
  80                 super();
  81                 this.add(  "a" + " " + name );
  82                 this.add(  "b" + " " + this.getClass().getName() );
  83                 this.add(  "c" );
  84                 this.add(  "e" );
  85                 this.add(  "f" );
  86             }
  87         }
  88 
  89         class ExtendedLabel extends Label {
  90             public ExtendedLabel( String name ) {
  91                 super();
  92                 setText(  name + " " + this.getClass().getName() );
  93             }
  94         }
  95 
  96         class ExtendedList extends List {
  97             public ExtendedList( String name ) {
  98                 super();
  99                 this.add(  "a" + " " + name );
 100                 this.add(  "b" + " " + this.getClass().getName() );
 101                 this.add(  "c" );
 102                 this.add(  "e" );
 103                 this.add(  "f" );
 104             }
 105         }
 106 
 107         class ExtendedComponent extends Component {
 108             protected String name;
 109 
 110             public ExtendedComponent( String name ) {
 111                 super();
 112                 this.name = name;
 113             }
 114 
 115             public void paint( Graphics g ) {
 116                 super.paint( g );
 117                 Dimension dim = getSize();
 118                 g.setColor( Color.red );
 119                 g.drawRect( 0, 0, dim.width - 1, dim.height - 1 );
 120                 FontMetrics fm = g.getFontMetrics();
 121                 int ascent = fm.getAscent();
 122                 g.drawString(name + " " + this.getClass().getName(), 4, ascent+4);
 123             }
 124         }
 125 
 126         class ExtendedPanel extends Panel {
 127             protected String name;
 128 
 129             public ExtendedPanel( String name ) {
 130                 super();
 131                 this.name = name;
 132             }
 133 
 134             public void paint( Graphics g ) {
 135                 super.paint( g );
 136                 Dimension dim = getSize();
 137                 g.setColor( Color.red );
 138                 g.drawRect( 0, 0, dim.width - 1, dim.height - 1 );
 139                 FontMetrics fm = g.getFontMetrics();
 140                 int ascent = fm.getAscent();
 141                 g.drawString(name + " " + this.getClass().getName(), 4, ascent+4);
 142             }
 143         }
 144 
 145         class ExtendedScrollBar extends Scrollbar {
 146             public ExtendedScrollBar( String name ) {
 147                 super();
 148                 setName(name);
 149                 // setLabel(  name + " " + this.getClass().getName() );
 150             }
 151         }
 152 
 153         class ExtendedScrollPane extends ScrollPane {
 154             class SPCanvas extends Canvas {
 155                 protected String name;
 156 
 157                 public SPCanvas( String name ) {
 158                     super();
 159                     this.name = name;
 160                 }
 161 
 162                 public Dimension getPreferredSize() {
 163                     return new Dimension(500,500);
 164                 }
 165 
 166                 public void paint( Graphics g ) {
 167                     super.paint( g );
 168                     Dimension dim = getSize();
 169                     g.setColor( Color.red );
 170                     g.drawRect( 0, 0, dim.width - 1, dim.height - 1 );
 171                     FontMetrics fm = g.getFontMetrics();
 172                     int ascent = fm.getAscent();
 173                     g.drawString(name + " " + this.getParent().getClass().getName(), 4, ascent+4);
 174                 }
 175             }
 176 
 177             public ExtendedScrollPane( String name ) {
 178                 super(SCROLLBARS_ALWAYS);
 179                 add( new SPCanvas(name) );
 180             }
 181         }
 182 
 183         class ExtendedTextArea extends TextArea {
 184             public ExtendedTextArea( String name ) {
 185                 super();
 186                 setText(  name + " " + this.getClass().getName() );
 187             }
 188         }
 189 
 190         class ExtendedTextField extends TextField {
 191             public ExtendedTextField( String name ) {
 192                 super();
 193                 setText(  name + " " + this.getClass().getName() );
 194             }
 195         }
 196 
 197 
 198         public BusyPanel( String name ) {
 199             int num_objects = 12;
 200             setLayout( new GridLayout( num_objects/4 + 1, 4, 5, 5 ) );
 201             for (int i = 0; i < num_objects; i++) {
 202                 switch (i % num_objects) {
 203                     case  0:  add( new ExtendedButton(name + " " + i) );        break;
 204                     case  1:  add( new ExtendedCanvas (name + " " + i) );        break;
 205                     case  2:  add( new ExtendedCheckBox (name + " " + i) );    break;
 206                     case  3:  add( new ExtendedChoice (name + " " + i) );        break;
 207                     case  4:  add( new ExtendedComponent (name + " " + i) );    break;
 208                     case  5:  add( new ExtendedLabel (name + " " + i) );        break;
 209                     case  6:  add( new ExtendedList (name + " " + i) );        break;
 210                     case  7:  add( new ExtendedPanel (name + " " + i) );        break;
 211                     case  8:  add( new ExtendedScrollBar (name + " " + i) );    break;
 212                     case  9:  add( new ExtendedScrollPane (name + " " + i) );    break;
 213                     case 10:  add( new ExtendedTextArea (name + " " + i) );    break;
 214                     case 11:  add( new ExtendedTextField (name + " " + i) );    break;
 215                 }
 216             }
 217         }
 218 
 219     }
 220 
 221     public void testMixedComponents() throws Exception {
 222         Frame frame = null;
 223         // Thread.currentThread().setName( "testMixedComponents" );
 224         for (int i = 0; i < 10; i++) {
 225             try {
 226                 // Bring up a test frame
 227                 frame = new Frame( "BusyFrame "  + i);
 228                 assertNotNull( frame);
 229                 BusyPanel panel = new BusyPanel("Test");
 230                 frame.add( panel );
 231                 frame.pack();
 232 
 233             /*
 234                 Dimension d = frame.getSize();
 235                 System.out.print( d.width + "\t" + d.height );
 236             */
 237 
 238                 VisibilityValidator.setVisibleAndConfirm(frame);
 239                 pause(250);
 240 
 241             /*
 242                 d = frame.getSize();
 243                 System.out.println("\t\t" + d.width + "\t" + d.height );
 244             */
 245 
 246                 frame.dispose();
 247                 frame = null;
 248             }
 249             finally {
 250                 if (frame != null) {
 251                     frame.setVisible(false);
 252                     frame.dispose();
 253                 }
 254             }
 255         }
 256     }
 257 
 258     //
 259     //    Boilerplate from here on down
 260     //
 261 
 262     public static void pause( int duration ) {
 263         try {
 264             Thread.sleep( duration );
 265         }
 266         catch (Throwable t) {
 267         }
 268     }
 269 
 270     public static Test suite() {
 271         return new TestSuite( MixedItemsTest01.class);
 272     }
 273 
 274     public static void main (String[] args) throws RuntimeException {
 275         TestResult tr = junit.textui.TestRunner.run(suite());
 276         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 277             throw new RuntimeException("### Unexpected JUnit errors or failures.");
 278         }
 279     }
 280 }