1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4654927
  27  * @summary Clicking on Greyed Menuitems closes the Menubar Dropdown
  28  * @author Alexander Potochkin
  29  * @library ../../regtesthelpers
  30  * @build Util
  31  * @run main bug4654927
  32  */
  33 
  34 import javax.swing.*;
  35 
  36 import java.awt.*;
  37 import java.awt.event.InputEvent;
  38 import java.util.concurrent.Callable;
  39 
  40 public class bug4654927 {
  41 
  42     private static volatile JMenu menu;
  43     private static volatile JMenuItem menuItem;
  44 
  45     public static void main(String[] args) throws Exception {
  46         String systemLAF = UIManager.getSystemLookAndFeelClassName();
  47         // the test is not applicable to Motif L&F
  48         if(systemLAF.endsWith("MotifLookAndFeel")){
  49             return;
  50         }
  51 
  52         UIManager.setLookAndFeel(systemLAF);
  53         Robot robot = new Robot();
  54         robot.setAutoDelay(10);
  55 
  56         SwingUtilities.invokeAndWait(new Runnable() {
  57 
  58             public void run() {
  59                 createAndShowUI();
  60             }
  61         });
  62         robot.waitForIdle();
  63 
  64         // test mouse press
  65         Point point = Util.getCenterPoint(menu);
  66         robot.mouseMove(point.x, point.y);
  67         robot.mousePress(InputEvent.BUTTON1_MASK);
  68         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  69         robot.waitForIdle();
  70 
  71         point = Util.getCenterPoint(menuItem);
  72         robot.mouseMove(point.x, point.y);
  73         robot.mousePress(InputEvent.BUTTON1_MASK);
  74         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  75         robot.waitForIdle();
  76 
  77         if (!isMenuItemShowing()) {
  78             throw new RuntimeException("Popup is unexpectedly closed");
  79         }
  80 
  81         // test mouse drag
  82         point = Util.getCenterPoint(menu);
  83         robot.mouseMove(point.x, point.y);
  84         Point menuLocation = Util.invokeOnEDT(new Callable<Point>() {
  85 
  86             @Override
  87             public Point call() throws Exception {
  88                 return menu.getLocationOnScreen();
  89             }
  90         });
  91 
  92         Point itemLocation = Util.invokeOnEDT(new Callable<Point>() {
  93 
  94             @Override
  95             public Point call() throws Exception {
  96                 return menuItem.getLocationOnScreen();
  97             }
  98         });
  99 
 100         int x0 = menuLocation.x + 10;
 101         int y0 = menuLocation.y + 10;
 102         int x1 = itemLocation.x + 10;
 103         int y1 = itemLocation.y + 10;
 104 
 105         // close menu
 106         robot.mousePress(InputEvent.BUTTON1_MASK);
 107         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 108         robot.waitForIdle();
 109 
 110         robot.mousePress(InputEvent.BUTTON1_MASK);
 111         Util.glide(robot, x0, y0, x1, y1);
 112         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 113         robot.waitForIdle();
 114 
 115         if (!isMenuItemShowing()) {
 116             throw new RuntimeException("Popup is unexpectedly closed");
 117         }
 118     }
 119 
 120     private static boolean isMenuItemShowing() throws Exception {
 121         return Util.invokeOnEDT(new Callable<Boolean>() {
 122 
 123             @Override
 124             public Boolean call() throws Exception {
 125                 return menuItem.isShowing();
 126             }
 127         });
 128     }
 129 
 130     private static void createAndShowUI() {
 131         JFrame frame = new JFrame();
 132         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 133 
 134         menu = new JMenu("Menu");
 135         menu.add(new JMenuItem("menuItem"));
 136         menuItem = new JMenuItem("menuItem");
 137         menuItem.setEnabled(false);
 138         menu.add(menuItem);
 139         menu.add(new JMenuItem("menuItem"));
 140 
 141         JMenuBar bar = new JMenuBar();
 142         bar.add(menu);
 143         frame.setJMenuBar(bar);
 144 
 145         frame.setSize(200, 200);
 146         frame.setLocationRelativeTo(null);
 147         frame.setVisible(true);
 148 
 149     }
 150 }