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 ActionEvent triggered when a TrayIcon is double 31 * (single, on Mac) clicked is visible by an AWTEventListener 32 * added to the Toolkit. It also checks if all listeners are 33 * triggered when multiple AWTEventListeners and ActionListeners 34 * are added. 35 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com) 36 * @library ../../../../lib/testlibrary ../ 37 * @build ExtendedRobot SystemTrayIconHelper 38 * @run main ActionEventMask 39 */ 40 41 public class ActionEventMask { 42 43 private Image image; 44 45 TrayIcon icon; 46 ExtendedRobot robot; 47 48 boolean actionPerformed = false; 49 boolean listenersInvoked = false; 50 Object actionLock = new Object(); 51 Object listenersLock = new Object(); 52 static boolean isMacOS = false; 53 static final int clickDelay = 50; 54 55 ActionListener[] listeners; 56 boolean[] listenerStatus; 57 58 Object lLock = new Object(); 59 boolean doTest, listenerAdded; 60 Button b1; 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("mac")) { 68 isMacOS = true; 69 } 70 new ActionEventMask().doTest(); 71 } 72 } 73 74 public ActionEventMask() throws Exception { 75 EventQueue.invokeAndWait(this::initializeGUI); 76 } 77 78 void initializeGUI() { 79 SystemTray tray = SystemTray.getSystemTray(); 80 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon"); 81 82 Toolkit.getDefaultToolkit().addAWTEventListener(event -> { 83 if (doTest) { 84 System.out.println("ActionListener AWTEventListener"); 85 if (! icon.equals(event.getSource())) { 86 throw new RuntimeException("FAIL: ActionEvent not triggered for icon"); 87 } 88 actionPerformed = true; 89 synchronized (actionLock) { 90 try { 91 actionLock.notifyAll(); 92 } catch (Exception e) { 93 } 94 } 95 } 96 }, AWTEvent.ACTION_EVENT_MASK); 97 98 try { 99 tray.add(icon); 100 } catch (AWTException e) { 101 throw new RuntimeException(e); 102 } 103 104 listeners = new ActionListener[3]; 105 listenerStatus = new boolean[3]; 106 for (int i = 0; i < listeners.length; i++) { 107 final int index = i; 108 listeners[i] = event -> { 109 listenerStatus[index] = true; 110 System.out.println("ActionListener listeners[" + index + "]"); 111 for (int j = 0; j < listenerStatus.length; j++) { 112 if (! listenerStatus[j]) { 113 break; 114 } 115 listenersInvoked = true; 116 synchronized (listenersLock) { 117 try { 118 listenersLock.notifyAll(); 119 } catch (Exception e) { 120 } 121 } 122 } 123 }; 124 } 125 126 Frame frame = new Frame("Test frame"); 127 b1 = new Button("Add ActionListener"); 128 b1.addActionListener(event -> { 129 for (int i = 0; i < listeners.length; i++) { 130 icon.addActionListener(listeners[i]); 131 } 132 listenerAdded = true; 133 synchronized (lLock) { 134 try { 135 lLock.notifyAll(); 136 } catch (Exception e) { 137 } 138 } 139 }); 140 frame.setLayout(new FlowLayout()); 141 frame.add(b1); 142 frame.setSize(200, 200); 143 frame.addWindowListener(new WindowAdapter() { 144 public void windowClosing(WindowEvent event) { 145 System.err.println("User closed the window"); 146 System.exit(1); 147 } 148 }); 149 frame.setVisible(true); 150 } 151 152 private void doTest() throws Exception { 153 154 robot = new ExtendedRobot(); 155 156 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 157 if (iconPosition == null) 158 throw new RuntimeException("Unable to find the icon location!"); 159 160 robot.mouseMove(iconPosition.x, iconPosition.y); 161 robot.waitForIdle(); 162 163 actionPerformed = false; 164 doTest = true; 165 166 if(isMacOS) { 167 robot.click(InputEvent.BUTTON3_MASK); 168 }else{ 169 robot.mousePress(InputEvent.BUTTON1_MASK); 170 robot.delay(clickDelay); 171 robot.mouseRelease(InputEvent.BUTTON1_MASK); 172 robot.delay(clickDelay); 173 robot.mousePress(InputEvent.BUTTON1_MASK); 174 robot.delay(clickDelay); 175 robot.mouseRelease(InputEvent.BUTTON1_MASK); 176 } 177 178 if (! actionPerformed) { 179 synchronized (actionLock) { 180 try { 181 actionLock.wait(3000); 182 } catch (Exception e) { 183 } 184 } 185 } 186 if (! actionPerformed) 187 throw new RuntimeException("FAIL: AWTEventListener not notified when TrayIcon " + 188 "is "+(isMacOS ? "" :"double ")+ "clicked"); 189 190 doTest = false; 191 listenerAdded = false; 192 robot.mouseMove(b1.getLocationOnScreen().x + b1.getSize().width / 2, 193 b1.getLocationOnScreen().y + b1.getSize().height / 2); 194 robot.waitForIdle(); 195 robot.click(); 196 197 if (! listenerAdded) { 198 synchronized (lLock) { 199 try { 200 lLock.wait(3000); 201 } catch (Exception e) { 202 } 203 } 204 } 205 if (! listenerAdded) 206 throw new RuntimeException("FAIL: ActionListener could not be added at runtime. " + 207 "b1 did not trigger ActionEvent"); 208 209 doTest = true; 210 actionPerformed = false; 211 robot.mouseMove(iconPosition.x, iconPosition.y); 212 robot.waitForIdle(); 213 214 if(isMacOS) { 215 robot.click(InputEvent.BUTTON3_MASK); 216 }else{ 217 robot.mousePress(InputEvent.BUTTON1_MASK); 218 robot.delay(clickDelay); 219 robot.mouseRelease(InputEvent.BUTTON1_MASK); 220 robot.delay(clickDelay); 221 robot.mousePress(InputEvent.BUTTON1_MASK); 222 robot.delay(clickDelay); 223 robot.mouseRelease(InputEvent.BUTTON1_MASK); 224 } 225 226 if (! listenersInvoked) { 227 synchronized (listenersLock) { 228 try { 229 listenersLock.wait(3000); 230 } catch (Exception e) { 231 } 232 } 233 } 234 if (! listenersInvoked) { 235 System.err.println("FAIL: All the listeners not invoked!"); 236 for (int i = 0; i < listenerStatus.length; i++) 237 throw new RuntimeException("Listener[" + i + "] invoked: " + listenerStatus[i]); 238 } 239 if (! actionPerformed) { 240 synchronized (actionLock) { 241 try { 242 actionLock.wait(3000); 243 } catch (Exception e) { 244 } 245 } 246 } 247 if (! actionPerformed) 248 throw new RuntimeException("FAIL: AWTEventListener not notified when TrayIcon " + 249 "is "+(isMacOS? "" : "double ")+ "clicked. A set of listeners were added after it"); 250 251 } 252 }