1 /* 2 * Copyright (c) 2007, 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 import java.awt.*; 25 import java.awt.image.BufferedImage; 26 27 /* 28 * @test 29 * @summary Check the return value of the getActionCommand method 30 * of the ActionEvent triggered when TrayIcon is double clicked 31 * (single clicked, on Mac) 32 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com) 33 * @library ../../../../lib/testlibrary ../ 34 * @build ExtendedRobot SystemTrayIconHelper 35 * @run main ActionCommand 36 */ 37 38 public class ActionCommand { 39 40 TrayIcon icon; 41 ExtendedRobot robot; 42 43 boolean actionPerformed = false; 44 Object actionLock = new Object(); 45 String actionCommand = null; 46 static boolean isMacOS = false; 47 48 public static void main(String[] args) throws Exception { 49 if (! SystemTray.isSupported()) { 50 System.out.println("SystemTray not supported on the platform under test. " + 51 "Marking the test passed"); 52 } else { 53 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 54 System.err.println("Test can fail if the icon hides to a tray icons pool " + 55 "in Windows 7, which is behavior by default.\n" + 56 "Set \"Right mouse click\" -> \"Customize notification icons\" -> " + 57 "\"Always show all icons and notifications on the taskbar\" true to " + 58 "avoid this problem. Or change behavior only for Java SE tray icon " + 59 "and rerun test."); 60 } else if (System.getProperty("os.name").toLowerCase().startsWith("mac")){ 61 isMacOS = true; 62 } 63 64 new ActionCommand().doTest(); 65 } 66 } 67 68 void doTest() throws Exception { 69 robot = new ExtendedRobot(); 70 71 EventQueue.invokeAndWait(() -> { 72 SystemTray tray = SystemTray.getSystemTray(); 73 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon"); 74 icon.addActionListener((event) -> { 75 actionPerformed = true; 76 actionCommand = event.getActionCommand(); 77 synchronized (actionLock) { 78 try { 79 actionLock.notifyAll(); 80 } catch (Exception e) { 81 } 82 } 83 }); 84 85 if (icon.getActionCommand() != null) 86 throw new RuntimeException("FAIL: getActionCommand did not return null " + 87 "when no action command set " + icon.getActionCommand()); 88 89 icon.setActionCommand("Sample Command"); 90 91 if (! "Sample Command".equals(icon.getActionCommand())) 92 throw new RuntimeException("FAIL: getActionCommand did not return the correct value. " + 93 icon.getActionCommand() + " Expected: Sample Command"); 94 95 try { 96 tray.add(icon); 97 } catch (AWTException e) { 98 throw new RuntimeException(e); 99 } 100 }); 101 102 robot.waitForIdle(); 103 104 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 105 if (iconPosition == null) 106 throw new RuntimeException("Unable to find the icon location!"); 107 108 robot.mouseMove(iconPosition.x, iconPosition.y); 109 robot.waitForIdle(2000); 110 actionPerformed = false; 111 SystemTrayIconHelper.doubleClick(robot); 112 113 if (! actionPerformed) { 114 synchronized (actionLock) { 115 try { 116 actionLock.wait(3000); 117 } catch (Exception e) { 118 } 119 } 120 } 121 if (! actionPerformed) { 122 throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is "+(isMacOS? "" : "double ")+"clicked"); 123 } else if (! "Sample Command".equals(actionCommand)) { 124 throw new RuntimeException("FAIL: ActionEvent.getActionCommand did not return the correct " + 125 "value. Returned: " + actionCommand + " ; Expected: Sample Command"); 126 } 127 128 EventQueue.invokeAndWait(() -> { 129 icon.setActionCommand(null); 130 if (icon.getActionCommand() != null) { 131 throw new RuntimeException("FAIL: ActionCommand set to null. getActionCommand did " + 132 "not return null " + icon.getActionCommand()); 133 } 134 }); 135 136 robot.mouseMove(0, 0); 137 robot.waitForIdle(); 138 robot.mouseMove(iconPosition.x, iconPosition.y); 139 robot.waitForIdle(); 140 141 actionPerformed = false; 142 actionCommand = null; 143 SystemTrayIconHelper.doubleClick(robot); 144 145 if (! actionPerformed) { 146 synchronized (actionLock) { 147 try { 148 actionLock.wait(3000); 149 } catch (Exception e) { 150 } 151 } 152 } 153 if (! actionPerformed) { 154 throw new RuntimeException("FAIL: ActionEvent not triggered when ActionCommand set to " + 155 "null and then TrayIcon is "+(isMacOS? "" : "double ")+ "clicked"); 156 } else if (actionCommand != null) { 157 throw new RuntimeException("FAIL: ActionEvent.getActionCommand did not return null " + 158 "when ActionCommand is set to null " + actionCommand); 159 } 160 161 } 162 }