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 for recieving TextEvents
  27  @summary com.apple.junit.java.awt.Font;
  28  @library ../../regtesthelpers
  29  @build VisibilityValidator
  30  @run junit MixedFonts01
  31  */
  32 
  33 import junit.framework.*;
  34 
  35 import java.awt.*;
  36 import java.awt.event.ActionEvent;
  37 import java.awt.event.MouseEvent;
  38 import java.util.Random;
  39 
  40 import test.java.awt.regtesthelpers.VisibilityValidator;
  41 
  42 class AllFonts {
  43     static final Random rand = new Random( 62566 );
  44     static final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
  45     static final String[] fontNames = env.getAvailableFontFamilyNames();
  46     static final int SIZES = 4;
  47     static Font[] fonts = null;
  48     static    {
  49         fonts = new Font[ fontNames.length * SIZES ];
  50         for (int i = 0; i < fontNames.length; i++) {
  51             int ii = i * SIZES;
  52             fonts[ii] = new Font( fontNames[i], Font.PLAIN, 10 );
  53             fonts[ii + 1] = new Font( fontNames[i], Font.ITALIC, 15 );
  54             fonts[ii + 2] = new Font( fontNames[i], Font.PLAIN, 20 );
  55             fonts[ii + 3] = new Font( fontNames[i], Font.PLAIN, 25 );
  56         }
  57     }
  58 
  59     static int index = 0;
  60     synchronized static public Font getNextFont() {
  61         return (fonts[index++ % fonts.length]);
  62     }
  63 
  64     synchronized static public Font getRandomFont() {
  65         int i = rand.nextInt(fonts.length);
  66         return (fonts[i]);
  67     }
  68 
  69 }
  70 
  71 public class MixedFonts01 extends TestCase {
  72     class FontButton extends Button {
  73         Font f = AllFonts.getRandomFont();
  74 
  75         public FontButton(String name) {
  76             super(name + "nopqrstuvwxyz");
  77             setFont(f);
  78 
  79             addActionListener( new java.awt.event.ActionListener() {
  80                 public void actionPerformed( ActionEvent e ) {
  81                     System.out.println( f );
  82                 }
  83             } );
  84         }
  85     }
  86 
  87     class FontLabel extends Label {
  88         Font f = AllFonts.getRandomFont();
  89 
  90         public FontLabel(String name) {
  91             super(name + "nopqrstuvwxyz");
  92             setFont(f);
  93 
  94             addMouseListener( new java.awt.event.MouseAdapter() {
  95                 public void mouseClicked( MouseEvent e ) {
  96                     System.out.println( f );
  97                 }
  98             } );
  99         }
 100     }
 101 
 102 
 103     class BusyPanel extends Panel {
 104         public BusyPanel(int num_objects) {
 105             setLayout(new GridLayout( num_objects/5, 5, 4, 4 ));
 106 
 107             for (int i = 1; i <= num_objects; i++) {
 108                 Component item;
 109 
 110                 if ( i % 2 == 0) {
 111                     item = new FontButton("B" + i);
 112                 }
 113                 else {
 114                     item = new FontLabel("L" + i);
 115                 }
 116                 add(item);
 117             }
 118         }
 119     }
 120 
 121     public void testMixedFonts() throws Exception {
 122         Frame frame = null;
 123         // Thread.currentThread().setName( "testMixedFonts" );
 124 
 125         for (int i = 0; i < 1; i++) {
 126             try {
 127                 // Bring up a test frame
 128                 frame = new Frame( "testMixedFonts " + i );
 129                 assertNotNull(frame);
 130 
 131                 frame.add( new BusyPanel(30) );
 132                 frame.pack();
 133 
 134                 VisibilityValidator.setVisibleAndConfirm(frame);
 135 
 136                 pause(500);
 137                 frame.dispose();
 138                 frame = null;
 139             }
 140             finally {
 141                 if (frame != null) {
 142                     frame.setVisible(false);
 143                     frame.dispose();
 144                 }
 145             }
 146         }
 147     }
 148 
 149     public static void pause( int duration ) {
 150         try {
 151             Thread.sleep( duration );
 152         }
 153         catch (Throwable t) {
 154         }
 155     }
 156 
 157     public static Test suite() {
 158         return new TestSuite( MixedFonts01.class);
 159     }
 160 
 161     public static void main (String[] args) throws RuntimeException {
 162         TestResult tr = junit.textui.TestRunner.run(suite());
 163         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 164             throw new RuntimeException("### Unexpected JUnit errors or failures.");
 165         }
 166     }
 167 }
 168