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.image.BufferedImage;
  26 
  27 /*
  28  * @test
  29  * @summary Check the return value of the getActionCommand method
  30  *          of the ActionEvent triggered when TrayIcon is double clicked
  31  *          (single clicked, on Mac)
  32  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  33  * @library ../../../../lib/testlibrary ../
  34  * @compile/module=java.desktop java/awt/Helper.java
  35  * @build ExtendedRobot SystemTrayIconHelper
  36  * @run main ActionCommand
  37  */
  38 
  39 public class ActionCommand {
  40 
  41     TrayIcon icon;
  42     ExtendedRobot robot;
  43 
  44     boolean actionPerformed = false;
  45     Object actionLock = new Object();
  46     String actionCommand = null;
  47     static boolean isMacOS = false;
  48 
  49     public static void main(String[] args) throws Exception {
  50         if (! SystemTray.isSupported()) {
  51             System.out.println("SystemTray not supported on the platform under test. " +
  52                     "Marking the test passed");
  53         } else {
  54             if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  55                 System.err.println("Test can fail if the icon hides to a tray icons pool " +
  56                         "in Windows 7, which is behavior by default.\n" +
  57                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
  58                         "\"Always show all icons and notifications on the taskbar\" true to " +
  59                         "avoid this problem. Or change behavior only for Java SE tray icon " +
  60                         "and rerun test.");
  61             } else  if (System.getProperty("os.name").toLowerCase().startsWith("mac")){
  62                 isMacOS = true;
  63             }
  64 
  65             new ActionCommand().doTest();
  66         }
  67     }
  68 
  69     void doTest() throws Exception {
  70         robot = new ExtendedRobot();
  71 
  72         EventQueue.invokeAndWait(() -> {
  73             SystemTray tray = SystemTray.getSystemTray();
  74             icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
  75             icon.addActionListener((event) -> {
  76                 actionPerformed = true;
  77                 actionCommand = event.getActionCommand();
  78                 synchronized (actionLock) {
  79                     try {
  80                         actionLock.notifyAll();
  81                     } catch (Exception e) {
  82                     }
  83                 }
  84             });
  85 
  86             if (icon.getActionCommand() != null)
  87                 throw new RuntimeException("FAIL: getActionCommand did not return null " +
  88                         "when no action command set " + icon.getActionCommand());
  89 
  90             icon.setActionCommand("Sample Command");
  91 
  92             if (! "Sample Command".equals(icon.getActionCommand()))
  93                 throw new RuntimeException("FAIL: getActionCommand did not return the correct value. " +
  94                         icon.getActionCommand() + " Expected: Sample Command");
  95 
  96             try {
  97                 tray.add(icon);
  98             } catch (AWTException e) {
  99                 throw new RuntimeException(e);
 100             }
 101         });
 102 
 103         robot.waitForIdle();
 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(2000);
 111         actionPerformed = false;
 112         SystemTrayIconHelper.doubleClick(robot);
 113 
 114         if (! actionPerformed) {
 115             synchronized (actionLock) {
 116                 try {
 117                     actionLock.wait(3000);
 118                 } catch (Exception e) {
 119                 }
 120             }
 121         }
 122         if (! actionPerformed) {
 123             throw new RuntimeException("FAIL: ActionEvent not triggered when TrayIcon is "+(isMacOS? "" : "double ")+"clicked");
 124         } else if (! "Sample Command".equals(actionCommand)) {
 125             throw new RuntimeException("FAIL: ActionEvent.getActionCommand did not return the correct " +
 126                     "value. Returned: " + actionCommand + " ; Expected: Sample Command");
 127         }
 128 
 129         EventQueue.invokeAndWait(() -> {
 130             icon.setActionCommand(null);
 131             if (icon.getActionCommand() != null) {
 132                 throw new RuntimeException("FAIL: ActionCommand set to null. getActionCommand did " +
 133                         "not return null " + icon.getActionCommand());
 134             }
 135         });
 136 
 137         robot.mouseMove(0, 0);
 138         robot.waitForIdle();
 139         robot.mouseMove(iconPosition.x, iconPosition.y);
 140         robot.waitForIdle();
 141 
 142         actionPerformed = false;
 143         actionCommand = null;
 144         SystemTrayIconHelper.doubleClick(robot);
 145 
 146         if (! actionPerformed) {
 147             synchronized (actionLock) {
 148                 try {
 149                     actionLock.wait(3000);
 150                 } catch (Exception e) {
 151                 }
 152             }
 153         }
 154         if (! actionPerformed) {
 155             throw new RuntimeException("FAIL: ActionEvent not triggered when ActionCommand set to " +
 156                     "null and then TrayIcon is "+(isMacOS? "" : "double ")+ "clicked");
 157         } else if (actionCommand != null) {
 158             throw new RuntimeException("FAIL: ActionEvent.getActionCommand did not return null " +
 159                     "when ActionCommand is set to null " + actionCommand);
 160         }
 161 
 162     }
 163 }