Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JProgressBar/8161664/ProgressBarMemoryLeakTest.java
38918 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* @run main/timeout=300/othervm -Xmx16m ProgressBarMemoryLeakTest29*/30import java.awt.EventQueue;31import java.lang.ref.WeakReference;3233import javax.swing.JFrame;34import javax.swing.JPanel;35import javax.swing.JProgressBar;36import javax.swing.UIManager;37import javax.swing.UnsupportedLookAndFeelException;3839public class ProgressBarMemoryLeakTest {4041private static JFrame sFrame;42private static WeakReference<JProgressBar> sProgressBar;4344public static void main(String[] args) throws Exception {45UIManager.LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels();46for ( UIManager.LookAndFeelInfo installedLookAndFeel : installedLookAndFeels ) {47executeTestCase(installedLookAndFeel.getClassName());48}49}5051private static void executeTestCase(String lookAndFeelString) throws Exception{52if (tryLookAndFeel(lookAndFeelString)) {53EventQueue.invokeAndWait( new Runnable() {54@Override55public void run() {56showUI();57}58} );59EventQueue.invokeAndWait( new Runnable() {60@Override61public void run() {62disposeUI();63}64} );65Util.generateOOME();66JProgressBar progressBar = sProgressBar.get();67if ( progressBar != null ) {68throw new RuntimeException( "Progress bar (using L&F: " + lookAndFeelString + ") should have been GC-ed" );69}70}71}7273private static void showUI(){74sFrame = new JFrame();7576JProgressBar progressBar = new JProgressBar();77progressBar.setVisible(false);78progressBar.setIndeterminate(false);79progressBar.setIndeterminate(true);80progressBar.setIndeterminate(false);81progressBar.setValue(10);82progressBar.setString("Progress");8384sFrame.add(progressBar);8586sProgressBar = new WeakReference<>(progressBar);8788sFrame.setSize(200,200);89sFrame.setVisible(true);90}9192private static void disposeUI(){93sFrame.setContentPane(new JPanel());94sFrame.dispose();95sFrame = null;96}9798private static boolean tryLookAndFeel(String lookAndFeelString) throws Exception {99try {100UIManager.setLookAndFeel(lookAndFeelString);101} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {102return false;103}104return true;105}106}107108109