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