< prev index next >

test/java/awt/TrayIcon/PopupMenuLeakTest/PopupMenuLeakTest.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 21,39 **** * questions. */ /* @test ! @bug 8007220 ! @summary Reference to the popup leaks after the TrayIcon is removed ! @author Petr Pchelko @library ../../../../lib/testlibrary/ @build ExtendedRobot @run main/othervm -Xmx50m PopupMenuLeakTest */ ! import java.awt.*; import javax.swing.SwingUtilities; import java.awt.image.BufferedImage; import java.lang.ref.WeakReference; import java.util.ArrayList; --- 21,48 ---- * questions. */ /* @test ! @key headful ! @bug 8007220 8039081 ! @summary Reference to the popup leaks after the TrayIcon is removed. ! @requires os.family != "windows" @library ../../../../lib/testlibrary/ @build ExtendedRobot @run main/othervm -Xmx50m PopupMenuLeakTest */ ! import java.awt.AWTException; ! import java.awt.Color; ! import java.awt.Graphics2D; ! import java.awt.Image; ! import java.awt.MenuItem; ! import java.awt.PopupMenu; ! import java.awt.RenderingHints; ! import java.awt.SystemTray; ! import java.awt.TrayIcon; import javax.swing.SwingUtilities; import java.awt.image.BufferedImage; import java.lang.ref.WeakReference; import java.util.ArrayList;
*** 42,63 **** public class PopupMenuLeakTest { static final AtomicReference<WeakReference<TrayIcon>> iconWeakReference = new AtomicReference<>(); static final AtomicReference<WeakReference<PopupMenu>> popupWeakReference = new AtomicReference<>(); static ExtendedRobot robot; - public static void main(String[] args) throws Exception { robot = new ExtendedRobot(); SwingUtilities.invokeAndWait(PopupMenuLeakTest::createSystemTrayIcon); sleep(); // To make the test automatic we explicitly call addNotify on a popup to create the peer SwingUtilities.invokeAndWait(PopupMenuLeakTest::addNotifyPopup); sleep(); SwingUtilities.invokeAndWait(PopupMenuLeakTest::removeIcon); sleep(); - assertCollected(popupWeakReference.get(), "Failed, reference to popup not collected"); assertCollected(iconWeakReference.get(), "Failed, reference to tray icon not collected"); } private static void addNotifyPopup() { PopupMenu menu = popupWeakReference.get().get(); if (menu == null) { --- 51,71 ---- public class PopupMenuLeakTest { static final AtomicReference<WeakReference<TrayIcon>> iconWeakReference = new AtomicReference<>(); static final AtomicReference<WeakReference<PopupMenu>> popupWeakReference = new AtomicReference<>(); static ExtendedRobot robot; public static void main(String[] args) throws Exception { robot = new ExtendedRobot(); SwingUtilities.invokeAndWait(PopupMenuLeakTest::createSystemTrayIcon); sleep(); // To make the test automatic we explicitly call addNotify on a popup to create the peer SwingUtilities.invokeAndWait(PopupMenuLeakTest::addNotifyPopup); sleep(); SwingUtilities.invokeAndWait(PopupMenuLeakTest::removeIcon); sleep(); assertCollected(iconWeakReference.get(), "Failed, reference to tray icon not collected"); + assertCollected(popupWeakReference.get(), "Failed, reference to popup not collected"); } private static void addNotifyPopup() { PopupMenu menu = popupWeakReference.get().get(); if (menu == null) {
*** 75,97 **** } private static void assertCollected(WeakReference<?> reference, String message) { java.util.List<byte[]> bytes = new ArrayList<>(); for (int i = 0; i < 5; i ++) { try { while (true) { ! bytes.add(new byte[1024]); } } catch (OutOfMemoryError err) { ! bytes = new ArrayList<>(); } } if (reference.get() != null) { throw new RuntimeException(message); } } private static void createSystemTrayIcon() { final TrayIcon trayIcon = new TrayIcon(createTrayIconImage()); trayIcon.setImageAutoSize(true); try { --- 83,117 ---- } private static void assertCollected(WeakReference<?> reference, String message) { java.util.List<byte[]> bytes = new ArrayList<>(); for (int i = 0; i < 5; i ++) { + if (reference.get() == null) { + // reference is collected, avoid OOMs. + break; + } try { while (true) { ! bytes.add(new byte[4096]); } } catch (OutOfMemoryError err) { ! bytes.clear(); ! causeGC(); } } if (reference.get() != null) { throw new RuntimeException(message); } } + private static void causeGC() { + System.gc(); + System.runFinalization(); + robot.delay(1000); + } + + private static void createSystemTrayIcon() { final TrayIcon trayIcon = new TrayIcon(createTrayIconImage()); trayIcon.setImageAutoSize(true); try {
< prev index next >