Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ModalBlockedStealsFocusTest/ModalBlockedStealsFocusTest.java
47497 views
/*1* Copyright (c) 2006, 2007, 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 642613226@summary Modal blocked window shouldn't steal focus when shown, or brought to front.27@author anton.tarasov@...: area=awt.focus28@run applet ModalBlockedStealsFocusTest.html29*/3031import java.awt.*;32import java.awt.event.*;33import java.applet.Applet;34import java.util.concurrent.atomic.AtomicBoolean;35import java.lang.reflect.InvocationTargetException;36import test.java.awt.regtesthelpers.Util;3738public class ModalBlockedStealsFocusTest extends Applet {39Frame frame = new Frame("Blocked Frame");40Dialog dialog = new Dialog(frame, "Modal Dialog", Dialog.ModalityType.TOOLKIT_MODAL);41AtomicBoolean lostFocus = new AtomicBoolean(false);4243public static void main(String[] args) {44ModalBlockedStealsFocusTest app = new ModalBlockedStealsFocusTest();45app.init();46app.start();47}4849public void init() {50// Create instructions for the user here, as well as set up51// the environment -- set the layout manager, add buttons,52// etc.53this.setLayout (new BorderLayout ());54Sysout.createDialogWithInstructions(new String[]55{"This is an automatic test. Simply wait until it is done."56});57}5859public void start() {60if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {61Sysout.println("The test is not for MToolkit.");62return;63}6465dialog.setBounds(800, 0, 200, 100);66frame.setBounds(800, 150, 200, 100);6768dialog.addWindowFocusListener(new WindowAdapter() {69public void windowLostFocus(WindowEvent e) {70Sysout.println(e.toString());71synchronized (lostFocus) {72lostFocus.set(true);73lostFocus.notifyAll();74}75}76});7778new Thread(new Runnable() {79public void run() {80dialog.setVisible(true);81}82}).start();8384Util.waitTillShown(dialog);85try {86Robot robot = new Robot();87robot.waitForIdle();88}catch(Exception ex) {89ex.printStackTrace();90throw new RuntimeException("Unexpected failure");91}9293// Test 1. Show a modal blocked frame, check that it doesn't steal focus.9495frame.setVisible(true);9697if (Util.waitForCondition(lostFocus, 2000L)) {98throw new TestFailedException("the modal blocked frame stole focus on its showing!");99}100101// Test 2. Brought a modal blocked frame to front, check that it doesn't steal focus.102103frame.toFront();104105if (Util.waitForCondition(lostFocus, 2000L)) {106throw new TestFailedException("the modal blocked frame stole focus on its bringing to front!");107} else {108Sysout.println("Test passed");109}110}111}112113class TestFailedException extends RuntimeException {114TestFailedException(String msg) {115super("Test failed: " + msg);116}117}118119/****************************************************120Standard Test Machinery121DO NOT modify anything below -- it's a standard122chunk of code whose purpose is to make user123interaction uniform, and thereby make it simpler124to read and understand someone else's test.125****************************************************/126127/**128This is part of the standard test machinery.129It creates a dialog (with the instructions), and is the interface130for sending text messages to the user.131To print the instructions, send an array of strings to Sysout.createDialog132WithInstructions method. Put one line of instructions per array entry.133To display a message for the tester to see, simply call Sysout.println134with the string to be displayed.135This mimics System.out.println but works within the test harness as well136as standalone.137*/138139class Sysout140{141static TestDialog dialog;142143public static void createDialogWithInstructions( String[] instructions )144{145dialog = new TestDialog( new Frame(), "Instructions" );146dialog.printInstructions( instructions );147dialog.setVisible(true);148println( "Any messages for the tester will display here." );149}150151public static void createDialog( )152{153dialog = new TestDialog( new Frame(), "Instructions" );154String[] defInstr = { "Instructions will appear here. ", "" } ;155dialog.printInstructions( defInstr );156dialog.setVisible(true);157println( "Any messages for the tester will display here." );158}159160161public static void printInstructions( String[] instructions )162{163dialog.printInstructions( instructions );164}165166167public static void println( String messageIn )168{169dialog.displayMessage( messageIn );170}171172}// Sysout class173174/**175This is part of the standard test machinery. It provides a place for the176test instructions to be displayed, and a place for interactive messages177to the user to be displayed.178To have the test instructions displayed, see Sysout.179To have a message to the user be displayed, see Sysout.180Do not call anything in this dialog directly.181*/182class TestDialog extends Dialog183{184185TextArea instructionsText;186TextArea messageText;187int maxStringLength = 80;188189//DO NOT call this directly, go through Sysout190public TestDialog( Frame frame, String name )191{192super( frame, name );193int scrollBoth = TextArea.SCROLLBARS_BOTH;194instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );195add( "North", instructionsText );196197messageText = new TextArea( "", 5, maxStringLength, scrollBoth );198add("Center", messageText);199200pack();201202setVisible(true);203}// TestDialog()204205//DO NOT call this directly, go through Sysout206public void printInstructions( String[] instructions )207{208//Clear out any current instructions209instructionsText.setText( "" );210211//Go down array of instruction strings212213String printStr, remainingStr;214for( int i=0; i < instructions.length; i++ )215{216//chop up each into pieces maxSringLength long217remainingStr = instructions[ i ];218while( remainingStr.length() > 0 )219{220//if longer than max then chop off first max chars to print221if( remainingStr.length() >= maxStringLength )222{223//Try to chop on a word boundary224int posOfSpace = remainingStr.225lastIndexOf( ' ', maxStringLength - 1 );226227if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;228229printStr = remainingStr.substring( 0, posOfSpace + 1 );230remainingStr = remainingStr.substring( posOfSpace + 1 );231}232//else just print233else234{235printStr = remainingStr;236remainingStr = "";237}238239instructionsText.append( printStr + "\n" );240241}// while242243}// for244245}//printInstructions()246247//DO NOT call this directly, go through Sysout248public void displayMessage( String messageIn )249{250messageText.append( messageIn + "\n" );251System.out.println(messageIn);252}253254}// TestDialog class255256257