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