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