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