Path: blob/master/test/jdk/java/awt/Focus/RequestFocusAndHideTest/RequestFocusAndHideTest.java
41152 views
/*1* Copyright (c) 2007, 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 6562853 6562853 656285327@summary Tests that focus can not be set to removed component.28@author Oleg Sukhodolsky: area=awt.focus29@library ../../regtesthelpers30@build Util31@run main RequestFocusAndHideTest32*/3334import java.awt.Button;35import java.awt.Component;36import java.awt.EventQueue;37import java.awt.FlowLayout;38import java.awt.Frame;39import java.awt.KeyboardFocusManager;40import java.awt.Robot;4142import test.java.awt.regtesthelpers.Util;4344public class RequestFocusAndHideTest {45public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException46{47final Frame frame = new Frame("the test");48frame.setLayout(new FlowLayout());49final Button btn1 = new Button("button 1");50frame.add(btn1);51frame.add(new Button("button 2"));52frame.add(new Button("button 3"));53frame.pack();54frame.setVisible(true);5556Robot r = Util.createRobot();57Util.waitForIdle(r);58Util.clickOnComp(btn1, r);59Util.waitForIdle(r);60KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();61if (kfm.getFocusOwner() != btn1) {62throw new RuntimeException("test error: can not set focus on " + btn1 + ".");63}6465EventQueue.invokeAndWait(new Runnable() {66public void run() {67final int n_comps = frame.getComponentCount();68for (int i = 0; i < n_comps; ++i) {69frame.getComponent(i).setVisible(false);70}71}72});73Util.waitForIdle(r);74final Component focus_owner = kfm.getFocusOwner();7576if (focus_owner != null && !focus_owner.isVisible()) {77throw new RuntimeException("we have invisible focus owner");78}79System.out.println("test passed");80frame.dispose();81}82}838485