1 /*
   2  * Copyright (c) 2007, 2016, 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.EventQueue;
  25 import java.awt.Point;
  26 import java.awt.SystemTray;
  27 import java.awt.TrayIcon;
  28 import java.awt.event.InputEvent;
  29 import java.awt.image.BufferedImage;
  30 
  31 /*
  32  * @test 6384991
  33  * @summary Check if ActionEvent is triggered by a TrayIcon when
  34  *          it is double clicked with mouse button 1 on windows
  35  *          or single clicked with button 3 on Mac OS X
  36  *          or single clicked with button 1 on rest.
  37  * @library /java/awt/patchlib
  38  * @library ../../../../lib/testlibrary ../
  39  * @build java.desktop/java.awt.Helper
  40  * @build ExtendedRobot SystemTrayIconHelper
  41  * @run main TrayIconMouseTest
  42  */
  43 
  44 public class TrayIconMouseTest {
  45 
  46     TrayIcon icon;
  47     ExtendedRobot robot;
  48     boolean actionPerformed = false;
  49     Object actionLock = new Object();
  50     static boolean isMacOS = false;
  51     static boolean isWinOS = false;
  52     static boolean isOelOS = false;
  53     String caption = "Sample Icon";
  54     int[] buttonTypes = {
  55         InputEvent.BUTTON1_MASK,
  56         InputEvent.BUTTON2_MASK,
  57         InputEvent.BUTTON3_MASK
  58     };
  59     String[] buttonNames = {
  60         "BUTTON1",
  61         "BUTTON2",
  62         "BUTTON3"
  63     };
  64 
  65     public static void main(String[] args) throws Exception {
  66         if (!SystemTray.isSupported()) {
  67             System.out.println("SystemTray not supported on the platform "
  68                     + "under test. Marking the test passed");
  69         } else {
  70             String osName = System.getProperty("os.name").toLowerCase();
  71             if (osName.startsWith("mac")) {
  72                 isMacOS = true;
  73             } else if (osName.startsWith("win")) {
  74                 isWinOS = true;
  75             } else {
  76                 isOelOS = SystemTrayIconHelper.isOel7();
  77             }
  78             new TrayIconMouseTest().doTest();
  79         }
  80     }
  81 
  82     TrayIconMouseTest() 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(
  90                 new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
  91         icon.addActionListener(event -> {
  92             actionPerformed = true;
  93             synchronized (actionLock) {
  94                 try {
  95                     actionLock.notifyAll();
  96                 } catch (Exception e) {
  97                 }
  98             }
  99         });
 100         try {
 101             tray.add(icon);
 102         } catch (Exception e) {
 103             throw new RuntimeException(e);
 104         }
 105     }
 106 
 107     private void doTest() throws Exception {
 108         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 109         if (iconPosition == null) {
 110             throw new RuntimeException("Unable to find the icon location!");
 111         }
 112         robot.mouseMove(iconPosition.x, iconPosition.y);
 113         robot.waitForIdle();
 114 
 115         for (int i = 0; i < buttonTypes.length; i++) {
 116             actionPerformed = false;
 117             robot.click(buttonTypes[i]);
 118             robot.waitForIdle();
 119             delayIfRequired();
 120 
 121             if (isMacOS && i == 2 && !actionPerformed) {
 122                 throw new RuntimeException("FAIL: ActionEvent NOT triggered "
 123                     + "when " + buttonNames[i] + " is single clicked on Mac");
 124             } else if (isWinOS && actionPerformed) {
 125                 throw new RuntimeException("FAIL: ActionEvent triggered "
 126                     + "when " + buttonNames[i] + " is single clicked");
 127             } else if (!isMacOS && !isWinOS && i == 0 && !actionPerformed) {
 128                 throw new RuntimeException("FAIL: ActionEvent NOT triggered "
 129                     + "when " + buttonNames[i] + " is single clicked");
 130             }
 131         }
 132 
 133         if (!isMacOS && !isOelOS) {
 134             for (int i = 0; i < buttonTypes.length; i++) {
 135                 for (int j = 0; j < buttonTypes.length; j++) {
 136                     if (j != i) {
 137                         actionPerformed = false;
 138                         robot.mousePress(buttonTypes[i]);
 139                         robot.mousePress(buttonTypes[j]);
 140                         robot.mouseRelease(buttonTypes[j]);
 141                         robot.mouseRelease(buttonTypes[i]);
 142                         robot.waitForIdle();
 143                         delayIfRequired();
 144 
 145                         if (isWinOS) {
 146                             if (actionPerformed) {
 147                                 throw new RuntimeException(
 148                                     "FAIL: ActionEvent triggered when "
 149                                     + buttonNames[i] + " & " + buttonNames[j]
 150                                     + " is clicked and released");
 151                             }
 152 
 153                         } else if ((i == 0 || j == 0) && !actionPerformed) {
 154                             throw new RuntimeException("FAIL: ActionEvent is "
 155                                 + "NOT triggered when " + buttonNames[i] + " & "
 156                                 + buttonNames[j] + " is pressed & released");
 157                         }
 158                     }
 159                 }
 160             }
 161 
 162             for (int i = 0; i < buttonTypes.length; i++) {
 163                 actionPerformed = false;
 164                 robot.mousePress(buttonTypes[i]);
 165                 robot.mouseRelease(buttonTypes[i]);
 166                 robot.delay(50);
 167                 robot.mousePress(buttonTypes[i]);
 168                 robot.mouseRelease(buttonTypes[i]);
 169                 robot.waitForIdle();
 170                 delayIfRequired();
 171 
 172                 if (i == 0) {
 173                     if (!actionPerformed) {
 174                         throw new RuntimeException("FAIL: ActionEvent not "
 175                                 + "triggered when " + buttonNames[i]
 176                                 + " is double clicked");
 177                     }
 178                 } else if (actionPerformed) {
 179                     throw new RuntimeException("FAIL: ActionEvent "
 180                             + "triggered when " + buttonNames[i]
 181                             + " is double clicked");
 182                 }
 183             }
 184         }
 185     }
 186 
 187     public void delayIfRequired() {
 188         if (!actionPerformed) {
 189             synchronized (actionLock) {
 190                 try {
 191                     actionLock.wait(500);
 192                 } catch (Exception e) {
 193                 }
 194             }
 195         }
 196     }
 197 }