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