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