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 * @build ExtendedRobot SystemTrayIconHelper 34 * @run main TrayIconPopupTest 35 */ 36 37 public class TrayIconPopupTest { 38 39 TrayIcon icon; 40 ExtendedRobot robot; 41 42 boolean actionPerformed = false; 43 Object actionLock = new Object(); 44 static final int ATTEMPTS = 50; 45 46 PopupMenu popup; 47 Dialog window; 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 " + 59 "to avoid this problem. Or change behavior only for Java SE " + 60 "tray icon."); 61 new TrayIconPopupTest().doTest(); 62 } 63 } 64 65 TrayIconPopupTest() throws Exception { 66 robot = new ExtendedRobot(); 67 EventQueue.invokeAndWait(this::initializeGUI); 68 robot.waitForIdle(1000); 69 EventQueue.invokeAndWait( () -> window.setLocation(100, 100)); 70 robot.waitForIdle(1000); 71 } 72 73 private void initializeGUI() { 74 window = new Dialog((Frame) null); 75 window.setSize(5, 5); 76 window.setVisible(true); 77 78 popup = new PopupMenu(""); 79 80 MenuItem item = new MenuItem("Sample"); 81 item.addActionListener(event -> { 82 actionPerformed = true; 83 84 synchronized (actionLock) { 85 try { 86 actionLock.notifyAll(); 87 } catch (Exception e) { 88 } 89 } 90 }); 91 popup.add(item); 92 popup.add(new MenuItem("Item2")); 93 popup.add(new MenuItem("Item3")); 94 95 window.add(popup); 96 97 SystemTray tray = SystemTray.getSystemTray(); 98 icon = new TrayIcon(new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "Sample Icon"); 99 icon.addMouseListener(new MouseAdapter() { 100 public void mousePressed(MouseEvent event) { 101 if (event.isPopupTrigger()) { 102 popup.show(window, 0, 0); 103 } 104 } 105 106 public void mouseReleased(MouseEvent event) { 107 if (event.isPopupTrigger()) { 108 popup.show(window, 0, 0); 109 } 110 } 111 }); 112 try { 113 tray.add(icon); 114 } catch (AWTException e) { 115 throw new RuntimeException(e); 116 } 117 } 118 119 void doTest() throws Exception { 120 121 Point iconPosition = SystemTrayIconHelper.getTrayIconLocation(icon); 122 if (iconPosition == null) 123 throw new RuntimeException("Unable to find the icon location!"); 124 125 robot.mouseMove(iconPosition.x, iconPosition.y); 126 robot.waitForIdle(); 127 robot.mousePress(InputEvent.BUTTON3_MASK); 128 robot.delay(50); 129 robot.mouseRelease(InputEvent.BUTTON3_MASK); 130 robot.delay(6000); 131 132 robot.mouseMove(window.getLocation().x + 10, window.getLocation().y + 10); 133 robot.mousePress(InputEvent.BUTTON3_MASK); 134 robot.delay(50); 135 robot.mouseRelease(InputEvent.BUTTON3_MASK); 136 137 int attempts = 0; 138 while (!actionPerformed && attempts++ < ATTEMPTS) { 139 synchronized (actionLock) { 140 try { 141 actionLock.wait(3000); 142 } catch (Exception e) { 143 } 144 } 145 } 146 if (!actionPerformed) 147 throw new RuntimeException("FAIL: ActionEvent not triggered when " + 148 "JPopupMenu shown and menu item selected using keyboard"); 149 150 } 151 }