Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/DisposedWindow/DisposeDialogNotActivateOwnerTest/DisposeDialogNotActivateOwnerTest.java
47867 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 638659226@summary Tests that disposing a dialog doesn't activate its invisible owner.27@author [email protected]: area=awt.focus28@run applet DisposeDialogNotActivateOwnerTest.html29*/3031import java.awt.*;32import java.awt.event.*;33import java.applet.Applet;3435public class DisposeDialogNotActivateOwnerTest extends Applet {36Robot robot;3738Frame frame = new Frame("Owner Frame");39Dialog dialog = new Dialog(new Frame(), "Owned Dialog");40Button frameButton = new Button("button");4142static boolean passed = false;4344public static void main(String[] args) {45DisposeDialogNotActivateOwnerTest app = new DisposeDialogNotActivateOwnerTest();46app.init();47app.start();48}4950public void init() {51try {52robot = new Robot();53} catch (AWTException e) {54throw new RuntimeException("Error: unable to create robot", e);55}56// Create instructions for the user here, as well as set up57// the environment -- set the layout manager, add buttons,58// etc.59this.setLayout (new BorderLayout ());60Sysout.createDialogWithInstructions(new String[]61{"This is automatic test. Simply wait until it is done."62});6364frame.setBounds(800, 50, 200, 100);65frame.add(frameButton);66dialog.setBounds(800, 300, 200, 100);67}6869public void start() {7071frameButton.addFocusListener(new FocusAdapter() {72public void focusGained(FocusEvent e) {73passed = true;74}75});7677frame.setVisible(true);78robot.waitForIdle();7980// make sure the frame is focused81clickOn(frame);82if (!frame.isFocused()) {83throw new RuntimeException("Error: a frame didn't get initial focus.");84}8586dialog.setVisible(true);87robot.waitForIdle();8889// make sure the dialog is focused90if (!dialog.isFocused()) {91throw new RuntimeException("Error: a dialog didn't get initial focus.");92}9394dialog.dispose();95robot.waitForIdle();9697if (passed) {98Sysout.println("Test passed.");99} else {100throw new RuntimeException("Test failed: a dialog activates invisible owner when disposed!");101}102}103104void clickOn(Component c) {105Point p = c.getLocationOnScreen();106Dimension d = c.getSize();107108if (c instanceof Frame) {109robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);110} else {111robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));112}113114robot.mousePress(InputEvent.BUTTON1_MASK);115robot.delay(20);116robot.mouseRelease(InputEvent.BUTTON1_MASK);117118robot.waitForIdle();119}120}121122/****************************************************123Standard Test Machinery124DO NOT modify anything below -- it's a standard125chunk of code whose purpose is to make user126interaction uniform, and thereby make it simpler127to read and understand someone else's test.128****************************************************/129130/**131This is part of the standard test machinery.132It creates a dialog (with the instructions), and is the interface133for sending text messages to the user.134To print the instructions, send an array of strings to Sysout.createDialog135WithInstructions method. Put one line of instructions per array entry.136To display a message for the tester to see, simply call Sysout.println137with the string to be displayed.138This mimics System.out.println but works within the test harness as well139as standalone.140*/141142class Sysout143{144static TestDialog dialog;145146public static void createDialogWithInstructions( String[] instructions )147{148dialog = new TestDialog( new Frame(), "Instructions" );149dialog.printInstructions( instructions );150dialog.setVisible(true);151println( "Any messages for the tester will display here." );152}153154public static void createDialog( )155{156dialog = new TestDialog( new Frame(), "Instructions" );157String[] defInstr = { "Instructions will appear here. ", "" } ;158dialog.printInstructions( defInstr );159dialog.setVisible(true);160println( "Any messages for the tester will display here." );161}162163164public static void printInstructions( String[] instructions )165{166dialog.printInstructions( instructions );167}168169170public static void println( String messageIn )171{172dialog.displayMessage( messageIn );173}174175}// Sysout class176177/**178This is part of the standard test machinery. It provides a place for the179test instructions to be displayed, and a place for interactive messages180to the user to be displayed.181To have the test instructions displayed, see Sysout.182To have a message to the user be displayed, see Sysout.183Do not call anything in this dialog directly.184*/185class TestDialog extends Dialog186{187188TextArea instructionsText;189TextArea messageText;190int maxStringLength = 80;191192//DO NOT call this directly, go through Sysout193public TestDialog( Frame frame, String name )194{195super( frame, name );196int scrollBoth = TextArea.SCROLLBARS_BOTH;197instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );198add( "North", instructionsText );199200messageText = new TextArea( "", 5, maxStringLength, scrollBoth );201add("Center", messageText);202203pack();204205setVisible(true);206}// TestDialog()207208//DO NOT call this directly, go through Sysout209public void printInstructions( String[] instructions )210{211//Clear out any current instructions212instructionsText.setText( "" );213214//Go down array of instruction strings215216String printStr, remainingStr;217for( int i=0; i < instructions.length; i++ )218{219//chop up each into pieces maxSringLength long220remainingStr = instructions[ i ];221while( remainingStr.length() > 0 )222{223//if longer than max then chop off first max chars to print224if( remainingStr.length() >= maxStringLength )225{226//Try to chop on a word boundary227int posOfSpace = remainingStr.228lastIndexOf( ' ', maxStringLength - 1 );229230if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;231232printStr = remainingStr.substring( 0, posOfSpace + 1 );233remainingStr = remainingStr.substring( posOfSpace + 1 );234}235//else just print236else237{238printStr = remainingStr;239remainingStr = "";240}241242instructionsText.append( printStr + "\n" );243244}// while245246}// for247248}//printInstructions()249250//DO NOT call this directly, go through Sysout251public void displayMessage( String messageIn )252{253messageText.append( messageIn + "\n" );254System.out.println(messageIn);255}256257}// TestDialog class258259260