1 /*
   2  * Copyright (c) 2015, 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  * @key headful
  27  * @bug 4960629 7124238
  28  * @summary  Tests if font for html text on widgets in correct.
  29  * @author Denis Sharypov
  30  * @run main bug4960629
  31  */
  32 
  33 import java.awt.Font;
  34 import java.lang.reflect.InvocationTargetException;
  35 import javax.swing.JFrame;
  36 import javax.swing.JLabel;
  37 import javax.swing.SwingUtilities;
  38 import javax.swing.UIManager;
  39 import javax.swing.plaf.basic.BasicHTML;
  40 import javax.swing.text.AttributeSet;
  41 import javax.swing.text.View;
  42 import javax.swing.text.html.StyleSheet;
  43 import javax.swing.text.html.HTMLDocument;
  44 
  45 public class bug4960629 {
  46     private boolean passed = false;
  47     private JLabel label = null;
  48     private JFrame f = null;
  49 
  50     public void createAndShowGUI() throws Exception {
  51         try {
  52             UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  53             label = new JLabel("<html><P>This is a test of the</P></html>");
  54             System.out.println("UIManager.getLookAndFeel()"
  55                    + UIManager.getLookAndFeel().getClass());
  56             f = new JFrame();
  57             f.getContentPane().add(label);
  58             f.pack();
  59             f.setVisible(true);
  60             test();
  61         } finally {
  62             if (f != null) { f.dispose(); }
  63         }
  64     }
  65 
  66     bug4960629() throws InvocationTargetException, InterruptedException {
  67         SwingUtilities.invokeAndWait(new Runnable() {
  68             @Override
  69             public void run() {
  70                 try {
  71                     createAndShowGUI();
  72                 } catch (Exception e) {
  73                     throw new RuntimeException("Exception "
  74                               + e.getMessage());
  75                 }
  76             }
  77         });
  78     }
  79 
  80     private void test() {
  81         View root = ((View)label.getClientProperty(BasicHTML.propertyKey))
  82                 .getView(0);
  83         int n = root.getViewCount();
  84         View v  = root.getView(n - 1);
  85         AttributeSet attrs = v.getAttributes();
  86         StyleSheet ss = ((HTMLDocument) v.getDocument()).getStyleSheet();
  87         Font font = ss.getFont(attrs);
  88         System.out.println(font.getSize());
  89         passed = (font.getSize() == 12);
  90         if(!passed) {
  91             throw new RuntimeException("Test failed.");
  92         }
  93     }
  94 
  95     public static void main(String args[]) throws Throwable {
  96         new bug4960629();
  97    }
  98 }