TrayIconEventsTest.java revision 13901:b2a69d66dc65
1142425Snectar/* 2142425Snectar * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved. 3142425Snectar * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4142425Snectar * 5142425Snectar * This code is free software; you can redistribute it and/or modify it 6142425Snectar * under the terms of the GNU General Public License version 2 only, as 7142425Snectar * published by the Free Software Foundation. 8142425Snectar * 9142425Snectar * This code is distributed in the hope that it will be useful, but WITHOUT 10142425Snectar * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11142425Snectar * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12142425Snectar * version 2 for more details (a copy is included in the LICENSE file that 13142425Snectar * accompanied this code). 14142425Snectar * 15142425Snectar * You should have received a copy of the GNU General Public License version 16142425Snectar * 2 along with this work; if not, write to the Free Software Foundation, 17142425Snectar * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18142425Snectar * 19142425Snectar * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20142425Snectar * or visit www.oracle.com if you need additional information or have any 21142425Snectar * questions. 22142425Snectar */ 23142425Snectar 24142425Snectarimport java.awt.*; 25142425Snectarimport java.awt.event.*; 26142425Snectarimport java.awt.image.BufferedImage; 27142425Snectar 28142425Snectar 29142425Snectar/* 30142425Snectar * @test 31142425Snectar * @summary Check for MouseEvents with all mouse buttons 32142425Snectar * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com) 33142425Snectar * @library /java/awt/patchlib 34142425Snectar * @library ../../../../lib/testlibrary ../ 35142425Snectar * @build java.desktop/java.awt.Helper 36142425Snectar * @build ExtendedRobot SystemTrayIconHelper 37142425Snectar * @run main TrayIconEventsTest 38142425Snectar */ 39142425Snectar 40142425Snectarpublic class TrayIconEventsTest { 41142425Snectar 42142425Snectar private static boolean isOEL7; 43142425Snectar TrayIcon icon; 44142425Snectar ExtendedRobot robot; 45142425Snectar 46142425Snectar boolean actionPerformed = false; 47142425Snectar Object actionLock = new Object(); 48142425Snectar Object pressLock = new Object(); 49142425Snectar Object releaseLock = new Object(); 50142425Snectar Object clickLock = new Object(); 51142425Snectar Object moveLock = new Object(); 52142425Snectar 53142425Snectar String caption = "Sample Icon"; 54142425Snectar boolean mousePressed = false; 55142425Snectar boolean mouseReleased = false; 56142425Snectar boolean mouseClicked = false; 57142425Snectar boolean mouseMoved = false; 58142425Snectar 59142425Snectar int[] buttonTypes = { 60142425Snectar InputEvent.BUTTON1_MASK, 61142425Snectar InputEvent.BUTTON2_MASK, 62142425Snectar InputEvent.BUTTON3_MASK 63142425Snectar }; 64142425Snectar 65142425Snectar String[] buttonNames = { 66142425Snectar "BUTTON1", 67142425Snectar "BUTTON2", 68142425Snectar "BUTTON3" 69142425Snectar }; 70142425Snectar 71142425Snectar public static void main(String[] args) throws Exception { 72142425Snectar if (! SystemTray.isSupported()) { 73142425Snectar System.out.println("SystemTray not supported on the platform under test. " + 74142425Snectar "Marking the test passed"); 75142425Snectar } else { 76142425Snectar if (System.getProperty("os.name").toLowerCase().startsWith("win")) 77142425Snectar System.err.println("Test can fail if the icon hides to a tray icons pool " + 78142425Snectar "in Windows 7, which is behavior by default.\n" + 79142425Snectar "Set \"Right mouse click\" -> \"Customize notification icons\" -> " + 80142425Snectar "\"Always show all icons and notifications on the taskbar\" true " + 81142425Snectar "to avoid this problem. Or change behavior only for Java SE " + 82142425Snectar "tray icon."); 83142425Snectar isOEL7 = SystemTrayIconHelper.isOel7(); 84142425Snectar new TrayIconEventsTest().doTest(); 85142425Snectar } 86142425Snectar } 87142425Snectar 88142425Snectar public TrayIconEventsTest() throws Exception { 89142425Snectar robot = new ExtendedRobot(); 90142425Snectar EventQueue.invokeAndWait(this::initializeGUI); 91142425Snectar } 92142425Snectar 93142425Snectar private void initializeGUI(){ 94142425Snectar SystemTray tray = SystemTray.getSystemTray(); 95142425Snectar icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption); 96142425Snectar icon.addActionListener(event -> { 97142425Snectar actionPerformed = true; 98142425Snectar synchronized (actionLock) { 99142425Snectar try { 100142425Snectar 101142425Snectar actionLock.notifyAll(); 102142425Snectar } catch (Exception e) { 103142425Snectar } 104142425Snectar } 105142425Snectar }); 106142425Snectar icon.addMouseListener(new MouseAdapter() { 107142425Snectar public void mousePressed(MouseEvent event) { 108142425Snectar mousePressed = true; 109142425Snectar Point p = event.getPoint(); 110142425Snectar if (p.x != event.getX() || p.y != event.getY()) 111142425Snectar throw new RuntimeException("FAIL: MouseEvent.getPoint() did " + 112142425Snectar "not return the same value as getX/getY " + 113142425Snectar "for mousePressed"); 114142425Snectar 115142425Snectar if (! icon.equals(event.getSource())) 116142425Snectar throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " + 117142425Snectar "did not return TrayIcon object"); 118142425Snectar 119142425Snectar synchronized (pressLock) { 120142425Snectar try { 121142425Snectar pressLock.notifyAll(); 122142425Snectar } catch (Exception e) { 123142425Snectar } 124142425Snectar } 125142425Snectar } 126142425Snectar 127142425Snectar public void mouseReleased(MouseEvent event) { 128142425Snectar mouseReleased = true; 129142425Snectar Point p = event.getPoint(); 130142425Snectar if (p.x != event.getX() || p.y != event.getY()) 131142425Snectar throw new RuntimeException("FAIL: MouseEvent.getPoint() did " + 132142425Snectar "not return the same value as getX/getY " + 133142425Snectar "for mouseReleased"); 134142425Snectar 135142425Snectar if (! icon.equals(event.getSource())) 136142425Snectar throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " + 137142425Snectar "did not return TrayIcon object"); 138142425Snectar 139142425Snectar synchronized (releaseLock) { 140142425Snectar try { 141142425Snectar releaseLock.notifyAll(); 142142425Snectar } catch (Exception e) { 143142425Snectar } 144142425Snectar } 145142425Snectar } 146142425Snectar 147142425Snectar public void mouseClicked(MouseEvent event) { 148142425Snectar mouseClicked = true; 149142425Snectar Point p = event.getPoint(); 150142425Snectar if (p.x != event.getX() || p.y != event.getY()) 151142425Snectar throw new RuntimeException("FAIL: MouseEvent.getPoint() did " + 152142425Snectar "not return the same value as getX/getY " + 153142425Snectar "for mouseClicked"); 154142425Snectar 155142425Snectar if (! icon.equals(event.getSource())) 156142425Snectar throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " + 157142425Snectar "did not return TrayIcon object"); 158142425Snectar 159142425Snectar synchronized (clickLock) { 160142425Snectar try { 161142425Snectar clickLock.notifyAll(); 162142425Snectar } catch (Exception e) { 163142425Snectar } 164142425Snectar } 165142425Snectar } 166142425Snectar }); 167142425Snectar 168142425Snectar icon.addMouseMotionListener(new MouseMotionAdapter() { 169142425Snectar public void mouseMoved(MouseEvent event) { 170142425Snectar mouseMoved = true; 171142425Snectar Point p = event.getPoint(); 172142425Snectar if (p.x != event.getX() || p.y != event.getY()) 173142425Snectar throw new RuntimeException("FAIL: MouseEvent.getPoint() did " + 174142425Snectar "not return the same value as getX/getY " + 175142425Snectar "for mouseMoved"); 176142425Snectar 177142425Snectar if (! icon.equals(event.getSource())) 178142425Snectar throw new RuntimeException("FAIL: mouseMoved: MouseEvent.getSource " + 179142425Snectar "did not return TrayIcon object"); 180142425Snectar 181142425Snectar synchronized (moveLock) { 182 try { 183 moveLock.notifyAll(); 184 } catch (Exception e) { 185 } 186 } 187 } 188 }); 189 190 try { 191 tray.add(icon); 192 } catch (AWTException e) { 193 throw new RuntimeException(e); 194 } 195 } 196 197 void doTest() throws Exception { 198 199 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 200 if (iconPosition == null) 201 throw new RuntimeException("Unable to find the icon location!"); 202 if (isOEL7) { 203 // close tray 204 robot.mouseMove(100,100); 205 robot.click(InputEvent.BUTTON1_MASK); 206 robot.waitForIdle(2000); 207 } 208 209 robot.mouseMove(iconPosition.x, iconPosition.y); 210 robot.waitForIdle(); 211 if(!isOEL7) { 212 SystemTrayIconHelper.doubleClick(robot); 213 214 if (!actionPerformed) { 215 synchronized (actionLock) { 216 try { 217 actionLock.wait(10000); 218 } catch (Exception e) { 219 } 220 } 221 } 222 if (!actionPerformed) 223 throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked"); 224 } 225 226 for (int i = 0; i < buttonTypes.length; i++) { 227 mousePressed = false; 228 if(isOEL7) { 229 SystemTrayIconHelper.openTrayIfNeeded(robot); 230 robot.mouseMove(iconPosition.x, iconPosition.y); 231 robot.click(buttonTypes[i]); 232 } else { 233 robot.mousePress(buttonTypes[i]); 234 } 235 236 if (! mousePressed) { 237 synchronized (pressLock) { 238 try { 239 pressLock.wait(6000); 240 } catch (Exception e) { 241 } 242 } 243 } 244 if (! mousePressed) 245 if (! SystemTrayIconHelper.skip(buttonTypes[i]) ) 246 throw new RuntimeException("FAIL: mousePressed not triggered when " + 247 buttonNames[i] + " pressed"); 248 249 mouseReleased = false; 250 mouseClicked = false; 251 if(isOEL7) { 252 SystemTrayIconHelper.openTrayIfNeeded(robot); 253 robot.mouseMove(iconPosition.x, iconPosition.y); 254 robot.click(buttonTypes[i]); 255 } else { 256 robot.mouseRelease(buttonTypes[i]); 257 } 258 259 if (! mouseReleased) { 260 synchronized (releaseLock) { 261 try { 262 releaseLock.wait(6000); 263 } catch (Exception e) { 264 } 265 } 266 } 267 if (! mouseReleased) 268 throw new RuntimeException("FAIL: mouseReleased not triggered when " + 269 buttonNames[i] + " released"); 270 271 if (! mouseClicked) { 272 synchronized (clickLock) { 273 try { 274 clickLock.wait(6000); 275 } catch (Exception e) { 276 } 277 } 278 } 279 if (! mouseClicked) 280 throw new RuntimeException("FAIL: mouseClicked not triggered when " + 281 buttonNames[i] + " pressed & released"); 282 } 283 284 if (!isOEL7) { 285 mouseMoved = false; 286 robot.mouseMove(iconPosition.x + 100, iconPosition.y); 287 robot.glide(iconPosition.x, iconPosition.y); 288 289 if (!mouseMoved) 290 if (!SystemTrayIconHelper.skip(0)) 291 throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon"); 292 } 293 } 294} 295