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