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