1 /*
   2  * Copyright (c) 2018, 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 import java.awt.TrayIcon;
  24 import java.awt.SystemTray;
  25 import java.awt.EventQueue;
  26 import java.awt.Point;
  27 import java.awt.AWTException;
  28 import java.awt.event.MouseEvent;
  29 import java.awt.event.ActionEvent;
  30 import java.awt.event.ActionListener;
  31 import java.awt.event.InputEvent;
  32 import java.awt.event.MouseAdapter;
  33 import java.awt.image.BufferedImage;
  34 
  35 /*
  36  * @test
  37  * @bug 6271624 8195991
  38  * @key headful
  39  * @summary Right clicking on TrayIcon shouldn't trigger ActionEvent when balloon is displayed.
  40  * @modules java.desktop/java.awt:open
  41  * @library /java/awt/patchlib
  42  * @library ../../../../lib/client ../
  43  * @build java.desktop/java.awt.Helper
  44  * @build ExtendedRobot SystemTrayIconHelper
  45  * @run main RightClickWhenBalloonDisplayed
  46  */
  47 
  48 public class RightClickWhenBalloonDisplayed {
  49 
  50     TrayIcon icon;
  51     ExtendedRobot robot;
  52     int actionPerformedCount = -1;
  53 
  54     public static void main(String[] args) throws Exception {
  55         if (!SystemTray.isSupported()) {
  56             System.out.println("SystemTray not supported on the platform under test. " +
  57                     "Marking the test passed");
  58         } else {
  59             if (System.getProperty("os.name").toLowerCase().startsWith("win"))
  60                 System.err.println("Test can fail if the icon hides to a tray icons pool " +
  61                         "in Windows 7/10, which is behavior by default.\n" +
  62                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
  63                         "\"Always show all icons and notifications on the taskbar\" true " +
  64                         "to avoid this problem. Or change behavior only for Java SE " +
  65                         "tray icon.");
  66             new RightClickWhenBalloonDisplayed().doTest();
  67         }
  68     }
  69 
  70     RightClickWhenBalloonDisplayed() throws Exception {
  71         robot = new ExtendedRobot();
  72         EventQueue.invokeAndWait(this::initializeGUI);
  73         robot.waitForIdle(1000);
  74     }
  75 
  76     private void initializeGUI() {
  77         SystemTray tray = SystemTray.getSystemTray();
  78         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
  79         icon.addMouseListener(new MouseAdapter() {
  80             public void mousePressed(MouseEvent event) {
  81                 icon.displayMessage("Sample Icon", "This is a test message for the tray icon", TrayIcon.MessageType.INFO);
  82             }
  83         });
  84 
  85         try {
  86             tray.add(icon);
  87         } catch (AWTException e) {
  88             throw new RuntimeException(e);
  89         }
  90 
  91         icon.getActionCommand();
  92         icon.addActionListener(new ActionListener() {
  93             @Override
  94             public void actionPerformed(ActionEvent e) {
  95                 actionPerformedCount++;
  96             }
  97         });
  98     }
  99 
 100     void doTest() throws Exception {
 101 
 102         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 103         if (iconPosition == null)
 104             throw new RuntimeException("Unable to find the icon location!");
 105 
 106         // Do left click to start displaying the message
 107         robot.delay(50);
 108         robot.mouseMove(iconPosition.x, iconPosition.y);
 109         robot.waitForIdle();
 110         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 111         robot.delay(50);
 112         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 113         robot.delay(1000);
 114 
 115         // Do right click
 116         robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
 117         robot.delay(50);
 118         robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
 119         robot.delay(50);
 120 
 121         if (actionPerformedCount > 0)
 122             throw new RuntimeException("FAIL: ActionEvent triggered when " +
 123                     "the tray icon was right clicked and at the same time the tray icon message is displayed");
 124     }
 125 }