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