1 /* 2 * Copyright (c) 2013, 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 /* 25 @test 26 @key headful 27 @bug 8007220 28 @summary Reference to the popup leaks after the TrayIcon is removed 29 @author Petr Pchelko 30 @library ../../../../lib/testlibrary/ 31 @build ExtendedRobot 32 @run main/othervm -Xmx50m PopupMenuLeakTest 33 */ 34 35 import java.awt.*; 36 import javax.swing.SwingUtilities; 37 38 import java.awt.image.BufferedImage; 39 import java.lang.ref.WeakReference; 40 import java.util.ArrayList; 41 import java.util.concurrent.atomic.AtomicReference; 42 43 public class PopupMenuLeakTest { 44 45 static final AtomicReference<WeakReference<TrayIcon>> iconWeakReference = new AtomicReference<>(); 46 static final AtomicReference<WeakReference<PopupMenu>> popupWeakReference = new AtomicReference<>(); 47 static ExtendedRobot robot; 48 49 public static void main(String[] args) throws Exception { 50 robot = new ExtendedRobot(); 51 SwingUtilities.invokeAndWait(PopupMenuLeakTest::createSystemTrayIcon); 52 sleep(); 53 // To make the test automatic we explicitly call addNotify on a popup to create the peer 54 SwingUtilities.invokeAndWait(PopupMenuLeakTest::addNotifyPopup); 55 sleep(); 56 SwingUtilities.invokeAndWait(PopupMenuLeakTest::removeIcon); 57 sleep(); 58 assertCollected(popupWeakReference.get(), "Failed, reference to popup not collected"); 59 assertCollected(iconWeakReference.get(), "Failed, reference to tray icon not collected"); 60 } 61 62 private static void addNotifyPopup() { 63 PopupMenu menu = popupWeakReference.get().get(); 64 if (menu == null) { 65 throw new RuntimeException("Failed: popup collected too early"); 66 } 67 menu.addNotify(); 68 } 69 70 private static void removeIcon() { 71 TrayIcon icon = iconWeakReference.get().get(); 72 if (icon == null) { 73 throw new RuntimeException("Failed: TrayIcon collected too early"); 74 } 75 SystemTray.getSystemTray().remove(icon); 76 } 77 78 private static void assertCollected(WeakReference<?> reference, String message) { 79 java.util.List<byte[]> bytes = new ArrayList<>(); 80 for (int i = 0; i < 5; i ++) { 81 try { 82 while (true) { 83 bytes.add(new byte[1024]); 84 } 85 } catch (OutOfMemoryError err) { 86 bytes = new ArrayList<>(); 87 } 88 } 89 if (reference.get() != null) { 90 throw new RuntimeException(message); 91 } 92 } 93 94 private static void createSystemTrayIcon() { 95 final TrayIcon trayIcon = new TrayIcon(createTrayIconImage()); 96 trayIcon.setImageAutoSize(true); 97 98 try { 99 // Add tray icon to system tray *before* adding popup menu to demonstrate buggy behaviour 100 trayIcon.setPopupMenu(createTrayIconPopupMenu()); 101 SystemTray.getSystemTray().add(trayIcon); 102 iconWeakReference.set(new WeakReference<>(trayIcon)); 103 popupWeakReference.set(new WeakReference<>(trayIcon.getPopupMenu())); 104 } catch (final AWTException awte) { 105 awte.printStackTrace(); 106 } 107 } 108 109 private static Image createTrayIconImage() { 110 /** 111 * Create a small image of a red circle to use as the icon for the tray icon 112 */ 113 int trayIconImageSize = 32; 114 final BufferedImage trayImage = new BufferedImage(trayIconImageSize, trayIconImageSize, BufferedImage.TYPE_INT_ARGB); 115 final Graphics2D trayImageGraphics = (Graphics2D) trayImage.getGraphics(); 116 117 trayImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 118 119 trayImageGraphics.setColor(new Color(255, 255, 255, 0)); 120 trayImageGraphics.fillRect(0, 0, trayImage.getWidth(), trayImage.getHeight()); 121 122 trayImageGraphics.setColor(Color.red); 123 124 int trayIconImageInset = 4; 125 trayImageGraphics.fillOval(trayIconImageInset, 126 trayIconImageInset, 127 trayImage.getWidth() - 2 * trayIconImageInset, 128 trayImage.getHeight() - 2 * trayIconImageInset); 129 130 trayImageGraphics.setColor(Color.darkGray); 131 132 trayImageGraphics.drawOval(trayIconImageInset, 133 trayIconImageInset, 134 trayImage.getWidth() - 2 * trayIconImageInset, 135 trayImage.getHeight() - 2 * trayIconImageInset); 136 137 return trayImage; 138 } 139 140 private static PopupMenu createTrayIconPopupMenu() { 141 final PopupMenu trayIconPopupMenu = new PopupMenu(); 142 final MenuItem popupMenuItem = new MenuItem("TEST!"); 143 trayIconPopupMenu.add(popupMenuItem); 144 return trayIconPopupMenu; 145 } 146 147 private static void sleep() { 148 robot.waitForIdle(100); 149 } 150 }