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