Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/awt/Focus/8013611/JDK8013611.java
48795 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@bug 801361126@summary Tests showing a modal dialog with requesting focus in frame.27@author Anton.Tarasov: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main JDK801361131*/3233import java.awt.*;34import java.awt.event.*;35import javax.swing.*;36import test.java.awt.regtesthelpers.Util;3738import java.awt.*;3940public class JDK8013611 extends JFrame {41static JTextField textField = new JTextField("text");42static JButton button1 = new JButton("button1");43static JButton button2 = new JButton("button2");44static Robot robot;4546static JDialog dialog;47static JButton button3 = new JButton("button3");4849public static void main(String[] args) {50robot = Util.createRobot();5152JDK8013611 frame = new JDK8013611();53frame.setLayout(new FlowLayout());54frame.add(textField);55frame.add(button1);56frame.add(button2);57frame.pack();5859dialog = new JDialog(frame, true);60dialog.add(button3);61dialog.pack();6263textField.addFocusListener(new FocusAdapter() {64@Override65public void focusLost(FocusEvent e) {66dialog.setVisible(true);67}68});6970button1.addFocusListener(new FocusAdapter() {71@Override72public void focusGained(FocusEvent e) {73button2.requestFocusInWindow();74}75});7677frame.setVisible(true);7879frame.test();80}8182public void test() {83if (!testFocused(textField)) {84Util.clickOnComp(textField, robot);85if (!testFocused(textField)) {86throw new RuntimeException("Error: couldn't focus " + textField);87}88}8990robot.keyPress(KeyEvent.VK_TAB);91robot.delay(50);92robot.keyRelease(KeyEvent.VK_TAB);9394if (!testFocused(button3)) {95throw new RuntimeException("Test failed: dialog didn't get focus!");96}9798System.out.println("Test passed.");99}100101boolean testFocused(Component c) {102for (int i=0; i<10; i++) {103if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == c) {104return true;105}106Util.waitForIdle(robot);107}108return false;109}110}111112113