jdk/test/java/awt/Focus/ResetMostRecentFocusOwnerTest/ResetMostRecentFocusOwnerTest.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 17893 a34977112c8b
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:
17893
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
     1
/*
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 17893
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
17893
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
     4
 *
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
     8
 *
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    13
 * accompanied this code).
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    14
 *
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    18
 *
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    21
 * questions.
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    22
 */
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    23
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    24
/*
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    25
  @test
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 17893
diff changeset
    26
  @key headful
17893
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    27
  @bug      8013773
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    28
  @summary  Tests that disabled component is not retained as most recent focus owner.
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    29
  @author   Anton.Tarasov: area=awt.focus
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    30
  @library  ../../regtesthelpers
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    31
  @build    Util
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    32
  @run      main ResetMostRecentFocusOwnerTest
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    33
*/
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    34
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    35
import java.applet.Applet;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    36
import java.awt.AWTEvent;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    37
import java.awt.FlowLayout;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    38
import java.awt.Robot;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    39
import java.awt.Toolkit;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    40
import java.awt.event.AWTEventListener;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    41
import java.awt.event.FocusEvent;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    42
import java.awt.event.WindowEvent;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    43
import javax.swing.JButton;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    44
import javax.swing.JFrame;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    45
import test.java.awt.regtesthelpers.Util;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    46
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    47
public class ResetMostRecentFocusOwnerTest extends Applet {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    48
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    49
    public static void main(String[] args) {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    50
        ResetMostRecentFocusOwnerTest app = new ResetMostRecentFocusOwnerTest();
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    51
        app.init();
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    52
        app.start();
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    53
    }
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    54
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    55
    @Override
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    56
    public void start() {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    57
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    58
        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    59
            public void eventDispatched(AWTEvent e) {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    60
                System.err.println(e);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    61
            }
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    62
        }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    63
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    64
        boolean gained = false;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    65
        final Robot robot = Util.createRobot();
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    66
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    67
        JFrame frame1 = new JFrame("Main Frame");
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    68
        final JButton b1 = new JButton("button1");
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    69
        frame1.add(b1);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    70
        frame1.pack();
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    71
        frame1.setLocation(0, 300);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    72
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    73
        Util.showWindowWait(frame1);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    74
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    75
        final JFrame frame2 = new JFrame("Test Frame");
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    76
        final JButton b2 = new JButton("button2");
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    77
        frame2.add(b2);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    78
        frame2.pack();
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    79
        frame2.setLocation(300, 300);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    80
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    81
        b2.setEnabled(false);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    82
        b2.requestFocus();
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    83
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    84
        Util.showWindowWait(frame2);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    85
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    86
        robot.delay(500);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    87
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    88
        //
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    89
        // It's expeced that the focus is restored to <button1>.
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    90
        // If not, click <button1> to set focus on it.
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    91
        //
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    92
        if (!b1.hasFocus()) {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    93
            gained = Util.trackFocusGained(b1, new Runnable() {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    94
                public void run() {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    95
                    Util.clickOnComp(b1, robot);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    96
                }
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    97
            }, 5000, false);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    98
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
    99
            if (!gained) {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   100
                throw new RuntimeException("Unexpected state: focus is not on <button1>");
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   101
            }
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   102
        }
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   103
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   104
        robot.delay(500);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   105
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   106
        //
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   107
        // Click <button2>, check that focus is set on the parent frame.
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   108
        //
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   109
        gained = false;
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   110
        gained = Util.trackFocusGained(frame2, new Runnable() {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   111
            public void run() {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   112
                Util.clickOnComp(b2, robot);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   113
            }
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   114
        }, 5000, false);
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   115
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   116
        if (!gained) {
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   117
            throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   118
        }
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   119
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   120
        System.out.println("Test passed.");
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   121
    }
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents:
diff changeset
   122
}