Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JMenu/6538132/bug6538132.java
38853 views
/*1* Copyright (c) 2011, 2014, 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/* @test23@bug 653813224@summary Regression: Pressing Escape key don't close the menu items from jdk7.0 b07 onwards25@author Alexander Potochkin26@library ../../../../lib/testlibrary27@build ExtendedRobot28@run main bug653813229*/3031import javax.swing.*;32import java.awt.*;33import java.awt.event.InputEvent;34import java.awt.event.KeyEvent;3536public class bug6538132 {37private static JMenu menu1;38private static JMenu menu2;39private static volatile boolean isWinLaf;4041private static void createGui() {42try {43UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");44isWinLaf = true;45} catch (Exception e) {46// If we can't set WinLaf it means we are not under Windows47// make the test pass48isWinLaf = false;49return;50}51JFrame frame = new JFrame();52frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);5354JMenuBar menuBar = new JMenuBar();55menu1 = createMenu();56menuBar.add(menu1);57menu2 = createMenu();58menuBar.add(menu2);59frame.setJMenuBar(menuBar);6061frame.setSize(200, 200);62frame.setVisible(true);63}6465static JMenu createMenu() {66JMenu menu = new JMenu("Menu");67menu.add(new JMenuItem("MenuItem"));68menu.add(new JMenuItem("MenuItem"));69menu.add(new JMenuItem("MenuItem"));70return menu;71}7273public static void main(String[] args) throws Exception {74SwingUtilities.invokeAndWait(new Runnable() {75public void run() {76bug6538132.createGui();77}78});79if(isWinLaf) {80ExtendedRobot robot = new ExtendedRobot();81robot.setAutoDelay(10);82robot.waitForIdle();83Point p1 = menu1.getLocationOnScreen();84final int x1 = p1.x + menu1.getWidth() / 2;85final int y1 = p1.y + menu1.getHeight() / 2;86robot.glide(0, 0, x1, y1);87robot.mousePress(InputEvent.BUTTON1_MASK);88robot.mouseRelease(InputEvent.BUTTON1_MASK);89assertPopupOpen();90Point p2 = menu2.getLocationOnScreen();91final int x2 = p2.x + menu2.getWidth() / 2;92final int y2 = p2.y + menu2.getHeight() / 2;93robot.glide(x1, y1, x2, y2);94assertPopupOpen();95robot.keyPress(KeyEvent.VK_ESCAPE);96robot.keyRelease(KeyEvent.VK_ESCAPE);97assertPopupNotOpen();98robot.glide(x2, y2, x1, y1);99assertPopupNotOpen();100robot.mousePress(InputEvent.BUTTON1_MASK);101robot.mouseRelease(InputEvent.BUTTON1_MASK);102assertPopupOpen();103}104}105106static void assertPopupOpen() {107if (getLastPopup() == null) {108throw new RuntimeException("PopupMenu is not open");109}110}111112static void assertPopupNotOpen() {113if (getLastPopup() != null) {114throw new RuntimeException("PopupMenu is unexpectedly open");115}116}117118// copied from BasicPopupMenuUI119static JPopupMenu getLastPopup() {120MenuSelectionManager msm = MenuSelectionManager.defaultManager();121MenuElement[] p = msm.getSelectedPath();122JPopupMenu popup = null;123124for (int i = p.length - 1; popup == null && i >= 0; i--) {125if (p[i] instanceof JPopupMenu)126popup = (JPopupMenu) p[i];127}128return popup;129}130}131132133