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