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 6272324 8204161 28 * @summary Tests that clicking TrayIcon with middle button generates events. 29 * @run main/manual MiddleButtonEventTest 30 */ 31 32 import java.awt.Button; 33 import java.awt.Color; 34 import java.awt.event.WindowAdapter; 35 import java.awt.event.WindowEvent; 36 import java.awt.image.BufferedImage; 37 import java.awt.Frame; 38 import java.awt.GridBagConstraints; 39 import java.awt.GridBagLayout; 40 import java.awt.Insets; 41 import java.awt.Graphics; 42 import java.awt.event.ActionEvent; 43 import java.awt.event.MouseAdapter; 44 import java.awt.event.MouseEvent; 45 import java.awt.TextArea; 46 import java.awt.Toolkit; 47 import java.awt.TrayIcon; 48 import java.awt.Panel; 49 import java.awt.SystemTray; 50 import java.util.concurrent.CountDownLatch; 51 import java.util.concurrent.TimeUnit; 52 53 public class MiddleButtonEventTest { 54 55 private static final Frame instructionFrame = new Frame(); 56 private static TrayIcon trayIcon = null; 57 private static final String INSTRUCTIONS = "INSTRUCTIONS:\n\n" + 58 "Tests that clicking TrayIcon with middle button generates events.\n"+ 59 "On MToolkit, this test will just Pass.\n" + 60 "When the test is started you will see three-color icon in the " + 61 "system tray.\n Click on it with the middle mouse button:\n" + 62 "- MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_CLICKED events should be\n"+ 63 " generated for the middle button.\n" + 64 " If so, the test passed, otherwise failed."; 65 private static final TextArea eventOutputArea = new TextArea("", 5, 50, 66 TextArea.SCROLLBARS_BOTH); 67 private static volatile boolean testResult = false; 68 private static volatile CountDownLatch countDownLatch; 69 70 public static void main(String[] args) throws Exception { 71 if ("sun.awt.motif.MToolkit" 72 .equals(Toolkit.getDefaultToolkit().getClass().getName())) { 73 System.out.println("The test can not be run on MToolkit"); 74 return; 75 } 76 if (!SystemTray.isSupported()) { 77 System.out.println("The System Tray is not supported, " + 78 "so this test can not be run in this scenario "); 79 return; 80 } 81 countDownLatch = new CountDownLatch(1); 82 83 createInstructionUI(); 84 createTestUI(); 85 countDownLatch.await(15, TimeUnit.MINUTES); 86 disposeUI(); 87 if (!testResult) { 88 throw new RuntimeException("Test failed!"); 89 } 90 } 91 92 private static void createInstructionUI() { 93 GridBagLayout layout = new GridBagLayout(); 94 Panel mainControlPanel = new Panel(layout); 95 Panel resultButtonPanel = new Panel(layout); 96 97 GridBagConstraints gbc = new GridBagConstraints(); 98 99 gbc.gridx = 0; 100 gbc.gridy = 0; 101 gbc.insets = new Insets(5, 15, 5, 15); 102 gbc.fill = GridBagConstraints.HORIZONTAL; 103 104 TextArea instructionTextArea = new TextArea(); 105 instructionTextArea.setText(INSTRUCTIONS); 106 instructionTextArea.setEditable(false); 107 instructionTextArea.setBackground(Color.white); 108 mainControlPanel.add(instructionTextArea, gbc); 109 110 Button passButton = new Button("Pass"); 111 passButton.setActionCommand("Pass"); 112 passButton.addActionListener((ActionEvent e) -> { 113 testResult = true; 114 countDownLatch.countDown(); 115 }); 116 117 Button failButton = new Button("Fail"); 118 failButton.setActionCommand("Fail"); 119 failButton.addActionListener(e -> { 120 countDownLatch.countDown(); 121 }); 122 123 gbc.gridx = 0; 124 gbc.gridy = 0; 125 126 resultButtonPanel.add(passButton, gbc); 127 128 gbc.gridx = 1; 129 gbc.gridy = 0; 130 resultButtonPanel.add(failButton, gbc); 131 132 gbc.gridx = 0; 133 gbc.gridy = 2; 134 mainControlPanel.add(resultButtonPanel, gbc); 135 136 gbc.gridx = 0; 137 gbc.gridy = 3; 138 mainControlPanel.add(eventOutputArea, gbc); 139 140 instructionFrame.addWindowListener(new WindowAdapter() { 141 @Override 142 public void windowClosing(WindowEvent e) { 143 super.windowClosing(e); 144 countDownLatch.countDown(); 145 } 146 }); 147 instructionFrame.pack(); 148 instructionFrame.add(mainControlPanel); 149 instructionFrame.pack(); 150 instructionFrame.setVisible(true); 151 } 152 153 private static void createTestUI() throws Exception { 154 BufferedImage im = new BufferedImage(16, 16, 155 BufferedImage.TYPE_INT_ARGB); 156 Graphics gr = im.createGraphics(); 157 gr.setColor(Color.white); 158 gr.fillRect(0, 0, 16, 5); 159 gr.setColor(Color.blue); 160 gr.fillRect(0, 5, 16, 10); 161 gr.setColor(Color.red); 162 gr.fillRect(0, 10, 16, 16); 163 164 trayIcon = new TrayIcon(im); 165 trayIcon.setImageAutoSize(true); 166 trayIcon.addMouseListener(new MouseAdapter() { 167 public void mousePressed(MouseEvent e) { 168 printEventStr(e.toString()); 169 } 170 171 public void mouseReleased(MouseEvent e) { 172 printEventStr(e.toString()); 173 } 174 175 public void mouseClicked(MouseEvent e) { 176 printEventStr(e.toString()); 177 } 178 }); 179 SystemTray.getSystemTray().add(trayIcon); 180 } 181 182 private static void disposeUI() { 183 SystemTray.getSystemTray().remove(trayIcon); 184 instructionFrame.dispose(); 185 } 186 187 private static void printEventStr(String msg) { 188 eventOutputArea.append(msg + "\n"); 189 System.out.println(msg); 190 } 191 }