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