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 new TrayIconEventModifiersTest().doTest(); 125 } 126 } 127 128 public TrayIconEventModifiersTest() throws Exception { 129 robot = new ExtendedRobot(); 130 EventQueue.invokeAndWait(this::initializeGUI); 131 } 132 133 private void initializeGUI() { 134 135 SystemTray tray = SystemTray.getSystemTray(); 136 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption); 137 icon.addMouseListener(new MouseAdapter() { 138 public void mousePressed(MouseEvent event) { 139 if (!icon.equals(event.getSource())) 140 throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " + 141 "did not return TrayIcon object"); 142 143 mousePressed = true; 144 modifiers = event.getModifiersEx(); 145 System.out.println("Icon mousePressed " + modifiers); 146 synchronized (mouseLock) { 147 try { 148 mouseLock.notifyAll(); 149 } catch (Exception e) { 150 } 151 } 152 } 153 154 public void mouseReleased(MouseEvent event) { 155 if (!icon.equals(event.getSource())) 156 throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " + 157 "did not return TrayIcon object"); 158 159 mouseReleased = true; 160 releaseModifiers = event.getModifiersEx(); 161 System.out.println("Icon mouseReleased " + releaseModifiers); 162 synchronized (mouseLock) { 163 try { 164 mouseLock.notifyAll(); 165 } catch (Exception e) { 166 } 167 } 168 } 169 170 public void mouseClicked(MouseEvent event) { 171 if (!icon.equals(event.getSource())) 172 throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " + 173 "did not return TrayIcon object"); 174 175 mouseClicked = true; 176 clickModifiers = event.getModifiersEx(); 177 System.out.println("Icon mouseClickedd " + clickModifiers); 178 synchronized (mouseLock) { 179 try { 180 mouseLock.notifyAll(); 181 } catch (Exception e) { 182 } 183 } 184 } 185 }); 186 187 try { 188 tray.add(icon); 189 } catch (Exception e) { 190 throw new RuntimeException(e); 191 } 192 } 193 194 void doTest() throws Exception { 195 196 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 197 if (iconPosition == null) 198 throw new RuntimeException("Unable to find the icon location!"); 199 200 robot.mouseMove(iconPosition.x, iconPosition.y); 201 robot.waitForIdle(); 202 203 for (int i = 0; i < buttonTypes.length; i++) { 204 for (int j = 0; j < keyTypes.length; j++) { 205 mousePressed = false; 206 207 robot.keyPress(keyTypes[j]); 208 robot.mousePress(buttonTypes[i]); 209 210 if (! mousePressed) { 211 synchronized (mouseLock) { 212 try { 213 mouseLock.wait(3000); 214 } catch (Exception e) { 215 } 216 } 217 } 218 if (! mousePressed) { 219 if (! SystemTrayIconHelper.skip(buttonTypes[i])) 220 throw new RuntimeException("FAIL: mousePressed not triggered when " + 221 keyNames[j] + " + " + buttonNames[i] + " pressed"); 222 } else { 223 int onMask = buttonMasks[i] | keyMasks[j]; 224 if ((modifiers & onMask) != onMask) { 225 throw new RuntimeException("FAIL: getModifiersEx did not return " + 226 "the correct value when " + keyNames[j] + " + " + 227 buttonNames[i] + " pressed"); 228 } 229 } 230 231 mouseReleased = false; 232 mouseClicked = false; 233 robot.mouseRelease(buttonTypes[i]); 234 235 if (! mouseReleased) { 236 synchronized (mouseLock) { 237 try { 238 mouseLock.wait(3000); 239 } catch (Exception e) { 240 } 241 } 242 } 243 244 robot.waitForIdle(1000); 245 robot.keyRelease(keyTypes[j]); 246 robot.waitForIdle(1000); 247 248 if (! mouseReleased) { 249 if (! SystemTrayIconHelper.skip(buttonTypes[i])) 250 throw new RuntimeException("FAIL: mouseReleased not triggered when " + 251 keyNames[j] + " + " + buttonNames[i] + " released"); 252 } else { 253 int onMask = keyMasks[j]; 254 if ((releaseModifiers & onMask) != onMask) 255 throw new RuntimeException("FAIL: getModifiersEx did not return " + 256 "the correct value when " + keyNames[j] + " + " + 257 buttonNames[i] + " released"); 258 } 259 if (! mouseClicked) { 260 throw new RuntimeException("FAIL: mouseClicked not triggered when " + 261 keyNames[j] + " + " + buttonNames[i] + 262 " pressed & released"); 263 } else { 264 int onMask = keyMasks[j]; 265 if ((clickModifiers & onMask) != onMask) 266 throw new RuntimeException("FAIL: getModifiersEx did not return " + 267 "the correct value when " + keyNames[j] + " + " + 268 buttonNames[i] + " pressed & released"); 269 } 270 robot.type(KeyEvent.VK_ESCAPE); 271 } 272 } 273 } 274 } 275