Path: blob/master/test/jdk/javax/swing/JProgressBar/8161664/ProgressBarMemoryLeakTest.java
41153 views
/*1* Copyright (c) 2016, 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*/22/*23* @test24* @bug 816166425* @summary Memory leak in com.apple.laf.AquaProgressBarUI: removed progress bar still referenced26* @library ../../regtesthelpers27* @build Util28* @key headful29* @run main/timeout=300/othervm -Xmx16m ProgressBarMemoryLeakTest30*/31import java.awt.EventQueue;32import java.lang.ref.WeakReference;3334import javax.swing.JFrame;35import javax.swing.JPanel;36import javax.swing.JProgressBar;37import javax.swing.UIManager;38import javax.swing.UnsupportedLookAndFeelException;3940public class ProgressBarMemoryLeakTest {4142private static JFrame sFrame;43private static WeakReference<JProgressBar> sProgressBar;4445public static void main(String[] args) throws Exception {46UIManager.LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels();47for ( UIManager.LookAndFeelInfo installedLookAndFeel : installedLookAndFeels ) {48executeTestCase(installedLookAndFeel.getClassName());49}50}5152private static void executeTestCase(String lookAndFeelString) throws Exception{53if (tryLookAndFeel(lookAndFeelString)) {54EventQueue.invokeAndWait( new Runnable() {55@Override56public void run() {57showUI();58}59} );60EventQueue.invokeAndWait( new Runnable() {61@Override62public void run() {63disposeUI();64}65} );66Util.generateOOME();67JProgressBar progressBar = sProgressBar.get();68if ( progressBar != null ) {69throw new RuntimeException( "Progress bar (using L&F: " + lookAndFeelString + ") should have been GC-ed" );70}71}72}7374private static void showUI(){75sFrame = new JFrame();7677JProgressBar progressBar = new JProgressBar();78progressBar.setVisible(false);79progressBar.setIndeterminate(false);80progressBar.setIndeterminate(true);81progressBar.setIndeterminate(false);82progressBar.setValue(10);83progressBar.setString("Progress");8485sFrame.add(progressBar);8687sProgressBar = new WeakReference<>(progressBar);8889sFrame.setSize(200,200);90sFrame.setVisible(true);91}9293private static void disposeUI(){94sFrame.setContentPane(new JPanel());95sFrame.dispose();96sFrame = null;97}9899private static boolean tryLookAndFeel(String lookAndFeelString) throws Exception {100try {101UIManager.setLookAndFeel(lookAndFeelString);102} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {103return false;104}105return true;106}107}108109110