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