Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ModalExcludedWindowClickTest/ModalExcludedWindowClickTest.java
47791 views
/*1* Copyright (c) 2005, 2015, 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 627184926@summary Tests that component in modal excluded Window which parent is blocked responses to mouse clicks.27@author [email protected]: area=awt.focus28@run applet ModalExcludedWindowClickTest.html29*/3031import java.applet.Applet;32import java.awt.*;33import java.awt.event.*;34import java.lang.reflect.*;3536public class ModalExcludedWindowClickTest extends Applet {37Robot robot;38Frame frame = new Frame("Frame");39Window w = new Window(frame);40Dialog d = new Dialog ((Dialog)null, "NullParentDialog", true);41Button button = new Button("Button");42boolean actionPerformed = false;4344public static void main (String args[]) {45ModalExcludedWindowClickTest app = new ModalExcludedWindowClickTest();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 an AUTOMATIC test", "simply wait until it is done"});62}6364public void start() {6566if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {67Sysout.println("No testing on MToolkit.");68return;69}7071button.addActionListener(new ActionListener() {72public void actionPerformed(ActionEvent e) {73actionPerformed = true;74Sysout.println(e.paramString());75}76});7778EventQueue.invokeLater(new Runnable() {79public void run() {80frame.setSize(200, 200);81frame.setVisible(true);8283w.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);84w.add(button);85w.setSize(200, 200);86w.setLocation(230, 230);87w.setVisible(true);8889d.setSize(200, 200);90d.setLocation(0, 230);91d.setVisible(true);9293}94});9596waitTillShown(d);9798test();99}100101void test() {102clickOn(button);103waitForIdle();104if (!actionPerformed) {105throw new RuntimeException("Test failed!");106}107Sysout.println("Test passed.");108}109110void clickOn(Component c) {111Point p = c.getLocationOnScreen();112Dimension d = c.getSize();113114Sysout.println("Clicking " + c);115116if (c instanceof Frame) {117robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);118} else {119robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));120}121robot.mousePress(InputEvent.BUTTON1_MASK);122robot.mouseRelease(InputEvent.BUTTON1_MASK);123waitForIdle();124}125void waitTillShown(Component c) {126while (true) {127try {128Thread.sleep(100);129c.getLocationOnScreen();130break;131} catch (InterruptedException e) {132throw new RuntimeException(e);133} catch (IllegalComponentStateException e) {}134}135}136void waitForIdle() {137try {138robot.waitForIdle();139EventQueue.invokeAndWait( new Runnable() {140public void run() {} // Dummy implementation141});142} catch(InterruptedException ie) {143Sysout.println("waitForIdle, non-fatal exception caught:");144ie.printStackTrace();145} catch(InvocationTargetException ite) {146Sysout.println("waitForIdle, non-fatal exception caught:");147ite.printStackTrace();148}149150// wait longer...151robot.delay(200);152}153}154155/****************************************************156Standard Test Machinery157DO NOT modify anything below -- it's a standard158chunk of code whose purpose is to make user159interaction uniform, and thereby make it simpler160to read and understand someone else's test.161****************************************************/162163/**164This is part of the standard test machinery.165It creates a dialog (with the instructions), and is the interface166for sending text messages to the user.167To print the instructions, send an array of strings to Sysout.createDialog168WithInstructions method. Put one line of instructions per array entry.169To display a message for the tester to see, simply call Sysout.println170with the string to be displayed.171This mimics System.out.println but works within the test harness as well172as standalone.173*/174175class Sysout176{177static TestDialog dialog;178179public static void createDialogWithInstructions( String[] instructions )180{181dialog = new TestDialog( new Frame(), "Instructions" );182dialog.printInstructions( instructions );183dialog.setVisible(true);184println( "Any messages for the tester will display here." );185}186187public static void createDialog( )188{189dialog = new TestDialog( new Frame(), "Instructions" );190String[] defInstr = { "Instructions will appear here. ", "" } ;191dialog.printInstructions( defInstr );192dialog.setVisible(true);193println( "Any messages for the tester will display here." );194}195196197public static void printInstructions( String[] instructions )198{199dialog.printInstructions( instructions );200}201202203public static void println( String messageIn )204{205dialog.displayMessage( messageIn );206}207208}// Sysout class209210/**211This is part of the standard test machinery. It provides a place for the212test instructions to be displayed, and a place for interactive messages213to the user to be displayed.214To have the test instructions displayed, see Sysout.215To have a message to the user be displayed, see Sysout.216Do not call anything in this dialog directly.217*/218class TestDialog extends Dialog219{220221TextArea instructionsText;222TextArea messageText;223int maxStringLength = 80;224225//DO NOT call this directly, go through Sysout226public TestDialog( Frame frame, String name )227{228super( frame, name );229int scrollBoth = TextArea.SCROLLBARS_BOTH;230instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );231add( "North", instructionsText );232233messageText = new TextArea( "", 5, maxStringLength, scrollBoth );234add("Center", messageText);235236pack();237238setVisible(true);239}// TestDialog()240241//DO NOT call this directly, go through Sysout242public void printInstructions( String[] instructions )243{244//Clear out any current instructions245instructionsText.setText( "" );246247//Go down array of instruction strings248249String printStr, remainingStr;250for( int i=0; i < instructions.length; i++ )251{252//chop up each into pieces maxSringLength long253remainingStr = instructions[ i ];254while( remainingStr.length() > 0 )255{256//if longer than max then chop off first max chars to print257if( remainingStr.length() >= maxStringLength )258{259//Try to chop on a word boundary260int posOfSpace = remainingStr.261lastIndexOf( ' ', maxStringLength - 1 );262263if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;264265printStr = remainingStr.substring( 0, posOfSpace + 1 );266remainingStr = remainingStr.substring( posOfSpace + 1 );267}268//else just print269else270{271printStr = remainingStr;272remainingStr = "";273}274275instructionsText.append( printStr + "\n" );276277}// while278279}// for280281}//printInstructions()282283//DO NOT call this directly, go through Sysout284public void displayMessage( String messageIn )285{286messageText.append( messageIn + "\n" );287System.out.println(messageIn);288}289290}// TestDialog class291292293