1 /*
   2  * Copyright (c) 2008, 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 
  24 /*
  25   @test      %W% %E%
  26   @key headful
  27   @bug       6637607
  28   @summary   Showing a modal dlg on TAB KEY_PRESS shouldn't consume inappropriate KEY_TYPED.
  29   @author    Anton Tarasov: area=awt-focus
  30   @library   ../../regtesthelpers
  31   @build     Util
  32   @run       main ConsumeNextKeyTypedOnModalShowTest
  33 */
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import java.applet.Applet;
  38 import java.util.concurrent.atomic.AtomicBoolean;
  39 import java.lang.reflect.InvocationTargetException;
  40 import test.java.awt.regtesthelpers.Util;
  41 
  42 public class ConsumeNextKeyTypedOnModalShowTest extends Applet {
  43     Robot robot;
  44     Frame frame = new Frame("Frame");
  45     Dialog dialog = new Dialog(frame, "Dialog", true);
  46     TextField tf0 = new TextField();
  47     TextField tf1 = new TextField();
  48     Button button = new Button("Button");
  49 
  50     public static void main(String[] args) {
  51         ConsumeNextKeyTypedOnModalShowTest app = new ConsumeNextKeyTypedOnModalShowTest();
  52         app.init();
  53         app.start();
  54     }
  55 
  56     public void init() {
  57         robot = Util.createRobot();
  58 
  59         tf0.setPreferredSize(new Dimension(50, 30));
  60         tf1.setPreferredSize(new Dimension(50, 30));
  61         frame.setLayout(new FlowLayout());
  62         frame.add(tf0);
  63         frame.add(tf1);
  64         frame.pack();
  65 
  66         dialog.add(button);
  67         dialog.pack();
  68 
  69         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  70             public void eventDispatched(AWTEvent e) {
  71                 if (e.getID() == KeyEvent.KEY_PRESSED && e.getSource() == tf0) {
  72                     dialog.setVisible(true);
  73                 }
  74             }
  75         }, KeyEvent.KEY_EVENT_MASK);
  76     }
  77 
  78     public void start() {
  79         frame.setVisible(true);
  80         Util.waitTillShown(frame);
  81 
  82         // Show the dialog.
  83         robot.keyPress(KeyEvent.VK_TAB);
  84         robot.delay(50);
  85         robot.keyRelease(KeyEvent.VK_TAB);
  86 
  87         Util.waitForIdle(robot);
  88 
  89         // Dispose the dialog.
  90         Runnable action = new Runnable() {
  91             public void run() {
  92                 dialog.dispose();
  93             }
  94         };
  95         if (!Util.trackFocusGained(tf1, action, 2000, false)) {
  96             throw new RuntimeException("Test failed: TAB was processed incorrectly!");
  97         }
  98 
  99         // Check for type-ability.
 100         robot.keyPress(KeyEvent.VK_A);
 101         robot.delay(50);
 102         robot.keyRelease(KeyEvent.VK_A);
 103 
 104         Util.waitForIdle(robot);
 105 
 106         if (tf1.getText().equals("")) {
 107             throw new RuntimeException("Test failed: couldn't type a char!");
 108         }
 109         System.out.println("Test passed.");
 110     }
 111 }