jdk/test/java/awt/Focus/RestoreFocusOnDisabledComponentTest/RestoreFocusOnDisabledComponentTest.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 5506 202f599c92aa
permissions -rw-r--r--
8159690: [TESTBUG] Mark headful tests with @key headful. Reviewed-by: simonis, alexsch
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
/*
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 5506
diff changeset
     2
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. 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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    21
 * questions.
134
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%
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 5506
diff changeset
    26
  @key headful
134
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    27
  @bug       6598089
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    28
  @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
    29
  @author    Anton Tarasov: area=awt-focus
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    30
  @library   ../../regtesthelpers
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    31
  @build     Util
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    32
  @run       main RestoreFocusOnDisabledComponentTest
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
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    35
import java.awt.*;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    36
import java.awt.event.*;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    37
import java.applet.Applet;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    38
import test.java.awt.regtesthelpers.Util;
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
/*
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    41
 * The bug is not reproducible on Windows.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    42
 */
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    43
public class RestoreFocusOnDisabledComponentTest extends Applet {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    44
    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
    45
    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
    46
    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
    47
    volatile int nFocused;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    48
    Robot robot;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    49
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    50
    public static void main(String[] args) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    51
        RestoreFocusOnDisabledComponentTest app = new RestoreFocusOnDisabledComponentTest();
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    52
        app.init();
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    53
        app.start();
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
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    56
    public void init() {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    57
        robot = Util.createRobot();
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
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    60
    public void start() {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    61
        frame.add(b0);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    62
        frame.add(b1);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    63
        frame.setLayout(new FlowLayout());
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    64
        frame.pack();
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    65
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    66
        frame.setVisible(true);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    67
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    68
        Util.waitForIdle(robot);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    69
        KeyboardFocusManager.setCurrentKeyboardFocusManager(new DefaultKeyboardFocusManager() {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    70
            public boolean dispatchEvent(AWTEvent e) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    71
                if (e.getID() == FocusEvent.FOCUS_GAINED) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    72
                    // 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
    73
                    // 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
    74
                    // focus auto transfer.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    75
                    if (e.getSource() == b1) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    76
                        b1.setEnabled(false);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    77
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    78
                    } else if (e.getSource() == b0) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    79
                        if (++nFocused > 10) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    80
                            nFocused = -1;
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    81
                            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
    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
                }
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    85
                return super.dispatchEvent(e);
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
        });
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    88
        // Initiating focus auto transfer.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    89
        // 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
    90
        // 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
    91
        // 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
    92
        // 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
    93
        // 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
    94
        // on the same component and break.
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    95
        b0.setEnabled(false);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    96
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    97
        Util.waitForIdle(robot);
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    98
        if (nFocused != -1) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
    99
            System.out.println("Test passed.");
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
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   104
class TestFailedException extends RuntimeException {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   105
    TestFailedException(String msg) {
6c750e108213 6598089: JDK 7: AWT often goes into busy loop when showing dialog
ant
parents:
diff changeset
   106
        super("Test failed: " + msg);
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
}