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