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