Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/6217905/bug6217905.java
32337 views
/*1* Copyright (c) 2011, 2014, 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*/22/*23@test24@bug 621790525@summary JPopupMenu keyboard navigation stops working26@author Alexander Potochkin27@library ../../../../lib/testlibrary28@build ExtendedRobot29@run main bug621790530*/3132import javax.swing.*;33import java.awt.*;34import java.awt.event.InputEvent;35import java.awt.event.KeyEvent;3637public class bug6217905 {38private static JPanel popupPanel;39private static JMenuItem firstItem;40private static JMenuItem lastItem;4142private static void createGui() {43final JFrame frame = new JFrame();44frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);4546JPopupMenu popup = new JPopupMenu("Menu");47firstItem = new JMenuItem("MenuItem");48popup.add(firstItem);49popup.add(new JMenuItem("MenuItem"));50lastItem = new JMenuItem("MenuItem");51popup.add(lastItem);5253popupPanel = new JPanel();54popupPanel.setComponentPopupMenu(popup);55frame.add(popupPanel);56frame.setSize(100, 100);57frame.setVisible(true);58}5960public static void main(String[] args) throws Exception {61try {62UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");63} catch (Exception e) {64// This test is for WinLaf only65System.out.println("This test is for Windows LaF only.");66return;67}6869ExtendedRobot robot = new ExtendedRobot();70robot.setAutoDelay(10);71SwingUtilities.invokeLater(new Runnable() {72public void run() {73bug6217905.createGui();74}75});76robot.waitForIdle();77Point loc = popupPanel.getLocationOnScreen();78int x = loc.x + popupPanel.getWidth()/2;79int y = loc.y + popupPanel.getHeight()/2;80robot.glide(0, 0, x, y);81robot.mousePress(InputEvent.BUTTON3_MASK);82robot.mouseRelease(InputEvent.BUTTON3_MASK);83robot.waitForIdle();84if (getSelectedPathLength() != 1) {85throw new RuntimeException("Only popup must be selected");86}87robot.glide(x, y, 0, 0);88robot.type(KeyEvent.VK_DOWN);89robot.waitForIdle();90if (getSelectedPathLength() != 2 || !firstItem.isArmed()) {91throw new RuntimeException("First item must be selected");92}93robot.type(KeyEvent.VK_ESCAPE);94robot.waitForIdle();95if (getSelectedPathLength() != 0) {96throw new RuntimeException("There must be no selected items");97}98robot.glide(0, 0, x, y);99robot.mousePress(InputEvent.BUTTON3_MASK);100robot.mouseRelease(InputEvent.BUTTON3_MASK);101robot.waitForIdle();102robot.glide(x, y, 0, 0);103robot.type(KeyEvent.VK_UP);104robot.waitForIdle();105if (getSelectedPathLength() != 2 || !lastItem.isArmed()) {106throw new RuntimeException("Last item must be selected");107}108}109110private static int getSelectedPathLength() {111return MenuSelectionManager.defaultManager().getSelectedPath().length;112}113}114115116