Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ChildWindowFocusTest/ChildWindowFocusTest.java
47512 views
/*1* Copyright (c) 2004, 2014, 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/*24test25@bug 509032526@summary Tests that Window's child can be focused on XAWT.27@author [email protected]: area=awt.focus28@run applet ChildWindowFocusTest.html29*/3031import java.awt.*;32import java.awt.event.*;33import java.applet.Applet;34import java.lang.reflect.*;3536public class ChildWindowFocusTest extends Applet {37Robot robot;38Frame frame = new Frame("Owner");39Button button0 = new Button("button-0");40TextField text0 = new TextField("text-0");41TextField text1 = new TextField("text-1");42Window win1 = new TestWindow(frame, text0, 110);43Window win2 = new TestWindow(win1, text1, 220);44Frame outerFrame = new Frame("Outer");45Button button1 = new Button("button-1");46int shift;4748public void init() {49try {50robot = new Robot();51} catch (AWTException e) {52throw new RuntimeException("Error: unable to create robot", e);53}54// Create instructions for the user here, as well as set up55// the environment -- set the layout manager, add buttons,56// etc.57this.setLayout (new BorderLayout ());58Sysout.createDialogWithInstructions(new String[]59{"This is an AUTOMATIC test", "simply wait until it is done"});6061Rectangle bounds = Sysout.dialog.getBounds();62shift = (int)(bounds.x + bounds.width + 10);63}6465public void start() {6667frame.setBounds(0, 50, 400, 100);68frame.setLayout(new FlowLayout());69frame.add(button0);7071outerFrame.setBounds(0, 390, 400, 100);72outerFrame.setLayout(new FlowLayout());73outerFrame.add(button1);7475adjustAndShow(new Component[] {frame, win1, win2, outerFrame});76robot.waitForIdle();7778test();79}8081void adjustAndShow(Component[] comps) {82for (Component comp: comps) {83comp.setLocation(shift, (int)comp.getLocation().getY());84comp.setVisible(true);85robot.waitForIdle();86}87}8889void test() {90clickOnCheckFocusOwner(button0);91clickOnCheckFocusOwner(text1);92clickOnCheckFocusOwner(button1);93clickOn(frame);94checkFocusOwner(text1);95clickOnCheckFocusOwner(text0);96clickOnCheckFocusOwner(button1);97clickOn(frame);98checkFocusOwner(text0);99100Sysout.println("Test passed.");101}102103void clickOnCheckFocusOwner(Component c) {104clickOn(c);105if (!checkFocusOwner(c)) {106throw new RuntimeException("Test failed: couldn't focus <" + c + "> by mouse click!");107}108}109110boolean checkFocusOwner(Component comp) {111return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());112}113114void clickOn(Component c) {115Point p = c.getLocationOnScreen();116Dimension d = c.getSize();117118Sysout.println("Clicking " + c);119120if (c instanceof Frame) {121robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);122} else {123robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));124}125robot.delay(50);126robot.mousePress(InputEvent.BUTTON1_MASK);127robot.delay(50);128robot.mouseRelease(InputEvent.BUTTON1_MASK);129robot.waitForIdle();130}131132}133134class TestWindow extends Window {135TestWindow(Window owner, Component comp, int x) {136super(owner);137setBackground(Color.blue);138setLayout(new FlowLayout());139add(comp);140comp.setBackground(Color.yellow);141setBounds(0, x, 100, 100);142}143}144145/****************************************************146Standard Test Machinery147DO NOT modify anything below -- it's a standard148chunk of code whose purpose is to make user149interaction uniform, and thereby make it simpler150to read and understand someone else's test.151****************************************************/152153/**154This is part of the standard test machinery.155It creates a dialog (with the instructions), and is the interface156for sending text messages to the user.157To print the instructions, send an array of strings to Sysout.createDialog158WithInstructions method. Put one line of instructions per array entry.159To display a message for the tester to see, simply call Sysout.println160with the string to be displayed.161This mimics System.out.println but works within the test harness as well162as standalone.163*/164165class Sysout166{167static TestDialog dialog;168169public static void createDialogWithInstructions( String[] instructions )170{171dialog = new TestDialog( new Frame(), "Instructions" );172dialog.printInstructions( instructions );173dialog.setVisible(true);174println( "Any messages for the tester will display here." );175}176177public static void createDialog( )178{179dialog = new TestDialog( new Frame(), "Instructions" );180String[] defInstr = { "Instructions will appear here. ", "" } ;181dialog.printInstructions( defInstr );182dialog.setVisible(true);183println( "Any messages for the tester will display here." );184}185186187public static void printInstructions( String[] instructions )188{189dialog.printInstructions( instructions );190}191192193public static void println( String messageIn )194{195dialog.displayMessage( messageIn );196}197198}// Sysout class199200/**201This is part of the standard test machinery. It provides a place for the202test instructions to be displayed, and a place for interactive messages203to the user to be displayed.204To have the test instructions displayed, see Sysout.205To have a message to the user be displayed, see Sysout.206Do not call anything in this dialog directly.207*/208class TestDialog extends Dialog209{210211TextArea instructionsText;212TextArea messageText;213int maxStringLength = 80;214215//DO NOT call this directly, go through Sysout216public TestDialog( Frame frame, String name )217{218super( frame, name );219int scrollBoth = TextArea.SCROLLBARS_BOTH;220instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );221add( "North", instructionsText );222223messageText = new TextArea( "", 5, maxStringLength, scrollBoth );224add("Center", messageText);225226pack();227228setVisible(true);229}// TestDialog()230231//DO NOT call this directly, go through Sysout232public void printInstructions( String[] instructions )233{234//Clear out any current instructions235instructionsText.setText( "" );236237//Go down array of instruction strings238239String printStr, remainingStr;240for( int i=0; i < instructions.length; i++ )241{242//chop up each into pieces maxSringLength long243remainingStr = instructions[ i ];244while( remainingStr.length() > 0 )245{246//if longer than max then chop off first max chars to print247if( remainingStr.length() >= maxStringLength )248{249//Try to chop on a word boundary250int posOfSpace = remainingStr.251lastIndexOf( ' ', maxStringLength - 1 );252253if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;254255printStr = remainingStr.substring( 0, posOfSpace + 1 );256remainingStr = remainingStr.substring( posOfSpace + 1 );257}258//else just print259else260{261printStr = remainingStr;262remainingStr = "";263}264265instructionsText.append( printStr + "\n" );266267}// while268269}// for270271}//printInstructions()272273//DO NOT call this directly, go through Sysout274public void displayMessage( String messageIn )275{276messageText.append( messageIn + "\n" );277System.out.println(messageIn);278}279280}// TestDialog class281282283