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