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