Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JMenuItem/4654927/bug4654927.java
38853 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* @bug 465492726* @summary Clicking on Greyed Menuitems closes the Menubar Dropdown27* @author Alexander Potochkin28* @library ../../regtesthelpers29* @build Util30* @run main bug465492731*/3233import javax.swing.*;3435import java.awt.*;36import java.awt.event.InputEvent;37import java.util.concurrent.Callable;3839public class bug4654927 {4041private static volatile JMenu menu;42private static volatile JMenuItem menuItem;4344public static void main(String[] args) throws Exception {45String systemLAF = UIManager.getSystemLookAndFeelClassName();46// the test is not applicable to Motif L&F47if(systemLAF.endsWith("MotifLookAndFeel")){48return;49}5051UIManager.setLookAndFeel(systemLAF);52Robot robot = new Robot();53robot.setAutoDelay(10);5455SwingUtilities.invokeAndWait(new Runnable() {5657public void run() {58createAndShowUI();59}60});61robot.waitForIdle();6263// test mouse press64Point point = Util.getCenterPoint(menu);65robot.mouseMove(point.x, point.y);66robot.mousePress(InputEvent.BUTTON1_MASK);67robot.mouseRelease(InputEvent.BUTTON1_MASK);68robot.waitForIdle();6970point = Util.getCenterPoint(menuItem);71robot.mouseMove(point.x, point.y);72robot.mousePress(InputEvent.BUTTON1_MASK);73robot.mouseRelease(InputEvent.BUTTON1_MASK);74robot.waitForIdle();7576if (!isMenuItemShowing()) {77throw new RuntimeException("Popup is unexpectedly closed");78}7980// test mouse drag81point = Util.getCenterPoint(menu);82robot.mouseMove(point.x, point.y);83Point menuLocation = Util.invokeOnEDT(new Callable<Point>() {8485@Override86public Point call() throws Exception {87return menu.getLocationOnScreen();88}89});9091Point itemLocation = Util.invokeOnEDT(new Callable<Point>() {9293@Override94public Point call() throws Exception {95return menuItem.getLocationOnScreen();96}97});9899int x0 = menuLocation.x + 10;100int y0 = menuLocation.y + 10;101int x1 = itemLocation.x + 10;102int y1 = itemLocation.y + 10;103104// close menu105robot.mousePress(InputEvent.BUTTON1_MASK);106robot.mouseRelease(InputEvent.BUTTON1_MASK);107robot.waitForIdle();108109robot.mousePress(InputEvent.BUTTON1_MASK);110Util.glide(robot, x0, y0, x1, y1);111robot.mouseRelease(InputEvent.BUTTON1_MASK);112robot.waitForIdle();113114if (!isMenuItemShowing()) {115throw new RuntimeException("Popup is unexpectedly closed");116}117}118119private static boolean isMenuItemShowing() throws Exception {120return Util.invokeOnEDT(new Callable<Boolean>() {121122@Override123public Boolean call() throws Exception {124return menuItem.isShowing();125}126});127}128129private static void createAndShowUI() {130JFrame frame = new JFrame();131frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);132133menu = new JMenu("Menu");134menu.add(new JMenuItem("menuItem"));135menuItem = new JMenuItem("menuItem");136menuItem.setEnabled(false);137menu.add(menuItem);138menu.add(new JMenuItem("menuItem"));139140JMenuBar bar = new JMenuBar();141bar.add(menu);142frame.setJMenuBar(bar);143144frame.setSize(200, 200);145frame.setLocationRelativeTo(null);146frame.setVisible(true);147148}149}150151152