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