Path: blob/master/test/jdk/java/awt/Focus/RestoreFocusOnDisabledComponentTest/RestoreFocusOnDisabledComponentTest.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 659808927@summary Tests restoring focus on a single disabled coponent28@library ../../regtesthelpers29@build Util30@run main RestoreFocusOnDisabledComponentTest31*/3233import java.awt.*;34import java.awt.event.*;35import test.java.awt.regtesthelpers.Util;3637/*38* The bug is not reproducible on Windows.39*/40public class RestoreFocusOnDisabledComponentTest {41Frame frame = new Frame("Frame") {public String toString() {return "FRAME";}};42Button b0 = new Button("button0") {public String toString() {return "B-0";}};43Button b1 = new Button("button1") {public String toString() {return "B-1";}};44volatile int nFocused;45Robot robot;4647public static void main(String[] args) {48RestoreFocusOnDisabledComponentTest app = new RestoreFocusOnDisabledComponentTest();49app.init();50app.start();51}5253public void init() {54robot = Util.createRobot();55}5657public void start() {58frame.add(b0);59frame.add(b1);60frame.setLayout(new FlowLayout());61frame.pack();62frame.setLocationRelativeTo(null);63frame.setVisible(true);6465Util.waitForIdle(robot);66KeyboardFocusManager.setCurrentKeyboardFocusManager(new DefaultKeyboardFocusManager() {67public boolean dispatchEvent(AWTEvent e) {68if (e.getID() == FocusEvent.FOCUS_GAINED) {69// Trying to emulate timings. b1 should be disabled just by the time it gets70// FOCUS_GAINED event. The latter is a result of disabling b0 that initiates71// focus auto transfer.72if (e.getSource() == b1) {73b1.setEnabled(false);7475} else if (e.getSource() == b0) {76if (++nFocused > 10) {77nFocused = -1;78throw new TestFailedException("Focus went into busy loop!");79}80}81}82return super.dispatchEvent(e);83}84});85// Initiating focus auto transfer.86// Focus will be requested to b1. When FOCUS_GAINED is being dispatched to b1, it will87// be disabled. This will trigger focus restoring. Focus will be requested to b0 (the88// last opposite component). When FOCUS_GAINED is being dispatched to b0, it will89// also be disabled. However, the last opposite component (and the most recent focus owner)90// will still be b0. When DKFM initiates focus restoring it should detect restoring91// on the same component and break.92b0.setEnabled(false);9394Util.waitForIdle(robot);95if (nFocused != -1) {96System.out.println("Test passed.");97}98}99}100101class TestFailedException extends RuntimeException {102TestFailedException(String msg) {103super("Test failed: " + msg);104}105}106107108