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