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