1 /* 2 * Copyright (c) 2017, 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 * @summary Check if a action performed event is received when TrayIcon display 38 * message is clicked on. 39 * @author Shashidhara Veerabhadraiah (shashidhara.veerabhadraiah@oracle.com) 40 * @modules java.desktop/java.awt:open 41 * @library /java/awt/patchlib 42 * @library ../../../../lib/testlibrary ../ 43 * @build java.desktop/java.awt.Helper 44 * @build ExtendedRobot SystemTrayIconHelper 45 * @run main TrayIconPopupClickTest 46 */ 47 48 public class TrayIconPopupClickTest { 49 50 TrayIcon icon; 51 ExtendedRobot robot; 52 boolean actionPerformed = false; 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 TrayIconPopupClickTest().doTest(); 67 } 68 } 69 70 TrayIconPopupClickTest() 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 actionPerformed = true; 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 robot.mouseMove(iconPosition.x, iconPosition.y); 107 robot.waitForIdle(); 108 robot.mousePress(InputEvent.BUTTON1_MASK); 109 robot.delay(50); 110 robot.mouseRelease(InputEvent.BUTTON1_MASK); 111 robot.delay(50); 112 113 robot.mouseMove(iconPosition.x, iconPosition.y + 10); 114 robot.waitForIdle(); 115 robot.mousePress(InputEvent.BUTTON1_MASK); 116 robot.delay(50); 117 robot.mouseRelease(InputEvent.BUTTON1_MASK); 118 robot.delay(50); 119 120 if (!actionPerformed) 121 throw new RuntimeException("FAIL: ActionEvent not triggered when " + 122 "tray icon message was clicked on"); 123 } 124 }