jdk/test/java/awt/Focus/RestoreFocusOnDisabledComponentTest/RestoreFocusOnDisabledComponentTest.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 134 6c750e108213
child 3111 fefdeafb7ab9
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
134
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 134
diff changeset
     2
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
134
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
     4
 *
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
     8
 *
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    13
 * accompanied this code).
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    14
 *
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    18
 *
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    21
 * have any questions.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    22
 */
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    23
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    24
/*
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    25
  @test      %W% %E%
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    26
  @bug       6598089
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    27
  @summary   Tests restoring focus on a single disabled coponent
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    28
  @author    Anton Tarasov: area=awt-focus
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    29
  @library   ../../regtesthelpers
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    30
  @build     Util
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    31
  @run       main RestoreFocusOnDisabledComponentTest
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    32
*/
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    33
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    34
import java.awt.*;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    35
import java.awt.event.*;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    36
import java.applet.Applet;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    37
import test.java.awt.regtesthelpers.Util;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    38
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    39
/*
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    40
 * The bug is not reproducible on Windows.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    41
 */
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    42
public class RestoreFocusOnDisabledComponentTest extends Applet {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    43
    Frame frame = new Frame("Frame") {public String toString() {return "FRAME";}};
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    44
    Button b0 = new Button("button0") {public String toString() {return "B-0";}};
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    45
    Button b1 = new Button("button1") {public String toString() {return "B-1";}};
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    46
    volatile int nFocused;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    47
    Robot robot;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    48
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    49
    public static void main(String[] args) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    50
        RestoreFocusOnDisabledComponentTest app = new RestoreFocusOnDisabledComponentTest();
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    51
        app.init();
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    52
        app.start();
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    53
    }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    54
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    55
    public void init() {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    56
        robot = Util.createRobot();
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    57
    }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    58
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    59
    public void start() {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    60
        frame.add(b0);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    61
        frame.add(b1);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    62
        frame.setLayout(new FlowLayout());
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    63
        frame.pack();
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    64
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    65
        frame.setVisible(true);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    66
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    67
        Util.waitForIdle(robot);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    68
        KeyboardFocusManager.setCurrentKeyboardFocusManager(new DefaultKeyboardFocusManager() {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    69
            public boolean dispatchEvent(AWTEvent e) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    70
                if (e.getID() == FocusEvent.FOCUS_GAINED) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    71
                    // Trying to emulate timings. b1 should be disabled just by the time it gets
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    72
                    // FOCUS_GAINED event. The latter is a result of disabling b0 that initiates
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    73
                    // focus auto transfer.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    74
                    if (e.getSource() == b1) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    75
                        b1.setEnabled(false);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    76
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    77
                    } else if (e.getSource() == b0) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    78
                        if (++nFocused > 10) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    79
                            nFocused = -1;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    80
                            throw new TestFailedException("Focus went into busy loop!");
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    81
                        }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    82
                    }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    83
                }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    84
                return super.dispatchEvent(e);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    85
            }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    86
        });
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    87
        // Initiating focus auto transfer.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    88
        // Focus will be requested to b1. When FOCUS_GAINED is being dispatched to b1, it will
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    89
        // be disabled. This will trigger focus restoring. Focus will be requested to b0 (the
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    90
        // last opposite component). When FOCUS_GAINED is being dispatched to b0, it will
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    91
        // also be disabled. However, the last opposite component (and the most recent focus owner)
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    92
        // will still be b0. When DKFM initiates focus restoring it should detect restoring
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    93
        // on the same component and break.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    94
        b0.setEnabled(false);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    95
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    96
        Util.waitForIdle(robot);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    97
        if (nFocused != -1) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    98
            System.out.println("Test passed.");
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    99
        }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   100
    }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   101
}
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   102
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   103
class TestFailedException extends RuntimeException {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   104
    TestFailedException(String msg) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   105
        super("Test failed: " + msg);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   106
    }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   107
}
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   108