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