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