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