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  * @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 /java/awt/patchlib
  35  * @library ../../../../lib/testlibrary ../
  36  * @build java.desktop/java.awt.Helper
  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 
  50     String caption = "Sample Icon";
  51 
  52     int[] buttonTypes = {
  53         InputEvent.BUTTON1_MASK,
  54         InputEvent.BUTTON2_MASK,
  55         InputEvent.BUTTON3_MASK
  56     };
  57 
  58     String[] buttonNames = {
  59         "BUTTON1",
  60         "BUTTON2",
  61         "BUTTON3"
  62     };
  63 
  64     public static void main(String[] args) throws Exception {
  65         if (! SystemTray.isSupported()) {
  66             System.out.println("SystemTray not supported on the platform under test. " +
  67                     "Marking the test passed");
  68         } else {
  69             if (System.getProperty("os.name").toLowerCase().startsWith("mac")) {
  70                 isMacOS = true;
  71             } else if (SystemTrayIconHelper.isOel7()) {
  72                 System.out.println("OEL 7 doesn't support double click in " +
  73                         "systray. Skipped");
  74                 return;
  75             }
  76             new TrayIconMouseTest().doTest();
  77         }
  78     }
  79 
  80     TrayIconMouseTest() throws Exception{
  81         robot = new ExtendedRobot();
  82         EventQueue.invokeAndWait(this::initializeGUI);
  83     }
  84 
  85     void initializeGUI() {
  86 
  87         SystemTray tray = SystemTray.getSystemTray();
  88         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), caption);
  89         icon.addActionListener(event -> {
  90             actionPerformed = true;
  91             synchronized (actionLock) {
  92                 try {
  93                     actionLock.notifyAll();
  94                 } catch (Exception e) {
  95                 }
  96             }
  97         });
  98         try {
  99             tray.add(icon);
 100         } catch (Exception e) {
 101             throw new RuntimeException(e);
 102         }
 103     }
 104 
 105     private void doTest() throws Exception {
 106 
 107         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 108         if (iconPosition == null)
 109             throw new RuntimeException("Unable to find the icon location!");
 110 
 111         robot.mouseMove(iconPosition.x, iconPosition.y);
 112         robot.waitForIdle();
 113 
 114         for (int i = 0; i < buttonTypes.length; i++) {
 115             actionPerformed = false;
 116             robot.click(buttonTypes[i]);
 117             robot.waitForIdle(6000);
 118 
 119             if (isMacOS && actionPerformed && i == 2) {
 120 
 121             }else if (isMacOS && i == 2) {
 122                 throw new RuntimeException("FAIL: ActionEvent NOT triggered when " +
 123                         buttonNames[i] + " is single clicked on Mac OS");
 124             }else if (actionPerformed) {
 125                 throw new RuntimeException("FAIL: ActionEvent triggered when " +
 126                         buttonNames[i] + " is single clicked");
 127             }
 128         }
 129 
 130         if(!isMacOS) {
 131             for (int i = 0; i < buttonTypes.length; i++) {
 132                 for (int j = 0; j < buttonTypes.length; j++) {
 133                     if (j != i) {
 134                         actionPerformed = false;
 135                         robot.mousePress(buttonTypes[i]);
 136                         robot.mousePress(buttonTypes[j]);
 137                         robot.mouseRelease(buttonTypes[j]);
 138                         robot.mouseRelease(buttonTypes[i]);
 139 
 140                         robot.waitForIdle();
 141 
 142                         if (actionPerformed)
 143                             throw new RuntimeException("FAIL: ActionEvent triggered when " +
 144                                     buttonNames[i] + " and " + buttonNames[j] +
 145                                     " is clicked and released");
 146                     }
 147                 }
 148             }
 149 
 150             for (int i = 0; i < buttonTypes.length; i++) {
 151                 actionPerformed = false;
 152                 robot.mousePress(buttonTypes[i]);
 153                 robot.delay(50);
 154                 robot.mouseRelease(buttonTypes[i]);
 155                 robot.delay(50);
 156                 robot.mousePress(buttonTypes[i]);
 157                 robot.delay(50);
 158                 robot.mouseRelease(buttonTypes[i]);
 159 
 160                 if (i == 0) {
 161                     if (! actionPerformed) {
 162                         synchronized (actionLock) {
 163                             try {
 164                                 actionLock.wait(6000);
 165                             } catch (Exception e) {
 166                             }
 167                         }
 168                     }
 169                     if (! actionPerformed)
 170                         throw new RuntimeException("FAIL: ActionEvent not triggered when " +
 171                                 buttonNames[i] + " is double clicked");
 172                 } else {
 173                     robot.waitForIdle();
 174 
 175                     if (actionPerformed)
 176                         throw new RuntimeException("FAIL: ActionEvent triggered when " +
 177                                 buttonNames[i] + " is double clicked");
 178                 }
 179             }
 180         }
 181     }
 182 }