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