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