1 /*
   2  * Copyright (c) 2013, 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 6817933
  27  * @summary Tests that HTMLEditorKit does not affect JFileChooser
  28  * @author Sergey Malenkov
  29  */
  30 
  31 import java.awt.Color;
  32 import java.awt.Component;
  33 import java.awt.Container;
  34 import java.awt.Point;
  35 import java.awt.Robot;
  36 import java.awt.Toolkit;
  37 import javax.swing.JFileChooser;
  38 import javax.swing.JFrame;
  39 import javax.swing.JToggleButton;
  40 import javax.swing.SwingUtilities;
  41 import javax.swing.UIManager;
  42 import javax.swing.text.html.HTMLEditorKit;
  43 import javax.swing.text.html.StyleSheet;
  44 
  45 import sun.awt.SunToolkit;
  46 import sun.swing.WindowsPlacesBar;
  47 
  48 public class Test6817933 {
  49 
  50     private static final String STYLE = "BODY {background:red}";
  51     private static final Color COLOR = Color.RED;
  52     private static JFileChooser chooser;
  53 
  54     public static void main(String[] args) throws Exception {
  55         try {
  56             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  57         }
  58         catch (Exception exception) {
  59             exception.printStackTrace();
  60             return; // ignore test on non-Windows machines
  61         }
  62         SwingUtilities.invokeAndWait(new Runnable() {
  63             public void run() {
  64                 StyleSheet css = new StyleSheet();
  65                 css.addRule(STYLE);
  66 
  67                 HTMLEditorKit kit = new HTMLEditorKit();
  68                 kit.setStyleSheet(css);
  69 
  70                 JFrame frame = new JFrame(STYLE);
  71                 frame.add(chooser = new JFileChooser());
  72                 frame.setSize(640, 480);
  73                 frame.setVisible(true);
  74             }
  75         });
  76 
  77         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  78         toolkit.realSync(500);
  79 
  80         SwingUtilities.invokeAndWait(new Runnable() {
  81             public void run() {
  82                 try {
  83                     JToggleButton button = get(JToggleButton.class,
  84                                            get(WindowsPlacesBar.class, chooser));
  85 
  86                     int width = button.getWidth();
  87                     int height = button.getHeight() / 3;
  88                     Point point = new Point(0, height * 2);
  89                     SwingUtilities.convertPointToScreen(point, button);
  90                     width += point.x;
  91                     height += point.y;
  92 
  93                     int count = 0;
  94                     Robot robot = new Robot();
  95                     for (int x = point.x; x < width; x++) {
  96                         for (int y = point.y; y < height; y++) {
  97                             count += COLOR.equals(robot.getPixelColor(x, y)) ? -2 : 1;
  98                         }
  99                     }
 100                     if (count < 0) {
 101                         throw new Exception("TEST ERROR: a lot of red pixels");
 102                     }
 103                 }
 104                 catch (Exception exception) {
 105                     throw new Error(exception);
 106                 }
 107                 finally {
 108                     SwingUtilities.getWindowAncestor(chooser).dispose();
 109                 }
 110             }
 111         });
 112     }
 113 
 114     private static <T> T get(Class<? extends T> type, Container container) {
 115         Component component = container.getComponent(0);
 116         if (!type.isAssignableFrom(component.getClass())) {
 117             throw new IllegalStateException("expected " + type + "; expected " + component.getClass());
 118         }
 119         return (T) component;
 120     }
 121 }