Path: blob/master/test/jdk/javax/swing/JMenuItem/4654927/bug4654927.java
41154 views
/*1* Copyright (c) 2012, 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 465492727* @summary Clicking on Greyed Menuitems closes the Menubar Dropdown28* @author Alexander Potochkin29* @library ../../regtesthelpers30* @build Util31* @run main bug465492732*/3334import javax.swing.*;3536import java.awt.*;37import java.awt.event.InputEvent;38import java.util.concurrent.Callable;3940public class bug4654927 {4142private static volatile JMenu menu;43private static volatile JMenuItem menuItem;44private static JFrame frame;4546public static void main(String[] args) throws Exception {47try {48String systemLAF = UIManager.getSystemLookAndFeelClassName();49// the test is not applicable to Motif L&F50if(systemLAF.endsWith("MotifLookAndFeel")){51return;52}5354UIManager.setLookAndFeel(systemLAF);55Robot robot = new Robot();56robot.setAutoDelay(10);5758SwingUtilities.invokeAndWait(new Runnable() {5960public void run() {61createAndShowUI();62}63});64robot.waitForIdle();6566// test mouse press67Point point = Util.getCenterPoint(menu);68robot.mouseMove(point.x, point.y);69robot.mousePress(InputEvent.BUTTON1_MASK);70robot.mouseRelease(InputEvent.BUTTON1_MASK);71robot.waitForIdle();7273point = Util.getCenterPoint(menuItem);74robot.mouseMove(point.x, point.y);75robot.mousePress(InputEvent.BUTTON1_MASK);76robot.mouseRelease(InputEvent.BUTTON1_MASK);77robot.waitForIdle();7879if (!isMenuItemShowing()) {80throw new RuntimeException("Popup is unexpectedly closed");81}8283// test mouse drag84point = Util.getCenterPoint(menu);85robot.mouseMove(point.x, point.y);86Point menuLocation = Util.invokeOnEDT(new Callable<Point>() {8788@Override89public Point call() throws Exception {90return menu.getLocationOnScreen();91}92});9394Point itemLocation = Util.invokeOnEDT(new Callable<Point>() {9596@Override97public Point call() throws Exception {98return menuItem.getLocationOnScreen();99}100});101102int x0 = menuLocation.x + 10;103int y0 = menuLocation.y + 10;104int x1 = itemLocation.x + 10;105int y1 = itemLocation.y + 10;106107// close menu108robot.mousePress(InputEvent.BUTTON1_MASK);109robot.mouseRelease(InputEvent.BUTTON1_MASK);110robot.waitForIdle();111112robot.mousePress(InputEvent.BUTTON1_MASK);113Util.glide(robot, x0, y0, x1, y1);114robot.mouseRelease(InputEvent.BUTTON1_MASK);115robot.waitForIdle();116117if (!isMenuItemShowing()) {118throw new RuntimeException("Popup is unexpectedly closed");119}120} finally {121if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());122}123}124125private static boolean isMenuItemShowing() throws Exception {126return Util.invokeOnEDT(new Callable<Boolean>() {127128@Override129public Boolean call() throws Exception {130return menuItem.isShowing();131}132});133}134135private static void createAndShowUI() {136frame = new JFrame();137frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);138139menu = new JMenu("Menu");140menu.add(new JMenuItem("menuItem"));141menuItem = new JMenuItem("menuItem");142menuItem.setEnabled(false);143menu.add(menuItem);144menu.add(new JMenuItem("menuItem"));145146JMenuBar bar = new JMenuBar();147bar.add(menu);148frame.setJMenuBar(bar);149150frame.setSize(200, 200);151frame.setLocationRelativeTo(null);152frame.setVisible(true);153154}155}156157158