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 if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) { 45 System.out.println("No testing on Motif! Passed."); 46 return; 47 } 48 49 frame.setBounds(0, 0, 400, 100); 50 frame.setLayout(new FlowLayout()); 51 frame.add(button0); 52 53 outerFrame.setBounds(0, 340, 400, 100); 54 outerFrame.setLayout(new FlowLayout()); 55 outerFrame.add(button1); 56 57 adjustAndShow(new Component[] {frame, win1, win2, outerFrame}); 58 waitForIdle(); 59 60 test(); 61 } 62 63 void adjustAndShow(Component[] comps) { 64 for (Component comp: comps) { 65 comp.setLocation(shift, (int)comp.getLocation().getY()); 66 comp.setVisible(true); 67 waitForIdle(); 68 } 69 } 70 71 void test() { 72 clickOnCheckFocusOwner(button0); 73 clickOnCheckFocusOwner(text1); 74 clickOnCheckFocusOwner(button1); 75 clickOn(frame); 76 checkFocusOwner(text1); 77 clickOnCheckFocusOwner(text0); 78 clickOnCheckFocusOwner(button1); 79 clickOn(frame); 80 checkFocusOwner(text0); 81 82 Sysout.println("Test passed."); 83 } 84 85 void clickOnCheckFocusOwner(Component c) { 86 clickOn(c); 87 if (!checkFocusOwner(c)) { 88 throw new RuntimeException("Test failed: couldn't focus <" + c + "> by mouse click!"); 89 } 90 } 91 92 boolean checkFocusOwner(Component comp) { 93 return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()); 94 } 95 96 void clickOn(Component c) { 97 Point p = c.getLocationOnScreen(); 98 Dimension d = c.getSize(); 99 100 Sysout.println("Clicking " + c); 101 102 if (c instanceof Frame) { 103 robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2); 104 } else { 105 robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2)); 106 } 107 robot.delay(50); 108 robot.mousePress(InputEvent.BUTTON1_MASK); 109 robot.delay(50); 110 robot.mouseRelease(InputEvent.BUTTON1_MASK); 111 waitForIdle(); 112 } 113 114 void waitForIdle() { 115 ((sun.awt.SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 116 } 117 } 118 119 class TestWindow extends Window { 120 TestWindow(Window owner, Component comp, int x) { 121 super(owner); 122 setBackground(Color.blue); 123 setLayout(new FlowLayout()); 124 add(comp); 125 comp.setBackground(Color.yellow); 126 setBounds(0, x, 100, 100); 127 } 128 } 129 130 /**************************************************** 131 Standard Test Machinery 132 DO NOT modify anything below -- it's a standard 133 chunk of code whose purpose is to make user 134 interaction uniform, and thereby make it simpler 135 to read and understand someone else's test. 136 ****************************************************/ 137 138 /** 139 This is part of the standard test machinery. 140 It creates a dialog (with the instructions), and is the interface 141 for sending text messages to the user. 142 To print the instructions, send an array of strings to Sysout.createDialog 143 WithInstructions method. Put one line of instructions per array entry. 144 To display a message for the tester to see, simply call Sysout.println 145 with the string to be displayed. 146 This mimics System.out.println but works within the test harness as well 147 as standalone. 148 */ 149 150 class Sysout 151 { 152 static TestDialog dialog; 153 154 public static void createDialogWithInstructions( String[] instructions ) 155 { 156 dialog = new TestDialog( new Frame(), "Instructions" ); 157 dialog.printInstructions( instructions ); 158 dialog.setVisible(true); 159 println( "Any messages for the tester will display here." ); 160 } 161 162 public static void createDialog( ) 163 { 164 dialog = new TestDialog( new Frame(), "Instructions" ); 165 String[] defInstr = { "Instructions will appear here. ", "" } ; 166 dialog.printInstructions( defInstr ); 167 dialog.setVisible(true); 168 println( "Any messages for the tester will display here." ); 169 } 170 171 172 public static void printInstructions( String[] instructions ) 173 { 174 dialog.printInstructions( instructions ); 175 } 176 177 178 public static void println( String messageIn ) 179 { 180 dialog.displayMessage( messageIn ); 181 } 182 183 }// Sysout class 184 185 /** 186 This is part of the standard test machinery. It provides a place for the 187 test instructions to be displayed, and a place for interactive messages 188 to the user to be displayed. 189 To have the test instructions displayed, see Sysout. 190 To have a message to the user be displayed, see Sysout. 191 Do not call anything in this dialog directly. 192 */ 193 class TestDialog extends Dialog 194 { 195 196 TextArea instructionsText; 197 TextArea messageText; 198 int maxStringLength = 80; 199 200 //DO NOT call this directly, go through Sysout 201 public TestDialog( Frame frame, String name ) 202 { 203 super( frame, name ); 204 int scrollBoth = TextArea.SCROLLBARS_BOTH; 205 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 206 add( "North", instructionsText ); 207 208 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 209 add("Center", messageText); 210 211 pack(); 212 213 setVisible(true); 214 }// TestDialog() 215 216 //DO NOT call this directly, go through Sysout 217 public void printInstructions( String[] instructions ) 218 { 219 //Clear out any current instructions 220 instructionsText.setText( "" ); 221 222 //Go down array of instruction strings 223 224 String printStr, remainingStr; 225 for( int i=0; i < instructions.length; i++ ) 226 { 227 //chop up each into pieces maxSringLength long 228 remainingStr = instructions[ i ]; 229 while( remainingStr.length() > 0 ) 230 { 231 //if longer than max then chop off first max chars to print 232 if( remainingStr.length() >= maxStringLength ) 233 { 234 //Try to chop on a word boundary 235 int posOfSpace = remainingStr. 236 lastIndexOf( ' ', maxStringLength - 1 ); 237 238 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 239 240 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 241 remainingStr = remainingStr.substring( posOfSpace + 1 ); 242 } 243 //else just print 244 else 245 { 246 printStr = remainingStr; 247 remainingStr = ""; 248 } 249 250 instructionsText.append( printStr + "\n" ); 251 252 }// while 253 254 }// for 255 256 }//printInstructions() 257 258 //DO NOT call this directly, go through Sysout 259 public void displayMessage( String messageIn ) 260 { 261 messageText.append( messageIn + "\n" ); 262 System.out.println(messageIn); 263 } 264 265 }// TestDialog class