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  * @key headful
  94  * @bug 6459798
  95  * @author Sergey Bylokhov
  96  */
  97 public final class DimensionEncapsulation implements Runnable {
  98 
  99     java.util.List<Component> failures = new ArrayList<>();
 100 
 101     public static void main(final String[] args) throws Exception {
 102         for (final LookAndFeelInfo laf : getInstalledLookAndFeels()) {
 103             SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
 104             SwingUtilities.invokeAndWait(new DimensionEncapsulation());
 105         }
 106     }
 107 
 108     @Override
 109     public void run() {
 110         runTest(new Panel());
 111         runTest(new Button());
 112         runTest(new Checkbox());
 113         runTest(new Canvas());
 114         runTest(new Choice());
 115         runTest(new Label());
 116         runTest(new Scrollbar());
 117         runTest(new TextArea());
 118         runTest(new TextField());
 119         runTest(new Dialog(new JFrame()));
 120         runTest(new Frame());
 121         runTest(new Window(new JFrame()));
 122         runTest(new FileDialog(new JFrame()));
 123         runTest(new List());
 124         runTest(new ScrollPane());
 125         runTest(new JFrame());
 126         runTest(new JDialog(new JFrame()));
 127         runTest(new JWindow(new JFrame()));
 128         runTest(new JLabel("hi"));
 129         runTest(new JMenu());
 130         runTest(new JTree());
 131         runTest(new JTable());
 132         runTest(new JMenuItem());
 133         runTest(new JCheckBoxMenuItem());
 134         runTest(new JToggleButton());
 135         runTest(new JSpinner());
 136         runTest(new JSlider());
 137         runTest(Box.createVerticalBox());
 138         runTest(Box.createHorizontalBox());
 139         runTest(new JTextField());
 140         runTest(new JTextArea());
 141         runTest(new JTextPane());
 142         runTest(new JPasswordField());
 143         runTest(new JFormattedTextField());
 144         runTest(new JEditorPane());
 145         runTest(new JButton());
 146         runTest(new JColorChooser());
 147         runTest(new JFileChooser());
 148         runTest(new JCheckBox());
 149         runTest(new JInternalFrame());
 150         runTest(new JDesktopPane());
 151         runTest(new JTableHeader());
 152         runTest(new JLayeredPane());
 153         runTest(new JRootPane());
 154         runTest(new JMenuBar());
 155         runTest(new JOptionPane());
 156         runTest(new JRadioButton());
 157         runTest(new JRadioButtonMenuItem());
 158         runTest(new JPopupMenu());
 159         //runTest(new JScrollBar()); --> don't test defines max and min in
 160         // terms of preferred
 161         runTest(new JScrollPane());
 162         runTest(new JViewport());
 163         runTest(new JSplitPane());
 164         runTest(new JTabbedPane());
 165         runTest(new JToolBar());
 166         runTest(new JSeparator());
 167         runTest(new JProgressBar());
 168         if (!failures.isEmpty()) {
 169             System.out.println("These classes failed");
 170             for (final Component failure : failures) {
 171                 System.out.println(failure.getClass());
 172             }
 173             throw new RuntimeException("Test failed");
 174         }
 175     }
 176 
 177     public void runTest(final Component c) {
 178         try {
 179             test(c);
 180             c.setMinimumSize(new Dimension(100, 10));
 181             c.setMaximumSize(new Dimension(200, 20));
 182             c.setPreferredSize(new Dimension(300, 30));
 183             test(c);
 184         } catch (final Throwable ignored) {
 185             failures.add(c);
 186         }
 187     }
 188 
 189     public void test(final Component component) {
 190         final Dimension psize = component.getPreferredSize();
 191         psize.width += 200;
 192         if (Objects.equals(psize, component.getPreferredSize())) {
 193             throw new RuntimeException("PreferredSize is wrong");
 194         }
 195         final Dimension msize = component.getMaximumSize();
 196         msize.width += 200;
 197         if (Objects.equals(msize, component.getMaximumSize())) {
 198             throw new RuntimeException("MaximumSize is wrong");
 199         }
 200         final Dimension misize = component.getMinimumSize();
 201         misize.width += 200;
 202         if (Objects.equals(misize, component.getMinimumSize())) {
 203             throw new RuntimeException("MinimumSize is wrong");
 204         }
 205     }
 206 
 207     private static void setLookAndFeel(final LookAndFeelInfo laf) {
 208         try {
 209             UIManager.setLookAndFeel(laf.getClassName());
 210             System.out.println("LookAndFeel: " + laf.getClassName());
 211         } catch (ClassNotFoundException | InstantiationException |
 212                 UnsupportedLookAndFeelException | IllegalAccessException e) {
 213             throw new RuntimeException(e);
 214         }
 215     }
 216 }