1 /*
   2  * Copyright (c) 2006, 2018, 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 
  24 /*
  25   @test
  26   @key headful
  27   @bug        6391688
  28   @summary    Tests that next mnemonic KeyTyped is consumed for a modal dialog.
  29   @run        main ConsumeForModalDialogTest
  30 */
  31 
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 
  36 public class ConsumeForModalDialogTest {
  37     Robot robot;
  38     JFrame frame = new JFrame("Test Frame");
  39     JDialog dialog = new JDialog((Window)null, "Test Dialog", Dialog.ModalityType.DOCUMENT_MODAL);
  40     JTextField text = new JTextField();
  41     static boolean passed = true;
  42 
  43     public static void main(String[] args) {
  44         ConsumeForModalDialogTest app = new ConsumeForModalDialogTest();
  45         app.init();
  46         app.start();
  47     }
  48 
  49     public void init() {
  50         try {
  51             robot = new Robot();
  52             robot.setAutoDelay(50);
  53         } catch (AWTException e) {
  54             throw new RuntimeException("Error: unable to create robot", e);
  55         }
  56     }
  57 
  58     public void start() {
  59 
  60         text.addKeyListener(new KeyAdapter() {
  61                 public void keyTyped(KeyEvent e) {
  62                     System.out.println(e.toString());
  63                     passed = false;
  64                 }
  65             });
  66 
  67         JMenuItem testItem = new JMenuItem();
  68         testItem.setMnemonic('s');
  69         testItem.setText("Test");
  70 
  71         testItem.addActionListener(new ActionListener() {
  72                 public void actionPerformed(ActionEvent ae) {
  73                     dialog.setVisible(true);
  74             }
  75         });
  76 
  77         JMenu menu = new JMenu();
  78         menu.setMnemonic('f');
  79         menu.setText("File");
  80         menu.add(testItem);
  81 
  82         JMenuBar menuBar = new JMenuBar();
  83         menuBar.add(menu);
  84 
  85         dialog.setSize(100, 100);
  86         dialog.add(text);
  87 
  88         frame.setJMenuBar(menuBar);
  89         frame.setSize(100, 100);
  90         frame.setLocationRelativeTo(null);
  91         frame.setVisible(true);
  92 
  93         robot.waitForIdle();
  94 
  95         if (!frame.isFocusOwner()) {
  96             Point loc = frame.getLocationOnScreen();
  97             Dimension size = frame.getSize();
  98             robot.mouseMove(loc.x + size.width/2, loc.y + size.height/2);
  99             robot.delay(10);
 100             robot.mousePress(MouseEvent.BUTTON1_MASK);
 101             robot.delay(10);
 102             robot.mouseRelease(MouseEvent.BUTTON1_MASK);
 103 
 104             robot.waitForIdle();
 105 
 106             int iter = 10;
 107             while (!frame.isFocusOwner() && iter-- > 0) {
 108                 robot.delay(200);
 109             }
 110             if (iter <= 0) {
 111                 System.out.println("Test: the frame couldn't be focused!");
 112                 return;
 113             }
 114         }
 115 
 116         robot.keyPress(KeyEvent.VK_ALT);
 117         robot.keyPress(KeyEvent.VK_F);
 118         robot.delay(10);
 119         robot.keyRelease(KeyEvent.VK_F);
 120         robot.keyRelease(KeyEvent.VK_ALT);
 121 
 122         robot.waitForIdle();
 123 
 124         robot.keyPress(KeyEvent.VK_S);
 125         robot.delay(10);
 126         robot.keyRelease(KeyEvent.VK_S);
 127 
 128         robot.delay(1000);
 129 
 130         if (passed) {
 131             System.out.println("Test passed.");
 132         } else {
 133             throw new RuntimeException("Test failed! Enexpected KeyTyped came into the JTextField.");
 134         }
 135     }
 136 }