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