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