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