1 /*
   2  * Copyright (c) 2008, 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 /*
  25  * @test
  26  * @bug 4251579
  27  * @summary  Tests if style sheets are working in JLabel
  28  * @author Denis Sharypov
  29  * @run main bug4251579
  30  */
  31 import java.awt.*;
  32 import javax.swing.*;
  33 
  34 public class bug4251579 {
  35 
  36     private static JLabel htmlComponent;
  37 
  38     public static void main(String[] args) throws Exception {
  39         final Robot robot = new Robot();
  40         robot.setAutoDelay(50);
  41 
  42         SwingUtilities.invokeAndWait(new Runnable() {
  43 
  44             @Override
  45             public void run() {
  46                 createAndShowGUI();
  47             }
  48         });
  49 
  50         robot.waitForIdle();
  51 
  52         SwingUtilities.invokeAndWait(new Runnable() {
  53 
  54             @Override
  55             public void run() {
  56                 boolean passed = false;
  57 
  58                 Point p = htmlComponent.getLocationOnScreen();
  59                 Dimension d = htmlComponent.getSize();
  60                 int x0 = p.x;
  61                 int y = p.y + d.height / 2;
  62 
  63                 for (int x = x0; x < x0 + d.width; x++) {
  64                     if (robot.getPixelColor(x, y).equals(Color.blue)) {
  65                         passed = true;
  66                         break;
  67                     }
  68                 }
  69 
  70                 if (!passed) {
  71                     throw new RuntimeException("Test failed.");
  72                 }
  73 
  74             }
  75         });
  76     }
  77 
  78     private static void createAndShowGUI() {
  79 
  80         String htmlText =
  81                 "<html>"
  82                 + "<head><style> .blue{ color:blue; } </style></head>"
  83                 + "<body"
  84                 + "<P class=\"blue\"> should be rendered with BLUE class definition</P>"
  85                 + "</body>";
  86 
  87         JFrame mainFrame = new JFrame("bug4251579");
  88         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  89 
  90         htmlComponent = new JLabel(htmlText);
  91         mainFrame.getContentPane().add(htmlComponent);
  92 
  93         mainFrame.pack();
  94         mainFrame.setVisible(true);
  95     }
  96 }