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