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 
  55     static final int[] buttonTypes = {
  56         InputEvent.BUTTON1_MASK,
  57         InputEvent.BUTTON2_MASK,
  58         InputEvent.BUTTON3_MASK
  59     };
  60 
  61     static final String[] buttonNames = {
  62         "BUTTON1",
  63         "BUTTON2",
  64         "BUTTON3"
  65     };
  66 
  67     public static void main(String[] args) throws Exception {
  68         if (! SystemTray.isSupported()) {
  69             System.out.println("SystemTray not supported on the platform under test. " +
  70                                "Marking the test passed");
  71         } else {
  72             new FunctionalityCheck().doTest();
  73         }
  74     }
  75 
  76     FunctionalityCheck() throws Exception {
  77         robot = new ExtendedRobot();
  78         EventQueue.invokeAndWait(this::initializeGUI);
  79     }
  80 
  81     void initializeGUI() {
  82         SystemTray tray = SystemTray.getSystemTray();
  83         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
  84         icon.addActionListener(event -> {
  85             actionPerformed = true;
  86             synchronized (actionLock) {
  87                 try {
  88                     actionLock.notifyAll();
  89                 } catch (Exception e) {
  90                 }
  91             }
  92         });
  93         icon.addMouseListener(new MouseAdapter() {
  94             public void mousePressed(MouseEvent event) {
  95                 mousePressed = true;
  96                 Point p = event.getPoint();
  97                 if (p.x != event.getX() || p.y != event.getY())
  98                     throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
  99                             "not return the same value as getX/getY " +
 100                             "for mousePressed");
 101 
 102                 if (! icon.equals(event.getSource()))
 103                     throw new RuntimeException("FAIL: mousePressed: MouseEvent.getSource " +
 104                             "did not return TrayIcon object");
 105 
 106                 synchronized (pressLock) {
 107                     try {
 108                         pressLock.notifyAll();
 109                     } catch (Exception e) {
 110                     }
 111                 }
 112             }
 113 
 114             public void mouseReleased(MouseEvent event) {
 115                 mouseReleased = true;
 116                 Point p = event.getPoint();
 117                 if (p.x != event.getX() || p.y != event.getY())
 118                     throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
 119                             "not return the same value as getX/getY " +
 120                             "for mouseReleased");
 121 
 122                 if (! icon.equals(event.getSource()))
 123                     throw new RuntimeException("FAIL: mouseReleased: MouseEvent.getSource " +
 124                             "did not return TrayIcon object");
 125 
 126                 synchronized (releaseLock) {
 127                     try {
 128                         releaseLock.notifyAll();
 129                     } catch (Exception e) {
 130                     }
 131                 }
 132             }
 133 
 134             public void mouseClicked(MouseEvent event) {
 135                 mouseClicked = true;
 136                 Point p = event.getPoint();
 137                 if (p.x != event.getX() || p.y != event.getY())
 138                     throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
 139                             "not return the same value as getX/getY " +
 140                             "for mouseClicked");
 141 
 142                 if (! icon.equals(event.getSource()))
 143                     throw new RuntimeException("FAIL: mouseClicked: MouseEvent.getSource " +
 144                             "did not return TrayIcon object");
 145 
 146                 synchronized (clickLock) {
 147                     try {
 148                         clickLock.notifyAll();
 149                     } catch (Exception e) {
 150                     }
 151                 }
 152             }
 153         });
 154 
 155         icon.addMouseMotionListener(new MouseMotionAdapter() {
 156             public void mouseMoved(MouseEvent event) {
 157                 mouseMoved = true;
 158                 Point p = event.getPoint();
 159                 if (p.x != event.getX() || p.y != event.getY())
 160                     throw new RuntimeException("FAIL: MouseEvent.getPoint() did " +
 161                             "not return the same value as getX/getY " +
 162                             "for mouseMoved");
 163 
 164                 if (! icon.equals(event.getSource()))
 165                     throw new RuntimeException("FAIL: mouseMoved: MouseEvent.getSource " +
 166                             "did not return TrayIcon object");
 167 
 168                 synchronized (moveLock) {
 169                     try {
 170                         moveLock.notifyAll();
 171                     } catch (Exception e) {
 172                     }
 173                 }
 174             }
 175         });
 176 
 177         try {
 178             tray.add(icon);
 179         } catch (Exception e) {
 180             throw new RuntimeException(e);
 181         }
 182     }
 183 
 184     private void doTest() throws Exception {
 185 
 186 
 187 
 188         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 189         if (iconPosition == null)
 190             throw new RuntimeException("Unable to find the icon location!");
 191 
 192         robot.mouseMove(iconPosition.x, iconPosition.y);
 193         robot.waitForIdle(2000);
 194 
 195         SystemTrayIconHelper.doubleClick(robot);
 196 
 197         if (! actionPerformed) {
 198             synchronized (actionLock) {
 199                 try {
 200                     actionLock.wait(3000);
 201                 } catch (Exception e) {
 202                 }
 203             }
 204         }
 205         if (! actionPerformed)
 206             throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is double clicked");
 207 
 208         for (int i = 0; i < buttonTypes.length; i++) {
 209             mousePressed = false;
 210             robot.mousePress(buttonTypes[i]);
 211 
 212             if (! mousePressed) {
 213                 synchronized (pressLock) {
 214                     try {
 215                         pressLock.wait(3000);
 216                     } catch (Exception e) {
 217                     }
 218                 }
 219             }
 220             if (! mousePressed)
 221                 if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
 222                     throw new RuntimeException("FAIL: mousePressed not triggered when " +
 223                             buttonNames[i] + " pressed");
 224 
 225             mouseReleased = false;
 226             mouseClicked = false;
 227             robot.mouseRelease(buttonTypes[i]);
 228 
 229             if (! mouseReleased) {
 230                 synchronized (releaseLock) {
 231                     try {
 232                         releaseLock.wait(3000);
 233                     } catch (Exception e) {
 234                     }
 235                 }
 236             }
 237             if (! mouseReleased)
 238                 if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
 239                     throw new RuntimeException("FAIL: mouseReleased not triggered when " +
 240                             buttonNames[i] + " released");
 241 
 242             if (! mouseClicked) {
 243                 synchronized (clickLock) {
 244                     try {
 245                         clickLock.wait(3000);
 246                     } catch (Exception e) {
 247                     }
 248                 }
 249             }
 250             if (! mouseClicked)
 251                 throw new RuntimeException("FAIL: mouseClicked not triggered when " +
 252                         buttonNames[i] + " pressed & released");
 253         }
 254 
 255         mouseMoved = false;
 256         robot.mouseMove(iconPosition.x + 100, iconPosition.y);
 257         robot.glide(iconPosition.x, iconPosition.y);
 258 
 259         if (! mouseMoved)
 260             if (! SystemTrayIconHelper.skip(0) )
 261                 throw new RuntimeException("FAIL: mouseMoved not triggered even when mouse moved over the icon");
 262     }
 263 }