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