1 /* 2 * Copyright (c) 2011, 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 @summary <rdar://problem/4082604> clearGlobalFocusOwner fails to remove 26 * focus from component 27 * @summary com.apple.junit.java.awt.Event; 28 * @library ../../../../java/awt/regtesthelpers 29 * @build RobotUtilities 30 * @build VisibilityValidator 31 * @run junit R4082604_ClearFocusTest 32 */ 33 import junit.framework.*; 34 35 import javax.swing.*; 36 import java.awt.*; 37 import java.awt.event.*; 38 import sun.awt.SunToolkit; 39 40 import test.java.awt.regtesthelpers.RobotUtilities; 41 import test.java.awt.regtesthelpers.VisibilityValidator; 42 43 public class R4082604_ClearFocusTest extends TestCase { 44 45 JFrame jframe; 46 JTextField field; 47 48 public static Test suite() { 49 return new TestSuite(R4082604_ClearFocusTest.class); 50 } 51 52 public static void main(String[] args) throws RuntimeException { 53 TestResult tr = junit.textui.TestRunner.run(suite()); 54 if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 55 throw new RuntimeException("### Unexpected JUnit errors or failures."); 56 } 57 } 58 59 public void testClearFocus() throws Exception { 60 SwingUtilities.invokeAndWait(new Runnable() { 61 @Override 62 public void run() { 63 jframe = new JFrame("Clear Focus Test"); 64 65 jframe.getContentPane().setLayout(new FlowLayout()); 66 67 field = new JTextField("CLICK ME THEN PRESS ESCAPE"); 68 field.registerKeyboardAction(new ActionListener() { 69 public void actionPerformed(ActionEvent e) { 70 KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner(); 71 } 72 }, 73 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED); 74 jframe.getContentPane().add(field); 75 76 jframe.setSize(600, 75); 77 } 78 }); 79 80 VisibilityValidator.setVisibleAndConfirm(jframe); 81 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 82 83 // put focus in the textfield 84 RobotUtilities.click(field); 85 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 86 87 // type 88 RobotUtilities.typeKey(KeyEvent.VK_H); 89 RobotUtilities.typeKey(KeyEvent.VK_I); 90 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 91 92 // test "hi" 93 String hiString = field.getText(); 94 assertTrue("Failed to find \"hi\" in JTextField: " + hiString, (hiString.indexOf("hi") != -1)); 95 96 // remove focus 97 RobotUtilities.typeKey(KeyEvent.VK_ESCAPE); 98 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 99 100 // type 101 RobotUtilities.typeKey(KeyEvent.VK_B); 102 RobotUtilities.typeKey(KeyEvent.VK_Y); 103 RobotUtilities.typeKey(KeyEvent.VK_E); 104 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 105 106 // test "bye" 107 String byeString = field.getText(); 108 assertTrue("Found \"bye\" in JTextField: " + byeString, (byeString.indexOf("bye") == -1)); 109 110 SwingUtilities.invokeAndWait(new Runnable() { 111 @Override 112 public void run() { 113 if (jframe != null) { 114 jframe.setVisible(false); 115 jframe.dispose(); 116 jframe = null; 117 } 118 } 119 }); 120 } 121 }