1/*
2 * Copyright (c) 2007, 2014, 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 various methods of the TrayIcon - whether the methods
31 *          return the proper values, throws the proper exceptions etc
32 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
33 * @run main TrayIconMethodsTest
34 */
35
36public class TrayIconMethodsTest {
37
38    public static void main(String[] args) throws Exception {
39        if (! SystemTray.isSupported()) {
40            System.out.println("SystemTray not supported on the platform under test. " +
41                    "Marking the test passed");
42        } else {
43            new TrayIconMethodsTest().doTest();
44        }
45    }
46
47    void doTest() throws Exception {
48        SystemTray tray = SystemTray.getSystemTray();
49
50        String toolTip = "Sample Icon";
51        PopupMenu pm = new PopupMenu();
52        pm.add(new MenuItem("Sample"));
53
54        Image image = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
55        TrayIcon icon = new TrayIcon(image, toolTip, pm);
56
57        ActionListener al1 = event -> {};
58        ActionListener al2 = event -> {};
59        MouseMotionListener mml1 = new MouseMotionAdapter() {};
60        MouseMotionListener mml2 = new MouseMotionAdapter() {};
61        MouseListener ml1 = new MouseAdapter() {};
62        MouseListener ml2 = new MouseAdapter() {};
63
64        icon.addActionListener(al1);
65        icon.addActionListener(al2);
66        icon.addMouseMotionListener(mml1);
67        icon.addMouseMotionListener(mml2);
68        icon.addMouseListener(ml1);
69        icon.addMouseListener(ml2);
70        tray.add(icon);
71
72        ActionListener[] actionListeners = icon.getActionListeners();
73        if (actionListeners == null || actionListeners.length != 2)
74            throw new RuntimeException("FAIL: getActionListeners did not return the correct value " +
75                    "when there were two listeners present " + actionListeners);
76
77        if (! isPresent(actionListeners, al1) || ! isPresent(actionListeners, al2))
78            throw new RuntimeException("FAIL: All the action listeners added are not returned " +
79                    "by the method");
80
81        MouseListener[] mouseListeners = icon.getMouseListeners();
82        if (mouseListeners == null || mouseListeners.length != 2)
83            throw new RuntimeException("FAIL: getMouseListeners did not return the correct value " +
84                    "when there were two listeners present " + mouseListeners);
85
86        if (! isPresent(mouseListeners, ml1) || ! isPresent(mouseListeners, ml2))
87            throw new RuntimeException("FAIL: All the mouse listeners added are not returned " +
88                    "by the method");
89
90        MouseMotionListener[] mouseMotionListeners = icon.getMouseMotionListeners();
91        if (mouseMotionListeners == null || mouseMotionListeners.length != 2)
92            throw new RuntimeException("FAIL: getMouseMotionListeners did not return the correct value " +
93                    "when there were two listeners present " + mouseMotionListeners);
94
95        if (! isPresent(mouseMotionListeners, mml1) || ! isPresent(mouseMotionListeners, mml2))
96            throw new RuntimeException("FAIL: All the mouse motion listeners added are not returned " +
97                    "by the method");
98
99        Image im = icon.getImage();
100        if (! image.equals(im))
101            throw new RuntimeException("FAIL: Images are not the same getImage()=" + im +
102                    " Image=" + image);
103
104        if (! pm.equals(icon.getPopupMenu()))
105            throw new RuntimeException("FAIL: PopupMenus are not the same getPopupMenu()=" +
106                    icon.getPopupMenu() + " PopupMenu=" + pm);
107
108        if (! toolTip.equals(icon.getToolTip()))
109            throw new RuntimeException("FAIL: ToolTips are not the same getToolTip()=" +
110                               icon.getToolTip() + " ToolTip=" + toolTip);
111
112        if (icon.isImageAutoSize())
113            throw new RuntimeException("FAIL: Auto size property is true by default");
114
115        icon.setImageAutoSize(true);
116        if (! icon.isImageAutoSize())
117            throw new RuntimeException("FAIL: Auto size property is not set to " +
118                    "true by call to setImageAutoSize(true)");
119
120        icon.removeActionListener(al1);
121        icon.removeActionListener(al2);
122        actionListeners = icon.getActionListeners();
123        if (actionListeners == null || actionListeners.length != 0)
124            throw new RuntimeException("FAIL: removeActionListener did not " +
125                    "remove the ActionListeners added " + actionListeners);
126
127        icon.removeMouseListener(ml1);
128        icon.removeMouseListener(ml2);
129        mouseListeners = icon.getMouseListeners();
130        if (mouseListeners == null || mouseListeners.length != 0)
131            throw new RuntimeException("FAIL: removeMouseListener did not " +
132                    "remove the MouseListeners added " + mouseListeners);
133
134        icon.removeMouseMotionListener(mml1);
135        icon.removeMouseMotionListener(mml2);
136        mouseMotionListeners = icon.getMouseMotionListeners();
137        if (mouseMotionListeners == null || mouseMotionListeners.length != 0)
138            throw new RuntimeException("FAIL: removeMouseMotionListener did not " +
139                    "remove the MouseMotionListeners added " + mouseMotionListeners);
140
141        try {
142            icon.setImage(null);
143            throw new RuntimeException("FAIL: setImage(null) did not throw NullPointerException");
144        } catch (NullPointerException npe) {
145        }
146    }
147
148    boolean isPresent(Object[] array, Object obj) {
149        if (array == null || array.length == 0 || obj == null) {
150            return false;
151        }
152        for (int i = 0; i < array.length; i++) {
153            if (obj.equals(array[i])) {
154                return true;
155            }
156        }
157        return false;
158    }
159}
160