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