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