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