1/* 2 * Copyright (c) 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/* 25 * @test 26 * @bug 6191390 8154328 27 * @summary Verify that ActionEvent is received with correct modifiers set. 28 * @modules java.desktop/java.awt:open 29 * @modules java.desktop/java.awt.peer 30 * @library ../../../../lib/testlibrary ../ 31 * @library /java/awt/patchlib 32 * @build java.desktop/java.awt.Helper 33 * @build ExtendedRobot SystemTrayIconHelper 34 * @run main ActionEventTest 35 */ 36 37import java.awt.Image; 38import java.awt.TrayIcon; 39import java.awt.SystemTray; 40import java.awt.Robot; 41import java.awt.EventQueue; 42import java.awt.Point; 43import java.awt.event.ActionEvent; 44import java.awt.event.ActionListener; 45import java.awt.event.InputEvent; 46import java.awt.event.KeyEvent; 47import java.awt.image.BufferedImage; 48 49public class ActionEventTest { 50 51 Image image; 52 TrayIcon icon; 53 Robot robot; 54 boolean actionPerformed; 55 56 public static void main(String[] args) throws Exception { 57 if (!SystemTray.isSupported()) { 58 System.out.println("SystemTray not supported on the platform." + 59 " Marking the test passed."); 60 } else { 61 if (System.getProperty("os.name").toLowerCase().startsWith("win")) { 62 System.err.println( 63 "Test can fail on Windows platform\n"+ 64 "On Windows 7, by default icon hides behind icon pool\n" + 65 "Due to which test might fail\n" + 66 "Set \"Right mouse click\" -> " + 67 "\"Customize notification icons\" -> \"Always show " + 68 "all icons and notifications on the taskbar\" true " + 69 "to avoid this problem.\nOR change behavior only for " + 70 "Java SE tray icon and rerun test."); 71 } 72 73 ActionEventTest test = new ActionEventTest(); 74 test.doTest(); 75 test.clear(); 76 } 77 } 78 79 public ActionEventTest() throws Exception { 80 robot = new Robot(); 81 EventQueue.invokeAndWait(this::initializeGUI); 82 } 83 84 private void initializeGUI() { 85 86 icon = new TrayIcon( 87 new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "ti"); 88 icon.addActionListener(new ActionListener() { 89 @Override 90 public void actionPerformed(ActionEvent ae) { 91 actionPerformed = true; 92 int md = ae.getModifiers(); 93 int expectedMask = ActionEvent.ALT_MASK | ActionEvent.CTRL_MASK 94 | ActionEvent.SHIFT_MASK; 95 96 if ((md & expectedMask) != expectedMask) { 97 clear(); 98 throw new RuntimeException("Action Event modifiers are not" 99 + " set correctly."); 100 } 101 } 102 }); 103 104 try { 105 SystemTray.getSystemTray().add(icon); 106 } catch (Exception e) { 107 throw new RuntimeException(e); 108 } 109 } 110 111 public void clear() { 112 robot.keyRelease(KeyEvent.VK_ALT); 113 robot.keyRelease(KeyEvent.VK_SHIFT); 114 robot.keyRelease(KeyEvent.VK_CONTROL); 115 SystemTray.getSystemTray().remove(icon); 116 } 117 118 void doTest() throws Exception { 119 robot.keyPress(KeyEvent.VK_ALT); 120 robot.keyPress(KeyEvent.VK_SHIFT); 121 robot.keyPress(KeyEvent.VK_CONTROL); 122 123 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 124 if (iconPosition == null) { 125 throw new RuntimeException("Unable to find the icon location!"); 126 } 127 128 robot.mouseMove(iconPosition.x, iconPosition.y); 129 robot.waitForIdle(); 130 131 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 132 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 133 robot.delay(100); 134 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 135 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 136 robot.waitForIdle(); 137 if (!actionPerformed) { 138 robot.delay(500); 139 } 140 } 141} 142