TrayIconEventModifiersTest.java revision 16177:89ef4b822745
1/*
2 * Copyright (c) 2007, 2016, 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
24import java.awt.EventQueue;
25import java.awt.Image;
26import java.awt.Point;
27import java.awt.SystemTray;
28import java.awt.TrayIcon;
29import java.awt.event.InputEvent;
30import java.awt.event.KeyEvent;
31import java.awt.event.MouseAdapter;
32import java.awt.event.MouseEvent;
33import java.awt.image.BufferedImage;
34
35
36/*
37 * @test
38 * @bug 8161473
39 * @key headful
40 * @summary Check if MouseEvent has the proper modifiers when
41 *          TrayIcon is clicked pressing the modifier keys
42 * @modules java.desktop/java.awt:open
43 * @library /java/awt/patchlib
44 * @library ../../../../lib/testlibrary ../
45 * @build java.desktop/java.awt.Helper
46 * @build ExtendedRobot SystemTrayIconHelper
47 * @run main TrayIconEventModifiersTest
48 */
49
50public class TrayIconEventModifiersTest {
51
52    Image image;
53
54    TrayIcon icon;
55    ExtendedRobot robot;
56
57    Object mouseLock = new Object();
58
59    String caption = "Sample Icon";
60    boolean mousePressed = false;
61    boolean mouseReleased = false;
62    boolean mouseClicked = false;
63    int modifiers, releaseModifiers, clickModifiers;
64
65    int[] buttonTypes = {
66        InputEvent.BUTTON1_MASK,
67        InputEvent.BUTTON2_MASK,
68        InputEvent.BUTTON3_MASK
69    };
70
71    String[] buttonNames = {
72        "BUTTON1",
73        "BUTTON2",
74        "BUTTON3"
75    };
76
77    int[] buttonMasks = {
78        InputEvent.BUTTON1_DOWN_MASK,
79        InputEvent.BUTTON2_DOWN_MASK,
80        InputEvent.BUTTON3_DOWN_MASK
81    };
82
83    static int[] keyTypes = {
84        KeyEvent.VK_SHIFT,
85        KeyEvent.VK_CONTROL
86    };
87
88    static String[] keyNames = {
89        "SHIFT",
90        "CONTROL"
91    };
92
93    static int[] keyMasks = {
94        KeyEvent.SHIFT_DOWN_MASK,
95        KeyEvent.CTRL_DOWN_MASK
96    };
97
98    public static void main(String[] args) throws Exception {
99        if (! SystemTray.isSupported()) {
100            System.out.println("SystemTray not supported on the platform under test. " +
101                    "Marking the test passed");
102        } else {
103            if (System.getProperty("os.name").toLowerCase().startsWith("win"))
104                System.err.println("Test can fail if the icon hides to a tray icons pool" +
105                        "in Windows 7, which is behavior by default.\n" +
106                        "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
107                        "\"Always show all icons and notifications on the taskbar\" true " +
108                        "to avoid this problem. Or change behavior only for Java SE tray " +
109                        "icon and rerun test.");
110
111            System.out.println(System.getProperty("os.arch"));
112            if (System.getProperty("os.name").indexOf("Sun") != -1 &&
113                    System.getProperty("os.arch").indexOf("sparc") != -1) {
114                keyTypes = new int[]{
115                        KeyEvent.VK_SHIFT,
116                        KeyEvent.VK_CONTROL,
117                        KeyEvent.VK_META
118                };
119
120                keyNames = new String[]{
121                        "SHIFT",
122                        "CONTROL",
123                        "META"
124                };
125                keyMasks = new int[]{
126                        KeyEvent.SHIFT_DOWN_MASK,
127                        KeyEvent.CTRL_DOWN_MASK,
128                        KeyEvent.META_DOWN_MASK
129                };
130            }
131
132            if (SystemTrayIconHelper.isOel7()) {
133                System.out.println("OEL 7 doesn't support click modifiers in " +
134                        "systray. Skipped");
135                return;
136            }
137
138            new TrayIconEventModifiersTest().doTest();
139        }
140    }
141
142    public TrayIconEventModifiersTest() throws Exception {
143        robot = new ExtendedRobot();
144        EventQueue.invokeAndWait(this::initializeGUI);
145    }
146
147    private void initializeGUI() {
148
149        SystemTray tray = SystemTray.getSystemTray();
150        icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
151        icon.addMouseListener(new MouseAdapter() {
152            public void mousePressed(MouseEvent event) {
153                if (!icon.equals(event.getSource()))
154                    throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " +
155                            "did not return TrayIcon object");
156
157                mousePressed = true;
158                modifiers = event.getModifiersEx();
159                System.out.println("Icon mousePressed " + modifiers);
160                synchronized (mouseLock) {
161                    try {
162                        mouseLock.notifyAll();
163                    } catch (Exception e) {
164                    }
165                }
166            }
167
168            public void mouseReleased(MouseEvent event) {
169                if (!icon.equals(event.getSource()))
170                    throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " +
171                            "did not return TrayIcon object");
172
173                mouseReleased = true;
174                releaseModifiers = event.getModifiersEx();
175                System.out.println("Icon mouseReleased " + releaseModifiers);
176                synchronized (mouseLock) {
177                    try {
178                        mouseLock.notifyAll();
179                    } catch (Exception e) {
180                    }
181                }
182            }
183
184            public void mouseClicked(MouseEvent event) {
185                if (!icon.equals(event.getSource()))
186                    throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " +
187                            "did not return TrayIcon object");
188
189                mouseClicked = true;
190                clickModifiers = event.getModifiersEx();
191                System.out.println("Icon mouseClickedd " + clickModifiers);
192                synchronized (mouseLock) {
193                    try {
194                        mouseLock.notifyAll();
195                    } catch (Exception e) {
196                    }
197                }
198            }
199        });
200
201        try {
202            tray.add(icon);
203        } catch (Exception e) {
204            throw new RuntimeException(e);
205        }
206    }
207
208    void doTest() throws Exception {
209
210        Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
211        if (iconPosition == null)
212            throw new RuntimeException("Unable to find the icon location!");
213
214        robot.mouseMove(iconPosition.x, iconPosition.y);
215        robot.waitForIdle();
216
217        for (int i = 0; i < buttonTypes.length; i++) {
218            for (int j = 0; j < keyTypes.length; j++) {
219                mousePressed = false;
220
221                robot.keyPress(keyTypes[j]);
222                robot.waitForIdle();
223                robot.mousePress(buttonTypes[i]);
224
225                if (! mousePressed) {
226                    synchronized (mouseLock) {
227                        try {
228                            mouseLock.wait(3000);
229                        } catch (Exception e) {
230                        }
231                    }
232                }
233                if (! mousePressed) {
234                    if (! SystemTrayIconHelper.skip(buttonTypes[i]))
235                        throw new RuntimeException("FAIL: mousePressed not triggered when " +
236                                keyNames[j] + " + " + buttonNames[i] + " pressed");
237                } else {
238                    int onMask = buttonMasks[i] | keyMasks[j];
239                    if ((modifiers & onMask) != onMask) {
240                        throw new RuntimeException("FAIL: getModifiersEx did not return " +
241                                "the correct value when " + keyNames[j] + " + " +
242                                buttonNames[i] + " pressed");
243                    }
244                }
245
246                mouseReleased = false;
247                mouseClicked = false;
248                robot.mouseRelease(buttonTypes[i]);
249
250                if (! mouseReleased) {
251                    synchronized (mouseLock) {
252                        try {
253                            mouseLock.wait(3000);
254                        } catch (Exception e) {
255                        }
256                    }
257                }
258
259                robot.waitForIdle(1000);
260                robot.keyRelease(keyTypes[j]);
261                robot.waitForIdle(1000);
262
263                if (! mouseReleased) {
264                    if (! SystemTrayIconHelper.skip(buttonTypes[i]))
265                        throw new RuntimeException("FAIL: mouseReleased not triggered when " +
266                                keyNames[j] + " + " + buttonNames[i] + " released");
267                } else {
268                    int onMask = keyMasks[j];
269                    if ((releaseModifiers & onMask) != onMask)
270                        throw new RuntimeException("FAIL: getModifiersEx did not return " +
271                                "the correct value when " + keyNames[j] + " + " +
272                                buttonNames[i] + " released");
273                }
274                if (! mouseClicked) {
275                    throw new RuntimeException("FAIL: mouseClicked not triggered when " +
276                            keyNames[j] + " + " + buttonNames[i] +
277                            " pressed & released");
278                } else {
279                    int onMask = keyMasks[j];
280                    if ((clickModifiers & onMask) != onMask)
281                        throw new RuntimeException("FAIL: getModifiersEx did not return " +
282                                "the correct value when " + keyNames[j] + " + " +
283                                buttonNames[i] + " pressed & released");
284                }
285                robot.type(KeyEvent.VK_ESCAPE);
286            }
287        }
288    }
289}
290
291