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 } 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 } | 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 private static boolean isOEL7; 41 TrayIcon icon; 42 ExtendedRobot robot; 43 44 boolean actionPerformed = false; 45 Object actionLock = new Object(); 46 Object pressLock = new Object(); 47 Object releaseLock = new Object(); 48 Object clickLock = new Object(); 49 Object moveLock = new Object(); 50 51 String caption = "Sample Icon"; 52 boolean mousePressed = false; 53 boolean mouseReleased = false; 54 boolean mouseClicked = false; 55 boolean mouseMoved = false; 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 public static void main(String[] args) throws Exception { 70 if (! SystemTray.isSupported()) { 71 System.out.println("SystemTray not supported on the platform under test. " + 72 "Marking the test passed"); 73 } else { 74 if (System.getProperty("os.name").toLowerCase().startsWith("win")) 75 System.err.println("Test can fail if the icon hides to a tray icons pool " + 76 "in Windows 7, which is behavior by default.\n" + 77 "Set \"Right mouse click\" -> \"Customize notification icons\" -> " + 78 "\"Always show all icons and notifications on the taskbar\" true " + 79 "to avoid this problem. Or change behavior only for Java SE " + 80 "tray icon."); 81 isOEL7 = SystemTrayIconHelper.isOel7(); 82 new TrayIconEventsTest().doTest(); 83 } 84 } 85 86 public TrayIconEventsTest() throws Exception { 87 robot = new ExtendedRobot(); 88 EventQueue.invokeAndWait(this::initializeGUI); 89 } 90 91 private void initializeGUI(){ 92 SystemTray tray = SystemTray.getSystemTray(); 93 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption); 94 icon.addActionListener(event -> { 95 actionPerformed = true; 96 synchronized (actionLock) { 97 try { 98 99 actionLock.notifyAll(); 100 } catch (Exception e) { 101 } 180 try { 181 moveLock.notifyAll(); 182 } catch (Exception e) { 183 } 184 } 185 } 186 }); 187 188 try { 189 tray.add(icon); 190 } catch (AWTException 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 if (isOEL7) { 201 // close tray 202 robot.mouseMove(100,100); 203 robot.click(InputEvent.BUTTON1_MASK); 204 robot.waitForIdle(2000); 205 } 206 207 robot.mouseMove(iconPosition.x, iconPosition.y); 208 robot.waitForIdle(); 209 if(!isOEL7) { 210 SystemTrayIconHelper.doubleClick(robot); 211 212 if (!actionPerformed) { 213 synchronized (actionLock) { 214 try { 215 actionLock.wait(10000); 216 } catch (Exception e) { 217 } 218 } 219 } 220 if (!actionPerformed) 221 throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked"); 222 } 223 224 for (int i = 0; i < buttonTypes.length; i++) { 225 mousePressed = false; 226 if(isOEL7) { 227 SystemTrayIconHelper.openTrayIfNeeded(robot); 228 robot.mouseMove(iconPosition.x, iconPosition.y); 229 robot.click(buttonTypes[i]); 230 } else { 231 robot.mousePress(buttonTypes[i]); 232 } 233 234 if (! mousePressed) { 235 synchronized (pressLock) { 236 try { 237 pressLock.wait(6000); 238 } catch (Exception e) { 239 } 240 } 241 } 242 if (! mousePressed) 243 if (! SystemTrayIconHelper.skip(buttonTypes[i]) ) 244 throw new RuntimeException("FAIL: mousePressed not triggered when " + 245 buttonNames[i] + " pressed"); 246 247 mouseReleased = false; 248 mouseClicked = false; 249 if(isOEL7) { 250 SystemTrayIconHelper.openTrayIfNeeded(robot); 251 robot.mouseMove(iconPosition.x, iconPosition.y); 252 robot.click(buttonTypes[i]); 253 } else { 254 robot.mouseRelease(buttonTypes[i]); 255 } 256 257 if (! mouseReleased) { 258 synchronized (releaseLock) { 259 try { 260 releaseLock.wait(6000); 261 } catch (Exception e) { 262 } 263 } 264 } 265 if (! mouseReleased) 266 throw new RuntimeException("FAIL: mouseReleased not triggered when " + 267 buttonNames[i] + " released"); 268 269 if (! mouseClicked) { 270 synchronized (clickLock) { 271 try { 272 clickLock.wait(6000); 273 } catch (Exception e) { 274 } 275 } 276 } 277 if (! mouseClicked) 278 throw new RuntimeException("FAIL: mouseClicked not triggered when " + 279 buttonNames[i] + " pressed & released"); 280 } 281 282 if (!isOEL7) { 283 mouseMoved = false; 284 robot.mouseMove(iconPosition.x + 100, iconPosition.y); 285 robot.glide(iconPosition.x, iconPosition.y); 286 287 if (!mouseMoved) 288 if (!SystemTrayIconHelper.skip(0)) 289 throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon"); 290 } 291 } 292 } |