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.EventQueue;
  25 import java.awt.FlowLayout;
  26 
  27 import javax.swing.*;
  28 import javax.swing.UIManager.LookAndFeelInfo;
  29 
  30 import static javax.swing.UIManager.getInstalledLookAndFeels;
  31 
  32 /**
  33  * @test
  34  * @key headful
  35  * @bug 8134947
  36  * @author Sergey Bylokhov
  37  * @run main/timeout=300/othervm -Xmx12m -XX:+HeapDumpOnOutOfMemoryError UnninstallUIMemoryLeaks
  38  */
  39 public final class UnninstallUIMemoryLeaks {
  40 
  41     private static JFrame frame;
  42 
  43     public static void main(final String[] args) throws Exception {
  44         try {
  45             createGUI();
  46             for (final LookAndFeelInfo laf : getInstalledLookAndFeels()) {
  47                 final String name = laf.getName();
  48                 if (name.contains("OS X") || name.contains("Metal")) {
  49                     SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
  50                     SwingUtilities.invokeAndWait(() -> {
  51                         for (int i = 0; i < 4000; ++i) {
  52                             SwingUtilities.updateComponentTreeUI(frame);
  53                         }
  54                     });
  55                 }
  56             }
  57         } finally {
  58             if (frame != null) { EventQueue.invokeAndWait(() -> frame.dispose()); }
  59         }
  60     }
  61 
  62     private static void createGUI() throws Exception {
  63         EventQueue.invokeAndWait(() -> {
  64             frame = new JFrame();
  65             frame.setLayout(new FlowLayout());
  66 
  67             frame.add(new JButton("JButton"));
  68             frame.add(new JCheckBox("JCheckBox"));
  69             frame.add(new JComboBox<>());
  70             frame.add(new JEditorPane());
  71             frame.add(new JFormattedTextField("JFormattedTextField"));
  72             frame.add(new JLabel("label"));
  73             frame.add(new JPanel());
  74             frame.add(new JPasswordField("JPasswordField"));
  75             frame.add(new JProgressBar());
  76             frame.add(new JRadioButton("JRadioButton"));
  77             frame.add(new JScrollBar());
  78             frame.add(new JScrollPane());
  79             frame.add(new JSeparator());
  80             frame.add(new JSlider());
  81             frame.add(new JSpinner());
  82             frame.add(new JSplitPane());
  83             frame.add(new JTabbedPane());
  84             frame.add(new JTable());
  85             frame.add(new JTextArea("JTextArea"));
  86             frame.add(new JTextField("JTextField"));
  87             frame.add(new JTextPane());
  88             frame.add(new JToggleButton());
  89             frame.add(new JToolBar());
  90             frame.add(new JToolTip());
  91             frame.add(new JTree());
  92             frame.add(new JViewport());
  93 
  94             final JMenuBar bar = new JMenuBar();
  95             final JMenu menu1 = new JMenu("menu1");
  96             final JMenu menu2 = new JMenu("menu2");
  97             menu1.add(new JMenuItem("menuitem"));
  98             menu2.add(new JCheckBoxMenuItem("JCheckBoxMenuItem"));
  99             menu2.add(new JRadioButtonMenuItem("JRadioButtonMenuItem"));
 100             bar.add(menu1);
 101             bar.add(menu2);
 102             frame.setJMenuBar(bar);
 103 
 104             final String[] data = {"one", "two", "three", "four"};
 105             final JList<String> list = new JList<>(data);
 106             frame.add(list);
 107 
 108             final JDesktopPane pane = new JDesktopPane();
 109             final JInternalFrame internalFrame = new JInternalFrame();
 110             internalFrame.setBounds(10, 10, 130, 130);
 111             internalFrame.setVisible(true);
 112             pane.add(internalFrame);
 113             pane.setSize(150, 150);
 114 
 115             frame.add(pane);
 116             frame.pack();
 117             frame.setSize(600, 600);
 118             frame.setLocationRelativeTo(null);
 119             // Commented to prevent a reference from RepaintManager
 120             // frame.setVisible(true);
 121         });
 122     }
 123 
 124     private static void setLookAndFeel(final LookAndFeelInfo laf) {
 125         try {
 126             UIManager.setLookAndFeel(laf.getClassName());
 127             System.out.println("LookAndFeel: " + laf.getClassName());
 128         } catch (ClassNotFoundException | InstantiationException |
 129                 UnsupportedLookAndFeelException | IllegalAccessException e) {
 130             throw new RuntimeException(e);
 131         }
 132     }
 133 }