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