1 /*
   2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
   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 /* @test
  24    @bug 6544309
  25    @summary Checks that 'Select Input Method' popup menu allows to select
  26             items with keyboard.
  27    @author Mikhail Lapshin
  28    @library ../../../../lib/testlibrary
  29    @build ExtendedRobot
  30    @run main bug6544309
  31 */
  32 
  33 import java.awt.event.ActionEvent;
  34 import java.awt.event.ActionListener;
  35 import java.awt.event.KeyEvent;
  36 
  37 import javax.swing.JDialog;
  38 import javax.swing.JMenuItem;
  39 import javax.swing.JPopupMenu;
  40 import javax.swing.SwingUtilities;
  41 
  42 public class bug6544309 {
  43     private JDialog dialog;
  44     private boolean passed;
  45     private static ExtendedRobot robot;
  46 
  47     public static void main(String[] args) throws Exception {
  48         robot = new ExtendedRobot();
  49         // move mouse outside menu to prevent auto selection
  50         robot.mouseMove(100,100);
  51         final bug6544309 test = new bug6544309();
  52         try {
  53             SwingUtilities.invokeAndWait(new Runnable() {
  54                 public void run() {
  55                     test.setupUI();
  56                 }
  57             });
  58             test.test();
  59             System.out.println("Test passed");
  60         } finally {
  61             if (test.dialog != null) {
  62                 test.dialog.dispose();
  63             }
  64         }
  65     }
  66 
  67     private void setupUI() {
  68         dialog = new JDialog();
  69         dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  70         dialog.setSize(200, 100);
  71         dialog.setLocationRelativeTo(null);
  72         dialog.setVisible(true);
  73 
  74         JPopupMenu popup = new JPopupMenu();
  75         popup.add(new JMenuItem("one"));
  76         JMenuItem two = new JMenuItem("two");
  77         two.addActionListener(new ActionListener() {
  78             public void actionPerformed(ActionEvent e) {
  79                 passed = true;
  80             }
  81         });
  82         popup.add(two);
  83         popup.add(new JMenuItem("three"));
  84         popup.show(dialog, 50, 50);
  85     }
  86 
  87     private void test() throws Exception {
  88         testImpl();
  89         checkResult();
  90     }
  91 
  92 
  93     private void testImpl() throws Exception {
  94         robot.waitForIdle();
  95         System.out.println("Pressing DOWN ARROW");
  96         robot.type(KeyEvent.VK_DOWN);
  97         robot.waitForIdle();
  98         System.out.println("Pressing DOWN ARROW");
  99         robot.type(KeyEvent.VK_DOWN);
 100         robot.waitForIdle();
 101         System.out.println("Pressing SPACE");
 102         robot.type(KeyEvent.VK_SPACE);
 103     }
 104 
 105     private void checkResult() {
 106         robot.waitForIdle();
 107         if (!passed) {
 108             throw new RuntimeException("If a JDialog is invoker for JPopupMenu, " +
 109                     "the menu cannot be handled by keyboard.");
 110         }
 111     }
 112 }