Path: blob/master/test/jdk/javax/swing/JComboBox/TestMemLeakComboBox.java
41149 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @key headful25* @bug 654243926* @summary Verifies memory leak in BasicComboBoxUI and MetalComboBoxButton27* @run main TestMemLeakComboBox28*/29import java.awt.Graphics;30import java.awt.Component;31import java.awt.Container;32import java.awt.Dimension;33import java.awt.FlowLayout;34import javax.swing.JFrame;35import javax.swing.JPanel;36import javax.swing.JComboBox;37import javax.swing.CellRendererPane;38import javax.swing.SwingUtilities;39import javax.swing.UIManager;40import javax.swing.UnsupportedLookAndFeelException;4142public class TestMemLeakComboBox {43private static JFrame frame;44private static String failed = null;4546private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {47try {48UIManager.setLookAndFeel(laf.getClassName());49} catch (UnsupportedLookAndFeelException ignored) {50System.out.println("Unsupported L&F: " + laf.getClassName());51} catch (ClassNotFoundException | InstantiationException52| IllegalAccessException e) {53throw new RuntimeException(e);54}55}5657private static class MyPanel extends JPanel {58public void paint(Graphics g) {59super.paint(g);60for (Component child : getComponents()) {61verifyChild(child);62}63}6465private void verifyChild(Component c) {66if (c instanceof JComboBox) {67for (Component child : ((Container)c).getComponents()) {68if (child instanceof CellRendererPane &&69((CellRendererPane)child).getComponentCount() > 0) {70failed = new String("CellRendererPane still has children for: " + c);71}72}73}74}75}7677public static void main(String[] args) throws Exception {78for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {79System.out.println("Testing l&f : " + laf.getClassName());80SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));81test();82if (failed != null) {83throw new RuntimeException(failed);84}85}86}8788private static void test() throws Exception {89try {90SwingUtilities.invokeAndWait(() -> {91frame = new JFrame();92JPanel panel = new MyPanel();93panel.setPreferredSize(new Dimension(100, 100));94panel.setLayout(new FlowLayout());95panel.add(new JComboBox(new String[]{"one", "two", "three"}));9697frame.add(panel);98frame.pack();99frame.setVisible(true);100});101} finally {102if (frame != null) {103SwingUtilities.invokeAndWait(() -> frame.dispose());104}105}106}107}108109110