Path: blob/master/test/jdk/java/awt/Focus/FocusTransitionTest/FocusTransitionTest.java
41152 views
/*1* Copyright (c) 2017, 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 815519726* @summary Tests whether the value of mostRecentFocusOwner for a window is correct, if27* another window is displayed during focus transition28* @key headful29* @library ../../regtesthelpers30* @build Util31* @run main FocusTransitionTest32*/3334import java.awt.Button;35import java.awt.Component;36import java.awt.Dialog;37import java.awt.FlowLayout;38import java.awt.Frame;39import java.awt.Robot;40import java.awt.TextField;41import java.awt.event.ActionEvent;42import java.awt.event.ActionListener;4344import test.java.awt.regtesthelpers.Util;4546public class FocusTransitionTest {47private static Frame frame;48private static TextField textField1;49private static Button button;5051public static void main(String[] args) throws InterruptedException {52try {53Robot robot = Util.createRobot();5455createAndShowGUI();56Util.waitForIdle(robot);5758for (int i = 0; i < 100; i++) {59Util.clickOnComp(button, robot);6061synchronized (frame) {62frame.wait();63}64Util.waitForIdle(robot);6566Component focusOwner = frame.getMostRecentFocusOwner();67if (focusOwner != textField1) {68throw new RuntimeException("Test FAILED: incorrect focus owner!");69}70}71} finally {72if (frame != null) {73frame.dispose();74}75}76}7778private static void createAndShowGUI() {79frame = new Frame("Test Frame");8081textField1 = new TextField(5);8283button = new Button("Go");84button.addActionListener(new ActionListener() {85@Override86public void actionPerformed(ActionEvent e) {87textField1.requestFocusInWindow();88startUpdate();89}90});9192frame.setLayout(new FlowLayout());93frame.setSize(400, 200);94frame.add(textField1);95frame.add(new TextField(5));96frame.add(new TextField(5));97frame.add(button);98frame.setVisible(true);99}100101private static void startUpdate() {102UpdateThread updateThread = new UpdateThread(frame, 10);103updateThread.start();104}105}106107class UpdateThread extends Thread {108private final Frame frame;109private final int delay;110111UpdateThread(Frame frame, int delay) {112this.frame = frame;113this.delay = delay;114}115116@Override117public void run() {118Dialog dialog = new Dialog(frame);119dialog.setSize(300, 100);120dialog.setVisible(true);121122try {123sleep(delay);124} catch (InterruptedException ie) {125ie.printStackTrace();126}127128dialog.setVisible(false);129dialog.dispose();130131synchronized (frame) {132frame.notify();133}134}135}136137138139