111N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
111N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
111N/A *
111N/A * This code is free software; you can redistribute it and/or modify it
111N/A * under the terms of the GNU General Public License version 2 only, as
111N/A * published by the Free Software Foundation.
111N/A *
111N/A * This code is distributed in the hope that it will be useful, but WITHOUT
111N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
111N/A * version 2 for more details (a copy is included in the LICENSE file that
111N/A * accompanied this code).
111N/A *
111N/A * You should have received a copy of the GNU General Public License version
111N/A * 2 along with this work; if not, write to the Free Software Foundation,
111N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
111N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
111N/A */
111N/A
111N/A/*
111N/A @test %W% %E%
111N/A @bug 6598089
111N/A @summary Tests restoring focus on a single disabled coponent
111N/A @author Anton Tarasov: area=awt-focus
111N/A @library ../../regtesthelpers
111N/A @build Util
111N/A @run main RestoreFocusOnDisabledComponentTest
111N/A*/
111N/A
111N/Aimport java.awt.*;
111N/Aimport java.awt.event.*;
111N/Aimport java.applet.Applet;
111N/Aimport test.java.awt.regtesthelpers.Util;
111N/A
111N/A/*
111N/A * The bug is not reproducible on Windows.
111N/A */
111N/Apublic class RestoreFocusOnDisabledComponentTest extends Applet {
111N/A Frame frame = new Frame("Frame") {public String toString() {return "FRAME";}};
111N/A Button b0 = new Button("button0") {public String toString() {return "B-0";}};
111N/A Button b1 = new Button("button1") {public String toString() {return "B-1";}};
111N/A volatile int nFocused;
111N/A Robot robot;
111N/A
111N/A public static void main(String[] args) {
111N/A RestoreFocusOnDisabledComponentTest app = new RestoreFocusOnDisabledComponentTest();
111N/A app.init();
111N/A app.start();
111N/A }
111N/A
111N/A public void init() {
111N/A robot = Util.createRobot();
111N/A }
111N/A
111N/A public void start() {
111N/A frame.add(b0);
111N/A frame.add(b1);
111N/A frame.setLayout(new FlowLayout());
111N/A frame.pack();
111N/A
111N/A frame.setVisible(true);
111N/A
111N/A Util.waitForIdle(robot);
111N/A KeyboardFocusManager.setCurrentKeyboardFocusManager(new DefaultKeyboardFocusManager() {
111N/A public boolean dispatchEvent(AWTEvent e) {
111N/A if (e.getID() == FocusEvent.FOCUS_GAINED) {
111N/A // Trying to emulate timings. b1 should be disabled just by the time it gets
111N/A // FOCUS_GAINED event. The latter is a result of disabling b0 that initiates
111N/A // focus auto transfer.
111N/A if (e.getSource() == b1) {
111N/A b1.setEnabled(false);
111N/A
111N/A } else if (e.getSource() == b0) {
111N/A if (++nFocused > 10) {
111N/A nFocused = -1;
111N/A throw new TestFailedException("Focus went into busy loop!");
111N/A }
111N/A }
111N/A }
111N/A return super.dispatchEvent(e);
111N/A }
111N/A });
111N/A // Initiating focus auto transfer.
111N/A // Focus will be requested to b1. When FOCUS_GAINED is being dispatched to b1, it will
111N/A // be disabled. This will trigger focus restoring. Focus will be requested to b0 (the
111N/A // last opposite component). When FOCUS_GAINED is being dispatched to b0, it will
111N/A // also be disabled. However, the last opposite component (and the most recent focus owner)
111N/A // will still be b0. When DKFM initiates focus restoring it should detect restoring
111N/A // on the same component and break.
111N/A b0.setEnabled(false);
111N/A
111N/A Util.waitForIdle(robot);
111N/A if (nFocused != -1) {
111N/A System.out.println("Test passed.");
111N/A }
111N/A }
111N/A}
111N/A
111N/Aclass TestFailedException extends RuntimeException {
111N/A TestFailedException(String msg) {
111N/A super("Test failed: " + msg);
111N/A }
111N/A}