NonfocusableOwnerTest.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2008, 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       6182359
28  @summary   Tests that Window having non-focusable owner can't be a focus owner.
29  @author    Anton.Tarasov: area=awt.focus
30  @library   ../../regtesthelpers
31  @build     Util
32  @run       main NonfocusableOwnerTest
33*/
34
35import java.awt.*;
36import java.awt.event.*;
37import java.applet.Applet;
38import java.lang.reflect.*;
39import java.io.*;
40import test.java.awt.regtesthelpers.Util;
41
42public class NonfocusableOwnerTest extends Applet {
43    Robot robot = Util.createRobot();
44    Frame frame;
45    Dialog dialog;
46    Window window1;
47    Window window2;
48    Button button = new Button("button");
49
50    public static void main(String[] args) {
51        NonfocusableOwnerTest test = new NonfocusableOwnerTest();
52        test.start();
53    }
54
55    public void start() {
56        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
57                public void eventDispatched(AWTEvent e) {
58                    System.out.println(e.toString());
59                }
60            }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);
61
62        frame = new Frame("Frame");
63        frame.setName("Frame-owner");
64        frame.setBounds(100, 0, 100, 100);
65        dialog = new Dialog(frame, "Dialog");
66        dialog.setName("Dialog-owner");
67        dialog.setBounds(100, 0, 100, 100);
68
69        window1 = new Window(frame);
70        window1.setName("1st child");
71        window1.setBounds(100, 300, 100, 100);
72        window2 = new Window(window1);
73        window2.setName("2nd child");
74        window2.setBounds(100, 500, 100, 100);
75
76        test1(frame, window1);
77        test2(frame, window1, window2);
78        test3(frame, window1, window2);
79
80        window1 = new Window(dialog);
81        window1.setBounds(100, 300, 100, 100);
82        window1.setName("1st child");
83        window2 = new Window(window1);
84        window2.setName("2nd child");
85        window2.setBounds(100, 500, 100, 100);
86
87        test1(dialog, window1);
88        test2(dialog, window1, window2);
89        test3(dialog, window1, window2);
90
91        System.out.println("Test passed.");
92    }
93
94    void test1(Window owner, Window child) {
95        System.out.println("* * * STAGE 1 * * *\nWindow owner: " + owner);
96
97        owner.setFocusableWindowState(false);
98        owner.setVisible(true);
99
100        child.add(button);
101        child.setVisible(true);
102
103        Util.waitTillShown(child);
104
105        Util.clickOnComp(button, robot);
106        if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
107            throw new RuntimeException("Test Failed.");
108        }
109        child.dispose();
110        owner.dispose();
111    }
112
113    void test2(Window owner, Window child1, Window child2) {
114        System.out.println("* * * STAGE 2 * * *\nWindow nowner: " + owner);
115
116        owner.setFocusableWindowState(false);
117        owner.setVisible(true);
118
119        child1.setFocusableWindowState(true);
120        child1.setVisible(true);
121
122        child2.add(button);
123        child2.setVisible(true);
124
125        Util.waitTillShown(child2);
126
127        Util.clickOnComp(button, robot);
128        if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
129            throw new RuntimeException("Test failed.");
130        }
131        child2.dispose();
132        child1.dispose();
133        owner.dispose();
134    }
135
136    void test3(Window owner, Window child1, Window child2) {
137        System.out.println("* * * STAGE 3 * * *\nWidow owner: " + owner);
138
139        owner.setFocusableWindowState(true);
140        owner.setVisible(true);
141
142        child1.setFocusableWindowState(false);
143        child1.setVisible(true);
144
145        child2.setFocusableWindowState(true);
146        child2.add(button);
147        child2.setVisible(true);
148
149        Util.waitTillShown(child2);
150
151        Util.clickOnComp(button, robot);
152        System.err.println("focus owner: " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
153        if (button != KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
154            throw new RuntimeException("Test failed.");
155        }
156        child1.dispose();
157        child2.dispose();
158        owner.dispose();
159    }
160}
161