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