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 triggered when a TrayIcon is double
  31  *          (single, on Mac) clicked is visible by an AWTEventListener
  32  *          added to the Toolkit. It also checks if all listeners are
  33  *          triggered when multiple AWTEventListeners and ActionListeners
  34  *          are added.
  35  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  36  * @library ../../../../lib/testlibrary ../
  37  * @build ExtendedRobot SystemTrayIconHelper
  38  * @run main ActionEventMask
  39  */
  40 
  41 public class ActionEventMask {
  42 
  43     private Image image;
  44 
  45     TrayIcon icon;
  46     ExtendedRobot robot;
  47 
  48     boolean actionPerformed = false;
  49     boolean listenersInvoked = false;
  50     Object actionLock = new Object();
  51     Object listenersLock = new Object();
  52     static boolean isMacOS = false;
  53     static final int clickDelay = 50;
  54 
  55     ActionListener[] listeners;
  56     boolean[] listenerStatus;
  57 
  58     Object lLock = new Object();
  59     boolean doTest, listenerAdded;
  60     Button b1;
  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( System.getProperty("os.name").toLowerCase()
  70                     .contains("linux") && System.getProperty("os.version")
  71                     .toLowerCase().contains("el7")) {
  72                 System.out.println("OEL 7 doesn't support double click in " +
  73                         "systray. Skipped");
  74                 return;
  75             }
  76             new ActionEventMask().doTest();
  77         }
  78     }
  79 
  80     public ActionEventMask() throws Exception {
  81         EventQueue.invokeAndWait(this::initializeGUI);
  82     }
  83 
  84     void initializeGUI() {
  85         SystemTray tray = SystemTray.getSystemTray();
  86         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
  87 
  88         Toolkit.getDefaultToolkit().addAWTEventListener(event -> {
  89             if (doTest) {
  90                 System.out.println("ActionListener AWTEventListener");
  91                 if (! icon.equals(event.getSource())) {
  92                     throw new RuntimeException("FAIL: ActionEvent not triggered for icon");
  93                 }
  94                 actionPerformed = true;
  95                 synchronized (actionLock) {
  96                     try {
  97                         actionLock.notifyAll();
  98                     } catch (Exception e) {
  99                     }
 100                 }
 101             }
 102         }, AWTEvent.ACTION_EVENT_MASK);
 103 
 104         try {
 105             tray.add(icon);
 106         } catch (AWTException e) {
 107             throw new RuntimeException(e);
 108         }
 109 
 110         listeners = new ActionListener[3];
 111         listenerStatus = new boolean[3];
 112         for (int i = 0; i < listeners.length; i++) {
 113             final int index = i;
 114             listeners[i] = event -> {
 115                 listenerStatus[index] = true;
 116                 System.out.println("ActionListener listeners[" + index + "]");
 117                 for (int j = 0; j < listenerStatus.length; j++) {
 118                     if (! listenerStatus[j]) {
 119                         break;
 120                     }
 121                     listenersInvoked = true;
 122                     synchronized (listenersLock) {
 123                         try {
 124                             listenersLock.notifyAll();
 125                         } catch (Exception e) {
 126                         }
 127                     }
 128                 }
 129             };
 130         }
 131 
 132         Frame frame = new Frame("Test frame");
 133         b1 = new Button("Add ActionListener");
 134         b1.addActionListener(event -> {
 135             for (int i = 0; i < listeners.length; i++) {
 136                 icon.addActionListener(listeners[i]);
 137             }
 138             listenerAdded = true;
 139             synchronized (lLock) {
 140                 try {
 141                     lLock.notifyAll();
 142                 } catch (Exception e) {
 143                 }
 144             }
 145         });
 146         frame.setLayout(new FlowLayout());
 147         frame.add(b1);
 148         frame.setSize(200, 200);
 149         frame.addWindowListener(new WindowAdapter() {
 150             public void windowClosing(WindowEvent event) {
 151                 System.err.println("User closed the window");
 152                 System.exit(1);
 153             }
 154         });
 155         frame.setVisible(true);
 156     }
 157 
 158     private void doTest() throws Exception {
 159 
 160         robot = new ExtendedRobot();
 161 
 162         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 163         if (iconPosition == null)
 164             throw new RuntimeException("Unable to find the icon location!");
 165 
 166         robot.mouseMove(iconPosition.x, iconPosition.y);
 167         robot.waitForIdle();
 168 
 169         actionPerformed = false;
 170         doTest = true;
 171 
 172         if(isMacOS) {
 173             robot.click(InputEvent.BUTTON3_MASK);
 174         }else{
 175             robot.mousePress(InputEvent.BUTTON1_MASK);
 176             robot.delay(clickDelay);
 177             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 178             robot.delay(clickDelay);
 179             robot.mousePress(InputEvent.BUTTON1_MASK);
 180             robot.delay(clickDelay);
 181             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 182         }
 183 
 184         if (! actionPerformed) {
 185             synchronized (actionLock) {
 186                 try {
 187                     actionLock.wait(3000);
 188                 } catch (Exception e) {
 189                 }
 190             }
 191         }
 192         if (! actionPerformed)
 193             throw new RuntimeException("FAIL: AWTEventListener not notified when TrayIcon " +
 194                                "is "+(isMacOS ? "" :"double ")+ "clicked");
 195 
 196         doTest = false;
 197         listenerAdded = false;
 198         robot.mouseMove(b1.getLocationOnScreen().x + b1.getSize().width / 2,
 199                         b1.getLocationOnScreen().y + b1.getSize().height / 2);
 200         robot.waitForIdle();
 201         robot.click();
 202 
 203         if (! listenerAdded) {
 204             synchronized (lLock) {
 205                 try {
 206                     lLock.wait(3000);
 207                 } catch (Exception e) {
 208                 }
 209             }
 210         }
 211         if (! listenerAdded)
 212             throw new RuntimeException("FAIL: ActionListener could not be added at runtime. " +
 213                                "b1 did not trigger ActionEvent");
 214 
 215         doTest = true;
 216         actionPerformed = false;
 217         robot.mouseMove(iconPosition.x, iconPosition.y);
 218         robot.waitForIdle();
 219 
 220         if(isMacOS) {
 221             robot.click(InputEvent.BUTTON3_MASK);
 222         }else{
 223             robot.mousePress(InputEvent.BUTTON1_MASK);
 224             robot.delay(clickDelay);
 225             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 226             robot.delay(clickDelay);
 227             robot.mousePress(InputEvent.BUTTON1_MASK);
 228             robot.delay(clickDelay);
 229             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 230         }
 231 
 232         if (! listenersInvoked) {
 233             synchronized (listenersLock) {
 234                 try {
 235                     listenersLock.wait(3000);
 236                 } catch (Exception e) {
 237                 }
 238             }
 239         }
 240         if (! listenersInvoked) {
 241             System.err.println("FAIL: All the listeners not invoked!");
 242             for (int i = 0; i < listenerStatus.length; i++)
 243                 throw new RuntimeException("Listener[" + i + "] invoked: " + listenerStatus[i]);
 244         }
 245         if (! actionPerformed) {
 246             synchronized (actionLock) {
 247                 try {
 248                     actionLock.wait(3000);
 249                 } catch (Exception e) {
 250                 }
 251             }
 252         }
 253         if (! actionPerformed)
 254             throw new RuntimeException("FAIL: AWTEventListener not notified when TrayIcon " +
 255                     "is "+(isMacOS? "" : "double ")+ "clicked. A set of listeners were added after it");
 256 
 257     }
 258 }