1 /* 2 @test 3 @bug 4980592 4 @summary switching user in XP causes an NPE in sun.awt.windows.WWindowPeer.displayChanged 5 @author son@sparc.spb.su: area=embedded 6 @modules java.desktop/java.awt.peer 7 java.desktop/sun.awt 8 @run main DisplayChangedTest 9 */ 10 11 /** 12 * DisplayChangedTest.java 13 * 14 * summary: switching user in XP causes an NPE in sun.awt.windows.WWindowPeer.displayChanged 15 */ 16 17 import java.awt.Frame; 18 import java.awt.Dialog; 19 import java.awt.TextArea; 20 import java.awt.peer.ComponentPeer; 21 import java.awt.peer.FramePeer; 22 23 import java.lang.reflect.Constructor; 24 import java.lang.reflect.Method; 25 import java.lang.reflect.Field; 26 27 import sun.awt.AWTAccessor; 28 29 public class DisplayChangedTest 30 { 31 private static void init() 32 { 33 //*** Create instructions for the user here *** 34 35 String[] instructions = 36 { 37 "This is an AUTOMATIC test, simply wait until it is done.", 38 "The result (passed or failed) will be shown in the", 39 "message window below." 40 }; 41 Sysout.createDialog( ); 42 Sysout.printInstructions( instructions ); 43 44 if (!System.getProperty("os.name").startsWith("Windows")) { 45 System.out.println("This is Windows only test."); 46 DisplayChangedTest.pass(); 47 return; 48 } 49 50 try { 51 Frame frame = new Frame("AWT Frame"); 52 frame.pack(); 53 54 FramePeer frame_peer = AWTAccessor.getComponentAccessor().getPeer(frame); 55 Sysout.println("frame's peer = " + frame_peer); 56 Class comp_peer_class = 57 Class.forName("sun.awt.windows.WComponentPeer"); 58 Sysout.println("comp peer class = " + comp_peer_class); 59 Field hwnd_field = comp_peer_class.getDeclaredField("hwnd"); 60 hwnd_field.setAccessible(true); 61 Sysout.println("hwnd_field =" + hwnd_field); 62 long hwnd = hwnd_field.getLong(frame_peer); 63 Sysout.println("hwnd = " + hwnd); 64 65 Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame"); 66 Constructor constructor = clazz.getConstructor (new Class [] {long.class}); 67 Frame embedded_frame = 68 (Frame) constructor.newInstance (new Object[] {new Long(hwnd)});; 69 Sysout.println("embedded_frame = " + embedded_frame); 70 frame.setVisible(true); 71 72 ComponentPeer peer = AWTAccessor.getComponentAccessor().getPeer(embedded_frame); 73 Sysout.println("peer = " + peer); 74 Class peerClass = peer.getClass(); 75 Sysout.println("peer's class = " + peerClass); 76 77 Method displayChangedM = 78 peerClass.getMethod("displayChanged", new Class[0]); 79 displayChangedM.invoke(peer, null); 80 embedded_frame.dispose(); 81 frame.dispose(); 82 } catch (Throwable thr) { 83 thr.printStackTrace(); 84 DisplayChangedTest.fail("TEST FAILED: " + thr); 85 } 86 DisplayChangedTest.pass(); 87 88 }//End init() 89 90 91 92 /***************************************************** 93 * Standard Test Machinery Section 94 * DO NOT modify anything in this section -- it's a 95 * standard chunk of code which has all of the 96 * synchronisation necessary for the test harness. 97 * By keeping it the same in all tests, it is easier 98 * to read and understand someone else's test, as 99 * well as insuring that all tests behave correctly 100 * with the test harness. 101 * There is a section following this for test- 102 * classes 103 ******************************************************/ 104 private static boolean theTestPassed = false; 105 private static boolean testGeneratedInterrupt = false; 106 private static String failureMessage = ""; 107 108 private static Thread mainThread = null; 109 110 private static int sleepTime = 300000; 111 112 // Not sure about what happens if multiple of this test are 113 // instantiated in the same VM. Being static (and using 114 // static vars), it aint gonna work. Not worrying about 115 // it for now. 116 public static void main( String args[] ) throws InterruptedException 117 { 118 mainThread = Thread.currentThread(); 119 try 120 { 121 init(); 122 } 123 catch( TestPassedException e ) 124 { 125 //The test passed, so just return from main and harness will 126 // interepret this return as a pass 127 return; 128 } 129 //At this point, neither test pass nor test fail has been 130 // called -- either would have thrown an exception and ended the 131 // test, so we know we have multiple threads. 132 133 //Test involves other threads, so sleep and wait for them to 134 // called pass() or fail() 135 try 136 { 137 Thread.sleep( sleepTime ); 138 //Timed out, so fail the test 139 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); 140 } 141 catch (InterruptedException e) 142 { 143 //The test harness may have interrupted the test. If so, rethrow the exception 144 // so that the harness gets it and deals with it. 145 if( ! testGeneratedInterrupt ) throw e; 146 147 //reset flag in case hit this code more than once for some reason (just safety) 148 testGeneratedInterrupt = false; 149 150 if ( theTestPassed == false ) 151 { 152 throw new RuntimeException( failureMessage ); 153 } 154 } 155 156 }//main 157 158 public static synchronized void setTimeoutTo( int seconds ) 159 { 160 sleepTime = seconds * 1000; 161 } 162 163 public static synchronized void pass() 164 { 165 Sysout.println( "The test passed." ); 166 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 167 //first check if this is executing in main thread 168 if ( mainThread == Thread.currentThread() ) 169 { 170 //Still in the main thread, so set the flag just for kicks, 171 // and throw a test passed exception which will be caught 172 // and end the test. 173 theTestPassed = true; 174 throw new TestPassedException(); 175 } 176 theTestPassed = true; 177 testGeneratedInterrupt = true; 178 mainThread.interrupt(); 179 }//pass() 180 181 public static synchronized void fail() 182 { 183 //test writer didn't specify why test failed, so give generic 184 fail( "it just plain failed! :-)" ); 185 } 186 187 public static synchronized void fail( String whyFailed ) 188 { 189 Sysout.println( "The test failed: " + whyFailed ); 190 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 191 //check if this called from main thread 192 if ( mainThread == Thread.currentThread() ) 193 { 194 //If main thread, fail now 'cause not sleeping 195 throw new RuntimeException( whyFailed ); 196 } 197 theTestPassed = false; 198 testGeneratedInterrupt = true; 199 failureMessage = whyFailed; 200 mainThread.interrupt(); 201 }//fail() 202 203 }// class DisplayChangedTest 204 205 //This exception is used to exit from any level of call nesting 206 // when it's determined that the test has passed, and immediately 207 // end the test. 208 class TestPassedException extends RuntimeException 209 { 210 } 211 212 //*********** End Standard Test Machinery Section ********** 213 214 215 //************ Begin classes defined for the test **************** 216 217 // if want to make listeners, here is the recommended place for them, then instantiate 218 // them in init() 219 220 /* Example of a class which may be written as part of a test 221 class NewClass implements anInterface 222 { 223 static int newVar = 0; 224 225 public void eventDispatched(AWTEvent e) 226 { 227 //Counting events to see if we get enough 228 eventCount++; 229 230 if( eventCount == 20 ) 231 { 232 //got enough events, so pass 233 234 DisplayChangedTest.pass(); 235 } 236 else if( tries == 20 ) 237 { 238 //tried too many times without getting enough events so fail 239 240 DisplayChangedTest.fail(); 241 } 242 243 }// eventDispatched() 244 245 }// NewClass class 246 247 */ 248 249 250 //************** End classes defined for the test ******************* 251 252 253 254 255 /**************************************************** 256 Standard Test Machinery 257 DO NOT modify anything below -- it's a standard 258 chunk of code whose purpose is to make user 259 interaction uniform, and thereby make it simpler 260 to read and understand someone else's test. 261 ****************************************************/ 262 263 /** 264 This is part of the standard test machinery. 265 It creates a dialog (with the instructions), and is the interface 266 for sending text messages to the user. 267 To print the instructions, send an array of strings to Sysout.createDialog 268 WithInstructions method. Put one line of instructions per array entry. 269 To display a message for the tester to see, simply call Sysout.println 270 with the string to be displayed. 271 This mimics System.out.println but works within the test harness as well 272 as standalone. 273 */ 274 275 class Sysout 276 { 277 private static TestDialog dialog; 278 279 public static void createDialogWithInstructions( String[] instructions ) 280 { 281 dialog = new TestDialog( new Frame(), "Instructions" ); 282 dialog.printInstructions( instructions ); 283 dialog.setVisible(true); 284 println( "Any messages for the tester will display here." ); 285 } 286 287 public static void createDialog( ) 288 { 289 dialog = new TestDialog( new Frame(), "Instructions" ); 290 String[] defInstr = { "Instructions will appear here. ", "" } ; 291 dialog.printInstructions( defInstr ); 292 dialog.setVisible(true); 293 println( "Any messages for the tester will display here." ); 294 } 295 296 297 public static void printInstructions( String[] instructions ) 298 { 299 dialog.printInstructions( instructions ); 300 } 301 302 303 public static void println( String messageIn ) 304 { 305 dialog.displayMessage( messageIn ); 306 System.out.println(messageIn); 307 } 308 309 }// Sysout class 310 311 /** 312 This is part of the standard test machinery. It provides a place for the 313 test instructions to be displayed, and a place for interactive messages 314 to the user to be displayed. 315 To have the test instructions displayed, see Sysout. 316 To have a message to the user be displayed, see Sysout. 317 Do not call anything in this dialog directly. 318 */ 319 class TestDialog extends Dialog 320 { 321 322 TextArea instructionsText; 323 TextArea messageText; 324 int maxStringLength = 80; 325 326 //DO NOT call this directly, go through Sysout 327 public TestDialog( Frame frame, String name ) 328 { 329 super( frame, name ); 330 int scrollBoth = TextArea.SCROLLBARS_BOTH; 331 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 332 add( "North", instructionsText ); 333 334 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 335 add("Center", messageText); 336 337 pack(); 338 339 setVisible(true); 340 }// TestDialog() 341 342 //DO NOT call this directly, go through Sysout 343 public void printInstructions( String[] instructions ) 344 { 345 //Clear out any current instructions 346 instructionsText.setText( "" ); 347 348 //Go down array of instruction strings 349 350 String printStr, remainingStr; 351 for( int i=0; i < instructions.length; i++ ) 352 { 353 //chop up each into pieces maxSringLength long 354 remainingStr = instructions[ i ]; 355 while( remainingStr.length() > 0 ) 356 { 357 //if longer than max then chop off first max chars to print 358 if( remainingStr.length() >= maxStringLength ) 359 { 360 //Try to chop on a word boundary 361 int posOfSpace = remainingStr. 362 lastIndexOf( ' ', maxStringLength - 1 ); 363 364 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 365 366 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 367 remainingStr = remainingStr.substring( posOfSpace + 1 ); 368 } 369 //else just print 370 else 371 { 372 printStr = remainingStr; 373 remainingStr = ""; 374 } 375 376 instructionsText.append( printStr + "\n" ); 377 378 }// while 379 380 }// for 381 382 }//printInstructions() 383 384 //DO NOT call this directly, go through Sysout 385 public void displayMessage( String messageIn ) 386 { 387 messageText.append( messageIn + "\n" ); 388 System.out.println(messageIn); 389 } 390 391 }// TestDialog class