Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFileChooser/6817933/Test6817933.java
38854 views
/*1* Copyright (c) 2013, 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* @bug 681793326* @summary Tests that HTMLEditorKit does not affect JFileChooser27* @author Sergey Malenkov28*/2930import java.awt.Color;31import java.awt.Component;32import java.awt.Container;33import java.awt.Point;34import java.awt.Robot;35import java.awt.Toolkit;36import javax.swing.JFileChooser;37import javax.swing.JFrame;38import javax.swing.JToggleButton;39import javax.swing.SwingUtilities;40import javax.swing.UIManager;41import javax.swing.text.html.HTMLEditorKit;42import javax.swing.text.html.StyleSheet;4344import sun.awt.SunToolkit;45import sun.swing.WindowsPlacesBar;4647public class Test6817933 {4849private static final String STYLE = "BODY {background:red}";50private static final Color COLOR = Color.RED;51private static JFileChooser chooser;5253public static void main(String[] args) throws Exception {54try {55UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");56}57catch (Exception exception) {58exception.printStackTrace();59return; // ignore test on non-Windows machines60}61SwingUtilities.invokeAndWait(new Runnable() {62public void run() {63StyleSheet css = new StyleSheet();64css.addRule(STYLE);6566HTMLEditorKit kit = new HTMLEditorKit();67kit.setStyleSheet(css);6869JFrame frame = new JFrame(STYLE);70frame.add(chooser = new JFileChooser());71frame.setSize(640, 480);72frame.setVisible(true);73}74});7576SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();77toolkit.realSync(500);7879SwingUtilities.invokeAndWait(new Runnable() {80public void run() {81try {82JToggleButton button = get(JToggleButton.class,83get(WindowsPlacesBar.class, chooser));8485int width = button.getWidth();86int height = button.getHeight() / 3;87Point point = new Point(0, height * 2);88SwingUtilities.convertPointToScreen(point, button);89width += point.x;90height += point.y;9192int count = 0;93Robot robot = new Robot();94for (int x = point.x; x < width; x++) {95for (int y = point.y; y < height; y++) {96count += COLOR.equals(robot.getPixelColor(x, y)) ? -2 : 1;97}98}99if (count < 0) {100throw new Exception("TEST ERROR: a lot of red pixels");101}102}103catch (Exception exception) {104throw new Error(exception);105}106finally {107SwingUtilities.getWindowAncestor(chooser).dispose();108}109}110});111}112113private static <T> T get(Class<? extends T> type, Container container) {114Component component = container.getComponent(0);115if (!type.isAssignableFrom(component.getClass())) {116throw new IllegalStateException("expected " + type + "; expected " + component.getClass());117}118return (T) component;119}120}121122123