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