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