1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/*
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @key headful
* @bug 4966112
* @summary Some Composite components does not show the Context Popup.
* @library ../../regtesthelpers
* @build Util
* @author Alexander Zuev
* @run main bug4966112
*/
import javax.swing.*;
import javax.swing.event.PopupMenuListener;
import javax.swing.event.PopupMenuEvent;
import java.awt.*;
import java.awt.event.*;
public class bug4966112 {
private static final int NO_MOUSE_BUTTON = -1;
private static volatile boolean shown = false;
private static volatile int popupButton = NO_MOUSE_BUTTON;
private static volatile JButton testButton;
private static volatile JSplitPane jsp;
private static volatile JSpinner spin;
private static volatile JFileChooser filec;
private static int buttonMask;
private static Robot robot;
public static void main(String[] args) throws Exception {
robot = new Robot();
robot.setAutoDelay(100);
createAndShowButton();
robot.waitForIdle();
setClickPoint(testButton);
clickMouse(InputEvent.BUTTON1_MASK);
clickMouse(InputEvent.BUTTON2_MASK);
clickMouse(InputEvent.BUTTON3_MASK);
robot.waitForIdle();
closeFrame();
if (popupButton == NO_MOUSE_BUTTON) {
System.out.println("Test can't identify the popup trigger button. Test skipped");
return;
}
setButtonMask();
// Test Split Pane
createAndShowSplitPane();
robot.waitForIdle();
clickMouse(jsp);
robot.waitForIdle();
closeFrame();
if (!shown) {
throw new RuntimeException("Popup was not shown on splitpane");
}
// Test Spinner
createAndShowSpinner();
robot.waitForIdle();
clickMouse(spin);
robot.waitForIdle();
closeFrame();
if (!shown) {
throw new RuntimeException("Popup was not shown on spinner");
}
// Test File Chooser
createAndShowFileChooser();
robot.waitForIdle();
clickMouse(filec);
robot.waitForIdle();
Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
robot.waitForIdle();
Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
robot.waitForIdle();
closeFrame();
if (!shown) {
throw new RuntimeException("Popup was not shown on filechooser");
}
}
private static void clickMouse(JComponent c) throws Exception {
setClickPoint(c);
clickMouse(buttonMask);
}
private static void clickMouse(int buttons) {
robot.mousePress(buttons);
robot.mouseRelease(buttons);
}
private static void setButtonMask() {
switch (popupButton) {
case MouseEvent.BUTTON1:
buttonMask = InputEvent.BUTTON1_MASK;
break;
case MouseEvent.BUTTON2:
buttonMask = InputEvent.BUTTON2_MASK;
break;
case MouseEvent.BUTTON3:
buttonMask = InputEvent.BUTTON3_MASK;
break;
}
}
private static void setClickPoint(final JComponent c) throws Exception {
final Point[] result = new Point[1];
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
Point p = c.getLocationOnScreen();
Dimension size = c.getSize();
result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
}
});
robot.mouseMove(result[0].x, result[0].y);
}
private static JPopupMenu createJPopupMenu() {
JPopupMenu jpm = new JPopupMenu();
jpm.add("One");
jpm.add("Two");
jpm.add("Three");
jpm.addPopupMenuListener(new PopupMenuListener() {
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
shown = true;
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
}
public void popupMenuCanceled(PopupMenuEvent e) {
}
});
AutoClosable.INSTANCE.setPopup(jpm);
return jpm;
}
private static void createAndShowButton() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Button Frame");
frame.setLayout(new BorderLayout());
testButton = new JButton("Popup Tester");
testButton.addMouseListener(new MouseAdapter() {
void setPopupTrigger(MouseEvent e) {
if (e.isPopupTrigger()) {
popupButton = e.getButton();
}
}
public void mouseClicked(MouseEvent e) {
setPopupTrigger(e);
}
public void mousePressed(MouseEvent e) {
setPopupTrigger(e);
}
public void mouseReleased(MouseEvent e) {
setPopupTrigger(e);
}
});
frame.add(testButton, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
AutoClosable.INSTANCE.setFrame(frame);
}
});
}
private static void createAndShowSplitPane() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test SplitPane");
frame.setSize(250, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
shown = false;
jsp = new JSplitPane();
jsp.setRightComponent(new JPanel());
jsp.setLeftComponent(new JPanel());
jsp.setComponentPopupMenu(createJPopupMenu());
frame.add(jsp, BorderLayout.CENTER);
jsp.setDividerLocation(150);
frame.setLocation(400, 300);
frame.setVisible(true);
AutoClosable.INSTANCE.setFrame(frame);
}
});
}
private static void createAndShowSpinner() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("JSpinner Test");
frame.setLayout(new BorderLayout());
frame.setSize(200, 100);
shown = false;
spin = new JSpinner();
spin.setComponentPopupMenu(createJPopupMenu());
frame.add(spin, BorderLayout.CENTER);
frame.setVisible(true);
AutoClosable.INSTANCE.setFrame(frame);
}
});
}
private static void createAndShowFileChooser() throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("FileChooser test dialog");
frame.setSize(100, 100);
shown = false;
filec = new JFileChooser();
filec.setComponentPopupMenu(createJPopupMenu());
filec.showOpenDialog(frame);
frame.setVisible(true);
AutoClosable.INSTANCE.setFrame(frame);
}
});
}
private static void closeFrame() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
AutoClosable.INSTANCE.close();
}
});
}
private static class AutoClosable {
static final AutoClosable INSTANCE = new AutoClosable();
private JFrame frame;
private JPopupMenu popup;
public void setFrame(JFrame frame) {
this.frame = frame;
}
public void setPopup(JPopupMenu popup) {
this.popup = popup;
}
public void close() {
frame.dispose();
if (popup != null) {
popup.setVisible(false);
}
}
}
}
|