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 6217905
  28  * @summary JPopupMenu keyboard navigation stops working
  29  * @author Alexander Potochkin
  30  * @requires (os.family == "windows")
  31  * @library ../../../../lib/testlibrary
  32  * @build ExtendedRobot
  33  * @run main bug6217905
  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 bug6217905 {
  42     private static JPanel popupPanel;
  43     private static JMenuItem firstItem;
  44     private static JMenuItem lastItem;
  45 
  46     private static void createGui() {
  47         final JFrame frame = new JFrame();
  48         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49 
  50         JPopupMenu popup = new JPopupMenu("Menu");
  51         firstItem = new JMenuItem("MenuItem");
  52         popup.add(firstItem);
  53         popup.add(new JMenuItem("MenuItem"));
  54         lastItem = new JMenuItem("MenuItem");
  55         popup.add(lastItem);
  56 
  57         popupPanel = new JPanel();
  58         popupPanel.setComponentPopupMenu(popup);
  59         frame.add(popupPanel);
  60         frame.setSize(100, 100);
  61         frame.setVisible(true);
  62     }
  63 
  64     public static void main(String[] args) throws Exception {
  65         try {
  66             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  67         } catch (Exception e) {
  68             // This test is for WinLaf only
  69             System.out.println("This test is for Windows LaF only.");
  70             return;
  71         }
  72 
  73         ExtendedRobot robot = new ExtendedRobot();
  74         robot.setAutoDelay(10);
  75         SwingUtilities.invokeLater(new Runnable() {
  76             public void run() {
  77                 bug6217905.createGui();
  78             }
  79         });
  80         robot.waitForIdle();
  81         Point loc = popupPanel.getLocationOnScreen();
  82         int x = loc.x + popupPanel.getWidth()/2;
  83         int y = loc.y + popupPanel.getHeight()/2;
  84         robot.glide(0, 0, x, y);
  85         robot.mousePress(InputEvent.BUTTON3_MASK);
  86         robot.mouseRelease(InputEvent.BUTTON3_MASK);
  87         robot.waitForIdle();
  88         if (getSelectedPathLength() != 1) {
  89             throw new RuntimeException("Only popup must be selected");
  90         }
  91         robot.glide(x, y, 0, 0);
  92         robot.type(KeyEvent.VK_DOWN);
  93         robot.waitForIdle();
  94         if (getSelectedPathLength() != 2 || !firstItem.isArmed()) {
  95             throw new RuntimeException("First item must be selected");
  96         }
  97         robot.type(KeyEvent.VK_ESCAPE);
  98         robot.waitForIdle();
  99         if (getSelectedPathLength() != 0) {
 100             throw new RuntimeException("There must be no selected items");
 101         }
 102         robot.glide(0, 0, x, y);
 103         robot.mousePress(InputEvent.BUTTON3_MASK);
 104         robot.mouseRelease(InputEvent.BUTTON3_MASK);
 105         robot.waitForIdle();
 106         robot.glide(x, y, 0, 0);
 107         robot.type(KeyEvent.VK_UP);
 108         robot.waitForIdle();
 109         if (getSelectedPathLength() != 2 || !lastItem.isArmed()) {
 110             throw new RuntimeException("Last item must be selected");
 111         }
 112     }
 113 
 114     private static int getSelectedPathLength() {
 115         return MenuSelectionManager.defaultManager().getSelectedPath().length;
 116     }
 117 }