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