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