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 6386592 27 @summary Tests that disposing a dialog doesn't activate its invisible owner. 28 @author anton.tarasov@sun.com: area=awt.focus 29 @run applet DisposeDialogNotActivateOwnerTest.html 30 */ 31 32 import java.awt.*; 33 import java.awt.event.*; 34 import java.applet.Applet; 35 36 public class DisposeDialogNotActivateOwnerTest extends Applet { 37 Robot robot; 38 39 Frame frame = new Frame("Owner Frame"); 40 Dialog dialog = new Dialog(new Frame(), "Owned Dialog"); 41 Button frameButton = new Button("button"); 42 43 static boolean passed = false; 44 45 public static void main(String[] args) { 46 DisposeDialogNotActivateOwnerTest app = new DisposeDialogNotActivateOwnerTest(); 47 app.init(); 48 app.start(); 49 } 50 51 public void init() { 52 try { 53 robot = new Robot(); 54 } catch (AWTException e) { 55 throw new RuntimeException("Error: unable to create robot", e); 56 } 57 // Create instructions for the user here, as well as set up 58 // the environment -- set the layout manager, add buttons, 59 // etc. 60 this.setLayout (new BorderLayout ()); 61 Sysout.createDialogWithInstructions(new String[] 62 {"This is automatic test. Simply wait until it is done." 63 }); 64 65 frame.setBounds(800, 50, 200, 100); 66 frame.add(frameButton); 67 dialog.setBounds(800, 300, 200, 100); 68 } 69 70 public void start() { 71 72 frameButton.addFocusListener(new FocusAdapter() { 73 public void focusGained(FocusEvent e) { 74 passed = true; 75 } 76 }); 77 78 frame.setVisible(true); 79 robot.waitForIdle(); 80 81 // make sure the frame is focused 82 clickOn(frame); 83 if (!frame.isFocused()) { 84 throw new RuntimeException("Error: a frame didn't get initial focus."); 85 } 86 87 dialog.setVisible(true); 88 robot.waitForIdle(); 89 90 // make sure the dialog is focused 91 if (!dialog.isFocused()) { 92 throw new RuntimeException("Error: a dialog didn't get initial focus."); 93 } 94 95 dialog.dispose(); 96 robot.waitForIdle(); 97 98 if (passed) { 99 Sysout.println("Test passed."); 100 } else { 101 throw new RuntimeException("Test failed: a dialog activates invisible owner when disposed!"); 102 } 103 } 104 105 void clickOn(Component c) { 106 Point p = c.getLocationOnScreen(); 107 Dimension d = c.getSize(); 108 109 if (c instanceof Frame) { 110 robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2); 111 } else { 112 robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2)); 113 } 114 115 robot.mousePress(InputEvent.BUTTON1_MASK); 116 robot.delay(20); 117 robot.mouseRelease(InputEvent.BUTTON1_MASK); 118 119 robot.waitForIdle(); 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