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