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