1 /* 2 * Copyright (c) 2007, 2016, 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.EventQueue; 25 import java.awt.Point; 26 import java.awt.SystemTray; 27 import java.awt.TrayIcon; 28 import java.awt.event.InputEvent; 29 import java.awt.image.BufferedImage; 30 31 /* 32 * @test 33 * @bug 6384991 34 * @summary Check if ActionEvent is triggered by a TrayIcon when 35 * it is double clicked with mouse button 1 on windows 36 * or single clicked with button 3 on Mac OS X 37 * or single clicked with button 1 on rest. 38 * @modules java.desktop/java.awt:open 39 * @library /java/awt/patchlib 40 * @library ../../../../lib/testlibrary ../ 41 * @build java.desktop/java.awt.Helper 42 * @build ExtendedRobot SystemTrayIconHelper 43 * @run main TrayIconMouseTest 44 */ 45 46 public class TrayIconMouseTest { 47 48 TrayIcon icon; 49 ExtendedRobot robot; 50 boolean actionPerformed = false; 51 Object actionLock = new Object(); 52 static boolean isMacOS = false; 53 static boolean isWinOS = false; 54 static boolean isOelOS = false; 55 String caption = "Sample Icon"; 56 int[] buttonTypes = { 57 InputEvent.BUTTON1_MASK, 58 InputEvent.BUTTON2_MASK, 59 InputEvent.BUTTON3_MASK 60 }; 61 String[] buttonNames = { 62 "BUTTON1", 63 "BUTTON2", 64 "BUTTON3" 65 }; 66 67 public static void main(String[] args) throws Exception { 68 if (!SystemTray.isSupported()) { 69 System.out.println("SystemTray not supported on the platform " 70 + "under test. Marking the test passed"); 71 } else { 72 String osName = System.getProperty("os.name").toLowerCase(); 73 if (osName.startsWith("mac")) { 74 isMacOS = true; 75 } else if (osName.startsWith("win")) { 76 isWinOS = true; 77 } else { 78 isOelOS = SystemTrayIconHelper.isOel7(); 79 } 80 new TrayIconMouseTest().doTest(); 81 } 82 } 83 84 TrayIconMouseTest() throws Exception { 85 robot = new ExtendedRobot(); 86 EventQueue.invokeAndWait(this::initializeGUI); 87 } 88 89 void initializeGUI() { 90 SystemTray tray = SystemTray.getSystemTray(); 91 icon = new TrayIcon( 92 new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption); 93 icon.addActionListener(event -> { 94 actionPerformed = true; 95 synchronized (actionLock) { 96 try { 97 actionLock.notifyAll(); 98 } catch (Exception e) { 99 } 100 } 101 }); 102 try { 103 tray.add(icon); 104 } catch (Exception e) { 105 throw new RuntimeException(e); 106 } 107 } 108 109 private void doTest() throws Exception { 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(); 116 117 for (int i = 0; i < buttonTypes.length; i++) { 118 actionPerformed = false; 119 robot.click(buttonTypes[i]); 120 robot.waitForIdle(); 121 delayIfRequired(); 122 123 if (isMacOS && i == 2 && !actionPerformed) { 124 throw new RuntimeException("FAIL: ActionEvent NOT triggered " 125 + "when " + buttonNames[i] + " is single clicked on Mac"); 126 } else if (isWinOS && actionPerformed) { 127 throw new RuntimeException("FAIL: ActionEvent triggered " 128 + "when " + buttonNames[i] + " is single clicked"); 129 } else if (!isMacOS && !isWinOS && i == 0 && !actionPerformed) { 130 throw new RuntimeException("FAIL: ActionEvent NOT triggered " 131 + "when " + buttonNames[i] + " is single clicked"); 132 } 133 } 134 135 if (!isMacOS && !isOelOS) { 136 for (int i = 0; i < buttonTypes.length; i++) { 137 for (int j = 0; j < buttonTypes.length; j++) { 138 if (j != i) { 139 actionPerformed = false; 140 robot.mousePress(buttonTypes[i]); 141 robot.mousePress(buttonTypes[j]); 142 robot.mouseRelease(buttonTypes[j]); 143 robot.mouseRelease(buttonTypes[i]); 144 robot.waitForIdle(); 145 delayIfRequired(); 146 147 if (isWinOS) { 148 if (actionPerformed) { 149 throw new RuntimeException( 150 "FAIL: ActionEvent triggered when " 151 + buttonNames[i] + " & " + buttonNames[j] 152 + " is clicked and released"); 153 } 154 155 } else if ((i == 0 || j == 0) && !actionPerformed) { 156 throw new RuntimeException("FAIL: ActionEvent is " 157 + "NOT triggered when " + buttonNames[i] + " & " 158 + buttonNames[j] + " is pressed & released"); 159 } 160 } 161 } 162 } 163 164 for (int i = 0; i < buttonTypes.length; i++) { 165 actionPerformed = false; 166 robot.mousePress(buttonTypes[i]); 167 robot.mouseRelease(buttonTypes[i]); 168 robot.delay(50); 169 robot.mousePress(buttonTypes[i]); 170 robot.mouseRelease(buttonTypes[i]); 171 robot.waitForIdle(); 172 delayIfRequired(); 173 174 if (i == 0) { 175 if (!actionPerformed) { 176 throw new RuntimeException("FAIL: ActionEvent not " 177 + "triggered when " + buttonNames[i] 178 + " is double clicked"); 179 } 180 } else if (actionPerformed) { 181 throw new RuntimeException("FAIL: ActionEvent " 182 + "triggered when " + buttonNames[i] 183 + " is double clicked"); 184 } 185 } 186 } 187 } 188 189 public void delayIfRequired() { 190 if (!actionPerformed) { 191 synchronized (actionLock) { 192 try { 193 actionLock.wait(500); 194 } catch (Exception e) { 195 } 196 } 197 } 198 } 199 }