Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/RequestOnCompWithNullParent/RequestOnCompWithNullParent1.java
47525 views
/*1* Copyright (c) 2006, 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@bug 641802826@summary java/awt/Focus/RequestOnCompWithNullParent/RequestOnCompWithNullParent_Barrier.java fails27@author oleg.sukhodolsky: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main RequestOnCompWithNullParent131*/3233/**34* RequestOnCompWithNullParent1.java35*36* summary: java/awt/Focus/RequestOnCompWithNullParent/RequestOnCompWithNullParent_Barrier.java fails37*/3839import java.awt.*;40import java.awt.event.*;41import java.awt.peer.ButtonPeer;42import java.awt.peer.ComponentPeer;43import java.lang.reflect.Field;44import java.lang.reflect.InvocationHandler;45import java.lang.reflect.InvocationTargetException;46import java.lang.reflect.Method;47import java.lang.reflect.Proxy;48import test.java.awt.regtesthelpers.Util;4950//*** global search and replace RequestOnCompWithNullParent1 with name of the test ***5152public class RequestOnCompWithNullParent153{5455private static void init() {56//*** Create instructions for the user here ***57String[] instructions =58{59"This is an AUTOMATIC test, simply wait until it is done.",60"The result (passed or failed) will be shown in the",61"message window below."62};63Sysout.createDialog( );64Sysout.printInstructions( instructions );656667Frame frame = new Frame("test for 6418028");68frame.setLayout(new FlowLayout());69Button btn1 = new Button("Button1");70frame.add(btn1);71TestButton btn2 = new TestButton("Button2");72frame.add(btn2);73frame.pack();74frame.addWindowListener(new WindowAdapter() {75@Override76public void windowClosing(WindowEvent we) {77we.getWindow().dispose();78}79});80frame.setVisible(true);8182Util.waitForIdle(null);8384btn2.instrumentPeer();85btn2.requestFocusInWindow();86btn2.restorePeer();87frame.dispose();88RequestOnCompWithNullParent1.pass();89}//End init()90919293/*****************************************************94* Standard Test Machinery Section95* DO NOT modify anything in this section -- it's a96* standard chunk of code which has all of the97* synchronisation necessary for the test harness.98* By keeping it the same in all tests, it is easier99* to read and understand someone else's test, as100* well as insuring that all tests behave correctly101* with the test harness.102* There is a section following this for test-103* classes104******************************************************/105private static boolean theTestPassed = false;106private static boolean testGeneratedInterrupt = false;107private static String failureMessage = "";108109private static Thread mainThread = null;110111private static int sleepTime = 300000;112113// Not sure about what happens if multiple of this test are114// instantiated in the same VM. Being static (and using115// static vars), it aint gonna work. Not worrying about116// it for now.117public static void main( String args[] ) throws InterruptedException118{119mainThread = Thread.currentThread();120try121{122init();123}124catch( TestPassedException e )125{126//The test passed, so just return from main and harness will127// interepret this return as a pass128return;129}130//At this point, neither test pass nor test fail has been131// called -- either would have thrown an exception and ended the132// test, so we know we have multiple threads.133134//Test involves other threads, so sleep and wait for them to135// called pass() or fail()136try137{138Thread.sleep( sleepTime );139//Timed out, so fail the test140throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );141}142catch (InterruptedException e)143{144//The test harness may have interrupted the test. If so, rethrow the exception145// so that the harness gets it and deals with it.146if( ! testGeneratedInterrupt ) throw e;147148//reset flag in case hit this code more than once for some reason (just safety)149testGeneratedInterrupt = false;150151if ( theTestPassed == false )152{153throw new RuntimeException( failureMessage );154}155}156157}//main158159public static synchronized void setTimeoutTo( int seconds )160{161sleepTime = seconds * 1000;162}163164public static synchronized void pass()165{166Sysout.println( "The test passed." );167Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );168//first check if this is executing in main thread169if ( mainThread == Thread.currentThread() )170{171//Still in the main thread, so set the flag just for kicks,172// and throw a test passed exception which will be caught173// and end the test.174theTestPassed = true;175throw new TestPassedException();176}177theTestPassed = true;178testGeneratedInterrupt = true;179mainThread.interrupt();180}//pass()181182public static synchronized void fail()183{184//test writer didn't specify why test failed, so give generic185fail( "it just plain failed! :-)" );186}187188public static synchronized void fail( String whyFailed )189{190Sysout.println( "The test failed: " + whyFailed );191Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );192//check if this called from main thread193if ( mainThread == Thread.currentThread() )194{195//If main thread, fail now 'cause not sleeping196throw new RuntimeException( whyFailed );197}198theTestPassed = false;199testGeneratedInterrupt = true;200failureMessage = whyFailed;201mainThread.interrupt();202}//fail()203204}// class RequestOnCompWithNullParent1205206//This exception is used to exit from any level of call nesting207// when it's determined that the test has passed, and immediately208// end the test.209class TestPassedException extends RuntimeException210{211}212213//*********** End Standard Test Machinery Section **********214215216//************ Begin classes defined for the test ****************217218class TestButton extends Button {219ButtonPeer origPeer;220ButtonPeer proxiedPeer;221222/** Creates a new instance of TestButton */223public TestButton(String text) {224super(text);225}226227public void instrumentPeer() {228origPeer = (ButtonPeer) getPeer();229InvocationHandler handler = new InvocationHandler() {230public Object invoke(Object proxy, Method method, Object[] args) {231if (method.getName().equals("requestFocus")) {232Container parent = getParent();233parent.remove(TestButton.this);234System.err.println("parent = " + parent);235System.err.println("target = " + TestButton.this);236System.err.println("new parent = " + TestButton.this.getParent());237}238Object ret = null;239try {240ret = method.invoke(origPeer, args);241} catch (IllegalAccessException iae) {242throw new Error("Test error.", iae);243} catch (InvocationTargetException ita) {244throw new Error("Test error.", ita);245}246return ret;247}248};249250proxiedPeer = (ButtonPeer) Proxy.newProxyInstance(ButtonPeer.class.getClassLoader(), new Class[] {ButtonPeer.class}, handler);251setPeer(proxiedPeer);252}253254private void setPeer(final ComponentPeer newPeer) {255try {256Field peer_field = Component.class.getDeclaredField("peer");257peer_field.setAccessible(true);258peer_field.set(this, newPeer);259} catch (IllegalArgumentException ex) {260throw new Error("Test error.", ex);261} catch (SecurityException ex) {262throw new Error("Test error.", ex);263} catch (IllegalAccessException ex) {264throw new Error("Test error.", ex);265} catch (NoSuchFieldException ex) {266throw new Error("Test error.", ex);267}268}269270public void restorePeer() {271if (origPeer != null) {272setPeer(origPeer);273proxiedPeer = null;274}275}276}277//************** End classes defined for the test *******************278279280281282/****************************************************283Standard Test Machinery284DO NOT modify anything below -- it's a standard285chunk of code whose purpose is to make user286interaction uniform, and thereby make it simpler287to read and understand someone else's test.288****************************************************/289290/**291This is part of the standard test machinery.292It creates a dialog (with the instructions), and is the interface293for sending text messages to the user.294To print the instructions, send an array of strings to Sysout.createDialog295WithInstructions method. Put one line of instructions per array entry.296To display a message for the tester to see, simply call Sysout.println297with the string to be displayed.298This mimics System.out.println but works within the test harness as well299as standalone.300*/301302class Sysout303{304private static TestDialog dialog;305306public static void createDialogWithInstructions( String[] instructions )307{308dialog = new TestDialog( new Frame(), "Instructions" );309dialog.printInstructions( instructions );310dialog.setVisible(true);311println( "Any messages for the tester will display here." );312}313314public static void createDialog( )315{316dialog = new TestDialog( new Frame(), "Instructions" );317String[] defInstr = { "Instructions will appear here. ", "" } ;318dialog.printInstructions( defInstr );319dialog.setVisible(true);320println( "Any messages for the tester will display here." );321}322323324public static void printInstructions( String[] instructions )325{326dialog.printInstructions( instructions );327}328329330public static void println( String messageIn )331{332dialog.displayMessage( messageIn );333System.out.println(messageIn);334}335336}// Sysout class337338/**339This is part of the standard test machinery. It provides a place for the340test instructions to be displayed, and a place for interactive messages341to the user to be displayed.342To have the test instructions displayed, see Sysout.343To have a message to the user be displayed, see Sysout.344Do not call anything in this dialog directly.345*/346class TestDialog extends Dialog347{348349TextArea instructionsText;350TextArea messageText;351int maxStringLength = 80;352353//DO NOT call this directly, go through Sysout354public TestDialog( Frame frame, String name )355{356super( frame, name );357int scrollBoth = TextArea.SCROLLBARS_BOTH;358instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );359add( "North", instructionsText );360361messageText = new TextArea( "", 5, maxStringLength, scrollBoth );362add("Center", messageText);363364pack();365366setVisible(true);367}// TestDialog()368369//DO NOT call this directly, go through Sysout370public void printInstructions( String[] instructions )371{372//Clear out any current instructions373instructionsText.setText( "" );374375//Go down array of instruction strings376377String printStr, remainingStr;378for( int i=0; i < instructions.length; i++ )379{380//chop up each into pieces maxSringLength long381remainingStr = instructions[ i ];382while( remainingStr.length() > 0 )383{384//if longer than max then chop off first max chars to print385if( remainingStr.length() >= maxStringLength )386{387//Try to chop on a word boundary388int posOfSpace = remainingStr.389lastIndexOf( ' ', maxStringLength - 1 );390391if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;392393printStr = remainingStr.substring( 0, posOfSpace + 1 );394remainingStr = remainingStr.substring( posOfSpace + 1 );395}396//else just print397else398{399printStr = remainingStr;400remainingStr = "";401}402403instructionsText.append( printStr + "\n" );404405}// while406407}// for408409}//printInstructions()410411//DO NOT call this directly, go through Sysout412public void displayMessage( String messageIn )413{414messageText.append( messageIn + "\n" );415System.out.println(messageIn);416}417418}// TestDialog class419420421