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.event.InputEvent; 26 import java.awt.event.KeyEvent; 27 import java.awt.event.MouseAdapter; 28 import java.awt.event.MouseEvent; 29 import java.awt.image.BufferedImage; 30 31 32 /* 33 * @test 34 * @summary Check if MouseEvent has the proper modifiers when 35 * TrayIcon is clicked pressing the modifier keys 36 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com) 37 * @library ../../../../lib/testlibrary ../ 38 * @build ExtendedRobot SystemTrayIconHelper 39 * @run main TrayIconEventModifiersTest 40 */ 41 42 public class TrayIconEventModifiersTest { 43 44 Image image; 45 46 TrayIcon icon; 47 ExtendedRobot robot; 48 49 Object mouseLock = new Object(); 50 51 String caption = "Sample Icon"; 52 boolean mousePressed = false; 53 boolean mouseReleased = false; 54 boolean mouseClicked = false; 55 int modifiers, releaseModifiers, clickModifiers; 56 57 int[] buttonTypes = { 58 InputEvent.BUTTON1_MASK, 59 InputEvent.BUTTON2_MASK, 60 InputEvent.BUTTON3_MASK 61 }; 62 63 String[] buttonNames = { 64 "BUTTON1", 65 "BUTTON2", 66 "BUTTON3" 67 }; 68 69 int[] buttonMasks = { 70 InputEvent.BUTTON1_DOWN_MASK, 71 InputEvent.BUTTON2_DOWN_MASK, 72 InputEvent.BUTTON3_DOWN_MASK 73 }; 74 75 static int[] keyTypes = { 76 KeyEvent.VK_SHIFT, 77 KeyEvent.VK_CONTROL 78 }; 79 80 static String[] keyNames = { 81 "SHIFT", 82 "CONTROL" 83 }; 84 85 static int[] keyMasks = { 86 KeyEvent.SHIFT_DOWN_MASK, 87 KeyEvent.CTRL_DOWN_MASK 88 }; 89 90 public static void main(String[] args) throws Exception { 91 if (! SystemTray.isSupported()) { 92 System.out.println("SystemTray not supported on the platform under test. " + 93 "Marking the test passed"); 94 } else { 95 if (System.getProperty("os.name").toLowerCase().startsWith("win")) 96 System.err.println("Test can fail if the icon hides to a tray icons pool" + 97 "in Windows 7, which is behavior by default.\n" + 98 "Set \"Right mouse click\" -> \"Customize notification icons\" -> " + 99 "\"Always show all icons and notifications on the taskbar\" true " + 100 "to avoid this problem. Or change behavior only for Java SE tray " + 101 "icon and rerun test."); 102 103 System.out.println(System.getProperty("os.arch")); 104 if (System.getProperty("os.name").indexOf("Sun") != -1 && 105 System.getProperty("os.arch").indexOf("sparc") != -1) { 106 keyTypes = new int[]{ 107 KeyEvent.VK_SHIFT, 108 KeyEvent.VK_CONTROL, 109 KeyEvent.VK_META 110 }; 111 112 keyNames = new String[]{ 113 "SHIFT", 114 "CONTROL", 115 "META" 116 }; 117 keyMasks = new int[]{ 118 KeyEvent.SHIFT_DOWN_MASK, 119 KeyEvent.CTRL_DOWN_MASK, 120 KeyEvent.META_DOWN_MASK 121 }; 122 } 123 124 if( System.getProperty("os.name").toLowerCase() 125 .contains("linux") && System.getProperty("os.version") 126 .toLowerCase().contains("el7") ) { 127 System.out.println("OEL 7 doesn't support click modifiers in " + 128 "systray. Skipped"); 129 return; 130 } 131 132 new TrayIconEventModifiersTest().doTest(); 133 } 134 } 135 136 public TrayIconEventModifiersTest() throws Exception { 137 robot = new ExtendedRobot(); 138 EventQueue.invokeAndWait(this::initializeGUI); 139 } 140 141 private void initializeGUI() { 142 143 SystemTray tray = SystemTray.getSystemTray(); 144 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption); 145 icon.addMouseListener(new MouseAdapter() { 146 public void mousePressed(MouseEvent event) { 147 if (!icon.equals(event.getSource())) 148 throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " + 149 "did not return TrayIcon object"); 150 151 mousePressed = true; 152 modifiers = event.getModifiersEx(); 153 System.out.println("Icon mousePressed " + modifiers); 154 synchronized (mouseLock) { 155 try { 156 mouseLock.notifyAll(); 157 } catch (Exception e) { 158 } 159 } 160 } 161 162 public void mouseReleased(MouseEvent event) { 163 if (!icon.equals(event.getSource())) 164 throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " + 165 "did not return TrayIcon object"); 166 167 mouseReleased = true; 168 releaseModifiers = event.getModifiersEx(); 169 System.out.println("Icon mouseReleased " + releaseModifiers); 170 synchronized (mouseLock) { 171 try { 172 mouseLock.notifyAll(); 173 } catch (Exception e) { 174 } 175 } 176 } 177 178 public void mouseClicked(MouseEvent event) { 179 if (!icon.equals(event.getSource())) 180 throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " + 181 "did not return TrayIcon object"); 182 183 mouseClicked = true; 184 clickModifiers = event.getModifiersEx(); 185 System.out.println("Icon mouseClickedd " + clickModifiers); 186 synchronized (mouseLock) { 187 try { 188 mouseLock.notifyAll(); 189 } catch (Exception e) { 190 } 191 } 192 } 193 }); 194 195 try { 196 tray.add(icon); 197 } catch (Exception e) { 198 throw new RuntimeException(e); 199 } 200 } 201 202 void doTest() throws Exception { 203 204 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 205 if (iconPosition == null) 206 throw new RuntimeException("Unable to find the icon location!"); 207 208 robot.mouseMove(iconPosition.x, iconPosition.y); 209 robot.waitForIdle(); 210 211 for (int i = 0; i < buttonTypes.length; i++) { 212 for (int j = 0; j < keyTypes.length; j++) { 213 mousePressed = false; 214 215 robot.keyPress(keyTypes[j]); 216 robot.mousePress(buttonTypes[i]); 217 218 if (! mousePressed) { 219 synchronized (mouseLock) { 220 try { 221 mouseLock.wait(3000); 222 } catch (Exception e) { 223 } 224 } 225 } 226 if (! mousePressed) { 227 if (! SystemTrayIconHelper.skip(buttonTypes[i])) 228 throw new RuntimeException("FAIL: mousePressed not triggered when " + 229 keyNames[j] + " + " + buttonNames[i] + " pressed"); 230 } else { 231 int onMask = buttonMasks[i] | keyMasks[j]; 232 if ((modifiers & onMask) != onMask) { 233 throw new RuntimeException("FAIL: getModifiersEx did not return " + 234 "the correct value when " + keyNames[j] + " + " + 235 buttonNames[i] + " pressed"); 236 } 237 } 238 239 mouseReleased = false; 240 mouseClicked = false; 241 robot.mouseRelease(buttonTypes[i]); 242 243 if (! mouseReleased) { 244 synchronized (mouseLock) { 245 try { 246 mouseLock.wait(3000); 247 } catch (Exception e) { 248 } 249 } 250 } 251 252 robot.waitForIdle(1000); 253 robot.keyRelease(keyTypes[j]); 254 robot.waitForIdle(1000); 255 256 if (! mouseReleased) { 257 if (! SystemTrayIconHelper.skip(buttonTypes[i])) 258 throw new RuntimeException("FAIL: mouseReleased not triggered when " + 259 keyNames[j] + " + " + buttonNames[i] + " released"); 260 } else { 261 int onMask = keyMasks[j]; 262 if ((releaseModifiers & onMask) != onMask) 263 throw new RuntimeException("FAIL: getModifiersEx did not return " + 264 "the correct value when " + keyNames[j] + " + " + 265 buttonNames[i] + " released"); 266 } 267 if (! mouseClicked) { 268 throw new RuntimeException("FAIL: mouseClicked not triggered when " + 269 keyNames[j] + " + " + buttonNames[i] + 270 " pressed & released"); 271 } else { 272 int onMask = keyMasks[j]; 273 if ((clickModifiers & onMask) != onMask) 274 throw new RuntimeException("FAIL: getModifiersEx did not return " + 275 "the correct value when " + keyNames[j] + " + " + 276 buttonNames[i] + " pressed & released"); 277 } 278 robot.type(KeyEvent.VK_ESCAPE); 279 } 280 } 281 } 282 } 283