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 } else if( System.getProperty("os.name").toLowerCase() 63 .contains("linux") && System.getProperty("os.version") 64 .toLowerCase().contains("el7")) { 65 System.out.println("OEL 7 doesn't support double click in " + 66 "systray. Skipped"); 67 return; 68 } 69 new ActionCommand().doTest(); 70 } 71 } 72 73 void doTest() throws Exception { 74 robot = new ExtendedRobot(); 75 76 EventQueue.invokeAndWait(() -> { 77 SystemTray tray = SystemTray.getSystemTray(); 78 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon"); 79 icon.addActionListener((event) -> { 80 actionPerformed = true; 81 actionCommand = event.getActionCommand(); 82 synchronized (actionLock) { 83 try { 84 actionLock.notifyAll(); 85 } catch (Exception e) { 86 } 87 } 88 }); 89 90 if (icon.getActionCommand() != null) 91 throw new RuntimeException("FAIL: getActionCommand did not return null " + 92 "when no action command set " + icon.getActionCommand()); 93 94 icon.setActionCommand("Sample Command"); 95 96 if (! "Sample Command".equals(icon.getActionCommand())) 97 throw new RuntimeException("FAIL: getActionCommand did not return the correct value. " + 98 icon.getActionCommand() + " Expected: Sample Command"); 99 100 try { 101 tray.add(icon); 102 } catch (AWTException e) { 103 throw new RuntimeException(e); 104 } 105 }); 106 107 robot.waitForIdle(); 108 109 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 110 if (iconPosition == null) 111 throw new RuntimeException("Unable to find the icon location!"); 112 113 robot.mouseMove(iconPosition.x, iconPosition.y); 114 robot.waitForIdle(2000); 115 actionPerformed = false; 116 SystemTrayIconHelper.doubleClick(robot); 117 118 if (! actionPerformed) { 119 synchronized (actionLock) { 120 try { 121 actionLock.wait(3000); 122 } catch (Exception e) { 123 } 124 } 125 } 126 if (! actionPerformed) { 127 throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is "+(isMacOS? "" : "double ")+"clicked"); 128 } else if (! "Sample Command".equals(actionCommand)) { 129 throw new RuntimeException("FAIL: ActionEvent.getActionCommand did not return the correct " + 130 "value. Returned: " + actionCommand + " ; Expected: Sample Command"); 131 } 132 133 EventQueue.invokeAndWait(() -> { 134 icon.setActionCommand(null); 135 if (icon.getActionCommand() != null) { 136 throw new RuntimeException("FAIL: ActionCommand set to null. getActionCommand did " + 137 "not return null " + icon.getActionCommand()); 138 } 139 }); 140 141 robot.mouseMove(0, 0); 142 robot.waitForIdle(); 143 robot.mouseMove(iconPosition.x, iconPosition.y); 144 robot.waitForIdle(); 145 146 actionPerformed = false; 147 actionCommand = null; 148 SystemTrayIconHelper.doubleClick(robot); 149 150 if (! actionPerformed) { 151 synchronized (actionLock) { 152 try { 153 actionLock.wait(3000); 154 } catch (Exception e) { 155 } 156 } 157 } 158 if (! actionPerformed) { 159 throw new RuntimeException("FAIL: ActionEvent not triggered when ActionCommand set to " + 160 "null and then TrayIcon is "+(isMacOS? "" : "double ")+ "clicked"); 161 } else if (actionCommand != null) { 162 throw new RuntimeException("FAIL: ActionEvent.getActionCommand did not return null " + 163 "when ActionCommand is set to null " + actionCommand); 164 } 165 166 } 167 }