Path: blob/master/test/jdk/java/awt/Focus/RequestOnCompWithNullParent/RequestOnCompWithNullParent1.java
41152 views
/*1* Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 641802827@author oleg.sukhodolsky: area=awt.focus28@library ../../regtesthelpers29@modules java.desktop/java.awt.peer30java.desktop/sun.awt31java.desktop/java.awt:open32@build Util33@run main RequestOnCompWithNullParent134*/3536import 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;4546import sun.awt.AWTAccessor;4748public class RequestOnCompWithNullParent1 {4950public static void main(final String[] args) throws Exception {51Frame frame = new Frame("test for 6418028");52try {53test(frame);54} finally {55frame.dispose();56}57}5859private static void test(final Frame frame) throws Exception {60frame.setLayout(new FlowLayout());61Button btn1 = new Button("Button1");62frame.add(btn1);63TestButton btn2 = new TestButton("Button2");64frame.add(btn2);65frame.pack();66frame.addWindowListener(new WindowAdapter() {67@Override68public void windowClosing(WindowEvent we) {69we.getWindow().dispose();70}71});72frame.setVisible(true);7374new Robot().waitForIdle();7576btn2.instrumentPeer();77btn2.requestFocusInWindow();78btn2.restorePeer();79}80}8182class TestButton extends Button {83ButtonPeer origPeer;84ButtonPeer proxiedPeer;8586/** Creates a new instance of TestButton */87TestButton(String text) {88super(text);89}9091public void instrumentPeer() {92origPeer = AWTAccessor.getComponentAccessor().getPeer(this);9394InvocationHandler handler = new InvocationHandler() {95public Object invoke(Object proxy, Method method, Object[] args) {96if (method.getName().equals("requestFocus")) {97Container parent = getParent();98parent.remove(TestButton.this);99System.err.println("parent = " + parent);100System.err.println("target = " + TestButton.this);101System.err.println("new parent = " + TestButton.this.getParent());102}103Object ret = null;104try {105ret = method.invoke(origPeer, args);106} catch (IllegalAccessException iae) {107throw new Error("Test error.", iae);108} catch (InvocationTargetException ita) {109throw new Error("Test error.", ita);110}111return ret;112}113};114115proxiedPeer = (ButtonPeer) Proxy.newProxyInstance(116ButtonPeer.class.getClassLoader(),117new Class[] {ButtonPeer.class}, handler);118setPeer(proxiedPeer);119}120121private void setPeer(final ComponentPeer newPeer) {122try {123Field peer_field = Component.class.getDeclaredField("peer");124peer_field.setAccessible(true);125peer_field.set(this, newPeer);126} catch (IllegalArgumentException ex) {127throw new Error("Test error.", ex);128} catch (SecurityException ex) {129throw new Error("Test error.", ex);130} catch (IllegalAccessException ex) {131throw new Error("Test error.", ex);132} catch (NoSuchFieldException ex) {133throw new Error("Test error.", ex);134}135}136137public void restorePeer() {138if (origPeer != null) {139setPeer(origPeer);140proxiedPeer = null;141}142}143}144145146