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