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  * @key headful
  27  * @bug 4654927
  28  * @summary Clicking on Greyed Menuitems closes the Menubar Dropdown
  29  * @author Alexander Potochkin
  30  * @library ../../regtesthelpers
  31  * @build Util
  32  * @run main bug4654927
  33  */
  34 
  35 import javax.swing.*;
  36 
  37 import java.awt.*;
  38 import java.awt.event.InputEvent;
  39 import java.util.concurrent.Callable;
  40 
  41 public class bug4654927 {
  42 
  43     private static volatile JMenu menu;
  44     private static volatile JMenuItem menuItem;
  45 
  46     public static void main(String[] args) throws Exception {
  47         String systemLAF = UIManager.getSystemLookAndFeelClassName();
  48         // the test is not applicable to Motif L&F
  49         if(systemLAF.endsWith("MotifLookAndFeel")){
  50             return;
  51         }
  52 
  53         UIManager.setLookAndFeel(systemLAF);
  54         Robot robot = new Robot();
  55         robot.setAutoDelay(10);
  56 
  57         SwingUtilities.invokeAndWait(new Runnable() {
  58 
  59             public void run() {
  60                 createAndShowUI();
  61             }
  62         });
  63         robot.waitForIdle();
  64 
  65         // test mouse press
  66         Point point = Util.getCenterPoint(menu);
  67         robot.mouseMove(point.x, point.y);
  68         robot.mousePress(InputEvent.BUTTON1_MASK);
  69         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  70         robot.waitForIdle();
  71 
  72         point = Util.getCenterPoint(menuItem);
  73         robot.mouseMove(point.x, point.y);
  74         robot.mousePress(InputEvent.BUTTON1_MASK);
  75         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  76         robot.waitForIdle();
  77 
  78         if (!isMenuItemShowing()) {
  79             throw new RuntimeException("Popup is unexpectedly closed");
  80         }
  81 
  82         // test mouse drag
  83         point = Util.getCenterPoint(menu);
  84         robot.mouseMove(point.x, point.y);
  85         Point menuLocation = Util.invokeOnEDT(new Callable<Point>() {
  86 
  87             @Override
  88             public Point call() throws Exception {
  89                 return menu.getLocationOnScreen();
  90             }
  91         });
  92 
  93         Point itemLocation = Util.invokeOnEDT(new Callable<Point>() {
  94 
  95             @Override
  96             public Point call() throws Exception {
  97                 return menuItem.getLocationOnScreen();
  98             }
  99         });
 100 
 101         int x0 = menuLocation.x + 10;
 102         int y0 = menuLocation.y + 10;
 103         int x1 = itemLocation.x + 10;
 104         int y1 = itemLocation.y + 10;
 105 
 106         // close menu
 107         robot.mousePress(InputEvent.BUTTON1_MASK);
 108         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 109         robot.waitForIdle();
 110 
 111         robot.mousePress(InputEvent.BUTTON1_MASK);
 112         Util.glide(robot, x0, y0, x1, y1);
 113         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 114         robot.waitForIdle();
 115 
 116         if (!isMenuItemShowing()) {
 117             throw new RuntimeException("Popup is unexpectedly closed");
 118         }
 119     }
 120 
 121     private static boolean isMenuItemShowing() throws Exception {
 122         return Util.invokeOnEDT(new Callable<Boolean>() {
 123 
 124             @Override
 125             public Boolean call() throws Exception {
 126                 return menuItem.isShowing();
 127             }
 128         });
 129     }
 130 
 131     private static void createAndShowUI() {
 132         JFrame frame = new JFrame();
 133         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 134 
 135         menu = new JMenu("Menu");
 136         menu.add(new JMenuItem("menuItem"));
 137         menuItem = new JMenuItem("menuItem");
 138         menuItem.setEnabled(false);
 139         menu.add(menuItem);
 140         menu.add(new JMenuItem("menuItem"));
 141 
 142         JMenuBar bar = new JMenuBar();
 143         bar.add(menu);
 144         frame.setJMenuBar(bar);
 145 
 146         frame.setSize(200, 200);
 147         frame.setLocationRelativeTo(null);
 148         frame.setVisible(true);
 149 
 150     }
 151 }