Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ModalDialogInitialFocusTest/ModalDialogInitialFocusTest.java
47791 views
/*1* Copyright (c) 2006, 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 638275026@summary Tests that modal dialog doesn't request extra initial focus on show.27@author [email protected]: area=awt.focus28@run applet ModalDialogInitialFocusTest.html29*/3031import java.awt.*;32import java.awt.event.*;33import java.applet.Applet;34import java.util.concurrent.atomic.AtomicBoolean;35import java.lang.reflect.InvocationTargetException;3637public class ModalDialogInitialFocusTest extends Applet {38Robot robot;3940Dialog dialog = new Dialog((Window)null, "Test Dialog", Dialog.ModalityType.TOOLKIT_MODAL);41Button button = new Button("button");4243volatile static boolean passed = true;4445public static void main(String[] args) {46ModalDialogInitialFocusTest app = new ModalDialogInitialFocusTest();47app.init();48app.start();49}5051public void init() {52try {53robot = new Robot();54} catch (AWTException e) {55throw new RuntimeException("Error: unable to create robot", e);56}57// Create instructions for the user here, as well as set up58// the environment -- set the layout manager, add buttons,59// etc.60this.setLayout (new BorderLayout ());61Sysout.createDialogWithInstructions(new String[]62{"This is automatic test. Simply wait until it is done."63});64}6566public void start() {6768dialog.setLayout(new FlowLayout());69dialog.add(button);70dialog.setBounds(800, 0, 100, 100);7172dialog.addFocusListener(new FocusAdapter() {73// The only expected FOCUS_GAINED is on the button.74public void focusGained(FocusEvent e) {75passed = false;76}77});7879test();80}8182void test() {83new Thread(new Runnable() {84public void run() {85dialog.setVisible(true);86}87}).start();8889waitTillShown(dialog);9091robot.waitForIdle();9293dialog.dispose();9495if (passed) {96Sysout.println("Test passed.");97} else {98throw new RuntimeException("Test failed: dialog requests extra focus on show!");99}100}101102void waitTillShown(Component c) {103while (true) {104try {105Thread.sleep(100);106c.getLocationOnScreen();107break;108} catch (InterruptedException ie) {109ie.printStackTrace();110break;111} catch (IllegalComponentStateException e) {112}113}114}115}116117/****************************************************118Standard Test Machinery119DO NOT modify anything below -- it's a standard120chunk of code whose purpose is to make user121interaction uniform, and thereby make it simpler122to read and understand someone else's test.123****************************************************/124125/**126This is part of the standard test machinery.127It creates a dialog (with the instructions), and is the interface128for sending text messages to the user.129To print the instructions, send an array of strings to Sysout.createDialog130WithInstructions method. Put one line of instructions per array entry.131To display a message for the tester to see, simply call Sysout.println132with the string to be displayed.133This mimics System.out.println but works within the test harness as well134as standalone.135*/136137class Sysout138{139static TestDialog dialog;140141public static void createDialogWithInstructions( String[] instructions )142{143dialog = new TestDialog( new Frame(), "Instructions" );144dialog.printInstructions( instructions );145dialog.setVisible(true);146println( "Any messages for the tester will display here." );147}148149public static void createDialog( )150{151dialog = new TestDialog( new Frame(), "Instructions" );152String[] defInstr = { "Instructions will appear here. ", "" } ;153dialog.printInstructions( defInstr );154dialog.setVisible(true);155println( "Any messages for the tester will display here." );156}157158159public static void printInstructions( String[] instructions )160{161dialog.printInstructions( instructions );162}163164165public static void println( String messageIn )166{167dialog.displayMessage( messageIn );168}169170}// Sysout class171172/**173This is part of the standard test machinery. It provides a place for the174test instructions to be displayed, and a place for interactive messages175to the user to be displayed.176To have the test instructions displayed, see Sysout.177To have a message to the user be displayed, see Sysout.178Do not call anything in this dialog directly.179*/180class TestDialog extends Dialog181{182183TextArea instructionsText;184TextArea messageText;185int maxStringLength = 80;186187//DO NOT call this directly, go through Sysout188public TestDialog( Frame frame, String name )189{190super( frame, name );191int scrollBoth = TextArea.SCROLLBARS_BOTH;192instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );193add( "North", instructionsText );194195messageText = new TextArea( "", 5, maxStringLength, scrollBoth );196add("Center", messageText);197198pack();199200setVisible(true);201}// TestDialog()202203//DO NOT call this directly, go through Sysout204public void printInstructions( String[] instructions )205{206//Clear out any current instructions207instructionsText.setText( "" );208209//Go down array of instruction strings210211String printStr, remainingStr;212for( int i=0; i < instructions.length; i++ )213{214//chop up each into pieces maxSringLength long215remainingStr = instructions[ i ];216while( remainingStr.length() > 0 )217{218//if longer than max then chop off first max chars to print219if( remainingStr.length() >= maxStringLength )220{221//Try to chop on a word boundary222int posOfSpace = remainingStr.223lastIndexOf( ' ', maxStringLength - 1 );224225if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;226227printStr = remainingStr.substring( 0, posOfSpace + 1 );228remainingStr = remainingStr.substring( posOfSpace + 1 );229}230//else just print231else232{233printStr = remainingStr;234remainingStr = "";235}236237instructionsText.append( printStr + "\n" );238239}// while240241}// for242243}//printInstructions()244245//DO NOT call this directly, go through Sysout246public void displayMessage( String messageIn )247{248messageText.append( messageIn + "\n" );249System.out.println(messageIn);250}251252}// TestDialog class253254255