1 /* 2 * Copyright (c) 2003, 2015, 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 /* 26 test 27 @bug 4828019 28 @summary Frame/Window deadlock 29 @author yan@sparc.spb.su: area= 30 @run applet NonEDT_GUI_Deadlock.html 31 */ 32 33 // Note there is no @ in front of test above. This is so that the 34 // harness will not mistake this file as a test file. It should 35 // only see the html file as a test file. (the harness runs all 36 // valid test files, so it would run this test twice if this file 37 // were valid as well as the html file.) 38 // Also, note the area= after Your Name in the author tag. Here, you 39 // should put which functional area the test falls in. See the 40 // AWT-core home page -> test areas and/or -> AWT team for a list of 41 // areas. 42 // Note also the 'AutomaticAppletTest.html' in the run tag. This should 43 // be changed to the name of the test. 44 45 46 /** 47 * NonEDT_GUI_Deadlock.java 48 * 49 * summary: 50 */ 51 52 import java.applet.Applet; 53 import java.awt.*; 54 import java.awt.event.*; 55 import java.net.*; 56 import java.io.*; 57 58 59 //Automated tests should run as applet tests if possible because they 60 // get their environments cleaned up, including AWT threads, any 61 // test created threads, and any system resources used by the test 62 // such as file descriptors. (This is normally not a problem as 63 // main tests usually run in a separate VM, however on some platforms 64 // such as the Mac, separate VMs are not possible and non-applet 65 // tests will cause problems). Also, you don't have to worry about 66 // synchronisation stuff in Applet tests they way you do in main 67 // tests... 68 69 70 public class NonEDT_GUI_Deadlock extends Applet 71 { 72 //Declare things used in the test, like buttons and labels here 73 boolean bOK = false; 74 Thread badThread = null; 75 76 public void init() 77 { 78 //Create instructions for the user here, as well as set up 79 // the environment -- set the layout manager, add buttons, 80 // etc. 81 82 83 String[] instructions = 84 { 85 "This is an AUTOMATIC test", 86 "simply wait until it is done" 87 }; 88 Sysout.createDialog( ); 89 Sysout.printInstructions( instructions ); 90 91 }//End init() 92 93 public void start () 94 { 95 //Get things going. Request focus, set size, et cetera 96 97 setSize (200,300); 98 setVisible(true); 99 validate(); 100 101 final Frame theFrame = new Frame("Window test"); 102 theFrame.setSize(240, 200); 103 104 Thread thKiller = new Thread() { 105 public void run() { 106 try { 107 Thread.sleep( 9000 ); 108 }catch( Exception ex ) { 109 } 110 if( !bOK ) { 111 // oops, 112 //Sysout.println("Deadlock!"); 113 Runtime.getRuntime().halt(0); 114 }else{ 115 //Sysout.println("Passed ok."); 116 } 117 } 118 }; 119 thKiller.setName("Killer thread"); 120 thKiller.start(); 121 Window w = new TestWindow(theFrame); 122 theFrame.toBack(); 123 theFrame.setVisible(true); 124 125 theFrame.setLayout(new FlowLayout(FlowLayout.CENTER)); 126 EventQueue.invokeLater(new Runnable() { 127 public void run() { 128 bOK = true; 129 } 130 }); 131 132 133 134 }// start() 135 class TestWindow extends Window implements Runnable { 136 137 TestWindow(Frame f) { 138 super(f); 139 140 //setSize(240, 75); 141 setLocation(0, 75); 142 143 show(); 144 toFront(); 145 146 badThread = new Thread(this); 147 badThread.setName("Bad Thread"); 148 badThread.start(); 149 150 } 151 152 public void paint(Graphics g) { 153 g.drawString("Deadlock or no deadlock?",20,80); 154 } 155 156 public void run() { 157 158 long ts = System.currentTimeMillis(); 159 160 while (true) { 161 if ((System.currentTimeMillis()-ts)>3000) { 162 this.setVisible( false ); 163 dispose(); 164 break; 165 } 166 167 toFront(); 168 try { 169 Thread.sleep(80); 170 } catch (Exception e) { 171 } 172 } 173 } 174 } 175 176 177 178 public static void main(String args[]) { 179 NonEDT_GUI_Deadlock imt = new NonEDT_GUI_Deadlock(); 180 imt.init(); 181 imt.start(); 182 } 183 184 185 }// class NonEDT_GUI_Deadlock 186 187 188 /**************************************************** 189 Standard Test Machinery 190 DO NOT modify anything below -- it's a standard 191 chunk of code whose purpose is to make user 192 interaction uniform, and thereby make it simpler 193 to read and understand someone else's test. 194 ****************************************************/ 195 196 /** 197 This is part of the standard test machinery. 198 It creates a dialog (with the instructions), and is the interface 199 for sending text messages to the user. 200 To print the instructions, send an array of strings to Sysout.createDialog 201 WithInstructions method. Put one line of instructions per array entry. 202 To display a message for the tester to see, simply call Sysout.println 203 with the string to be displayed. 204 This mimics System.out.println but works within the test harness as well 205 as standalone. 206 */ 207 208 class Sysout 209 { 210 private static TestDialog dialog; 211 212 public static void createDialogWithInstructions( String[] instructions ) 213 { 214 dialog = new TestDialog( new Frame(), "Instructions" ); 215 dialog.printInstructions( instructions ); 216 dialog.setVisible(true); 217 println( "Any messages for the tester will display here." ); 218 } 219 220 public static void createDialog( ) 221 { 222 dialog = new TestDialog( new Frame(), "Instructions" ); 223 String[] defInstr = { "Instructions will appear here. ", "" } ; 224 dialog.printInstructions( defInstr ); 225 dialog.setVisible(true); 226 println( "Any messages for the tester will display here." ); 227 } 228 229 230 public static void printInstructions( String[] instructions ) 231 { 232 dialog.printInstructions( instructions ); 233 } 234 235 236 public static void println( String messageIn ) 237 { 238 dialog.displayMessage( messageIn ); 239 } 240 241 }// Sysout class 242 243 /** 244 This is part of the standard test machinery. It provides a place for the 245 test instructions to be displayed, and a place for interactive messages 246 to the user to be displayed. 247 To have the test instructions displayed, see Sysout. 248 To have a message to the user be displayed, see Sysout. 249 Do not call anything in this dialog directly. 250 */ 251 class TestDialog extends Dialog 252 { 253 254 TextArea instructionsText; 255 TextArea messageText; 256 int maxStringLength = 80; 257 258 //DO NOT call this directly, go through Sysout 259 public TestDialog( Frame frame, String name ) 260 { 261 super( frame, name ); 262 int scrollBoth = TextArea.SCROLLBARS_BOTH; 263 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 264 add( "North", instructionsText ); 265 266 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 267 add("Center", messageText); 268 269 pack(); 270 271 show(); 272 }// TestDialog() 273 274 //DO NOT call this directly, go through Sysout 275 public void printInstructions( String[] instructions ) 276 { 277 //Clear out any current instructions 278 instructionsText.setText( "" ); 279 280 //Go down array of instruction strings 281 282 String printStr, remainingStr; 283 for( int i=0; i < instructions.length; i++ ) 284 { 285 //chop up each into pieces maxSringLength long 286 remainingStr = instructions[ i ]; 287 while( remainingStr.length() > 0 ) 288 { 289 //if longer than max then chop off first max chars to print 290 if( remainingStr.length() >= maxStringLength ) 291 { 292 //Try to chop on a word boundary 293 int posOfSpace = remainingStr. 294 lastIndexOf( ' ', maxStringLength - 1 ); 295 296 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 297 298 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 299 remainingStr = remainingStr.substring( posOfSpace + 1 ); 300 } 301 //else just print 302 else 303 { 304 printStr = remainingStr; 305 remainingStr = ""; 306 } 307 308 instructionsText.append( printStr + "\n" ); 309 310 }// while 311 312 }// for 313 314 }//printInstructions() 315 316 //DO NOT call this directly, go through Sysout 317 public void displayMessage( String messageIn ) 318 { 319 messageText.append( messageIn + "\n" ); 320 System.out.println(messageIn); 321 } 322 323 }// TestDialog class