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