1 /* 2 * Copyright (c) 2013, 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 24 25 import test.java.awt.regtesthelpers.Sysout; 26 27 import java.applet.Applet; 28 import java.awt.*; 29 import java.awt.image.BufferedImage; 30 31 public class AddPopupAfterShowTest extends Applet { 32 @Override 33 public void init() { 34 if (!SystemTray.isSupported()) { 35 Sysout.createDialogWithInstructions(new String[]{ 36 "Press PASS, the System Tray is not supported"}); 37 return; 38 } 39 40 41 String[] instructions = { 42 "1) The red circle icon was added to the system tray.", 43 "2) Check that a popup menu is opened when the icon is clicked.", 44 "3) If true the test is passed, otherwise failed."}; 45 Sysout.createDialogWithInstructions(instructions); 46 } 47 48 @Override 49 public void start() { 50 setSize(200, 200); 51 show(); 52 53 createSystemTrayIcon(); 54 } 55 56 private static void createSystemTrayIcon() { 57 final TrayIcon trayIcon = new TrayIcon(createTrayIconImage()); 58 trayIcon.setImageAutoSize(true); 59 60 try { 61 // Add tray icon to system tray *before* adding popup menu to demonstrate buggy behaviour 62 SystemTray.getSystemTray().add(trayIcon); 63 trayIcon.setPopupMenu(createTrayIconPopupMenu()); 64 } catch (final AWTException awte) { 65 awte.printStackTrace(); 66 } 67 } 68 69 private static Image createTrayIconImage() { 70 /** 71 * Create a small image of a red circle to use as the icon for the tray icon 72 */ 73 int trayIconImageSize = 32; 74 final BufferedImage trayImage = new BufferedImage(trayIconImageSize, trayIconImageSize, BufferedImage.TYPE_INT_ARGB); 75 final Graphics2D trayImageGraphics = (Graphics2D) trayImage.getGraphics(); 76 77 trayImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 78 79 trayImageGraphics.setColor(new Color(255, 255, 255, 0)); 80 trayImageGraphics.fillRect(0, 0, trayImage.getWidth(), trayImage.getHeight()); 81 82 trayImageGraphics.setColor(Color.red); 83 84 int trayIconImageInset = 4; 85 trayImageGraphics.fillOval(trayIconImageInset, 86 trayIconImageInset, 87 trayImage.getWidth() - 2 * trayIconImageInset, 88 trayImage.getHeight() - 2 * trayIconImageInset); 89 90 trayImageGraphics.setColor(Color.darkGray); 91 92 trayImageGraphics.drawOval(trayIconImageInset, 93 trayIconImageInset, 94 trayImage.getWidth() - 2 * trayIconImageInset, 95 trayImage.getHeight() - 2 * trayIconImageInset); 96 97 return trayImage; 98 } 99 100 private static PopupMenu createTrayIconPopupMenu() { 101 final PopupMenu trayIconPopupMenu = new PopupMenu(); 102 final MenuItem popupMenuItem = new MenuItem("TEST PASSED!"); 103 trayIconPopupMenu.add(popupMenuItem); 104 return trayIconPopupMenu; 105 } 106 } | 1 /* 2 * Copyright (c) 2013, 2020, 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 24 /* 25 * @test 26 * @key headful 27 * @bug 8007220 8204161 28 * @summary The popup menu is not added to the tray icon after it was added to tray 29 * @run main/manual AddPopupAfterShowTest 30 */ 31 32 import java.awt.AWTException; 33 import java.awt.Button; 34 import java.awt.Color; 35 import java.awt.event.WindowAdapter; 36 import java.awt.event.WindowEvent; 37 import java.awt.image.BufferedImage; 38 import java.awt.Frame; 39 import java.awt.GridBagConstraints; 40 import java.awt.GridBagLayout; 41 import java.awt.Insets; 42 import java.awt.Image; 43 import java.awt.Graphics2D; 44 import java.awt.event.ActionEvent; 45 import java.awt.TextArea; 46 import java.awt.TrayIcon; 47 import java.awt.Panel; 48 import java.awt.PopupMenu; 49 import java.awt.RenderingHints; 50 import java.awt.MenuItem; 51 import java.awt.SystemTray; 52 import java.util.concurrent.CountDownLatch; 53 import java.util.concurrent.TimeUnit; 54 55 public class AddPopupAfterShowTest { 56 57 private static final Frame instructionFrame = new Frame(); 58 private static TrayIcon trayIcon = null; 59 60 private static volatile boolean testResult = false; 61 private static volatile CountDownLatch countDownLatch; 62 private static final String INSTRUCTIONS = "INSTRUCTIONS:\n\n" + 63 "1) The red circle icon was added to the system tray.\n"+ 64 "2) Check that a popup menu is opened when the icon is clicked.\n"+ 65 "3) If true the test is passed, otherwise failed."; 66 67 public static void main(String[] args) throws Exception { 68 if (!SystemTray.isSupported()) { 69 System.out.println("The System Tray is not supported," + 70 "so this test can not be run in this scenario."); 71 return; 72 } 73 countDownLatch = new CountDownLatch(1); 74 75 createInstructionUI(); 76 createTestUI(); 77 countDownLatch.await(15, TimeUnit.MINUTES); 78 disposeUI(); 79 if (!testResult) { 80 throw new RuntimeException("Test failed!"); 81 } 82 } 83 84 private static void createInstructionUI() { 85 GridBagLayout layout = new GridBagLayout(); 86 Panel mainControlPanel = new Panel(layout); 87 Panel resultButtonPanel = new Panel(layout); 88 89 GridBagConstraints gbc = new GridBagConstraints(); 90 91 gbc.gridx = 0; 92 gbc.gridy = 0; 93 gbc.insets = new Insets(5, 15, 5, 15); 94 gbc.fill = GridBagConstraints.HORIZONTAL; 95 96 TextArea instructionTextArea = new TextArea(); 97 instructionTextArea.setText(INSTRUCTIONS); 98 instructionTextArea.setEditable(false); 99 instructionTextArea.setBackground(Color.white); 100 mainControlPanel.add(instructionTextArea, gbc); 101 102 Button passButton = new Button("Pass"); 103 passButton.setActionCommand("Pass"); 104 passButton.addActionListener((ActionEvent e) -> { 105 testResult = true; 106 countDownLatch.countDown(); 107 }); 108 109 Button failButton = new Button("Fail"); 110 failButton.setActionCommand("Fail"); 111 failButton.addActionListener(e -> { 112 countDownLatch.countDown(); 113 }); 114 115 gbc.gridx = 0; 116 gbc.gridy = 0; 117 118 resultButtonPanel.add(passButton, gbc); 119 120 gbc.gridx = 1; 121 gbc.gridy = 0; 122 resultButtonPanel.add(failButton, gbc); 123 124 gbc.gridx = 0; 125 gbc.gridy = 2; 126 mainControlPanel.add(resultButtonPanel, gbc); 127 128 instructionFrame.addWindowListener(new WindowAdapter() { 129 @Override 130 public void windowClosing(WindowEvent e) { 131 super.windowClosing(e); 132 countDownLatch.countDown(); 133 } 134 }); 135 instructionFrame.pack(); 136 instructionFrame.add(mainControlPanel); 137 instructionFrame.pack(); 138 instructionFrame.setVisible(true); 139 } 140 141 private static void createTestUI() throws Exception { 142 trayIcon = new TrayIcon(createTrayIconImage()); 143 trayIcon.setImageAutoSize(true); 144 // Add tray icon to system tray *before* adding popup menu 145 // to demonstrate buggy behaviour 146 SystemTray.getSystemTray().add(trayIcon); 147 trayIcon.setPopupMenu(createTrayIconPopupMenu()); 148 } 149 150 private static Image createTrayIconImage() { 151 /** 152 * Create a small image of a red circle to use as the icon 153 * for the tray icon 154 */ 155 int trayIconImageSize = 32; 156 final BufferedImage trayImage = new BufferedImage(trayIconImageSize, 157 trayIconImageSize, BufferedImage.TYPE_INT_ARGB); 158 final Graphics2D trayImageGraphics = 159 (Graphics2D) trayImage.getGraphics(); 160 161 trayImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 162 RenderingHints.VALUE_ANTIALIAS_ON); 163 164 trayImageGraphics.setColor(new Color(255, 255, 255, 0)); 165 trayImageGraphics.fillRect(0, 0, trayImage.getWidth(), 166 trayImage.getHeight()); 167 168 trayImageGraphics.setColor(Color.red); 169 170 int trayIconImageInset = 4; 171 trayImageGraphics.fillOval(trayIconImageInset, 172 trayIconImageInset, 173 trayImage.getWidth() - 2 * trayIconImageInset, 174 trayImage.getHeight() - 2 * trayIconImageInset); 175 176 trayImageGraphics.setColor(Color.darkGray); 177 178 trayImageGraphics.drawOval(trayIconImageInset, 179 trayIconImageInset, 180 trayImage.getWidth() - 2 * trayIconImageInset, 181 trayImage.getHeight() - 2 * trayIconImageInset); 182 183 return trayImage; 184 } 185 186 private static PopupMenu createTrayIconPopupMenu() { 187 final PopupMenu trayIconPopupMenu = new PopupMenu(); 188 final MenuItem popupMenuItem = new MenuItem("TEST PASSED!"); 189 trayIconPopupMenu.add(popupMenuItem); 190 return trayIconPopupMenu; 191 } 192 193 private static void disposeUI() { 194 SystemTray.getSystemTray().remove(trayIcon); 195 instructionFrame.dispose(); 196 } 197 } |