Path: blob/master/test/jdk/java/awt/Focus/8013611/JDK8013611.java
41153 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 801361127@summary Tests showing a modal dialog with requesting focus in frame.28@author Anton.Tarasov: area=awt.focus29@library ../../regtesthelpers30@build Util31@run main JDK801361132*/3334import java.awt.*;35import java.awt.event.*;36import javax.swing.*;37import test.java.awt.regtesthelpers.Util;3839import java.awt.*;4041public class JDK8013611 extends JFrame {42static JTextField textField = new JTextField("text");43static JButton button1 = new JButton("button1");44static JButton button2 = new JButton("button2");45static Robot robot;4647static JDialog dialog;48static JButton button3 = new JButton("button3");4950public static void main(String[] args) {51robot = Util.createRobot();5253JDK8013611 frame = new JDK8013611();54frame.setLayout(new FlowLayout());55frame.add(textField);56frame.add(button1);57frame.add(button2);58frame.pack();5960dialog = new JDialog(frame, true);61dialog.add(button3);62dialog.pack();6364textField.addFocusListener(new FocusAdapter() {65@Override66public void focusLost(FocusEvent e) {67dialog.setVisible(true);68}69});7071button1.addFocusListener(new FocusAdapter() {72@Override73public void focusGained(FocusEvent e) {74button2.requestFocusInWindow();75}76});7778frame.setVisible(true);7980frame.test();81}8283public void test() {84if (!testFocused(textField)) {85Util.clickOnComp(textField, robot);86if (!testFocused(textField)) {87throw new RuntimeException("Error: couldn't focus " + textField);88}89}9091robot.keyPress(KeyEvent.VK_TAB);92robot.delay(50);93robot.keyRelease(KeyEvent.VK_TAB);9495if (!testFocused(button3)) {96throw new RuntimeException("Test failed: dialog didn't get focus!");97}9899System.out.println("Test passed.");100}101102boolean testFocused(Component c) {103for (int i=0; i<10; i++) {104if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == c) {105return true;106}107Util.waitForIdle(robot);108}109return false;110}111}112113114