1 /*
   2  * Copyright (c) 2008, 2013, 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        6183877 6216005 6225560
  28  * @library    ../../regtesthelpers
  29  * @build      Util
  30  * @summary    Tests that keyboard input doesn't freeze due to type-ahead problems
  31  * @author     Denis.Mikhalkin, Anton.Tarasov: area=awt.focus
  32  * @run        main TestFocusFreeze
  33  */
  34 
  35 import java.awt.Component;
  36 import java.awt.DefaultKeyboardFocusManager;
  37 import java.awt.KeyboardFocusManager;
  38 import java.awt.Robot;
  39 import java.awt.event.ActionEvent;
  40 import java.awt.event.ActionListener;
  41 import java.awt.event.KeyEvent;
  42 import java.util.concurrent.atomic.AtomicBoolean;
  43 import javax.swing.JButton;
  44 import javax.swing.JDialog;
  45 import javax.swing.JFrame;
  46 import test.java.awt.regtesthelpers.Util;
  47 
  48 public class TestFocusFreeze {
  49     private static JFrame frame;
  50     private static JDialog dialog;
  51     private static JButton dlgButton;
  52     private static JButton frameButton;
  53     private static AtomicBoolean lock = new AtomicBoolean(false);
  54     private static Robot robot = Util.createRobot();
  55 
  56     public static void main(String[] args) {
  57         boolean all_passed = true;
  58         KeyboardFocusManager testKFM = new TestKFM(robot);
  59         KeyboardFocusManager defKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();
  60 
  61         for (int i = 0; i < 10; i++) {
  62             test(testKFM, defKFM);
  63             Util.waitForIdle(robot);
  64             System.out.println("Iter " + i + ": " + (lock.get() ? "passed." : "failed!"));
  65             if (!lock.get()) {
  66                 all_passed = false;
  67             }
  68         }
  69         if (!all_passed) {
  70             throw new RuntimeException("Test failed: not all iterations passed!");
  71         }
  72         System.out.println("Test passed.");
  73     }
  74 
  75     public static void test(final KeyboardFocusManager testKFM, final KeyboardFocusManager defKFM) {
  76         frame = new JFrame("Frame");
  77         dialog = new JDialog(frame, "Dialog", true);
  78         dlgButton = new JButton("Dialog_Button");
  79         frameButton = new JButton("Frame_Button");
  80 
  81         lock.set(false);
  82 
  83         dialog.add(dlgButton);
  84         dialog.setLocation(200, 0);
  85         dialog.pack();
  86         frame.add(frameButton);
  87         frame.pack();
  88 
  89         dlgButton.addActionListener(new ActionListener() {
  90             public void actionPerformed(ActionEvent e) {
  91                 dialog.dispose();
  92                 frame.dispose();
  93                 synchronized (lock) {
  94                     lock.set(true);
  95                     lock.notifyAll();
  96                 }
  97             }
  98         });
  99 
 100         frameButton.addActionListener(new ActionListener() {
 101                 public void actionPerformed(ActionEvent e) {
 102                     // Right before the dialog will be shown, there will be called
 103                     // enqueuKeyEvents() method. We are to catch it.
 104                     KeyboardFocusManager.setCurrentKeyboardFocusManager(testKFM);
 105                     dialog.setVisible(true);
 106                     KeyboardFocusManager.setCurrentKeyboardFocusManager(defKFM);
 107                 }
 108             });
 109 
 110         Runnable showAction = new Runnable() {
 111             public void run() {
 112                 frame.setVisible(true);
 113             }
 114         };
 115         if (!Util.trackFocusGained(frameButton, showAction, 2000, false)) {
 116             System.out.println("Test error: wrong initial focus!");
 117             return;
 118         }
 119 
 120         robot.keyPress(KeyEvent.VK_SPACE);
 121         robot.delay(50);
 122         robot.keyRelease(KeyEvent.VK_SPACE);
 123 
 124         Util.waitForCondition(lock, 2000);
 125         Util.waitForIdle(robot);
 126     }
 127 }
 128 
 129 class TestKFM extends DefaultKeyboardFocusManager {
 130     Robot robot;
 131     public TestKFM(Robot robot) {
 132         this.robot = robot;
 133     }
 134     protected synchronized void enqueueKeyEvents(long after, Component untilFocused) {
 135         super.enqueueKeyEvents(after, untilFocused);
 136         robot.delay(1);
 137         robot.keyPress(KeyEvent.VK_SPACE);
 138         robot.delay(50);
 139         robot.keyRelease(KeyEvent.VK_SPACE);
 140     }
 141 }