Path: blob/master/test/jdk/java/awt/Focus/ResetMostRecentFocusOwnerTest/ResetMostRecentFocusOwnerTest.java
41152 views
/*1* Copyright (c) 2013, 2016, 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 801377327@summary Tests that disabled component is not retained as most recent focus owner.28@library ../../regtesthelpers29@build Util30@run main ResetMostRecentFocusOwnerTest31*/3233import java.awt.AWTEvent;34import java.awt.Robot;35import java.awt.Toolkit;36import java.awt.event.AWTEventListener;37import java.awt.event.FocusEvent;38import java.awt.event.WindowEvent;39import javax.swing.JButton;40import javax.swing.JFrame;41import test.java.awt.regtesthelpers.Util;4243public class ResetMostRecentFocusOwnerTest {4445public static void main(String[] args) {46ResetMostRecentFocusOwnerTest app = new ResetMostRecentFocusOwnerTest();47app.start();48}4950public void start() {5152Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {53public void eventDispatched(AWTEvent e) {54System.err.println(e);55}56}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);5758boolean gained = false;59final Robot robot = Util.createRobot();6061JFrame frame1 = new JFrame("Main Frame");62final JButton b1 = new JButton("button1");63frame1.add(b1);64frame1.pack();65frame1.setLocation(100, 300);6667Util.showWindowWait(frame1);6869final JFrame frame2 = new JFrame("Test Frame");70final JButton b2 = new JButton("button2");71frame2.add(b2);72frame2.pack();73frame2.setLocation(300, 300);7475b2.setEnabled(false);76b2.requestFocus();7778Util.showWindowWait(frame2);7980robot.delay(500);8182//83// It's expeced that the focus is restored to <button1>.84// If not, click <button1> to set focus on it.85//86if (!b1.hasFocus()) {87gained = Util.trackFocusGained(b1, new Runnable() {88public void run() {89Util.clickOnComp(b1, robot);90}91}, 5000, false);9293if (!gained) {94throw new RuntimeException("Unexpected state: focus is not on <button1>");95}96}9798robot.delay(500);99100//101// Click <button2>, check that focus is set on the parent frame.102//103gained = false;104gained = Util.trackFocusGained(frame2, new Runnable() {105public void run() {106Util.clickOnComp(b2, robot);107}108}, 5000, false);109110if (!gained) {111throw new RuntimeException("Test failed: focus wasn't set to <frame2>");112}113114System.out.println("Test passed.");115}116}117118119