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