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