Path: blob/master/test/jdk/javax/swing/JPopupMenu/4870644/bug4870644.java
41153 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @key headful25* @bug 4870644 719053926* @summary Default button responds to CTRL-ENTER while popup menu is active.27* @run main bug487064428*/29import java.awt.BorderLayout;30import java.awt.Dimension;31import java.awt.Point;32import java.awt.Robot;33import java.awt.event.KeyEvent;34import java.awt.event.ActionEvent;35import java.awt.event.InputEvent;36import javax.swing.AbstractAction;37import javax.swing.JButton;38import javax.swing.JComponent;39import javax.swing.JFrame;40import javax.swing.JMenu;41import javax.swing.JMenuBar;42import javax.swing.JMenuItem;43import javax.swing.JPopupMenu;44import javax.swing.SwingUtilities;45import javax.swing.UIManager;46import javax.swing.UnsupportedLookAndFeelException;474849public class bug4870644 {50JButton b1, b2, b3;51JFrame frame;52JMenu menu;53JPopupMenu popup;54static Robot robot;55static boolean passed = true;56private volatile Point p = null;57private volatile Dimension d = null;58void blockTillDisplayed(JComponent comp) throws Exception {59while (p == null) {60try {61SwingUtilities.invokeAndWait(() -> {62p = comp.getLocationOnScreen();63d = comp.getSize();64});65} catch (IllegalStateException e) {66try {67Thread.sleep(1000);68} catch (InterruptedException ie) {69}70}71}72}7374private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {75try {76UIManager.setLookAndFeel(laf.getClassName());77System.out.println("LookAndFeel: " + laf.getClassName());78} catch (ClassNotFoundException | InstantiationException |79UnsupportedLookAndFeelException | IllegalAccessException e) {80throw new RuntimeException(e);81}82}8384public static void main(String[] args) throws Exception {85robot = new Robot();86robot.setAutoDelay(100);87for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {88try {89SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));90System.out.println("Test for LookAndFeel " + laf.getClassName());91new bug4870644();92System.out.println("Test passed for LookAndFeel " + laf.getClassName());93} catch (Exception e) {94throw new RuntimeException(e);95}96}97}9899public bug4870644() throws Exception {100101SwingUtilities.invokeAndWait(() -> {102JMenuBar menuBar = new JMenuBar();103menu = new JMenu("Menu");104menuBar.add(menu);105JMenuItem menuItem = new JMenuItem("Item");106menu.add(menuItem);107menu.add(new JMenuItem("Item 2"));108frame = new JFrame("test");109frame.setJMenuBar(menuBar);110111b1 = new JButton("One");112b2 = new JButton("Two");113b3 = new JButton("Default");114b3.addActionListener(new AbstractAction() {115public void actionPerformed(ActionEvent e) {116passed = false;117}118});119frame.getContentPane().add(b1, BorderLayout.NORTH);120frame.getContentPane().add(b2, BorderLayout.CENTER);121frame.getContentPane().add(b3, BorderLayout.SOUTH);122frame.getRootPane().setDefaultButton(b3);123frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);124frame.pack();125frame.setVisible(true);126});127128blockTillDisplayed(b1);129robot.waitForIdle();130131robot.delay(500);132robot.mouseMove(p.x + d.width-1, p.y + d.height/2);133robot.mousePress(InputEvent.BUTTON1_MASK);134robot.mouseRelease(InputEvent.BUTTON1_MASK);135robot.waitForIdle();136robot.keyPress(KeyEvent.VK_F10);137robot.keyRelease(KeyEvent.VK_F10);138robot.keyPress(KeyEvent.VK_DOWN);139robot.keyRelease(KeyEvent.VK_DOWN);140robot.waitForIdle();141142SwingUtilities.invokeAndWait(() -> {143popup = menu.getPopupMenu();144});145146blockTillDisplayed(popup);147robot.waitForIdle();148robot.mouseMove(p.x + d.width-1, p.y + d.height/2);149robot.keyPress(KeyEvent.VK_CONTROL);150robot.keyPress(KeyEvent.VK_ENTER);151robot.keyRelease(KeyEvent.VK_ENTER);152robot.keyRelease(KeyEvent.VK_CONTROL);153robot.waitForIdle();154155SwingUtilities.invokeAndWait(() -> frame.dispose());156if(!passed) {157String cause = "Default button reacted on \"ctrl ENTER\" while menu is active.";158throw new RuntimeException(cause);159}160}161}162163164