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