FunctionalityCheck.java revision 13358:54ee396d17f0
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
24import java.awt.*;
25import java.awt.event.*;
26import java.awt.image.BufferedImage;
27
28/*
29 * @test
30 * @summary Check for MouseEvents with all mouse buttons
31 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
32 * @library ../../../../../lib/testlibrary ../../
33 * @build ExtendedRobot SystemTrayIconHelper
34 * @run main/othervm/policy=tray.policy -Djava.security.manager FunctionalityCheck
35 */
36
37public class FunctionalityCheck {
38
39    TrayIcon icon;
40    ExtendedRobot robot;
41
42    boolean actionPerformed = false;
43    Object actionLock = new Object();
44    Object pressLock = new Object();
45    Object releaseLock = new Object();
46    Object clickLock = new Object();
47    Object moveLock = new Object();
48
49    String caption = "Sample Icon";
50    boolean mousePressed = false;
51    boolean mouseReleased = false;
52    boolean mouseClicked = false;
53    boolean mouseMoved = false;
54    static boolean isOEL7;
55
56    static final int[] buttonTypes = {
57        InputEvent.BUTTON1_MASK,
58        InputEvent.BUTTON2_MASK,
59        InputEvent.BUTTON3_MASK
60    };
61
62    static final String[] buttonNames = {
63        "BUTTON1",
64        "BUTTON2",
65        "BUTTON3"
66    };
67
68    public static void main(String[] args) throws Exception {
69        if (! SystemTray.isSupported()) {
70            System.out.println("SystemTray not supported on the platform under test. " +
71                               "Marking the test passed");
72        } else {
73            isOEL7 = SystemTrayIconHelper.isOel7();
74            new FunctionalityCheck().doTest();
75        }
76    }
77
78    FunctionalityCheck() throws Exception {
79        robot = new ExtendedRobot();
80        EventQueue.invokeAndWait(this::initializeGUI);
81    }
82
83    void initializeGUI() {
84        SystemTray tray = SystemTray.getSystemTray();
85        icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
86        icon.addActionListener(event -> {
87            actionPerformed = true;
88            synchronized (actionLock) {
89                try {
90                    actionLock.notifyAll();
91                } catch (Exception e) {
92                }
93            }
94        });
95        icon.addMouseListener(new MouseAdapter() {
96            public void mousePressed(MouseEvent event) {
97                mousePressed = true;
98                Point p = event.getPoint();
99                if (p.x != event.getX() || p.y != event.getY())
100                    throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
101                            "not return the same value as getX/getY " +
102                            "for mousePressed");
103
104                if (! icon.equals(event.getSource()))
105                    throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " +
106                            "did not return TrayIcon object");
107
108                synchronized (pressLock) {
109                    try {
110                        pressLock.notifyAll();
111                    } catch (Exception e) {
112                    }
113                }
114            }
115
116            public void mouseReleased(MouseEvent event) {
117                mouseReleased = true;
118                Point p = event.getPoint();
119                if (p.x != event.getX() || p.y != event.getY())
120                    throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
121                            "not return the same value as getX/getY " +
122                            "for mouseReleased");
123
124                if (! icon.equals(event.getSource()))
125                    throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " +
126                            "did not return TrayIcon object");
127
128                synchronized (releaseLock) {
129                    try {
130                        releaseLock.notifyAll();
131                    } catch (Exception e) {
132                    }
133                }
134            }
135
136            public void mouseClicked(MouseEvent event) {
137                mouseClicked = true;
138                Point p = event.getPoint();
139                if (p.x != event.getX() || p.y != event.getY())
140                    throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
141                            "not return the same value as getX/getY " +
142                            "for mouseClicked");
143
144                if (! icon.equals(event.getSource()))
145                    throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " +
146                            "did not return TrayIcon object");
147
148                synchronized (clickLock) {
149                    try {
150                        clickLock.notifyAll();
151                    } catch (Exception e) {
152                    }
153                }
154            }
155        });
156
157        icon.addMouseMotionListener(new MouseMotionAdapter() {
158            public void mouseMoved(MouseEvent event) {
159                mouseMoved = true;
160                Point p = event.getPoint();
161                if (p.x != event.getX() || p.y != event.getY())
162                    throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
163                            "not return the same value as getX/getY " +
164                            "for mouseMoved");
165
166                if (! icon.equals(event.getSource()))
167                    throw new RuntimeException("FAIL: mouseMoved: MouseEvent.getSource " +
168                            "did not return TrayIcon object");
169
170                synchronized (moveLock) {
171                    try {
172                        moveLock.notifyAll();
173                    } catch (Exception e) {
174                    }
175                }
176            }
177        });
178
179        try {
180            tray.add(icon);
181        } catch (Exception e) {
182            throw new RuntimeException(e);
183        }
184    }
185
186    private void doTest() throws Exception {
187
188
189
190        Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
191        if (iconPosition == null)
192            throw new RuntimeException("Unable to find the icon location!");
193        if (isOEL7) {
194            // close tray
195            robot.mouseMove(100,100);
196            robot.click(InputEvent.BUTTON1_MASK);
197            robot.waitForIdle(2000);
198        }
199
200        robot.mouseMove(iconPosition.x, iconPosition.y);
201        robot.waitForIdle();
202        if(!isOEL7) {
203            SystemTrayIconHelper.doubleClick(robot);
204
205            if (!actionPerformed) {
206                synchronized (actionLock) {
207                    try {
208                        actionLock.wait(3000);
209                    } catch (Exception e) {
210                    }
211                }
212            }
213            if (!actionPerformed)
214                throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");
215        }
216
217        for (int i = 0; i < buttonTypes.length; i++) {
218            mousePressed = false;
219            if(isOEL7) {
220                SystemTrayIconHelper.openTrayIfNeeded(robot);
221                robot.mouseMove(iconPosition.x, iconPosition.y);
222                robot.click(buttonTypes[i]);
223            } else {
224                robot.mousePress(buttonTypes[i]);
225            }
226
227            if (! mousePressed) {
228                synchronized (pressLock) {
229                    try {
230                        pressLock.wait(6000);
231                    } catch (Exception e) {
232                    }
233                }
234            }
235            if (! mousePressed)
236                if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
237                    throw new RuntimeException("FAIL: mousePressed not triggered when " +
238                            buttonNames[i] + " pressed");
239
240            mouseReleased = false;
241            mouseClicked = false;
242            if(isOEL7) {
243                SystemTrayIconHelper.openTrayIfNeeded(robot);
244                robot.mouseMove(iconPosition.x, iconPosition.y);
245                robot.click(buttonTypes[i]);
246            } else {
247                robot.mouseRelease(buttonTypes[i]);
248            }
249            if (! mouseReleased) {
250                synchronized (releaseLock) {
251                    try {
252                        releaseLock.wait(6000);
253                    } catch (Exception e) {
254                    }
255                }
256            }
257            if (! mouseReleased)
258                if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
259                    throw new RuntimeException("FAIL: mouseReleased not triggered when " +
260                            buttonNames[i] + " released");
261
262            if (! mouseClicked) {
263                synchronized (clickLock) {
264                    try {
265                        clickLock.wait(6000);
266                    } catch (Exception e) {
267                    }
268                }
269            }
270            if (! mouseClicked)
271                throw new RuntimeException("FAIL: mouseClicked not triggered when " +
272                        buttonNames[i] + " pressed & released");
273        }
274        if(!isOEL7) {
275            mouseMoved = false;
276            robot.mouseMove(iconPosition.x + 100, iconPosition.y);
277            robot.glide(iconPosition.x, iconPosition.y);
278
279            if (!mouseMoved)
280                if (!SystemTrayIconHelper.skip(0))
281                    throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");
282        }
283    }
284}
285