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