Path: blob/master/test/jdk/java/awt/Component/DimensionEncapsulation/DimensionEncapsulation.java
41153 views
/*1* Copyright (c) 2015, 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*/2223import java.awt.Button;24import java.awt.Canvas;25import java.awt.Checkbox;26import java.awt.Choice;27import java.awt.Component;28import java.awt.Dialog;29import java.awt.Dimension;30import java.awt.FileDialog;31import java.awt.Frame;32import java.awt.Label;33import java.awt.List;34import java.awt.Panel;35import java.awt.ScrollPane;36import java.awt.Scrollbar;37import java.awt.TextArea;38import java.awt.TextField;39import java.awt.Window;40import java.util.ArrayList;41import java.util.Objects;4243import javax.swing.Box;44import javax.swing.JButton;45import javax.swing.JCheckBox;46import javax.swing.JCheckBoxMenuItem;47import javax.swing.JColorChooser;48import javax.swing.JDesktopPane;49import javax.swing.JDialog;50import javax.swing.JEditorPane;51import javax.swing.JFileChooser;52import javax.swing.JFormattedTextField;53import javax.swing.JFrame;54import javax.swing.JInternalFrame;55import javax.swing.JLabel;56import javax.swing.JLayeredPane;57import javax.swing.JMenu;58import javax.swing.JMenuBar;59import javax.swing.JMenuItem;60import javax.swing.JOptionPane;61import javax.swing.JPasswordField;62import javax.swing.JPopupMenu;63import javax.swing.JProgressBar;64import javax.swing.JRadioButton;65import javax.swing.JRadioButtonMenuItem;66import javax.swing.JRootPane;67import javax.swing.JScrollPane;68import javax.swing.JSeparator;69import javax.swing.JSlider;70import javax.swing.JSpinner;71import javax.swing.JSplitPane;72import javax.swing.JTabbedPane;73import javax.swing.JTable;74import javax.swing.JTextArea;75import javax.swing.JTextField;76import javax.swing.JTextPane;77import javax.swing.JToggleButton;78import javax.swing.JToolBar;79import javax.swing.JTree;80import javax.swing.JViewport;81import javax.swing.JWindow;82import javax.swing.SwingUtilities;83import javax.swing.UIManager;84import javax.swing.UIManager.LookAndFeelInfo;85import javax.swing.UnsupportedLookAndFeelException;86import javax.swing.table.JTableHeader;8788import static javax.swing.UIManager.getInstalledLookAndFeels;8990/**91* @test92* @key headful93* @bug 645979894* @author Sergey Bylokhov95*/96public final class DimensionEncapsulation implements Runnable {9798java.util.List<Component> failures = new ArrayList<>();99100public static void main(final String[] args) throws Exception {101for (final LookAndFeelInfo laf : getInstalledLookAndFeels()) {102SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));103SwingUtilities.invokeAndWait(new DimensionEncapsulation());104}105}106107@Override108public void run() {109runTest(new Panel());110runTest(new Button());111runTest(new Checkbox());112runTest(new Canvas());113runTest(new Choice());114runTest(new Label());115runTest(new Scrollbar());116runTest(new TextArea());117runTest(new TextField());118runTest(new Dialog(new JFrame()));119runTest(new Frame());120runTest(new Window(new JFrame()));121runTest(new FileDialog(new JFrame()));122runTest(new List());123runTest(new ScrollPane());124runTest(new JFrame());125runTest(new JDialog(new JFrame()));126runTest(new JWindow(new JFrame()));127runTest(new JLabel("hi"));128runTest(new JMenu());129runTest(new JTree());130runTest(new JTable());131runTest(new JMenuItem());132runTest(new JCheckBoxMenuItem());133runTest(new JToggleButton());134runTest(new JSpinner());135runTest(new JSlider());136runTest(Box.createVerticalBox());137runTest(Box.createHorizontalBox());138runTest(new JTextField());139runTest(new JTextArea());140runTest(new JTextPane());141runTest(new JPasswordField());142runTest(new JFormattedTextField());143runTest(new JEditorPane());144runTest(new JButton());145runTest(new JColorChooser());146runTest(new JFileChooser());147runTest(new JCheckBox());148runTest(new JInternalFrame());149runTest(new JDesktopPane());150runTest(new JTableHeader());151runTest(new JLayeredPane());152runTest(new JRootPane());153runTest(new JMenuBar());154runTest(new JOptionPane());155runTest(new JRadioButton());156runTest(new JRadioButtonMenuItem());157runTest(new JPopupMenu());158//runTest(new JScrollBar()); --> don't test defines max and min in159// terms of preferred160runTest(new JScrollPane());161runTest(new JViewport());162runTest(new JSplitPane());163runTest(new JTabbedPane());164runTest(new JToolBar());165runTest(new JSeparator());166runTest(new JProgressBar());167if (!failures.isEmpty()) {168System.out.println("These classes failed");169for (final Component failure : failures) {170System.out.println(failure.getClass());171}172throw new RuntimeException("Test failed");173}174}175176public void runTest(final Component c) {177try {178test(c);179c.setMinimumSize(new Dimension(100, 10));180c.setMaximumSize(new Dimension(200, 20));181c.setPreferredSize(new Dimension(300, 30));182test(c);183} catch (final Throwable ignored) {184failures.add(c);185}186}187188public void test(final Component component) {189final Dimension psize = component.getPreferredSize();190psize.width += 200;191if (Objects.equals(psize, component.getPreferredSize())) {192throw new RuntimeException("PreferredSize is wrong");193}194final Dimension msize = component.getMaximumSize();195msize.width += 200;196if (Objects.equals(msize, component.getMaximumSize())) {197throw new RuntimeException("MaximumSize is wrong");198}199final Dimension misize = component.getMinimumSize();200misize.width += 200;201if (Objects.equals(misize, component.getMinimumSize())) {202throw new RuntimeException("MinimumSize is wrong");203}204}205206private static void setLookAndFeel(final LookAndFeelInfo laf) {207try {208UIManager.setLookAndFeel(laf.getClassName());209System.out.println("LookAndFeel: " + laf.getClassName());210} catch (ClassNotFoundException | InstantiationException |211UnsupportedLookAndFeelException | IllegalAccessException e) {212throw new RuntimeException(e);213}214}215}216217218