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  * @bug 6384991
  31  * @summary Check if ActionEvent is triggered by a TrayIcon only when
  32  *          it is double clicked on windows & single clicked on unix
  33  *          using mouse button 1 (or single clicked
  34  *          with button 3 (on Mac OS X))
  35  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  36  * @library ../../../../lib/testlibrary ../
  37  * @build ExtendedRobot SystemTrayIconHelper
  38  * @run main TrayIconMouseTest
  39  */
  40 
  41 public class TrayIconMouseTest {
  42 
  43     TrayIcon icon;
  44     ExtendedRobot robot;
  45 
  46     boolean actionPerformed = false;
  47     Object actionLock = new Object();
  48     static boolean isMacOS = false;
  49     static boolean isWinOS = false;
  50 
  51     String caption = "Sample Icon";
  52 
  53     int[] buttonTypes = {
  54         InputEvent.BUTTON1_MASK,
  55         InputEvent.BUTTON2_MASK,
  56         InputEvent.BUTTON3_MASK
  57     };
  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 under test. " +
  68                     "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 if (SystemTrayIconHelper.isOel7()) {
  76                 System.out.println("OEL 7 doesn't support double click in " +
  77                         "systray. Skipped");
  78                 return;
  79             }
  80             new TrayIconMouseTest().doTest();
  81         }
  82     }
  83 
  84     TrayIconMouseTest() throws Exception{
  85         robot = new ExtendedRobot();
  86         EventQueue.invokeAndWait(this::initializeGUI);
  87     }
  88 
  89     void initializeGUI() {
  90 
  91         SystemTray tray = SystemTray.getSystemTray();
  92         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
  93         icon.addActionListener(event -> {
  94             actionPerformed = true;
  95             synchronized (actionLock) {
  96                 try {
  97                     actionLock.notifyAll();
  98                 } catch (Exception e) {
  99                 }
 100             }
 101         });
 102         try {
 103             tray.add(icon);
 104         } catch (Exception e) {
 105             throw new RuntimeException(e);
 106         }
 107     }
 108 
 109     private void doTest() throws Exception {
 110 
 111         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 112         if (iconPosition == null)
 113             throw new RuntimeException("Unable to find the icon location!");
 114 
 115         robot.mouseMove(iconPosition.x, iconPosition.y);
 116         robot.waitForIdle();
 117 
 118         for (int i = 0; i < buttonTypes.length; i++) {
 119             actionPerformed = false;
 120             robot.click(buttonTypes[i]);
 121             robot.waitForIdle(6000);
 122 
 123             if (isMacOS && actionPerformed && i == 2) {
 124 
 125             }else if (isMacOS && i == 2) {
 126                 throw new RuntimeException("FAIL: ActionEvent NOT triggered when " +
 127                         buttonNames[i] + " is single clicked on Mac OS");
 128             } else if (actionPerformed && isWinOS) {
 129                 throw new RuntimeException("FAIL: ActionEvent triggered when " +
 130                         buttonNames[i] + " is single clicked");
 131             }
 132         }
 133 
 134         if(!isMacOS) {
 135             for (int i = 0; i < buttonTypes.length; i++) {
 136                 for (int j = 0; j < buttonTypes.length; j++) {
 137                     if (j != i) {
 138                         actionPerformed = false;
 139                         robot.mousePress(buttonTypes[i]);
 140                         robot.mousePress(buttonTypes[j]);
 141                         robot.mouseRelease(buttonTypes[j]);
 142                         robot.mouseRelease(buttonTypes[i]);
 143 
 144                         robot.waitForIdle();
 145 
 146                         if (actionPerformed && isWinOS)
 147                             throw new RuntimeException("FAIL: ActionEvent triggered when " +
 148                                     buttonNames[i] + " and " + buttonNames[j] +
 149                                     " is clicked and released");
 150                     }
 151                 }
 152             }
 153 
 154             for (int i = 0; i < buttonTypes.length; i++) {
 155                 actionPerformed = false;
 156                 robot.mousePress(buttonTypes[i]);
 157                 robot.delay(50);
 158                 robot.mouseRelease(buttonTypes[i]);
 159                 robot.delay(50);
 160                 robot.mousePress(buttonTypes[i]);
 161                 robot.delay(50);
 162                 robot.mouseRelease(buttonTypes[i]);
 163 
 164                 if (i == 0) {
 165                     if (! actionPerformed) {
 166                         synchronized (actionLock) {
 167                             try {
 168                                 actionLock.wait(6000);
 169                             } catch (Exception e) {
 170                             }
 171                         }
 172                     }
 173                     if (! actionPerformed)
 174                         throw new RuntimeException("FAIL: ActionEvent not triggered when " +
 175                                 buttonNames[i] + " is double clicked");
 176                 } else {
 177                     robot.waitForIdle();
 178 
 179                     if (actionPerformed)
 180                         throw new RuntimeException("FAIL: ActionEvent triggered when " +
 181                                 buttonNames[i] + " is double clicked");
 182                 }
 183             }
 184         }
 185     }
 186 }