1 /*
   2  * Copyright (c) 2004, 2018, 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        5082319
  28   @summary    Tests that focus request for already focused component doesn't block key events.
  29   @run main FocusSubRequestTest
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 
  35 public class FocusSubRequestTest {
  36     Frame frame = new Frame("Test Frame");
  37     Button button = new Button("button");
  38     boolean passed = false;
  39     Robot robot;
  40 
  41     public static void main(final String[] args) {
  42         FocusSubRequestTest app = new FocusSubRequestTest();
  43         app.init();
  44         app.start();
  45     }
  46 
  47     public void init() {
  48         frame.add(button);
  49         button.addFocusListener(new FocusAdapter() {
  50                 public void focusGained(FocusEvent e) {
  51                     System.out.println("FocusSubRequestTest: focusGained for: " + e.getSource());
  52                     ((Component)e.getSource()).requestFocus();
  53                 }
  54             });
  55 
  56         button.addKeyListener(new KeyAdapter() {
  57                 public void keyPressed(KeyEvent e) {
  58                     System.out.println("FocusSubRequestTest: keyPressed for: " + e.getSource());
  59                     passed = true;
  60                 }
  61             });
  62 
  63         try {
  64             robot = new Robot();
  65         } catch(Exception e) {
  66             throw new RuntimeException("Error: unable to create robot", e);
  67         }
  68     }
  69 
  70     public void start() {
  71         frame.pack();
  72         frame.setLocationRelativeTo(null);
  73         frame.setVisible(true);
  74 
  75         waitTillShown(button);
  76         frame.toFront();
  77 
  78         robot.delay(100);
  79         robot.keyPress(KeyEvent.VK_K);
  80         robot.delay(100);
  81         robot.keyRelease(KeyEvent.VK_K);
  82 
  83         robot.waitForIdle();
  84 
  85         if(passed) {
  86             System.out.println("Test passed.");
  87         } else {
  88             throw new RuntimeException("Test failed.");
  89         }
  90     }
  91 
  92     private void waitTillShown(Component component) {
  93         while (true) {
  94             try {
  95                 Thread.sleep(100);
  96                 component.getLocationOnScreen();
  97                 break;
  98             } catch(InterruptedException ie) {
  99                 throw new RuntimeException(ie);
 100             } catch(IllegalComponentStateException icse) {}
 101         }
 102     }
 103 }