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