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