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