1 
   2 /*
   3  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * @test
  27  * @key headful
  28  * @bug 4251579
  29  * @summary  Tests if style sheets are working in JLabel
  30  * @author Denis Sharypov
  31  * @run main bug4251579
  32  */
  33 import java.awt.*;
  34 import javax.swing.*;
  35 
  36 import sun.awt.SunToolkit;
  37 
  38 public class bug4251579 {
  39 
  40     private static JLabel htmlComponent;
  41 
  42     public static void main(String[] args) throws Exception {
  43         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  44         final Robot robot = new Robot();
  45         robot.setAutoDelay(50);
  46 
  47         SwingUtilities.invokeAndWait(new Runnable() {
  48 
  49             @Override
  50             public void run() {
  51                 createAndShowGUI();
  52             }
  53         });
  54 
  55         toolkit.realSync();
  56 
  57         SwingUtilities.invokeAndWait(new Runnable() {
  58 
  59             @Override
  60             public void run() {
  61                 boolean passed = false;
  62 
  63                 Point p = htmlComponent.getLocationOnScreen();
  64                 Dimension d = htmlComponent.getSize();
  65                 int x0 = p.x;
  66                 int y = p.y + d.height / 2;
  67 
  68                 for (int x = x0; x < x0 + d.width; x++) {
  69                     if (robot.getPixelColor(x, y).equals(Color.blue)) {
  70                         passed = true;
  71                         break;
  72                     }
  73                 }
  74 
  75                 if (!passed) {
  76                     throw new RuntimeException("Test failed.");
  77                 }
  78 
  79             }
  80         });
  81     }
  82 
  83     private static void createAndShowGUI() {
  84 
  85         String htmlText =
  86                 "<html>"
  87                 + "<head><style> .blue{ color:blue; } </style></head>"
  88                 + "<body"
  89                 + "<P class=\"blue\"> should be rendered with BLUE class definition</P>"
  90                 + "</body>";
  91 
  92         JFrame mainFrame = new JFrame("bug4251579");
  93         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  94 
  95         htmlComponent = new JLabel(htmlText);
  96         mainFrame.getContentPane().add(htmlComponent);
  97 
  98         mainFrame.pack();
  99         mainFrame.setVisible(true);
 100     }
 101 }