1 /*
   2  * Copyright (c) 2012, 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 import java.awt.Color;
  25 import java.awt.Font;
  26 
  27 import javax.swing.border.EmptyBorder;
  28 import javax.swing.border.TitledBorder;
  29 import javax.swing.UIManager;
  30 import javax.swing.UnsupportedLookAndFeelException;
  31 
  32 /* @test
  33  * @bug 7022041
  34  * @summary This test check the behaviour of getTitleFont() and getTitleColor()
  35  *          methods of the TitledBorder class.
  36  * @author Pavel Tisnovsky
  37  */
  38 public class Test7022041 {
  39 
  40     public static void main(String[] args) throws Exception {
  41         UIManager.LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels();
  42         // try to test all installed Look and Feels
  43         for (UIManager.LookAndFeelInfo lookAndFeel : installedLookAndFeels) {
  44             String name = lookAndFeel.getName();
  45             System.out.println("Testing " + name);
  46             // Some Look and Feels work only when test is run in a GUI environment
  47             // (GTK+ LAF is an example)
  48             try {
  49                 UIManager.setLookAndFeel(lookAndFeel.getClassName());
  50                 checkTitleColor();
  51                 System.out.println("    titleColor test ok");
  52                 checkTitleFont();
  53                 System.out.println("    titleFont test ok");
  54             }
  55             catch (UnsupportedLookAndFeelException e) {
  56                 System.out.println("    Note: LookAndFeel " + name
  57                                  + " is not supported on this configuration");
  58             }
  59         }
  60     }
  61 
  62     /**
  63       * Check behaviour of method TitledBorder.getTitleColor()
  64       */
  65     private static void checkTitleColor() {
  66         TitledBorder titledBorder = new TitledBorder(new EmptyBorder(1, 1, 1, 1));
  67         Color defaultColor = UIManager.getLookAndFeelDefaults().getColor("TitledBorder.titleColor");
  68         Color titledBorderColor = titledBorder.getTitleColor();
  69 
  70         // check default configuration
  71         if (defaultColor == null) {
  72             if (titledBorderColor == null) {
  73                 return;
  74             }
  75             else {
  76                 throw new RuntimeException("TitledBorder default color should be null");
  77             }
  78         }
  79         if (!defaultColor.equals(titledBorderColor)) {
  80             throw new RuntimeException("L&F default color " + defaultColor.toString()
  81                                      + " differs from TitledBorder color " + titledBorderColor.toString());
  82         }
  83 
  84         // title color is explicitly specified
  85         Color color = Color.green;
  86         titledBorder.setTitleColor(color);
  87         if (!color.equals(titledBorder.getTitleColor())) {
  88             throw new RuntimeException("TitledBorder color should be " + color.toString());
  89         }
  90 
  91         // title color is unspecified
  92         titledBorder.setTitleColor(null);
  93         if (!defaultColor.equals(titledBorder.getTitleColor())) {
  94             throw new RuntimeException("L&F default color " + defaultColor.toString()
  95                                      + " differs from TitledBorder color " + titledBorderColor.toString());
  96         }
  97     }
  98 
  99     /**
 100       * Check behaviour of method TitledBorder.getTitleFont()
 101       */
 102     private static void checkTitleFont() {
 103         TitledBorder titledBorder = new TitledBorder(new EmptyBorder(1, 1, 1, 1));
 104         Font defaultFont = UIManager.getLookAndFeelDefaults().getFont("TitledBorder.font");
 105         Font titledBorderFont = titledBorder.getTitleFont();
 106 
 107         // check default configuration
 108         if (defaultFont == null) {
 109             if (titledBorderFont == null) {
 110                 return;
 111             }
 112             else {
 113                 throw new RuntimeException("TitledBorder default font should be null");
 114             }
 115         }
 116         if (!defaultFont.equals(titledBorderFont)) {
 117             throw new RuntimeException("L&F default font " + defaultFont.toString()
 118                                      + " differs from TitledBorder font " + titledBorderFont.toString());
 119         }
 120 
 121         // title font is explicitly specified
 122         Font font = new Font("Dialog", Font.PLAIN, 10);
 123         titledBorder.setTitleFont(font);
 124         if (!font.equals(titledBorder.getTitleFont())) {
 125             throw new RuntimeException("TitledBorder font should be " + font.toString());
 126         }
 127 
 128         // title Font is unspecified
 129         titledBorder.setTitleFont(null);
 130         if (!defaultFont.equals(titledBorder.getTitleFont())) {
 131             throw new RuntimeException("L&F default font " + defaultFont.toString()
 132                                      + " differs from TitledBorder font " + titledBorderFont.toString());
 133         }
 134     }
 135 }
 136