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 import java.awt.*;
  24 import java.awt.event.*;
  25 import java.awt.image.BufferedImage;
  26 
  27 /*
  28  * @test
  29  * @summary Check if a JPopupMenu can be displayed when TrayIcon is
  30  *          right clicked. It uses a JWindow as the parent of the JPopupMenu
  31  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  32  * @library ../../../../lib/testlibrary ../
  33  * @compile/module=java.desktop java/awt/Helper.java
  34  * @build ExtendedRobot SystemTrayIconHelper
  35  * @run main TrayIconPopupTest
  36  */
  37 
  38 public class TrayIconPopupTest {
  39 
  40     TrayIcon icon;
  41     ExtendedRobot robot;
  42 
  43     boolean actionPerformed = false;
  44     Object actionLock = new Object();
  45     static final int ATTEMPTS = 50;
  46 
  47     PopupMenu popup;
  48     Dialog window;
  49 
  50     public static void main(String[] args) throws Exception {
  51         if (!SystemTray.isSupported()) {
  52             System.out.println("SystemTray not supported on the platform under test. " +
  53                     "Marking the test passed");
  54         } else {
  55             if (System.getProperty("os.name").toLowerCase().startsWith("win"))
  56                 System.err.println("Test can fail if the icon hides to a tray icons pool " +
  57                         "in Windows 7, which is behavior by default.\n" +
  58                         "Set \"Right mouse click\" -> \"Customize notification icons\" -> " +
  59                         "\"Always show all icons and notifications on the taskbar\" true " +
  60                         "to avoid this problem. Or change behavior only for Java SE " +
  61                         "tray icon.");
  62             new TrayIconPopupTest().doTest();
  63         }
  64     }
  65 
  66     TrayIconPopupTest() throws Exception {
  67         robot = new ExtendedRobot();
  68         EventQueue.invokeAndWait(this::initializeGUI);
  69         robot.waitForIdle(1000);
  70         EventQueue.invokeAndWait( () ->  window.setLocation(100, 100));
  71         robot.waitForIdle(1000);
  72     }
  73 
  74     private void initializeGUI() {
  75         window = new Dialog((Frame) null);
  76         window.setSize(5, 5);
  77         window.setVisible(true);
  78 
  79         popup = new PopupMenu("");
  80 
  81         MenuItem item = new MenuItem("Sample");
  82         item.addActionListener(event -> {
  83             actionPerformed = true;
  84 
  85             synchronized (actionLock) {
  86                 try {
  87                     actionLock.notifyAll();
  88                 } catch (Exception e) {
  89                 }
  90             }
  91         });
  92         popup.add(item);
  93         popup.add(new MenuItem("Item2"));
  94         popup.add(new MenuItem("Item3"));
  95 
  96         window.add(popup);
  97 
  98         SystemTray tray = SystemTray.getSystemTray();
  99         icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon");
 100         icon.addMouseListener(new MouseAdapter() {
 101             public void mousePressed(MouseEvent event) {
 102                 if (event.isPopupTrigger()) {
 103                     popup.show(window, 0, 0);
 104                 }
 105             }
 106 
 107             public void mouseReleased(MouseEvent event) {
 108                 if (event.isPopupTrigger()) {
 109                     popup.show(window, 0, 0);
 110                 }
 111             }
 112         });
 113         try {
 114             tray.add(icon);
 115         } catch (AWTException e) {
 116             throw new RuntimeException(e);
 117         }
 118     }
 119 
 120     void doTest() throws Exception {
 121 
 122         Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon);
 123         if (iconPosition == null)
 124             throw new RuntimeException("Unable to find the icon location!");
 125 
 126         robot.mouseMove(iconPosition.x, iconPosition.y);
 127         robot.waitForIdle();
 128         robot.mousePress(InputEvent.BUTTON3_MASK);
 129         robot.delay(50);
 130         robot.mouseRelease(InputEvent.BUTTON3_MASK);
 131         robot.delay(1000);
 132 
 133         robot.mouseMove(window.getLocation().x + 10, window.getLocation().y + 10);
 134         robot.mousePress(InputEvent.BUTTON3_MASK);
 135         robot.delay(50);
 136         robot.mouseRelease(InputEvent.BUTTON3_MASK);
 137 
 138         int attempts = 0;
 139         while (!actionPerformed && attempts++ < ATTEMPTS) {
 140             synchronized (actionLock) {
 141                 try {
 142                     actionLock.wait(3000);
 143                 } catch (Exception e) {
 144                 }
 145             }
 146         }
 147         if (!actionPerformed)
 148             throw new RuntimeException("FAIL: ActionEvent not triggered when " +
 149                     "JPopupMenu shown and menu item selected using keyboard");
 150 
 151     }
 152 }