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