Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/RestoreFocusOnDisabledComponentTest/RestoreFocusOnDisabledComponentTest.java
47490 views
/*1* Copyright (c) 2007, 2008, 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@test %W% %E%25@bug 659808926@summary Tests restoring focus on a single disabled coponent27@author Anton Tarasov: area=awt-focus28@library ../../regtesthelpers29@build Util30@run main RestoreFocusOnDisabledComponentTest31*/3233import java.awt.*;34import java.awt.event.*;35import java.applet.Applet;36import test.java.awt.regtesthelpers.Util;3738/*39* The bug is not reproducible on Windows.40*/41public class RestoreFocusOnDisabledComponentTest extends Applet {42Frame frame = new Frame("Frame") {public String toString() {return "FRAME";}};43Button b0 = new Button("button0") {public String toString() {return "B-0";}};44Button b1 = new Button("button1") {public String toString() {return "B-1";}};45volatile int nFocused;46Robot robot;4748public static void main(String[] args) {49RestoreFocusOnDisabledComponentTest app = new RestoreFocusOnDisabledComponentTest();50app.init();51app.start();52}5354public void init() {55robot = Util.createRobot();56}5758public void start() {59frame.add(b0);60frame.add(b1);61frame.setLayout(new FlowLayout());62frame.pack();6364frame.setVisible(true);6566Util.waitForIdle(robot);67KeyboardFocusManager.setCurrentKeyboardFocusManager(new DefaultKeyboardFocusManager() {68public boolean dispatchEvent(AWTEvent e) {69if (e.getID() == FocusEvent.FOCUS_GAINED) {70// Trying to emulate timings. b1 should be disabled just by the time it gets71// FOCUS_GAINED event. The latter is a result of disabling b0 that initiates72// focus auto transfer.73if (e.getSource() == b1) {74b1.setEnabled(false);7576} else if (e.getSource() == b0) {77if (++nFocused > 10) {78nFocused = -1;79throw new TestFailedException("Focus went into busy loop!");80}81}82}83return super.dispatchEvent(e);84}85});86// Initiating focus auto transfer.87// Focus will be requested to b1. When FOCUS_GAINED is being dispatched to b1, it will88// be disabled. This will trigger focus restoring. Focus will be requested to b0 (the89// last opposite component). When FOCUS_GAINED is being dispatched to b0, it will90// also be disabled. However, the last opposite component (and the most recent focus owner)91// will still be b0. When DKFM initiates focus restoring it should detect restoring92// on the same component and break.93b0.setEnabled(false);9495Util.waitForIdle(robot);96if (nFocused != -1) {97System.out.println("Test passed.");98}99}100}101102class TestFailedException extends RuntimeException {103TestFailedException(String msg) {104super("Test failed: " + msg);105}106}107108109