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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/*
* Copyright (c) 2013, 2025, 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.
*/
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import jtreg.SkippedException;
/*
* @test
* @bug 4245587 4474813 4425878 4767478 8015599
* @key headful
* @summary Tests the location of the heavy weight popup portion of JComboBox,
* JMenu and JPopupMenu.
* The test uses Ctrl+Down Arrow which is a system shortcut on macOS,
* disable it in system settings, otherwise the test will fail
* @library ../regtesthelpers
* @library /test/lib
* @build Util
* @build jtreg.SkippedException
* @run main TaskbarPositionTest
*/
public class TaskbarPositionTest implements ActionListener {
private static JFrame frame;
private static JPopupMenu popupMenu;
private static JPanel panel;
private static JComboBox<String> combo1;
private static JComboBox<String> combo2;
private static JMenu menu1;
private static JMenu menu2;
private static JMenu submenu;
private static Rectangle fullScreenBounds;
// The usable desktop space: screen size - screen insets.
private static Rectangle screenBounds;
private static final String[] numData = {
"One", "Two", "Three", "Four", "Five", "Six", "Seven"
};
private static final String[] dayData = {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
};
private static final char[] mnDayData = {
'M', 'T', 'W', 'R', 'F', 'S', 'U'
};
public TaskbarPositionTest() {
frame = new JFrame("Use CTRL-down to show a JPopupMenu");
frame.setContentPane(panel = createContentPane());
frame.setJMenuBar(createMenuBar());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// CTRL-down will show the popup.
panel.getInputMap().put(KeyStroke.getKeyStroke(
KeyEvent.VK_DOWN, InputEvent.CTRL_DOWN_MASK), "OPEN_POPUP");
panel.getActionMap().put("OPEN_POPUP", new PopupHandler());
frame.pack();
Toolkit toolkit = Toolkit.getDefaultToolkit();
fullScreenBounds = new Rectangle(new Point(), toolkit.getScreenSize());
screenBounds = new Rectangle(new Point(), toolkit.getScreenSize());
// Reduce the screen bounds by the insets.
GraphicsConfiguration gc = frame.getGraphicsConfiguration();
if (gc != null) {
Insets screenInsets = toolkit.getScreenInsets(gc);
screenBounds = gc.getBounds();
screenBounds.width -= (screenInsets.left + screenInsets.right);
screenBounds.height -= (screenInsets.top + screenInsets.bottom);
screenBounds.x += screenInsets.left;
screenBounds.y += screenInsets.top;
}
// Place the frame near the bottom.
frame.setLocation(screenBounds.x,
screenBounds.y + screenBounds.height - frame.getHeight());
frame.setVisible(true);
}
private static class ComboPopupCheckListener implements PopupMenuListener {
@Override
public void popupMenuCanceled(PopupMenuEvent ev) {
}
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent ev) {
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent ev) {
JComboBox<?> combo = (JComboBox<?>) ev.getSource();
Point comboLoc = combo.getLocationOnScreen();
JPopupMenu popupMenu = (JPopupMenu) combo.getUI().getAccessibleChild(combo, 0);
Point popupMenuLoc = popupMenu.getLocationOnScreen();
Dimension popupSize = popupMenu.getSize();
isPopupOnScreen(popupMenu, fullScreenBounds);
if (comboLoc.x > 0) {
// The frame is located at the bottom of the screen,
// the combo popups should open upwards
if (popupMenuLoc.y + popupSize.height < comboLoc.y) {
System.err.println("popup " + popupMenuLoc
+ " combo " + comboLoc);
throw new RuntimeException("ComboBox popup should open upwards");
}
} else {
// The frame has been moved to negative position away from
// the bottom of the screen, the combo popup should
// open downwards in this case
if (popupMenuLoc.y + 1 < comboLoc.y) {
System.err.println("popup " + popupMenuLoc
+ " combo " + comboLoc);
throw new RuntimeException("ComboBox popup should open downwards");
}
}
}
}
private static class PopupHandler extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
if (!popupMenu.isVisible()) {
popupMenu.show((Component) e.getSource(), 40, 40);
}
isPopupOnScreen(popupMenu, fullScreenBounds);
}
}
private static class PopupListener extends MouseAdapter {
private final JPopupMenu popup;
public PopupListener(JPopupMenu popup) {
this.popup = popup;
}
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
isPopupOnScreen(popup, fullScreenBounds);
}
}
}
/**
* Tests if the popup is on the screen.
*/
private static void isPopupOnScreen(JPopupMenu popup, Rectangle checkBounds) {
if (!popup.isVisible()) {
throw new RuntimeException("Popup not visible");
}
Dimension dim = popup.getSize();
Point pt = popup.getLocationOnScreen();
Rectangle bounds = new Rectangle(pt, dim);
if (!SwingUtilities.isRectangleContainingRectangle(checkBounds, bounds)) {
throw new RuntimeException("Popup is outside of screen bounds "
+ checkBounds + " / " + bounds);
}
}
private static void isComboPopupOnScreen(JComboBox<?> comboBox) {
if (!comboBox.isPopupVisible()) {
throw new RuntimeException("ComboBox popup not visible");
}
JPopupMenu popupMenu = (JPopupMenu) comboBox.getUI().getAccessibleChild(comboBox, 0);
isPopupOnScreen(popupMenu, screenBounds);
}
private JPanel createContentPane() {
combo1 = new JComboBox<>(numData);
combo1.addPopupMenuListener(new ComboPopupCheckListener());
combo2 = new JComboBox<>(dayData);
combo2.setEditable(true);
combo2.addPopupMenuListener(new ComboPopupCheckListener());
popupMenu = new JPopupMenu();
for (int i = 0; i < dayData.length; i++) {
JMenuItem item = popupMenu.add(new JMenuItem(dayData[i], mnDayData[i]));
item.addActionListener(this);
}
JTextField field = new JTextField("CTRL+down for Popup");
// CTRL-down will show the popup.
field.getInputMap().put(KeyStroke.getKeyStroke(
KeyEvent.VK_DOWN, InputEvent.CTRL_DOWN_MASK), "OPEN_POPUP");
field.getActionMap().put("OPEN_POPUP", new PopupHandler());
JPanel panel = new JPanel();
panel.add(combo1);
panel.add(combo2);
panel.setSize(300, 200);
panel.addMouseListener(new PopupListener(popupMenu));
panel.add(field);
return panel;
}
private JMenuBar createMenuBar() {
JMenuBar menubar = new JMenuBar();
menu1 = new JMenu("1 - First Menu");
menu1.setMnemonic('1');
createSubMenu(menu1, "1 JMenuItem", 8, null);
menubar.add(menu1);
menu2 = new JMenu("2 - Second Menu");
menu2.setMnemonic('2');
createSubMenu(menu2, "2 JMenuItem", 4, null);
menu2.add(new JSeparator());
menubar.add(menu2);
submenu = new JMenu("Sub Menu");
submenu.setMnemonic('S');
createSubMenu(submenu, "S JMenuItem", 4, this);
menu2.add(submenu);
return menubar;
}
private static void createSubMenu(JMenu menu, String prefix, int count, ActionListener action) {
for (int i = 0; i < count; ++i) {
JMenuItem menuitem = new JMenuItem(prefix + i);
menu.add(menuitem);
if (action != null) {
menuitem.addActionListener(action);
}
}
}
public void actionPerformed(ActionEvent evt) {
Object obj = evt.getSource();
if (obj instanceof JMenuItem) {
// put the focus on the non-editable combo.
combo1.requestFocus();
}
}
private static void hidePopup(Robot robot) {
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
}
public static void main(String[] args) throws Throwable {
GraphicsDevice mainScreen = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
Rectangle mainScreenBounds = mainScreen.getDefaultConfiguration()
.getBounds();
GraphicsDevice[] screens = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getScreenDevices();
for (GraphicsDevice screen : screens) {
if (screen == mainScreen) {
continue;
}
Rectangle bounds = screen.getDefaultConfiguration()
.getBounds();
if (bounds.x < 0) {
// The test may fail if a screen have negative origin
throw new SkippedException("Configurations with negative screen"
+ " origin are not supported");
}
if (bounds.y >= mainScreenBounds.height) {
// The test may fail if there's a screen to bottom of the main monitor
throw new SkippedException("Configurations with a screen beneath"
+ " the main one are not supported");
}
}
// Use Robot to automate the test
Robot robot = new Robot();
robot.setAutoDelay(50);
try {
SwingUtilities.invokeAndWait(TaskbarPositionTest::new);
robot.waitForIdle();
robot.delay(1000);
// 1 - menu
Util.hitMnemonics(robot, KeyEvent.VK_1);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> isPopupOnScreen(menu1.getPopupMenu(), screenBounds));
// 2 menu with sub menu
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
// Open the submenu
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> isPopupOnScreen(menu2.getPopupMenu(), screenBounds));
SwingUtilities.invokeAndWait(() -> isPopupOnScreen(submenu.getPopupMenu(), screenBounds));
// Hit Enter to perform the action of
// a selected menu item in the submenu
// which requests focus on combo1, non-editable combo box
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.waitForIdle();
// Focus should go to combo1
// Open combo1 popup
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> isComboPopupOnScreen(combo1));
hidePopup(robot);
// Move focus to combo2, editable combo box
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.waitForIdle();
// Open combo2 popup
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> isComboPopupOnScreen(combo2));
hidePopup(robot);
// Move focus to the text field
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.waitForIdle();
// Open its popup
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> isPopupOnScreen(popupMenu, fullScreenBounds));
hidePopup(robot);
// Popup from a mouse click
SwingUtilities.invokeAndWait(() -> {
Point pt = panel.getLocationOnScreen();
pt.translate(4, 4);
robot.mouseMove(pt.x, pt.y);
});
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
// Ensure popupMenu is shown within screen bounds
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> isPopupOnScreen(popupMenu, fullScreenBounds));
hidePopup(robot);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
frame.setLocation(-30, 100);
combo1.requestFocus();
});
robot.waitForIdle();
// Open combo1 popup again
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> isComboPopupOnScreen(combo1));
hidePopup(robot);
robot.waitForIdle();
} catch (Throwable t) {
saveScreenCapture(robot, screens);
throw t;
} finally {
SwingUtilities.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
private static void saveScreenCapture(Robot robot, GraphicsDevice[] screens) {
for (int i = 0; i < screens.length; i++) {
Rectangle bounds = screens[i].getDefaultConfiguration()
.getBounds();
BufferedImage image = robot.createScreenCapture(bounds);
try {
ImageIO.write(image, "png", new File("Screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|