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