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  * @bug 6459800
  78  * @author Sergey Bylokhov
  79  */
  80 public final class InsetsEncapsulation implements Runnable {
  81 
  82     private final Collection<Component> failures = new ArrayList<>();
  83 
  84     public static void main(final String[] args) throws Exception {
  85         for (final LookAndFeelInfo laf : getInstalledLookAndFeels()) {
  86             SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
  87             SwingUtilities.invokeAndWait(new InsetsEncapsulation());
  88         }
  89     }
  90 
  91     @Override
  92     public void run() {
  93         runTest(new JLabel("hi"));
  94         runTest(new JMenu());
  95         runTest(new JTree());
  96         runTest(new JTable());
  97         runTest(new JMenuItem());
  98         runTest(new JCheckBoxMenuItem());
  99         runTest(new JToggleButton());
 100         runTest(new JSpinner());
 101         runTest(new JSlider());
 102         runTest(Box.createVerticalBox());
 103         runTest(Box.createHorizontalBox());
 104         runTest(new JTextField());
 105         runTest(new JTextArea());
 106         runTest(new JTextPane());
 107         runTest(new JPasswordField());
 108         runTest(new JFormattedTextField());
 109         runTest(new JEditorPane());
 110         runTest(new JButton());
 111         runTest(new JColorChooser());
 112         runTest(new JFileChooser());
 113         runTest(new JCheckBox());
 114         runTest(new JInternalFrame());
 115         runTest(new JDesktopPane());
 116         runTest(new JTableHeader());
 117         runTest(new JLayeredPane());
 118         runTest(new JRootPane());
 119         runTest(new JMenuBar());
 120         runTest(new JOptionPane());
 121         runTest(new JRadioButton());
 122         runTest(new JRadioButtonMenuItem());
 123         runTest(new JPopupMenu());
 124         runTest(new JScrollBar());
 125         runTest(new JScrollPane());
 126         runTest(new JViewport());
 127         runTest(new JSplitPane());
 128         runTest(new JTabbedPane());
 129         runTest(new JToolBar());
 130         runTest(new JSeparator());
 131         runTest(new JProgressBar());
 132         if (!failures.isEmpty()) {
 133             System.out.println("These classes failed");
 134             for (final Component failure : failures) {
 135                 System.out.println(failure.getClass());
 136             }
 137             throw new RuntimeException("Test failed");
 138         }
 139     }
 140 
 141     void runTest(final JComponent component) {
 142         try {
 143             test(component);
 144         } catch (final Throwable ignored) {
 145             failures.add(component);
 146         }
 147     }
 148 
 149     void test(final JComponent component) {
 150         final Insets p = component.getInsets();
 151         p.top += 3;
 152         if (p.equals(component.getInsets())) {
 153             throw new RuntimeException("Insets altered by altering Insets!");
 154         }
 155     }
 156 
 157     private static void setLookAndFeel(final LookAndFeelInfo laf) {
 158         try {
 159             UIManager.setLookAndFeel(laf.getClassName());
 160             System.out.println("LookAndFeel: " + laf.getClassName());
 161         } catch (ClassNotFoundException | InstantiationException |
 162                 UnsupportedLookAndFeelException | IllegalAccessException e) {
 163             throw new RuntimeException(e);
 164         }
 165     }
 166 }