1 
   2 /*
   3  * Copyright (c) 2008, 2018, 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  * @run main bug4251579
  31  */
  32 import java.awt.Dimension;
  33 import java.awt.Point;
  34 import java.awt.Color;
  35 import java.awt.Robot;
  36 import java.awt.event.WindowAdapter;
  37 import java.awt.event.WindowEvent;
  38 import java.awt.event.WindowListener;
  39 import java.util.concurrent.CountDownLatch;
  40 
  41 import javax.swing.JLabel;
  42 import javax.swing.JFrame;
  43 import javax.swing.SwingUtilities;
  44 
  45 public class bug4251579 {
  46 
  47     private static JLabel htmlComponent;
  48     private static CountDownLatch latch = new CountDownLatch(1);
  49     public static void main(String[] args) throws Exception {
  50         final Robot robot = new Robot();
  51         robot.setAutoDelay(50);
  52 
  53         SwingUtilities.invokeAndWait(bug4251579::createAndShowGUI);
  54         try {
  55             latch.await();
  56         }catch (InterruptedException ex) {
  57             throw new RuntimeException(ex.getCause());
  58         }
  59 
  60         robot.waitForIdle();
  61 
  62         SwingUtilities.invokeAndWait(() -> {
  63                 boolean passed = false;
  64 
  65                 Point p = htmlComponent.getLocationOnScreen();
  66                 Dimension d = htmlComponent.getSize();
  67                 int x0 = p.x;
  68                 int y = p.y + d.height / 2;
  69 
  70                 for (int x = x0; x < x0 + d.width; x++) {
  71                     if (robot.getPixelColor(x, y).equals(Color.blue)) {
  72                         passed = true;
  73                         break;
  74                     }
  75                 }
  76 
  77                 if (!passed) {
  78                     throw new RuntimeException("Test failed.");
  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         WindowListener l = new WindowAdapter() {
 100             @Override public void windowOpened(WindowEvent e){
 101                 latch.countDown();
 102             }
 103         };
 104         mainFrame.addWindowListener(l);
 105         mainFrame.setVisible(true);
 106     }
 107 }