RequestOnCompWithNullParent1.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2006, 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 6418028
28  @author oleg.sukhodolsky: area=awt.focus
29  @library ../../regtesthelpers
30  @modules java.desktop/java.awt.peer
31           java.desktop/sun.awt
32  @build Util
33  @run main RequestOnCompWithNullParent1
34*/
35
36import java.awt.*;
37import java.awt.event.*;
38import java.awt.peer.ButtonPeer;
39import java.awt.peer.ComponentPeer;
40import java.lang.reflect.Field;
41import java.lang.reflect.InvocationHandler;
42import java.lang.reflect.InvocationTargetException;
43import java.lang.reflect.Method;
44import java.lang.reflect.Proxy;
45
46import sun.awt.AWTAccessor;
47
48public class RequestOnCompWithNullParent1 {
49
50    public static void main(final String[] args) throws Exception {
51        Frame frame = new Frame("test for 6418028");
52        try {
53            test(frame);
54        } finally {
55            frame.dispose();
56        }
57    }
58
59    private static void test(final Frame frame) throws Exception {
60        frame.setLayout(new FlowLayout());
61        Button btn1 = new Button("Button1");
62        frame.add(btn1);
63        TestButton btn2 = new TestButton("Button2");
64        frame.add(btn2);
65        frame.pack();
66        frame.addWindowListener(new WindowAdapter() {
67            @Override
68            public void windowClosing(WindowEvent we) {
69                we.getWindow().dispose();
70            }
71        });
72        frame.setVisible(true);
73
74        new Robot().waitForIdle();
75
76        btn2.instrumentPeer();
77        btn2.requestFocusInWindow();
78        btn2.restorePeer();
79    }
80}
81
82class TestButton extends Button {
83    ButtonPeer origPeer;
84    ButtonPeer proxiedPeer;
85
86    /** Creates a new instance of TestButton */
87    TestButton(String text) {
88        super(text);
89    }
90
91    public void instrumentPeer() {
92        origPeer = AWTAccessor.getComponentAccessor().getPeer(this);
93
94        InvocationHandler handler = new InvocationHandler() {
95            public Object invoke(Object proxy, Method method, Object[] args) {
96                if (method.getName().equals("requestFocus")) {
97                    Container parent = getParent();
98                    parent.remove(TestButton.this);
99                    System.err.println("parent = " + parent);
100                    System.err.println("target = " + TestButton.this);
101                    System.err.println("new parent = " + TestButton.this.getParent());
102                }
103                Object ret = null;
104                try {
105                    ret = method.invoke(origPeer, args);
106                } catch (IllegalAccessException iae) {
107                    throw new Error("Test error.", iae);
108                } catch (InvocationTargetException ita) {
109                    throw new Error("Test error.", ita);
110                }
111                return ret;
112            }
113        };
114
115        proxiedPeer = (ButtonPeer) Proxy.newProxyInstance(
116                                    ButtonPeer.class.getClassLoader(),
117                                    new Class[] {ButtonPeer.class}, handler);
118        setPeer(proxiedPeer);
119    }
120
121    private void setPeer(final ComponentPeer newPeer) {
122        try {
123            Field peer_field = Component.class.getDeclaredField("peer");
124            peer_field.setAccessible(true);
125            peer_field.set(this, newPeer);
126        } catch (IllegalArgumentException ex) {
127            throw new Error("Test error.", ex);
128        } catch (SecurityException ex) {
129            throw new Error("Test error.", ex);
130        } catch (IllegalAccessException ex) {
131            throw new Error("Test error.", ex);
132        } catch (NoSuchFieldException ex) {
133            throw new Error("Test error.", ex);
134        }
135    }
136
137    public void restorePeer() {
138        if (origPeer != null) {
139            setPeer(origPeer);
140            proxiedPeer = null;
141        }
142    }
143}
144