1 /*
   2  * Copyright (c) 2011, 2014, 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 /* @test
  24    @bug 6538132
  25    @summary Regression: Pressing Escape key don't close the menu items from jdk7.0 b07 onwards
  26    @author Alexander Potochkin
  27    @library ../../../../lib/testlibrary
  28    @build ExtendedRobot
  29    @run main bug6538132
  30 */
  31 
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.event.InputEvent;
  35 import java.awt.event.KeyEvent;
  36 
  37 public class bug6538132 {
  38     private static JMenu menu1;
  39     private static JMenu menu2;
  40     private static volatile boolean isWinLaf;
  41 
  42     private static void createGui() {
  43         try {
  44             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  45             isWinLaf = true;
  46         } catch (Exception e) {
  47             // If we can't set WinLaf it means we are not under Windows
  48             // make the test pass
  49             isWinLaf = false;
  50             return;
  51         }
  52         JFrame frame = new JFrame();
  53         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54 
  55         JMenuBar menuBar = new JMenuBar();
  56         menu1 = createMenu();
  57         menuBar.add(menu1);
  58         menu2 = createMenu();
  59         menuBar.add(menu2);
  60         frame.setJMenuBar(menuBar);
  61 
  62         frame.setSize(200, 200);
  63         frame.setVisible(true);
  64     }
  65 
  66     static JMenu createMenu() {
  67         JMenu menu = new JMenu("Menu");
  68         menu.add(new JMenuItem("MenuItem"));
  69         menu.add(new JMenuItem("MenuItem"));
  70         menu.add(new JMenuItem("MenuItem"));
  71         return menu;
  72     }
  73 
  74     public static void main(String[] args) throws Exception {
  75         SwingUtilities.invokeAndWait(new Runnable() {
  76             public void run() {
  77                 bug6538132.createGui();
  78             }
  79         });
  80         if(isWinLaf) {
  81             ExtendedRobot robot = new ExtendedRobot();
  82             robot.setAutoDelay(10);
  83             robot.waitForIdle();
  84             Point p1 = menu1.getLocationOnScreen();
  85             final int x1 = p1.x + menu1.getWidth() / 2;
  86             final int y1 = p1.y + menu1.getHeight() / 2;
  87             robot.glide(0, 0, x1, y1);
  88             robot.mousePress(InputEvent.BUTTON1_MASK);
  89             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  90             assertPopupOpen();
  91             Point p2 = menu2.getLocationOnScreen();
  92             final int x2 = p2.x + menu2.getWidth() / 2;
  93             final int y2 = p2.y + menu2.getHeight() / 2;
  94             robot.glide(x1, y1, x2, y2);
  95             assertPopupOpen();
  96             robot.keyPress(KeyEvent.VK_ESCAPE);
  97             robot.keyRelease(KeyEvent.VK_ESCAPE);
  98             assertPopupNotOpen();
  99             robot.glide(x2, y2, x1, y1);
 100             assertPopupNotOpen();
 101             robot.mousePress(InputEvent.BUTTON1_MASK);
 102             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 103             assertPopupOpen();
 104         }
 105     }
 106 
 107     static void assertPopupOpen() {
 108         if (getLastPopup() == null) {
 109             throw new RuntimeException("PopupMenu is not open");
 110         }
 111     }
 112 
 113     static void assertPopupNotOpen() {
 114         if (getLastPopup() != null) {
 115             throw new RuntimeException("PopupMenu is unexpectedly open");
 116         }
 117     }
 118 
 119     // copied from BasicPopupMenuUI
 120     static JPopupMenu getLastPopup() {
 121         MenuSelectionManager msm = MenuSelectionManager.defaultManager();
 122         MenuElement[] p = msm.getSelectedPath();
 123         JPopupMenu popup = null;
 124 
 125         for (int i = p.length - 1; popup == null && i >= 0; i--) {
 126             if (p[i] instanceof JPopupMenu)
 127                 popup = (JPopupMenu) p[i];
 128         }
 129         return popup;
 130     }
 131 }