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