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             } else if (SystemTrayIconHelper.isOel7()) {
  70                 System.out.println("OEL 7 doesn't support double click in " +
  71                         "systray. Skipped");
  72                 return;
  73             }
  74             new TrayIconMouseTest().doTest();
  75         }
  76     }
  77 
  78     TrayIconMouseTest() throws Exception{
  79         robot = new ExtendedRobot();
  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), caption);
  87         icon.addActionListener(event -> {
  88             actionPerformed = true;
  89             synchronized (actionLock) {
  90                 try {
  91                     actionLock.notifyAll();
  92                 } catch (Exception e) {
  93                 }
  94             }
  95         });
  96         try {
  97             tray.add(icon);
  98         } catch (Exception e) {
  99             throw new RuntimeException(e);
 100         }
 101     }
 102 
 103     private void doTest() throws Exception {
 104 
 105         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 106         if (iconPosition == null)
 107             throw new RuntimeException("Unable to find the icon location!");
 108 
 109         robot.mouseMove(iconPosition.x, iconPosition.y);
 110         robot.waitForIdle();
 111 
 112         for (int i = 0; i < buttonTypes.length; i++) {
 113             actionPerformed = false;
 114             robot.click(buttonTypes[i]);
 115             robot.waitForIdle(6000);
 116 
 117             if (isMacOS && actionPerformed && i == 2) {
 118 
 119             }else if (isMacOS && i == 2) {
 120                 throw new RuntimeException("FAIL: ActionEvent NOT triggered when " +
 121                         buttonNames[i] + " is single clicked on Mac OS");
 122             }else if (actionPerformed) {
 123                 throw new RuntimeException("FAIL: ActionEvent triggered when " +
 124                         buttonNames[i] + " is single clicked");
 125             }
 126         }
 127 
 128         if(!isMacOS) {
 129             for (int i = 0; i < buttonTypes.length; i++) {
 130                 for (int j = 0; j < buttonTypes.length; j++) {
 131                     if (j != i) {
 132                         actionPerformed = false;
 133                         robot.mousePress(buttonTypes[i]);
 134                         robot.mousePress(buttonTypes[j]);
 135                         robot.mouseRelease(buttonTypes[j]);
 136                         robot.mouseRelease(buttonTypes[i]);
 137 
 138                         robot.waitForIdle();
 139 
 140                         if (actionPerformed)
 141                             throw new RuntimeException("FAIL: ActionEvent triggered when " +
 142                                     buttonNames[i] + " and " + buttonNames[j] +
 143                                     " is clicked and released");
 144                     }
 145                 }
 146             }
 147 
 148             for (int i = 0; i < buttonTypes.length; i++) {
 149                 actionPerformed = false;
 150                 robot.mousePress(buttonTypes[i]);
 151                 robot.delay(50);
 152                 robot.mouseRelease(buttonTypes[i]);
 153                 robot.delay(50);
 154                 robot.mousePress(buttonTypes[i]);
 155                 robot.delay(50);
 156                 robot.mouseRelease(buttonTypes[i]);
 157 
 158                 if (i == 0) {
 159                     if (! actionPerformed) {
 160                         synchronized (actionLock) {
 161                             try {
 162                                 actionLock.wait(6000);
 163                             } catch (Exception e) {
 164                             }
 165                         }
 166                     }
 167                     if (! actionPerformed)
 168                         throw new RuntimeException("FAIL: ActionEvent not triggered when " +
 169                                 buttonNames[i] + " is double clicked");
 170                 } else {
 171                     robot.waitForIdle();
 172 
 173                     if (actionPerformed)
 174                         throw new RuntimeException("FAIL: ActionEvent triggered when " +
 175                                 buttonNames[i] + " is double clicked");
 176                 }
 177             }
 178         }
 179     }
 180 }