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 * @test 30 * @summary Check if MouseEvents triggered by TrayIcon are visible 31 * by an AWTEventListener added to the Toolkit. It also 32 * checks if all listeners are triggered when AWTEventListeners 33 * and MouseListeners are added. 34 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com) 35 * @library ../../../../lib/testlibrary ../ 36 * @build ExtendedRobot SystemTrayIconHelper 37 * @run main MouseEventMaskTest 38 */ 39 40 public class MouseEventMaskTest { 41 42 TrayIcon icon; 43 Robot robot; 44 int[] buttonTypes = { 45 InputEvent.BUTTON1_MASK, 46 InputEvent.BUTTON2_MASK, 47 InputEvent.BUTTON3_MASK 48 }; 49 50 String[] buttonNames = { 51 "BUTTON1", 52 "BUTTON2", 53 "BUTTON3" 54 }; 55 56 boolean mouseEventTriggered = false; 57 boolean mouseMotionEventTriggered = false; 58 Object mouseEventLock = new Object(); 59 Object mouseMotionEventLock = new Object(); 60 boolean mouseMotionTest, mouseTest; 61 62 public static void main(String[] args) throws Exception { 63 if (! SystemTray.isSupported()) { 64 System.out.println("SystemTray not supported on the platform under test. " + 65 "Marking the test passed"); 66 } else { 67 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 68 System.err.println("Test can fail if the icon hides to a tray icons pool " + 69 "in Windows 7, which is behavior by default.\n" + 70 "Set \"Right mouse click\" -> \"Customize notification icons\" -> " + 71 "\"Always show all icons and notifications on the taskbar\" true " + 72 "to avoid this problem. Or change behavior only for Java SE tray " + 73 "icon and rerun test."); 74 } else if (SystemTrayIconHelper.isOel7()) { 75 return; 76 } 77 new MouseEventMaskTest().doTest(); 78 } 79 } 80 81 public MouseEventMaskTest() throws Exception{ 82 EventQueue.invokeAndWait(this::initializeGUI); 83 } 84 85 void initializeGUI() { 86 87 SystemTray tray = SystemTray.getSystemTray(); 88 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon"); 89 90 Toolkit.getDefaultToolkit().addAWTEventListener(event -> { 91 if (mouseTest) { 92 if (! event.getSource().getClass().getName().contains("Canvas")) { 93 if (!icon.equals(event.getSource())) 94 throw new RuntimeException("FAIL: MouseEvent not triggered for icon " + event); 95 96 mouseEventTriggered = true; 97 synchronized (mouseEventLock) { 98 try { 99 mouseEventLock.notifyAll(); 100 } catch (Exception e) { 101 } 102 } 103 } 104 } 105 }, AWTEvent.MOUSE_EVENT_MASK); 106 Toolkit.getDefaultToolkit().addAWTEventListener(event -> { 107 if (mouseMotionTest) { 108 if (! event.getSource().getClass().getName().contains("Canvas")) { 109 if (!icon.equals(event.getSource())) 110 throw new RuntimeException("FAIL: MouseMotionEvent not triggered for icon " + event); 111 112 mouseMotionEventTriggered = true; 113 synchronized (mouseMotionEventLock) { 114 try { 115 mouseMotionEventLock.notifyAll(); 116 } catch (Exception e) { 117 } 118 } 119 } 120 } 121 }, AWTEvent.MOUSE_MOTION_EVENT_MASK); 122 123 try { 124 tray.add(icon); 125 } catch (AWTException e) { 126 throw new RuntimeException(e); 127 } 128 } 129 130 void doTest() throws Exception { 131 132 robot = new Robot(); 133 134 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 135 if (iconPosition == null) 136 throw new RuntimeException("Unable to find the icon location!"); 137 138 robot.mouseMove(iconPosition.x, iconPosition.y); 139 robot.waitForIdle(); 140 141 for (int i = 0; i < buttonTypes.length; i++) { 142 System.out.println("Verify button "+buttonTypes[i]); 143 mouseTest = true; 144 mouseEventTriggered = false; 145 robot.mousePress(buttonTypes[i]); 146 147 if (! mouseEventTriggered) { 148 synchronized (mouseEventLock) { 149 try { 150 mouseEventLock.wait(3000); 151 } catch (Exception e) { 152 } 153 } 154 } 155 if (! mouseEventTriggered) 156 if (! SystemTrayIconHelper.skip(buttonTypes[i]) ) 157 throw new RuntimeException("FAIL: AWTEventListener not notified when " + 158 buttonNames[i] + " pressed on TrayIcon"); 159 160 mouseEventTriggered = false; 161 robot.mouseRelease(buttonTypes[i]); 162 if (! mouseEventTriggered) { 163 synchronized (mouseEventLock) { 164 try { 165 mouseEventLock.wait(3000); 166 } catch (Exception e) { 167 } 168 } 169 } 170 171 if (! mouseEventTriggered) 172 throw new RuntimeException("FAIL: AWTEventListener not notified when " + 173 buttonNames[i] + " released on TrayIcon"); 174 } 175 176 mouseMotionTest = true; 177 mouseTest = false; 178 mouseMotionEventTriggered = false; 179 180 for (int i = 0; i < 20; i++) { 181 robot.mouseMove(iconPosition.x + i, iconPosition.y); 182 robot.delay(25); 183 } 184 if (! mouseMotionEventTriggered) { 185 synchronized (mouseMotionEventLock) { 186 try { 187 mouseMotionEventLock.wait(3000); 188 } catch (Exception e) { 189 } 190 } 191 } 192 if (! mouseMotionEventTriggered) 193 if (! SystemTrayIconHelper.skip(0) ) 194 throw new RuntimeException("FAIL: AWTEventListener not notified when " + 195 "mouse moved"); 196 } 197 }