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 if MouseEvents triggered by TrayIcon are visible
  31  *          by an AWTEventListener added to the Toolkit. It also
  32  *          checks if all listeners are triggered when AWTEventListeners
  33  *          and MouseListeners are added.
  34  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  35  * @library ../../../../lib/testlibrary ../
  36  * @build ExtendedRobot SystemTrayIconHelper
  37  * @run main MouseEventMaskTest
  38  */
  39 
  40 public class MouseEventMaskTest {
  41 
  42     TrayIcon icon;
  43     Robot robot;
  44     int[] buttonTypes = {
  45         InputEvent.BUTTON1_MASK,
  46         InputEvent.BUTTON2_MASK,
  47         InputEvent.BUTTON3_MASK
  48     };
  49 
  50     String[] buttonNames = {
  51         "BUTTON1",
  52         "BUTTON2",
  53         "BUTTON3"
  54     };
  55 
  56     boolean mouseEventTriggered = false;
  57     boolean mouseMotionEventTriggered = false;
  58     Object mouseEventLock = new Object();
  59     Object mouseMotionEventLock = new Object();
  60     boolean mouseMotionTest, mouseTest;
  61 
  62     public static void main(String[] args) throws Exception {
  63         if (! SystemTray.isSupported()) {
  64             System.out.println("SystemTray not supported on the platform under test. " +
  65                     "Marking the test passed");
  66         } else {
  67             if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  68                 System.err.println("Test can fail if the icon hides to a tray icons pool " +
  69                         "in Windows 7, which is behavior by default.\n" +
  70                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
  71                         "\"Always show all icons and notifications on the taskbar\" true " +
  72                         "to avoid this problem. Or change behavior only for Java SE tray " +
  73                         "icon and rerun test.");
  74             }
  75             new MouseEventMaskTest().doTest();
  76         }
  77     }
  78 
  79     public MouseEventMaskTest() throws Exception{
  80         EventQueue.invokeAndWait(this::initializeGUI);
  81     }
  82 
  83     void initializeGUI() {
  84 
  85         SystemTray tray = SystemTray.getSystemTray();
  86         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
  87 
  88         Toolkit.getDefaultToolkit().addAWTEventListener(event -> {
  89             if (mouseTest) {
  90                 if (! event.getSource().getClass().getName().contains("Canvas")) {
  91                     if (!icon.equals(event.getSource()))
  92                         throw new RuntimeException("FAIL: MouseEvent not triggered for icon " + event);
  93 
  94                     mouseEventTriggered = true;
  95                     synchronized (mouseEventLock) {
  96                         try {
  97                             mouseEventLock.notifyAll();
  98                         } catch (Exception e) {
  99                         }
 100                     }
 101                 }
 102             }
 103         }, AWTEvent.MOUSE_EVENT_MASK);
 104         Toolkit.getDefaultToolkit().addAWTEventListener(event -> {
 105             if (mouseMotionTest) {
 106                 if (! event.getSource().getClass().getName().contains("Canvas")) {
 107                     if (!icon.equals(event.getSource()))
 108                         throw new RuntimeException("FAIL: MouseMotionEvent not triggered for icon " + event);
 109 
 110                     mouseMotionEventTriggered = true;
 111                     synchronized (mouseMotionEventLock) {
 112                         try {
 113                             mouseMotionEventLock.notifyAll();
 114                         } catch (Exception e) {
 115                         }
 116                     }
 117                 }
 118             }
 119         }, AWTEvent.MOUSE_MOTION_EVENT_MASK);
 120 
 121         try {
 122             tray.add(icon);
 123         } catch (AWTException e) {
 124             throw new RuntimeException(e);
 125         }
 126     }
 127 
 128     void doTest() throws Exception {
 129 
 130         robot = new Robot();
 131 
 132         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 133         if (iconPosition == null)
 134             throw new RuntimeException("Unable to find the icon location!");
 135 
 136         robot.mouseMove(iconPosition.x, iconPosition.y);
 137         robot.waitForIdle();
 138 
 139         for (int i = 0; i < buttonTypes.length; i++) {
 140             System.out.println("Verify button "+buttonTypes[i]);
 141             mouseTest = true;
 142             mouseEventTriggered = false;
 143             robot.mousePress(buttonTypes[i]);
 144 
 145             if (! mouseEventTriggered) {
 146                 synchronized (mouseEventLock) {
 147                     try {
 148                         mouseEventLock.wait(3000);
 149                     } catch (Exception e) {
 150                     }
 151                 }
 152             }
 153             if (! mouseEventTriggered)
 154                 if (! SystemTrayIconHelper.skip(buttonTypes[i]) )
 155                     throw new RuntimeException("FAIL: AWTEventListener not notified when " +
 156                         buttonNames[i] + " pressed on TrayIcon");
 157 
 158             mouseEventTriggered = false;
 159             robot.mouseRelease(buttonTypes[i]);
 160             if (! mouseEventTriggered) {
 161                 synchronized (mouseEventLock) {
 162                     try {
 163                         mouseEventLock.wait(3000);
 164                     } catch (Exception e) {
 165                     }
 166                 }
 167             }
 168 
 169             if (! mouseEventTriggered)
 170                 throw new RuntimeException("FAIL: AWTEventListener not notified when " +
 171                         buttonNames[i] + " released on TrayIcon");
 172         }
 173 
 174         mouseMotionTest = true;
 175         mouseTest = false;
 176         mouseMotionEventTriggered = false;
 177 
 178         for (int i = 0; i < 20; i++) {
 179             robot.mouseMove(iconPosition.x + i, iconPosition.y);
 180             robot.delay(25);
 181         }
 182         if (! mouseMotionEventTriggered) {
 183             synchronized (mouseMotionEventLock) {
 184                 try {
 185                     mouseMotionEventLock.wait(3000);
 186                 } catch (Exception e) {
 187                 }
 188             }
 189         }
 190         if (! mouseMotionEventTriggered)
 191             if (! SystemTrayIconHelper.skip(0) )
 192                 throw new RuntimeException("FAIL: AWTEventListener not notified when " +
 193                         "mouse moved");
 194     }
 195 }