Path: blob/master/test/jdk/javax/swing/JPopupMenu/6544309/bug6544309.java
41155 views
/*1* Copyright (c) 2011, 2016, 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@key headful25@bug 654430926@summary Checks that 'Select Input Method' popup menu allows to select27items with keyboard.28@run main bug654430929*/30import java.awt.Robot;31import java.awt.event.ActionEvent;32import java.awt.event.ActionListener;33import java.awt.event.KeyEvent;3435import javax.swing.JDialog;36import javax.swing.JMenuItem;37import javax.swing.JPopupMenu;38import javax.swing.SwingUtilities;3940public class bug6544309 {41private JDialog dialog;42private boolean passed;43private static Robot robot;4445public static void main(String[] args) throws Exception {46robot = new Robot();47robot.setAutoDelay(100);48// move mouse outside menu to prevent auto selection49robot.mouseMove(100,100);50robot.waitForIdle();5152final bug6544309 test = new bug6544309();53try {54SwingUtilities.invokeAndWait(new Runnable() {55public void run() {56test.setupUI();57}58});59robot.waitForIdle();60robot.delay(1000);61test.test();62System.out.println("Test passed");63} finally {64if (test.dialog != null) {65test.dialog.dispose();66}67}68}6970private void setupUI() {71dialog = new JDialog();72dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);73dialog.setSize(200, 100);74dialog.setLocationRelativeTo(null);75dialog.setVisible(true);7677JPopupMenu popup = new JPopupMenu();78popup.add(new JMenuItem("one"));79JMenuItem two = new JMenuItem("two");80two.addActionListener(new ActionListener() {81public void actionPerformed(ActionEvent e) {82passed = true;83}84});85popup.add(two);86popup.add(new JMenuItem("three"));87popup.show(dialog, 50, 50);88}8990private void test() throws Exception {91testImpl();92checkResult();93}949596private void testImpl() throws Exception {97robot.waitForIdle();98System.out.println("Pressing DOWN ARROW");99robot.keyPress(KeyEvent.VK_DOWN);100robot.keyRelease(KeyEvent.VK_DOWN);101robot.waitForIdle();102System.out.println("Pressing DOWN ARROW");103robot.keyPress(KeyEvent.VK_DOWN);104robot.keyRelease(KeyEvent.VK_DOWN);105robot.waitForIdle();106System.out.println("Pressing SPACE");107robot.keyPress(KeyEvent.VK_SPACE);108robot.keyRelease(KeyEvent.VK_SPACE);109}110111private void checkResult() {112robot.waitForIdle();113if (!passed) {114throw new RuntimeException("If a JDialog is invoker for JPopupMenu, " +115"the menu cannot be handled by keyboard.");116}117}118}119120121