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.Image;
  26 import java.awt.Point;
  27 import java.awt.SystemTray;
  28 import java.awt.TrayIcon;
  29 import java.awt.event.InputEvent;
  30 import java.awt.event.KeyEvent;
  31 import java.awt.event.MouseAdapter;
  32 import java.awt.event.MouseEvent;
  33 import java.awt.image.BufferedImage;
  34 
  35 
  36 /*
  37  * @test
  38  * @bug 8161473
  39  * @key headful
  40  * @summary Check if MouseEvent has the proper modifiers when
  41  *          TrayIcon is clicked pressing the modifier keys
  42  * @modules java.desktop/java.awt:open
  43  * @library /java/awt/patchlib
  44  * @library /lib/client ../
  45  * @build java.desktop/java.awt.Helper
  46  * @build ExtendedRobot SystemTrayIconHelper
  47  * @run main TrayIconEventModifiersTest
  48  */
  49 
  50 public class TrayIconEventModifiersTest {
  51 
  52     Image image;
  53 
  54     TrayIcon icon;
  55     ExtendedRobot robot;
  56 
  57     Object mouseLock = new Object();
  58 
  59     String caption = "Sample Icon";
  60     boolean mousePressed = false;
  61     boolean mouseReleased = false;
  62     boolean mouseClicked = false;
  63     int modifiers, releaseModifiers, clickModifiers;
  64 
  65     int[] buttonTypes = {
  66         InputEvent.BUTTON1_MASK,
  67         InputEvent.BUTTON2_MASK,
  68         InputEvent.BUTTON3_MASK
  69     };
  70 
  71     String[] buttonNames = {
  72         "BUTTON1",
  73         "BUTTON2",
  74         "BUTTON3"
  75     };
  76 
  77     int[] buttonMasks = {
  78         InputEvent.BUTTON1_DOWN_MASK,
  79         InputEvent.BUTTON2_DOWN_MASK,
  80         InputEvent.BUTTON3_DOWN_MASK
  81     };
  82 
  83     static int[] keyTypes = {
  84         KeyEvent.VK_SHIFT,
  85         KeyEvent.VK_CONTROL
  86     };
  87 
  88     static String[] keyNames = {
  89         "SHIFT",
  90         "CONTROL"
  91     };
  92 
  93     static int[] keyMasks = {
  94         KeyEvent.SHIFT_DOWN_MASK,
  95         KeyEvent.CTRL_DOWN_MASK
  96     };
  97 
  98     public static void main(String[] args) throws Exception {
  99         if (! SystemTray.isSupported()) {
 100             System.out.println("SystemTray not supported on the platform under test. " +
 101                     "Marking the test passed");
 102         } else {
 103             if (System.getProperty("os.name").toLowerCase().startsWith("win"))
 104                 System.err.println("Test can fail if the icon hides to a tray icons pool" +
 105                         "in Windows 7, which is behavior by default.\n" +
 106                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
 107                         "\"Always show all icons and notifications on the taskbar\" true " +
 108                         "to avoid this problem. Or change behavior only for Java SE tray " +
 109                         "icon and rerun test.");
 110 
 111             System.out.println(System.getProperty("os.arch"));
 112 
 113             if (SystemTrayIconHelper.isOel7()) {
 114                 System.out.println("OEL 7 doesn't support click modifiers in " +
 115                         "systray. Skipped");
 116                 return;
 117             }
 118 
 119             new TrayIconEventModifiersTest().doTest();
 120         }
 121     }
 122 
 123     public TrayIconEventModifiersTest() throws Exception {
 124         robot = new ExtendedRobot();
 125         EventQueue.invokeAndWait(this::initializeGUI);
 126     }
 127 
 128     private void initializeGUI() {
 129 
 130         SystemTray tray = SystemTray.getSystemTray();
 131         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
 132         icon.addMouseListener(new MouseAdapter() {
 133             public void mousePressed(MouseEvent event) {
 134                 if (!icon.equals(event.getSource()))
 135                     throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " +
 136                             "did not return TrayIcon object");
 137 
 138                 mousePressed = true;
 139                 modifiers = event.getModifiersEx();
 140                 System.out.println("Icon mousePressed " + modifiers);
 141                 synchronized (mouseLock) {
 142                     try {
 143                         mouseLock.notifyAll();
 144                     } catch (Exception e) {
 145                     }
 146                 }
 147             }
 148 
 149             public void mouseReleased(MouseEvent event) {
 150                 if (!icon.equals(event.getSource()))
 151                     throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " +
 152                             "did not return TrayIcon object");
 153 
 154                 mouseReleased = true;
 155                 releaseModifiers = event.getModifiersEx();
 156                 System.out.println("Icon mouseReleased " + releaseModifiers);
 157                 synchronized (mouseLock) {
 158                     try {
 159                         mouseLock.notifyAll();
 160                     } catch (Exception e) {
 161                     }
 162                 }
 163             }
 164 
 165             public void mouseClicked(MouseEvent event) {
 166                 if (!icon.equals(event.getSource()))
 167                     throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " +
 168                             "did not return TrayIcon object");
 169 
 170                 mouseClicked = true;
 171                 clickModifiers = event.getModifiersEx();
 172                 System.out.println("Icon mouseClickedd " + clickModifiers);
 173                 synchronized (mouseLock) {
 174                     try {
 175                         mouseLock.notifyAll();
 176                     } catch (Exception e) {
 177                     }
 178                 }
 179             }
 180         });
 181 
 182         try {
 183             tray.add(icon);
 184         } catch (Exception e) {
 185             throw new RuntimeException(e);
 186         }
 187     }
 188 
 189     void doTest() throws Exception {
 190 
 191         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 192         if (iconPosition == null)
 193             throw new RuntimeException("Unable to find the icon location!");
 194 
 195         robot.mouseMove(iconPosition.x, iconPosition.y);
 196         robot.waitForIdle();
 197 
 198         for (int i = 0; i < buttonTypes.length; i++) {
 199             for (int j = 0; j < keyTypes.length; j++) {
 200                 mousePressed = false;
 201 
 202                 robot.keyPress(keyTypes[j]);
 203                 robot.waitForIdle();
 204                 robot.mousePress(buttonTypes[i]);
 205 
 206                 if (! mousePressed) {
 207                     synchronized (mouseLock) {
 208                         try {
 209                             mouseLock.wait(3000);
 210                         } catch (Exception e) {
 211                         }
 212                     }
 213                 }
 214                 if (! mousePressed) {
 215                     if (! SystemTrayIconHelper.skip(buttonTypes[i]))
 216                         throw new RuntimeException("FAIL: mousePressed not triggered when " +
 217                                 keyNames[j] + " + " + buttonNames[i] + " pressed");
 218                 } else {
 219                     int onMask = buttonMasks[i] | keyMasks[j];
 220                     if ((modifiers & onMask) != onMask) {
 221                         throw new RuntimeException("FAIL: getModifiersEx did not return " +
 222                                 "the correct value when " + keyNames[j] + " + " +
 223                                 buttonNames[i] + " pressed");
 224                     }
 225                 }
 226 
 227                 mouseReleased = false;
 228                 mouseClicked = false;
 229                 robot.mouseRelease(buttonTypes[i]);
 230 
 231                 if (! mouseReleased) {
 232                     synchronized (mouseLock) {
 233                         try {
 234                             mouseLock.wait(3000);
 235                         } catch (Exception e) {
 236                         }
 237                     }
 238                 }
 239 
 240                 robot.waitForIdle(1000);
 241                 robot.keyRelease(keyTypes[j]);
 242                 robot.waitForIdle(1000);
 243 
 244                 if (! mouseReleased) {
 245                     if (! SystemTrayIconHelper.skip(buttonTypes[i]))
 246                         throw new RuntimeException("FAIL: mouseReleased not triggered when " +
 247                                 keyNames[j] + " + " + buttonNames[i] + " released");
 248                 } else {
 249                     int onMask = keyMasks[j];
 250                     if ((releaseModifiers & onMask) != onMask)
 251                         throw new RuntimeException("FAIL: getModifiersEx did not return " +
 252                                 "the correct value when " + keyNames[j] + " + " +
 253                                 buttonNames[i] + " released");
 254                 }
 255                 if (! mouseClicked) {
 256                     throw new RuntimeException("FAIL: mouseClicked not triggered when " +
 257                             keyNames[j] + " + " + buttonNames[i] +
 258                             " pressed & released");
 259                 } else {
 260                     int onMask = keyMasks[j];
 261                     if ((clickModifiers & onMask) != onMask)
 262                         throw new RuntimeException("FAIL: getModifiersEx did not return " +
 263                                 "the correct value when " + keyNames[j] + " + " +
 264                                 buttonNames[i] + " pressed & released");
 265                 }
 266                 robot.type(KeyEvent.VK_ESCAPE);
 267             }
 268         }
 269     }
 270 }
 271