1 /*
   2  * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   test
  26   @bug       6386592
  27   @summary   Tests that disposing a dialog doesn't activate its invisible owner.
  28   @author    anton.tarasov@sun.com: area=awt.focus
  29   @run       applet DisposeDialogNotActivateOwnerTest.html
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.applet.Applet;
  35 
  36 public class DisposeDialogNotActivateOwnerTest extends Applet {
  37     Robot robot;
  38 
  39     Frame frame = new Frame("Owner Frame");
  40     Dialog dialog = new Dialog(new Frame(), "Owned Dialog");
  41     Button frameButton = new Button("button");
  42 
  43     static boolean passed = false;
  44 
  45     public static void main(String[] args) {
  46         DisposeDialogNotActivateOwnerTest app = new DisposeDialogNotActivateOwnerTest();
  47         app.init();
  48         app.start();
  49     }
  50 
  51     public void init() {
  52         try {
  53             robot = new Robot();
  54         } catch (AWTException e) {
  55             throw new RuntimeException("Error: unable to create robot", e);
  56         }
  57         // Create instructions for the user here, as well as set up
  58         // the environment -- set the layout manager, add buttons,
  59         // etc.
  60         this.setLayout (new BorderLayout ());
  61         Sysout.createDialogWithInstructions(new String[]
  62             {"This is automatic test. Simply wait until it is done."
  63             });
  64 
  65         frame.setBounds(0, 0, 400, 200);
  66         frame.add(frameButton);
  67         dialog.setBounds(100, 50, 200, 100);
  68     }
  69 
  70     public void start() {
  71 
  72         frameButton.addFocusListener(new FocusAdapter() {
  73                 public void focusGained(FocusEvent e) {
  74                     passed = true;
  75                 }
  76             });
  77 
  78         frame.setVisible(true);
  79         robot.waitForIdle();
  80 
  81         // make sure the frame is focused
  82         clickOn(frame);
  83         robot.waitForIdle();
  84         robot.delay(200);
  85         if (!frame.isFocused()) {
  86             throw new RuntimeException("Error: a frame didn't get initial focus.");
  87         }
  88 
  89         dialog.setVisible(true);
  90         robot.waitForIdle();
  91         robot.delay(200);
  92 
  93         // make sure the dialog is focused
  94         if (!dialog.isFocused()) {
  95             throw new RuntimeException("Error: a dialog didn't get initial focus.");
  96         }
  97 
  98         dialog.dispose();
  99         robot.waitForIdle();
 100 
 101         if (passed) {
 102             Sysout.println("Test passed.");
 103         } else {
 104             throw new RuntimeException("Test failed: a dialog activates invisible owner when disposed!");
 105         }
 106     }
 107 
 108     void clickOn(Component c) {
 109         Point p = c.getLocationOnScreen();
 110         Dimension d = c.getSize();
 111 
 112         if (c instanceof Frame) {
 113             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
 114         } else {
 115             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 116         }
 117 
 118         robot.mousePress(InputEvent.BUTTON1_MASK);
 119         robot.delay(20);
 120         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 121     }
 122 }
 123 
 124 /****************************************************
 125  Standard Test Machinery
 126  DO NOT modify anything below -- it's a standard
 127   chunk of code whose purpose is to make user
 128   interaction uniform, and thereby make it simpler
 129   to read and understand someone else's test.
 130  ****************************************************/
 131 
 132 /**
 133  This is part of the standard test machinery.
 134  It creates a dialog (with the instructions), and is the interface
 135   for sending text messages to the user.
 136  To print the instructions, send an array of strings to Sysout.createDialog
 137   WithInstructions method.  Put one line of instructions per array entry.
 138  To display a message for the tester to see, simply call Sysout.println
 139   with the string to be displayed.
 140  This mimics System.out.println but works within the test harness as well
 141   as standalone.
 142  */
 143 
 144 class Sysout
 145 {
 146     static TestDialog dialog;
 147 
 148     public static void createDialogWithInstructions( String[] instructions )
 149     {
 150         dialog = new TestDialog( new Frame(), "Instructions" );
 151         dialog.printInstructions( instructions );
 152         dialog.setVisible(true);
 153         println( "Any messages for the tester will display here." );
 154     }
 155 
 156     public static void createDialog( )
 157     {
 158         dialog = new TestDialog( new Frame(), "Instructions" );
 159         String[] defInstr = { "Instructions will appear here. ", "" } ;
 160         dialog.printInstructions( defInstr );
 161         dialog.setVisible(true);
 162         println( "Any messages for the tester will display here." );
 163     }
 164 
 165 
 166     public static void printInstructions( String[] instructions )
 167     {
 168         dialog.printInstructions( instructions );
 169     }
 170 
 171 
 172     public static void println( String messageIn )
 173     {
 174         dialog.displayMessage( messageIn );
 175     }
 176 
 177 }// Sysout  class
 178 
 179 /**
 180   This is part of the standard test machinery.  It provides a place for the
 181    test instructions to be displayed, and a place for interactive messages
 182    to the user to be displayed.
 183   To have the test instructions displayed, see Sysout.
 184   To have a message to the user be displayed, see Sysout.
 185   Do not call anything in this dialog directly.
 186   */
 187 class TestDialog extends Dialog
 188 {
 189 
 190     TextArea instructionsText;
 191     TextArea messageText;
 192     int maxStringLength = 80;
 193 
 194     //DO NOT call this directly, go through Sysout
 195     public TestDialog( Frame frame, String name )
 196     {
 197         super( frame, name );
 198         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 199         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 200         add( "North", instructionsText );
 201 
 202         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 203         add("Center", messageText);
 204 
 205         pack();
 206 
 207         setVisible(true);
 208     }// TestDialog()
 209 
 210     //DO NOT call this directly, go through Sysout
 211     public void printInstructions( String[] instructions )
 212     {
 213         //Clear out any current instructions
 214         instructionsText.setText( "" );
 215 
 216         //Go down array of instruction strings
 217 
 218         String printStr, remainingStr;
 219         for( int i=0; i < instructions.length; i++ )
 220         {
 221             //chop up each into pieces maxSringLength long
 222             remainingStr = instructions[ i ];
 223             while( remainingStr.length() > 0 )
 224             {
 225                 //if longer than max then chop off first max chars to print
 226                 if( remainingStr.length() >= maxStringLength )
 227                 {
 228                     //Try to chop on a word boundary
 229                     int posOfSpace = remainingStr.
 230                         lastIndexOf( ' ', maxStringLength - 1 );
 231 
 232                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 233 
 234                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 235                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 236                 }
 237                 //else just print
 238                 else
 239                 {
 240                     printStr = remainingStr;
 241                     remainingStr = "";
 242                 }
 243 
 244                 instructionsText.append( printStr + "\n" );
 245 
 246             }// while
 247 
 248         }// for
 249 
 250     }//printInstructions()
 251 
 252     //DO NOT call this directly, go through Sysout
 253     public void displayMessage( String messageIn )
 254     {
 255         messageText.append( messageIn + "\n" );
 256         System.out.println(messageIn);
 257     }
 258 
 259 }// TestDialog  class