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
  26   @bug      4782886
  27   @summary  FocusManager consumes wrong KEY_TYPED events
  28   @author   Oleg.Sukhodolsky: area=awt.focus
  29   @library  ../../regtesthelpers
  30   @build    Util
  31   @run      main WrongKeyTypedConsumedTest
  32 */
  33 
  34 import java.applet.Applet;
  35 import java.awt.AWTException;
  36 import java.awt.AWTKeyStroke;
  37 import java.awt.BorderLayout;
  38 import java.awt.Dialog;
  39 import java.awt.Dimension;
  40 import java.awt.FlowLayout;
  41 import java.awt.Frame;
  42 import java.awt.KeyboardFocusManager;
  43 import java.awt.Point;
  44 import java.awt.Robot;
  45 import java.awt.TextArea;
  46 
  47 import java.awt.event.KeyEvent;
  48 
  49 import java.util.HashSet;
  50 import java.util.Set;
  51 
  52 import javax.swing.JCheckBox;
  53 import javax.swing.JFrame;
  54 import javax.swing.JTextArea;
  55 
  56 import test.java.awt.regtesthelpers.Util;
  57 
  58 public class WrongKeyTypedConsumedTest extends Applet
  59 {
  60     Robot robot = Util.createRobot();
  61 
  62     public static void main(String[] args) {
  63         WrongKeyTypedConsumedTest test = new WrongKeyTypedConsumedTest();
  64         test.start();
  65     }
  66 
  67     public void start ()
  68     {
  69         setSize (200,200);
  70         setVisible(true);
  71         validate();
  72 
  73         JFrame frame = new JFrame("The Frame");
  74         Set ftk = new HashSet();
  75         ftk.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_DOWN, 0));
  76         frame.getContentPane().
  77             setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  78                                   ftk);
  79 
  80         JCheckBox checkbox = new JCheckBox("test");
  81         frame.getContentPane().add(checkbox, BorderLayout.NORTH);
  82 
  83         JTextArea textarea = new JTextArea(40, 10);
  84         frame.getContentPane().add(textarea);
  85 
  86         frame.pack();
  87         frame.setVisible(true);
  88         Util.waitForIdle(robot);
  89 
  90         if (!frame.isActive()) {
  91             throw new RuntimeException("Test Fialed: frame isn't active");
  92         }
  93 
  94         // verify if checkbox has focus
  95         if (!checkbox.isFocusOwner()) {
  96             checkbox.requestFocusInWindow();
  97             Util.waitForIdle(robot);
  98             if (!checkbox.isFocusOwner()) {
  99                 throw new RuntimeException("Test Failed: checkbox doesn't have focus");
 100             }
 101         }
 102 
 103         // press VK_DOWN
 104         robot.keyPress(KeyEvent.VK_DOWN);
 105         robot.delay(50);
 106         robot.keyRelease(KeyEvent.VK_DOWN);
 107         robot.delay(50);
 108 
 109         Util.waitForIdle(robot);
 110 
 111         // verify if text area has focus
 112         if (!textarea.isFocusOwner()) {
 113             throw new RuntimeException("Test Failed: focus wasn't transfered to text area");
 114         }
 115         // press '1'
 116         robot.keyPress(KeyEvent.VK_1);
 117         robot.delay(50);
 118         robot.keyRelease(KeyEvent.VK_1);
 119         robot.delay(50);
 120 
 121         Util.waitForIdle(robot);
 122 
 123         // verify if KEY_TYPED arrived
 124         if (!"1".equals(textarea.getText())) {
 125             throw new RuntimeException("Test Failed: text area text is \"" + textarea.getText() + "\", not \"1\"");
 126         }
 127         System.out.println("Test Passed");
 128     }
 129 }