1/*
2 * Copyright (c) 2007, 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 6562853 7035459
28  @summary Tests that focus transfered directy to window w/o transfering it to frame.
29  @author Oleg Sukhodolsky: area=awt.focus
30  @library ../../regtesthelpers
31  @build Util
32  @run main TranserFocusToWindow
33*/
34
35import java.awt.Button;
36import java.awt.Component;
37import java.awt.Frame;
38import java.awt.Robot;
39import java.awt.Window;
40
41import java.awt.event.WindowEvent;
42import java.awt.event.WindowFocusListener;
43
44import test.java.awt.regtesthelpers.Util;
45
46public class TranserFocusToWindow
47{
48    private static final int WIDTH = 300;
49    private static final int HEIGHT = 200;
50
51    public static void main(String[] args) {
52        Robot robot = Util.createRobot();
53        Frame owner_frame = new Frame("Owner frame");
54        owner_frame.setBounds(0, 0, WIDTH, HEIGHT);
55        owner_frame.setVisible(true);
56        Util.waitForIdle(robot);
57
58        Window window = new Window(owner_frame);
59        Button btn1 = new Button("button for focus");
60        window.add(btn1);
61        window.pack();
62        window.setLocation(0, HEIGHT + 100);
63        window.setVisible(true);
64        Util.waitForIdle(robot);
65
66        Frame another_frame = new Frame("Another frame");
67        Button btn2 = new Button("button in a frame");
68        another_frame.add(btn2);
69        another_frame.pack();
70        another_frame.setLocation(WIDTH + 100, 0);
71        another_frame.setVisible(true);
72        Util.waitForIdle(robot);
73
74        owner_frame.addWindowFocusListener(new WindowFocusListener() {
75                public void windowLostFocus(WindowEvent we) {
76                    System.out.println(we);
77                }
78                public void windowGainedFocus(WindowEvent we) {
79                    System.out.println(we);
80                    throw new RuntimeException("owner frame must not receive WINDWO_GAINED_FOCUS");
81                }
82            });
83        window.addWindowFocusListener(new WindowFocusListener() {
84                public void windowLostFocus(WindowEvent we) {
85                    System.out.println(we);
86                }
87                public void windowGainedFocus(WindowEvent we) {
88                    System.out.println(we);
89                }
90            });
91        another_frame.addWindowFocusListener(new WindowFocusListener() {
92                public void windowLostFocus(WindowEvent we) {
93                    System.out.println(we);
94                }
95                public void windowGainedFocus(WindowEvent we) {
96                    System.out.println(we);
97                }
98            });
99
100        Util.clickOnTitle(owner_frame, robot);
101        Util.waitForIdle(robot);
102        setFocus(btn1, robot);
103        setFocus(btn2, robot);
104        // we need this delay so WM can not treat two clicks on title as double click
105        robot.delay(500);
106        Util.clickOnTitle(owner_frame, robot);
107        Util.waitForIdle(robot);
108
109        System.out.println("test passed");
110    }
111
112    private static void setFocus(final Component comp, final Robot r) {
113        if (comp.hasFocus()) {
114            return;
115        }
116
117        Util.clickOnComp(comp, r);
118        Util.waitForIdle(r);
119
120        if (!comp.hasFocus()) {
121            throw new RuntimeException("can not set focus on " + comp);
122        }
123    }
124}
125