Path: blob/master/test/jdk/javax/swing/JPopupMenu/4760494/bug4760494.java
41153 views
/*1* Copyright (c) 2002, 2020, 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/* @test24@key headful25@bug 4760494 815959726@summary JPopupMenu doessn't accept keyboard input (4212563 not fixed)27*/2829import java.awt.Robot;30import java.awt.event.ActionEvent;31import java.awt.event.ActionListener;32import java.awt.event.InputEvent;33import java.awt.event.KeyEvent;34import java.awt.event.MouseAdapter;35import java.awt.event.MouseEvent;36import java.awt.event.WindowAdapter;37import java.awt.event.WindowEvent;38import javax.swing.JFrame;39import javax.swing.JMenuItem;40import javax.swing.JPopupMenu;41import javax.swing.SwingUtilities;4243public class bug4760494 {4445static JFrame frame = null;4647public static PassedListener pass;48public static TestStateListener tester;49public static Robot robot;50private static volatile boolean pressed = false;51private static volatile boolean passed = false;52private static volatile JPopupMenu popup = null;5354public static void main(String args[]) throws Throwable {55pass = new PassedListener();56tester = new TestStateListener();57robot = new Robot();58SwingUtilities.invokeAndWait(() -> createUI());59while (!pressed) {60try {61Thread.sleep(1000);62} catch (InterruptedException e) {63}64}65int count = 0;66while (!passed && count < 10) {67try {68count++;69Thread.sleep(1000);70} catch (InterruptedException e) {71}72}73SwingUtilities.invokeAndWait(() -> frame.dispose());74if (!passed) {75throw new RuntimeException("Menu item not selected");76}77}7879static void createUI() {80frame = new JFrame("Bug 4760494");81frame.addWindowListener(tester);8283popup = new JPopupMenu();84JMenuItem popupItem = popup.add(new JMenuItem("Test item"));85popupItem.setMnemonic('T');86popupItem.addActionListener(new PassedListener());8788frame.addMouseListener(new MouseAdapter() {89public void mouseReleased( MouseEvent e ){90popup.show(frame,e.getX(),e.getY());91}92});9394frame.setSize(200, 200);95frame.setLocation(200, 200);96frame.setVisible(true);97frame.toFront();98}99100public static class PassedListener implements ActionListener {101public void actionPerformed(ActionEvent ev) {102passed = true;103System.out.println("passed!");104}105}106107public static class TestStateListener extends WindowAdapter {108public void windowOpened(WindowEvent ev) {109try {110new Thread(new RobotThread()).start();111} catch (Exception ex) {112throw new RuntimeException("Thread Exception");113}114}115}116117public static class RobotThread implements Runnable {118public void run() {119robot.setAutoDelay(500);120robot.waitForIdle();121// Move over the window122robot.mouseMove(250, 250);123// display the popup124robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);125robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);126robot.waitForIdle();127// These delays are so a human has a chance to see it128try {129Thread.sleep(2000);130} catch (InterruptedException e) {131}132while (!popup.isVisible()) {133try {134Thread.sleep(2000);135} catch (InterruptedException e) {136}137}138// select the item using the keyboard mnemonic139robot.keyPress(KeyEvent.VK_T);140robot.keyRelease(KeyEvent.VK_T);141robot.waitForIdle();142pressed = true;143}144}145}146147148