1 /*
   2  * Copyright (c) 2014, 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 /*
  26   test
  27   @bug 6299866
  28   @summary Tests that no NPE is thrown when the tray icon is disposed from the
  29   handler of action event caused by clicking on this icon.
  30   @library ../../regtesthelpers
  31   @build Sysout
  32   @author artem.ananiev: area=awt.tray
  33   @run applet/manual=yesno DisposeInActionEventTest.html
  34 */
  35 
  36 import java.applet.*;
  37 
  38 import java.awt.*;
  39 import java.awt.image.*;
  40 
  41 import jdk.testlibrary.OSInfo;
  42 import test.java.awt.regtesthelpers.Sysout;
  43 
  44 public class DisposeInActionEventTest extends Applet {
  45     private boolean traySupported;
  46 
  47     private SystemTray systemTray;
  48     private TrayIcon trayIcon;
  49 
  50     public void init() {
  51         this.setLayout(new BorderLayout());
  52 
  53         String[] instructions;
  54         traySupported = SystemTray.isSupported();
  55         if (!traySupported) {
  56             instructions = new String[]{
  57                     "The test cannot be run because SystemTray is not supported.",
  58                     "Simply press PASS button."
  59             };
  60         } else {
  61             String clickInstruction;
  62             if (OSInfo.getOSType().equals(OSInfo.OSType.MACOSX)) {
  63                 clickInstruction = "right";
  64             } else {
  65                 clickInstruction = "left";
  66             }
  67             instructions = new String[]{
  68                     "When the test starts, it adds the icon to the tray aread. If you",
  69                     "  don't see a tray icon, please, make sure that the tray area",
  70                     "  (also called Taskbar Status Area on MS Windows, Notification",
  71                     "  Area on Gnome or System Tray on KDE) is visible.",
  72                     "Double-click with " + clickInstruction + " button on the tray icon to trigger the",
  73                     "  action event. Brief information about action events is printed",
  74                     "  below. After each action event the tray icon is removed from",
  75                     "  the tray and then added back in a second.",
  76                     "The test performs some automatic checks when removing the icon. If",
  77                     "  something is wrong the corresponding message is displayed below.",
  78                     "  Repeat double-clicks several times. If no 'Test FAILED' messages",
  79                     "  are printed, press PASS button else FAIL button."
  80             };
  81         }
  82         Sysout.createDialogWithInstructions(instructions);
  83     }
  84 
  85     @Override
  86     public void start() {
  87         setSize(200, 200);
  88         setVisible(true);
  89         validate();
  90 
  91         if (!traySupported) return;
  92 
  93         System.setProperty("sun.awt.exception.handler", "DisposeInActionEventTest$EDTExceptionHandler");
  94 
  95         BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
  96         Graphics g = img.createGraphics();
  97         g.setColor(Color.WHITE);
  98         g.fillRect(0, 0, 32, 32);
  99         g.setColor(Color.RED);
 100         g.fillRect(6, 6, 20, 20);
 101         g.dispose();
 102 
 103         systemTray = SystemTray.getSystemTray();
 104         trayIcon = new TrayIcon(img);
 105         trayIcon.setImageAutoSize(true);
 106         trayIcon.addActionListener(ev -> {
 107             Sysout.println(ev.toString());
 108             systemTray.remove(trayIcon);
 109             new Thread(() -> {
 110                 try {
 111                     Thread.sleep(1000);
 112                     systemTray.add(trayIcon);
 113                 } catch (AWTException | InterruptedException e) {
 114                     Sysout.println(e.toString());
 115                     Sysout.println("!!! The test coudn't be performed !!!");
 116                 }
 117             }
 118             ).start();
 119         }
 120         );
 121 
 122         try {
 123             systemTray.add(trayIcon);
 124         } catch (AWTException e) {
 125             Sysout.println(e.toString());
 126             Sysout.println("!!! The test coudn't be performed !!!");
 127         }
 128     }
 129 }