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