Path: blob/master/test/jdk/javax/swing/JPopupMenu/6217905/bug6217905.java
41155 views
/*1* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 621790527* @summary JPopupMenu keyboard navigation stops working28* @author Alexander Potochkin29* @requires (os.family == "windows")30* @library /lib/client31* @build ExtendedRobot32* @run main bug621790533*/3435import javax.swing.*;36import java.awt.*;37import java.awt.event.InputEvent;38import java.awt.event.KeyEvent;3940public class bug6217905 {41private static JPanel popupPanel;42private static JMenuItem firstItem;43private static JMenuItem lastItem;44private static JFrame frame;4546private static void createGui() {47frame = new JFrame();48frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);4950JPopupMenu popup = new JPopupMenu("Menu");51firstItem = new JMenuItem("MenuItem");52popup.add(firstItem);53popup.add(new JMenuItem("MenuItem"));54lastItem = new JMenuItem("MenuItem");55popup.add(lastItem);5657popupPanel = new JPanel();58popupPanel.setComponentPopupMenu(popup);59frame.add(popupPanel);60frame.setSize(100, 100);61frame.setVisible(true);62}6364public static void main(String[] args) throws Exception {65try {66UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");67} catch (Exception e) {68// This test is for WinLaf only69System.out.println("This test is for Windows LaF only.");70return;71}7273try {74ExtendedRobot robot = new ExtendedRobot();75robot.setAutoDelay(10);76SwingUtilities.invokeLater(new Runnable() {77public void run() {78bug6217905.createGui();79}80});81robot.waitForIdle();82Point loc = popupPanel.getLocationOnScreen();83int x = loc.x + popupPanel.getWidth()/2;84int y = loc.y + popupPanel.getHeight()/2;85robot.glide(0, 0, x, y);86robot.mousePress(InputEvent.BUTTON3_MASK);87robot.mouseRelease(InputEvent.BUTTON3_MASK);88robot.waitForIdle();89if (getSelectedPathLength() != 1) {90throw new RuntimeException("Only popup must be selected");91}92robot.glide(x, y, 0, 0);93robot.type(KeyEvent.VK_DOWN);94robot.waitForIdle();95if (getSelectedPathLength() != 2 || !firstItem.isArmed()) {96throw new RuntimeException("First item must be selected");97}98robot.type(KeyEvent.VK_ESCAPE);99robot.waitForIdle();100if (getSelectedPathLength() != 0) {101throw new RuntimeException("There must be no selected items");102}103robot.glide(0, 0, x, y);104robot.mousePress(InputEvent.BUTTON3_MASK);105robot.mouseRelease(InputEvent.BUTTON3_MASK);106robot.waitForIdle();107robot.glide(x, y, 0, 0);108robot.type(KeyEvent.VK_UP);109robot.waitForIdle();110if (getSelectedPathLength() != 2 || !lastItem.isArmed()) {111throw new RuntimeException("Last item must be selected");112}113} finally {114if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());115}116}117118private static int getSelectedPathLength() {119return MenuSelectionManager.defaultManager().getSelectedPath().length;120}121}122123124