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.Component;
  25 import java.awt.Insets;
  26 import java.util.ArrayList;
  27 import java.util.Collection;
  28 
  29 import javax.swing.Box;
  30 import javax.swing.JButton;
  31 import javax.swing.JCheckBox;
  32 import javax.swing.JCheckBoxMenuItem;
  33 import javax.swing.JColorChooser;
  34 import javax.swing.JComponent;
  35 import javax.swing.JDesktopPane;
  36 import javax.swing.JEditorPane;
  37 import javax.swing.JFileChooser;
  38 import javax.swing.JFormattedTextField;
  39 import javax.swing.JInternalFrame;
  40 import javax.swing.JLabel;
  41 import javax.swing.JLayeredPane;
  42 import javax.swing.JMenu;
  43 import javax.swing.JMenuBar;
  44 import javax.swing.JMenuItem;
  45 import javax.swing.JOptionPane;
  46 import javax.swing.JPasswordField;
  47 import javax.swing.JPopupMenu;
  48 import javax.swing.JProgressBar;
  49 import javax.swing.JRadioButton;
  50 import javax.swing.JRadioButtonMenuItem;
  51 import javax.swing.JRootPane;
  52 import javax.swing.JScrollBar;
  53 import javax.swing.JScrollPane;
  54 import javax.swing.JSeparator;
  55 import javax.swing.JSlider;
  56 import javax.swing.JSpinner;
  57 import javax.swing.JSplitPane;
  58 import javax.swing.JTabbedPane;
  59 import javax.swing.JTable;
  60 import javax.swing.JTextArea;
  61 import javax.swing.JTextField;
  62 import javax.swing.JTextPane;
  63 import javax.swing.JToggleButton;
  64 import javax.swing.JToolBar;
  65 import javax.swing.JTree;
  66 import javax.swing.JViewport;
  67 import javax.swing.SwingUtilities;
  68 import javax.swing.UIManager;
  69 import javax.swing.UnsupportedLookAndFeelException;
  70 import javax.swing.table.JTableHeader;
  71 
  72 import static javax.swing.UIManager.LookAndFeelInfo;
  73 import static javax.swing.UIManager.getInstalledLookAndFeels;
  74 
  75 /**
  76  * @test
  77  * @key headful
  78  * @bug 6459800
  79  * @author Sergey Bylokhov
  80  */
  81 public final class InsetsEncapsulation implements Runnable {
  82 
  83     private final Collection<Component> failures = new ArrayList<>();
  84 
  85     public static void main(final String[] args) throws Exception {
  86         for (final LookAndFeelInfo laf : getInstalledLookAndFeels()) {
  87             SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
  88             SwingUtilities.invokeAndWait(new InsetsEncapsulation());
  89         }
  90     }
  91 
  92     @Override
  93     public void run() {
  94         runTest(new JLabel("hi"));
  95         runTest(new JMenu());
  96         runTest(new JTree());
  97         runTest(new JTable());
  98         runTest(new JMenuItem());
  99         runTest(new JCheckBoxMenuItem());
 100         runTest(new JToggleButton());
 101         runTest(new JSpinner());
 102         runTest(new JSlider());
 103         runTest(Box.createVerticalBox());
 104         runTest(Box.createHorizontalBox());
 105         runTest(new JTextField());
 106         runTest(new JTextArea());
 107         runTest(new JTextPane());
 108         runTest(new JPasswordField());
 109         runTest(new JFormattedTextField());
 110         runTest(new JEditorPane());
 111         runTest(new JButton());
 112         runTest(new JColorChooser());
 113         runTest(new JFileChooser());
 114         runTest(new JCheckBox());
 115         runTest(new JInternalFrame());
 116         runTest(new JDesktopPane());
 117         runTest(new JTableHeader());
 118         runTest(new JLayeredPane());
 119         runTest(new JRootPane());
 120         runTest(new JMenuBar());
 121         runTest(new JOptionPane());
 122         runTest(new JRadioButton());
 123         runTest(new JRadioButtonMenuItem());
 124         runTest(new JPopupMenu());
 125         runTest(new JScrollBar());
 126         runTest(new JScrollPane());
 127         runTest(new JViewport());
 128         runTest(new JSplitPane());
 129         runTest(new JTabbedPane());
 130         runTest(new JToolBar());
 131         runTest(new JSeparator());
 132         runTest(new JProgressBar());
 133         if (!failures.isEmpty()) {
 134             System.out.println("These classes failed");
 135             for (final Component failure : failures) {
 136                 System.out.println(failure.getClass());
 137             }
 138             throw new RuntimeException("Test failed");
 139         }
 140     }
 141 
 142     void runTest(final JComponent component) {
 143         try {
 144             test(component);
 145         } catch (final Throwable ignored) {
 146             failures.add(component);
 147         }
 148     }
 149 
 150     void test(final JComponent component) {
 151         final Insets p = component.getInsets();
 152         p.top += 3;
 153         if (p.equals(component.getInsets())) {
 154             throw new RuntimeException("Insets altered by altering Insets!");
 155         }
 156     }
 157 
 158     private static void setLookAndFeel(final LookAndFeelInfo laf) {
 159         try {
 160             UIManager.setLookAndFeel(laf.getClassName());
 161             System.out.println("LookAndFeel: " + laf.getClassName());
 162         } catch (ClassNotFoundException | InstantiationException |
 163                 UnsupportedLookAndFeelException | IllegalAccessException e) {
 164             throw new RuntimeException(e);
 165         }
 166     }
 167 }