1 /* 2 * Copyright (c) 2011, 2012, 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 * @bug 4966112 27 * @summary Some Composite components does not show the Context Popup. 28 * @library ../../regtesthelpers 29 * @build Util 30 * @author Alexander Zuev 31 * @run main bug4966112 32 */ 33 import javax.swing.*; 34 import javax.swing.event.PopupMenuListener; 35 import javax.swing.event.PopupMenuEvent; 36 import java.awt.*; 37 import java.awt.event.*; 38 import sun.awt.SunToolkit; 39 40 public class bug4966112 { 41 42 private static final int NO_MOUSE_BUTTON = -1; 43 private static volatile boolean shown = false; 44 private static volatile int popupButton = NO_MOUSE_BUTTON; 45 private static volatile JButton testButton; 46 private static volatile JSplitPane jsp; 47 private static volatile JSpinner spin; 48 private static volatile JFileChooser filec; 49 private static int buttonMask; 50 private static Robot robot; 51 52 public static void main(String[] args) throws Exception { 53 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 54 robot = new Robot(); 55 robot.setAutoDelay(100); 56 57 createAndShowButton(); 58 toolkit.realSync(); 59 60 setClickPoint(testButton); 61 clickMouse(InputEvent.BUTTON1_MASK); 62 clickMouse(InputEvent.BUTTON2_MASK); 63 clickMouse(InputEvent.BUTTON3_MASK); 64 65 toolkit.realSync(); 66 closeFrame(); 67 68 if (popupButton == NO_MOUSE_BUTTON) { 69 System.out.println("Test can't identify the popup trigger button. Test skipped"); 70 return; 71 } 72 73 setButtonMask(); 74 75 // Test Split Pane 76 createAndShowSplitPane(); 77 toolkit.realSync(); 78 79 clickMouse(jsp); 80 toolkit.realSync(); 81 closeFrame(); 82 83 if (!shown) { 84 throw new RuntimeException("Popup was not shown on splitpane"); 85 } 86 87 // Test Spinner 88 createAndShowSpinner(); 89 toolkit.realSync(); 90 91 clickMouse(spin); 92 toolkit.realSync(); 93 closeFrame(); 94 95 if (!shown) { 96 throw new RuntimeException("Popup was not shown on spinner"); 97 } 98 99 // Test File Chooser 100 createAndShowFileChooser(); 101 toolkit.realSync(); 102 103 clickMouse(filec); 104 toolkit.realSync(); 105 106 Util.hitKeys(robot, KeyEvent.VK_ESCAPE); 107 toolkit.realSync(); 108 109 Util.hitKeys(robot, KeyEvent.VK_ESCAPE); 110 toolkit.realSync(); 111 closeFrame(); 112 113 if (!shown) { 114 throw new RuntimeException("Popup was not shown on filechooser"); 115 } 116 } 117 118 private static void clickMouse(JComponent c) throws Exception { 119 setClickPoint(c); 120 clickMouse(buttonMask); 121 } 122 123 private static void clickMouse(int buttons) { 124 robot.mousePress(buttons); 125 robot.mouseRelease(buttons); 126 } 127 128 private static void setButtonMask() { 129 switch (popupButton) { 130 case MouseEvent.BUTTON1: 131 buttonMask = InputEvent.BUTTON1_MASK; 132 break; 133 case MouseEvent.BUTTON2: 134 buttonMask = InputEvent.BUTTON2_MASK; 135 break; 136 case MouseEvent.BUTTON3: 137 buttonMask = InputEvent.BUTTON3_MASK; 138 break; 139 } 140 } 141 142 private static void setClickPoint(final JComponent c) throws Exception { 143 final Point[] result = new Point[1]; 144 SwingUtilities.invokeAndWait(new Runnable() { 145 146 @Override 147 public void run() { 148 Point p = c.getLocationOnScreen(); 149 Dimension size = c.getSize(); 150 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2); 151 } 152 }); 153 154 robot.mouseMove(result[0].x, result[0].y); 155 } 156 157 private static JPopupMenu createJPopupMenu() { 158 JPopupMenu jpm = new JPopupMenu(); 159 jpm.add("One"); 160 jpm.add("Two"); 161 jpm.add("Three"); 162 jpm.addPopupMenuListener(new PopupMenuListener() { 163 164 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 165 shown = true; 166 } 167 168 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 169 } 170 171 public void popupMenuCanceled(PopupMenuEvent e) { 172 } 173 }); 174 175 AutoClosable.INSTANCE.setPopup(jpm); 176 return jpm; 177 } 178 179 private static void createAndShowButton() throws Exception { 180 SwingUtilities.invokeAndWait(new Runnable() { 181 182 @Override 183 public void run() { 184 JFrame frame = new JFrame("Button Frame"); 185 frame.setLayout(new BorderLayout()); 186 testButton = new JButton("Popup Tester"); 187 188 testButton.addMouseListener(new MouseAdapter() { 189 190 void setPopupTrigger(MouseEvent e) { 191 if (e.isPopupTrigger()) { 192 popupButton = e.getButton(); 193 } 194 } 195 196 public void mouseClicked(MouseEvent e) { 197 setPopupTrigger(e); 198 } 199 200 public void mousePressed(MouseEvent e) { 201 setPopupTrigger(e); 202 } 203 204 public void mouseReleased(MouseEvent e) { 205 setPopupTrigger(e); 206 } 207 }); 208 209 frame.add(testButton, BorderLayout.CENTER); 210 frame.pack(); 211 frame.setVisible(true); 212 AutoClosable.INSTANCE.setFrame(frame); 213 } 214 }); 215 } 216 217 private static void createAndShowSplitPane() throws Exception { 218 SwingUtilities.invokeAndWait(new Runnable() { 219 220 @Override 221 public void run() { 222 JFrame frame = new JFrame("Test SplitPane"); 223 frame.setSize(250, 200); 224 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 225 frame.setLayout(new BorderLayout()); 226 227 shown = false; 228 jsp = new JSplitPane(); 229 jsp.setRightComponent(new JPanel()); 230 jsp.setLeftComponent(new JPanel()); 231 jsp.setComponentPopupMenu(createJPopupMenu()); 232 233 frame.add(jsp, BorderLayout.CENTER); 234 235 jsp.setDividerLocation(150); 236 237 frame.setLocation(400, 300); 238 frame.setVisible(true); 239 AutoClosable.INSTANCE.setFrame(frame); 240 } 241 }); 242 } 243 244 private static void createAndShowSpinner() throws Exception { 245 SwingUtilities.invokeAndWait(new Runnable() { 246 247 @Override 248 public void run() { 249 JFrame frame = new JFrame("JSpinner Test"); 250 frame.setLayout(new BorderLayout()); 251 frame.setSize(200, 100); 252 shown = false; 253 spin = new JSpinner(); 254 spin.setComponentPopupMenu(createJPopupMenu()); 255 frame.add(spin, BorderLayout.CENTER); 256 frame.setVisible(true); 257 AutoClosable.INSTANCE.setFrame(frame); 258 } 259 }); 260 } 261 262 private static void createAndShowFileChooser() throws Exception { 263 SwingUtilities.invokeLater(new Runnable() { 264 265 @Override 266 public void run() { 267 JFrame frame = new JFrame("FileChooser test dialog"); 268 frame.setSize(100, 100); 269 270 shown = false; 271 filec = new JFileChooser(); 272 filec.setComponentPopupMenu(createJPopupMenu()); 273 filec.showOpenDialog(frame); 274 275 frame.setVisible(true); 276 AutoClosable.INSTANCE.setFrame(frame); 277 } 278 }); 279 } 280 281 private static void closeFrame() { 282 SwingUtilities.invokeLater(new Runnable() { 283 284 @Override 285 public void run() { 286 AutoClosable.INSTANCE.close(); 287 } 288 }); 289 } 290 291 private static class AutoClosable { 292 293 static final AutoClosable INSTANCE = new AutoClosable(); 294 private JFrame frame; 295 private JPopupMenu popup; 296 297 public void setFrame(JFrame frame) { 298 this.frame = frame; 299 } 300 301 public void setPopup(JPopupMenu popup) { 302 this.popup = popup; 303 } 304 305 public void close() { 306 frame.dispose(); 307 if (popup != null) { 308 popup.setVisible(false); 309 } 310 } 311 } 312 }