Path: blob/master/test/jdk/javax/swing/JFileChooser/6817933/Test6817933.java
41153 views
/*1* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 681793327* @summary Tests that HTMLEditorKit does not affect JFileChooser28* @author Sergey Malenkov29* @requires (os.family == "windows")30* @modules java.desktop/sun.awt31* java.desktop/sun.swing32*/3334import java.awt.Color;35import java.awt.Component;36import java.awt.Container;37import java.awt.Point;38import java.awt.Robot;39import java.awt.Toolkit;40import javax.swing.JFileChooser;41import javax.swing.JFrame;42import javax.swing.JToggleButton;43import javax.swing.SwingUtilities;44import javax.swing.UIManager;45import javax.swing.text.html.HTMLEditorKit;46import javax.swing.text.html.StyleSheet;4748import sun.awt.SunToolkit;49import sun.swing.WindowsPlacesBar;5051public class Test6817933 {5253private static final String STYLE = "BODY {background:red}";54private static final Color COLOR = Color.RED;55private static JFileChooser chooser;5657public static void main(String[] args) throws Exception {58try {59UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");60}61catch (Exception exception) {62exception.printStackTrace();63return; // ignore test on non-Windows machines64}65SwingUtilities.invokeAndWait(new Runnable() {66public void run() {67StyleSheet css = new StyleSheet();68css.addRule(STYLE);6970HTMLEditorKit kit = new HTMLEditorKit();71kit.setStyleSheet(css);7273JFrame frame = new JFrame(STYLE);74frame.add(chooser = new JFileChooser());75frame.setSize(640, 480);76frame.setVisible(true);77}78});7980SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();81toolkit.realSync(500);8283SwingUtilities.invokeAndWait(new Runnable() {84public void run() {85try {86JToggleButton button = get(JToggleButton.class,87get(WindowsPlacesBar.class, chooser));8889int width = button.getWidth();90int height = button.getHeight() / 3;91Point point = new Point(0, height * 2);92SwingUtilities.convertPointToScreen(point, button);93width += point.x;94height += point.y;9596int count = 0;97Robot robot = new Robot();98for (int x = point.x; x < width; x++) {99for (int y = point.y; y < height; y++) {100count += COLOR.equals(robot.getPixelColor(x, y)) ? -2 : 1;101}102}103if (count < 0) {104throw new Exception("TEST ERROR: a lot of red pixels");105}106}107catch (Exception exception) {108throw new Error(exception);109}110finally {111SwingUtilities.getWindowAncestor(chooser).dispose();112}113}114});115}116117private static <T> T get(Class<? extends T> type, Container container) {118Component component = container.getComponent(0);119if (!type.isAssignableFrom(component.getClass())) {120throw new IllegalStateException("expected " + type + "; expected " + component.getClass());121}122return (T) component;123}124}125126127