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