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