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 * @summary Check if ActionEvent is triggered by a TrayIcon only when 31 * it is double clicked using mouse button 1 (or single clicked 32 * with button 3 (on Mac OS X)) 33 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com) 34 * @library ../../../../lib/testlibrary ../ 35 * @compile/module=java.desktop java/awt/Helper.java 36 * @build ExtendedRobot SystemTrayIconHelper 37 * @run main TrayIconMouseTest 38 */ 39 40 public class TrayIconMouseTest { 41 42 TrayIcon icon; 43 ExtendedRobot robot; 44 45 boolean actionPerformed = false; 46 Object actionLock = new Object(); 47 static boolean isMacOS = false; 48 49 String caption = "Sample Icon"; 50 51 int[] buttonTypes = { 52 InputEvent.BUTTON1_MASK, 53 InputEvent.BUTTON2_MASK, 54 InputEvent.BUTTON3_MASK 55 }; 56 57 String[] buttonNames = { 58 "BUTTON1", 59 "BUTTON2", 60 "BUTTON3" 61 }; 62 63 public static void main(String[] args) throws Exception { 64 if (! SystemTray.isSupported()) { 65 System.out.println("SystemTray not supported on the platform under test. " + 66 "Marking the test passed"); 67 } else { 68 if (System.getProperty("os.name").toLowerCase().startsWith("mac")) { 69 isMacOS = true; 70 } 71 new TrayIconMouseTest().doTest(); 72 } 73 } 74 75 TrayIconMouseTest() throws Exception{ 76 robot = new ExtendedRobot(); 77 EventQueue.invokeAndWait(this::initializeGUI); 78 } 79 80 void initializeGUI() { 81 82 SystemTray tray = SystemTray.getSystemTray(); 83 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption); 84 icon.addActionListener(event -> { 85 actionPerformed = true; 86 synchronized (actionLock) { 87 try { 88 actionLock.notifyAll(); 89 } catch (Exception e) { 90 } 91 } 92 }); 93 try { 94 tray.add(icon); 95 } catch (Exception e) { 96 throw new RuntimeException(e); 97 } 98 } 99 100 private void doTest() throws Exception { 101 102 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 103 if (iconPosition == null) 104 throw new RuntimeException("Unable to find the icon location!"); 105 106 robot.mouseMove(iconPosition.x, iconPosition.y); 107 robot.waitForIdle(); 108 109 for (int i = 0; i < buttonTypes.length; i++) { 110 actionPerformed = false; 111 robot.click(buttonTypes[i]); 112 robot.waitForIdle(2000); 113 114 if (isMacOS && actionPerformed && i == 2) { 115 116 }else if (isMacOS && i == 2) { 117 throw new RuntimeException("FAIL: ActionEvent NOT triggered when " + 118 buttonNames[i] + " is single clicked on Mac OS"); 119 }else if (actionPerformed) { 120 throw new RuntimeException("FAIL: ActionEvent triggered when " + 121 buttonNames[i] + " is single clicked"); 122 } 123 } 124 125 if(!isMacOS) { 126 for (int i = 0; i < buttonTypes.length; i++) { 127 for (int j = 0; j < buttonTypes.length; j++) { 128 if (j != i) { 129 actionPerformed = false; 130 robot.mousePress(buttonTypes[i]); 131 robot.mousePress(buttonTypes[j]); 132 robot.mouseRelease(buttonTypes[j]); 133 robot.mouseRelease(buttonTypes[i]); 134 135 robot.waitForIdle(); 136 137 if (actionPerformed) 138 throw new RuntimeException("FAIL: ActionEvent triggered when " + 139 buttonNames[i] + " and " + buttonNames[j] + 140 " is clicked and released"); 141 } 142 } 143 } 144 145 for (int i = 0; i < buttonTypes.length; i++) { 146 actionPerformed = false; 147 robot.mousePress(buttonTypes[i]); 148 robot.delay(50); 149 robot.mouseRelease(buttonTypes[i]); 150 robot.delay(50); 151 robot.mousePress(buttonTypes[i]); 152 robot.delay(50); 153 robot.mouseRelease(buttonTypes[i]); 154 155 if (i == 0) { 156 if (! actionPerformed) { 157 synchronized (actionLock) { 158 try { 159 actionLock.wait(3000); 160 } catch (Exception e) { 161 } 162 } 163 } 164 if (! actionPerformed) 165 throw new RuntimeException("FAIL: ActionEvent not triggered when " + 166 buttonNames[i] + " is double clicked"); 167 } else { 168 robot.waitForIdle(); 169 170 if (actionPerformed) 171 throw new RuntimeException("FAIL: ActionEvent triggered when " + 172 buttonNames[i] + " is double clicked"); 173 } 174 } 175 } 176 } 177 }