|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 2020, Oracle and/or its affiliates. |
| 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 | +/* @test |
| 25 | + @key headful |
| 26 | + @bug 4760494 8159597 |
| 27 | + @summary JPopupMenu doessn't accept keyboard input (4212563 not fixed) |
| 28 | +*/ |
| 29 | + |
| 30 | +import java.awt.Robot; |
| 31 | +import java.awt.event.ActionEvent; |
| 32 | +import java.awt.event.ActionListener; |
| 33 | +import java.awt.event.InputEvent; |
| 34 | +import java.awt.event.KeyEvent; |
| 35 | +import java.awt.event.MouseAdapter; |
| 36 | +import java.awt.event.MouseEvent; |
| 37 | +import java.awt.event.WindowAdapter; |
| 38 | +import java.awt.event.WindowEvent; |
| 39 | +import javax.swing.JFrame; |
| 40 | +import javax.swing.JMenuItem; |
| 41 | +import javax.swing.JPopupMenu; |
| 42 | +import javax.swing.SwingUtilities; |
| 43 | + |
| 44 | +public class bug4760494 { |
| 45 | + |
| 46 | + static JFrame frame = null; |
| 47 | + |
| 48 | + public static PassedListener pass; |
| 49 | + public static TestStateListener tester; |
| 50 | + public static Robot robot; |
| 51 | + private static volatile boolean pressed = false; |
| 52 | + private static volatile boolean passed = false; |
| 53 | + private static volatile JPopupMenu popup = null; |
| 54 | + |
| 55 | + public static void main(String args[]) throws Throwable { |
| 56 | + pass = new PassedListener(); |
| 57 | + tester = new TestStateListener(); |
| 58 | + robot = new Robot(); |
| 59 | + SwingUtilities.invokeAndWait(() -> createUI()); |
| 60 | + while (!pressed) { |
| 61 | + try { |
| 62 | + Thread.sleep(1000); |
| 63 | + } catch (InterruptedException e) { |
| 64 | + } |
| 65 | + } |
| 66 | + int count = 0; |
| 67 | + while (!passed && count < 10) { |
| 68 | + try { |
| 69 | + count++; |
| 70 | + Thread.sleep(1000); |
| 71 | + } catch (InterruptedException e) { |
| 72 | + } |
| 73 | + } |
| 74 | + SwingUtilities.invokeAndWait(() -> frame.dispose()); |
| 75 | + if (!passed) { |
| 76 | + throw new RuntimeException("Menu item not selected"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + static void createUI() { |
| 81 | + frame = new JFrame("Bug 4760494"); |
| 82 | + frame.addWindowListener(tester); |
| 83 | + |
| 84 | + popup = new JPopupMenu(); |
| 85 | + JMenuItem popupItem = popup.add(new JMenuItem("Test item")); |
| 86 | + popupItem.setMnemonic('T'); |
| 87 | + popupItem.addActionListener(new PassedListener()); |
| 88 | + |
| 89 | + frame.addMouseListener(new MouseAdapter() { |
| 90 | + public void mouseReleased( MouseEvent e ){ |
| 91 | + popup.show(frame,e.getX(),e.getY()); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + frame.setSize(200, 200); |
| 96 | + frame.setLocation(200, 200); |
| 97 | + frame.setVisible(true); |
| 98 | + frame.toFront(); |
| 99 | + } |
| 100 | + |
| 101 | + public static class PassedListener implements ActionListener { |
| 102 | + public void actionPerformed(ActionEvent ev) { |
| 103 | + passed = true; |
| 104 | + System.out.println("passed!"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static class TestStateListener extends WindowAdapter { |
| 109 | + public void windowOpened(WindowEvent ev) { |
| 110 | + try { |
| 111 | + new Thread(new RobotThread()).start(); |
| 112 | + } catch (Exception ex) { |
| 113 | + throw new RuntimeException("Thread Exception"); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public static class RobotThread implements Runnable { |
| 119 | + public void run() { |
| 120 | + robot.setAutoDelay(500); |
| 121 | + robot.waitForIdle(); |
| 122 | + // Move over the window |
| 123 | + robot.mouseMove(250, 250); |
| 124 | + // display the popup |
| 125 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 126 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 127 | + robot.waitForIdle(); |
| 128 | + // These delays are so a human has a chance to see it |
| 129 | + try { |
| 130 | + Thread.sleep(2000); |
| 131 | + } catch (InterruptedException e) { |
| 132 | + } |
| 133 | + while (!popup.isVisible()) { |
| 134 | + try { |
| 135 | + Thread.sleep(2000); |
| 136 | + } catch (InterruptedException e) { |
| 137 | + } |
| 138 | + } |
| 139 | + // select the item using the keyboard mnemonic |
| 140 | + robot.keyPress(KeyEvent.VK_T); |
| 141 | + robot.keyRelease(KeyEvent.VK_T); |
| 142 | + robot.waitForIdle(); |
| 143 | + pressed = true; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments