Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ResetMostRecentFocusOwnerTest/ResetMostRecentFocusOwnerTest.java
47490 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 801377326@summary Tests that disabled component is not retained as most recent focus owner.27@author Anton.Tarasov: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main ResetMostRecentFocusOwnerTest31*/3233import java.applet.Applet;34import java.awt.AWTEvent;35import java.awt.FlowLayout;36import java.awt.Robot;37import java.awt.Toolkit;38import java.awt.event.AWTEventListener;39import java.awt.event.FocusEvent;40import java.awt.event.WindowEvent;41import javax.swing.JButton;42import javax.swing.JFrame;43import test.java.awt.regtesthelpers.Util;4445public class ResetMostRecentFocusOwnerTest extends Applet {4647public static void main(String[] args) {48ResetMostRecentFocusOwnerTest app = new ResetMostRecentFocusOwnerTest();49app.init();50app.start();51}5253@Override54public void start() {5556Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {57public void eventDispatched(AWTEvent e) {58System.err.println(e);59}60}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);6162boolean gained = false;63final Robot robot = Util.createRobot();6465JFrame frame1 = new JFrame("Main Frame");66final JButton b1 = new JButton("button1");67frame1.add(b1);68frame1.pack();69frame1.setLocation(0, 300);7071Util.showWindowWait(frame1);7273final JFrame frame2 = new JFrame("Test Frame");74final JButton b2 = new JButton("button2");75frame2.add(b2);76frame2.pack();77frame2.setLocation(300, 300);7879b2.setEnabled(false);80b2.requestFocus();8182Util.showWindowWait(frame2);8384robot.delay(500);8586//87// It's expeced that the focus is restored to <button1>.88// If not, click <button1> to set focus on it.89//90if (!b1.hasFocus()) {91gained = Util.trackFocusGained(b1, new Runnable() {92public void run() {93Util.clickOnComp(b1, robot);94}95}, 5000, false);9697if (!gained) {98throw new RuntimeException("Unexpected state: focus is not on <button1>");99}100}101102robot.delay(500);103104//105// Click <button2>, check that focus is set on the parent frame.106//107gained = false;108gained = Util.trackFocusGained(frame2, new Runnable() {109public void run() {110Util.clickOnComp(b2, robot);111}112}, 5000, false);113114if (!gained) {115throw new RuntimeException("Test failed: focus wasn't set to <frame2>");116}117118System.out.println("Test passed.");119}120}121122123