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 = SystemTrayIconHelper.isOel7();
  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         if (isOEL7) {
 201             // close tray
 202             robot.mouseMove(100,100);
 203             robot.click(InputEvent.BUTTON1_MASK);
 204             robot.waitForIdle(2000);
 205         }
 206 
 207         robot.mouseMove(iconPosition.x, iconPosition.y);
 208         robot.waitForIdle();
 209         if(!isOEL7) {
 210             SystemTrayIconHelper.doubleClick(robot);
 211 
 212             if (!actionPerformed) {
 213                 synchronized (actionLock) {
 214                     try {
 215                         actionLock.wait(10000);
 216                     } catch (Exception e) {
 217                     }
 218                 }
 219             }
 220             if (!actionPerformed)
 221                 throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");
 222         }
 223 
 224         for (int i = 0; i < buttonTypes.length; i++) {
 225             mousePressed = false;
 226             if(isOEL7) {
 227                 SystemTrayIconHelper.openTrayIfNeeded(robot);
 228                 robot.mouseMove(iconPosition.x, iconPosition.y);
 229                 robot.click(buttonTypes[i]);
 230             } else {
 231                 robot.mousePress(buttonTypes[i]);
 232             }
 233 
 234             if (! mousePressed) {
 235                 synchronized (pressLock) {
 236                     try {
 237                         pressLock.wait(6000);
 238                     } catch (Exception e) {
 239                     }
 240                 }
 241             }
 242             if (! mousePressed)
 243                 if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
 244                     throw new RuntimeException("FAIL: mousePressed not triggered when " +
 245                             buttonNames[i] + " pressed");
 246 
 247             mouseReleased = false;
 248             mouseClicked = false;
 249             if(isOEL7) {
 250                 SystemTrayIconHelper.openTrayIfNeeded(robot);
 251                 robot.mouseMove(iconPosition.x, iconPosition.y);
 252                 robot.click(buttonTypes[i]);
 253             } else {
 254                 robot.mouseRelease(buttonTypes[i]);
 255             }
 256 
 257             if (! mouseReleased) {
 258                 synchronized (releaseLock) {
 259                     try {
 260                         releaseLock.wait(6000);
 261                     } catch (Exception e) {
 262                     }
 263                 }
 264             }
 265             if (! mouseReleased)
 266                 throw new RuntimeException("FAIL: mouseReleased not triggered when " +
 267                         buttonNames[i] + " released");
 268 
 269             if (! mouseClicked) {
 270                 synchronized (clickLock) {
 271                     try {
 272                         clickLock.wait(6000);
 273                     } catch (Exception e) {
 274                     }
 275                 }
 276             }
 277             if (! mouseClicked)
 278                 throw new RuntimeException("FAIL: mouseClicked not triggered when " +
 279                         buttonNames[i] + " pressed & released");
 280         }
 281 
 282         if (!isOEL7) {
 283             mouseMoved = false;
 284             robot.mouseMove(iconPosition.x + 100, iconPosition.y);
 285             robot.glide(iconPosition.x, iconPosition.y);
 286 
 287             if (!mouseMoved)
 288                 if (!SystemTrayIconHelper.skip(0))
 289                     throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");
 290         }
 291     }
 292 }