Path: blob/master/test/jdk/javax/swing/JMenu/6538132/bug6538132.java
41153 views
/*1* Copyright (c) 2011, 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*/2223/**24* @test25* @key headful26* @bug 653813227* @summary Regression: Pressing Escape key don't close the menu items from jdk7.0 b07 onwards28* @author Alexander Potochkin29* @requires (os.family == "windows")30* @library /lib/client31* @build ExtendedRobot32* @run main bug653813233*/3435import javax.swing.*;36import java.awt.*;37import java.awt.event.InputEvent;38import java.awt.event.KeyEvent;3940public class bug6538132 {41private static JMenu menu1;42private static JMenu menu2;43private static volatile boolean isWinLaf;44private static JFrame frame;4546private static void createGui() {47try {48UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");49isWinLaf = true;50} catch (Exception e) {51// If we can't set WinLaf it means we are not under Windows52// make the test pass53isWinLaf = false;54return;55}56frame = new JFrame();57frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);5859JMenuBar menuBar = new JMenuBar();60menu1 = createMenu();61menuBar.add(menu1);62menu2 = createMenu();63menuBar.add(menu2);64frame.setJMenuBar(menuBar);6566frame.setSize(200, 200);67frame.setVisible(true);68}6970static JMenu createMenu() {71JMenu menu = new JMenu("Menu");72menu.add(new JMenuItem("MenuItem"));73menu.add(new JMenuItem("MenuItem"));74menu.add(new JMenuItem("MenuItem"));75return menu;76}7778public static void main(String[] args) throws Exception {79try {80SwingUtilities.invokeAndWait(new Runnable() {81public void run() {82bug6538132.createGui();83}84});85if(isWinLaf) {86ExtendedRobot robot = new ExtendedRobot();87robot.setAutoDelay(10);88robot.waitForIdle();89Point p1 = menu1.getLocationOnScreen();90final int x1 = p1.x + menu1.getWidth() / 2;91final int y1 = p1.y + menu1.getHeight() / 2;92robot.glide(0, 0, x1, y1);93robot.mousePress(InputEvent.BUTTON1_MASK);94robot.mouseRelease(InputEvent.BUTTON1_MASK);95assertPopupOpen();96Point p2 = menu2.getLocationOnScreen();97final int x2 = p2.x + menu2.getWidth() / 2;98final int y2 = p2.y + menu2.getHeight() / 2;99robot.glide(x1, y1, x2, y2);100assertPopupOpen();101robot.keyPress(KeyEvent.VK_ESCAPE);102robot.keyRelease(KeyEvent.VK_ESCAPE);103assertPopupNotOpen();104robot.glide(x2, y2, x1, y1);105assertPopupNotOpen();106robot.mousePress(InputEvent.BUTTON1_MASK);107robot.mouseRelease(InputEvent.BUTTON1_MASK);108assertPopupOpen();109}110} finally {111if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());112}113}114115static void assertPopupOpen() {116if (getLastPopup() == null) {117throw new RuntimeException("PopupMenu is not open");118}119}120121static void assertPopupNotOpen() {122if (getLastPopup() != null) {123throw new RuntimeException("PopupMenu is unexpectedly open");124}125}126127// copied from BasicPopupMenuUI128static JPopupMenu getLastPopup() {129MenuSelectionManager msm = MenuSelectionManager.defaultManager();130MenuElement[] p = msm.getSelectedPath();131JPopupMenu popup = null;132133for (int i = p.length - 1; popup == null && i >= 0; i--) {134if (p[i] instanceof JPopupMenu)135popup = (JPopupMenu) p[i];136}137return popup;138}139}140141142