Skip to content

Commit f14eb40

Browse files
author
duke
committed
Backport 0ff2deab5d6b8228ddfed9ae08820d2adf2330c2
1 parent 9da2208 commit f14eb40

File tree

1 file changed

+135
-197
lines changed

1 file changed

+135
-197
lines changed
Lines changed: 135 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,224 +21,162 @@
2121
* questions.
2222
*/
2323

24+
import java.awt.BasicStroke;
25+
import java.awt.Graphics;
26+
import java.awt.Graphics2D;
27+
import java.awt.geom.Rectangle2D;
28+
import java.awt.print.PageFormat;
29+
import java.awt.print.Pageable;
30+
import java.awt.print.Paper;
31+
import java.awt.print.Printable;
32+
import java.awt.print.PrinterException;
33+
import java.awt.print.PrinterJob;
34+
35+
import jtreg.SkippedException;
36+
2437
/*
2538
* @test
2639
* @bug 4355514
40+
* @key printer
41+
* @summary Prints a rectangle to show the imageable area of a
42+
* 12in x 14in custom paper size.
43+
* @library /java/awt/regtesthelpers
44+
* @library /test/lib
45+
* @build PassFailJFrame
46+
* @build jtreg.SkippedException
47+
* @run main/manual CustomPaper 4355514
48+
*/
49+
50+
/*
51+
* @test
2752
* @bug 4385157
2853
* @key printer
2954
* @summary Prints a rectangle to show the imageable area of a
3055
* 12in x 14in custom paper size.
31-
* @run main/manual CustomPaper
56+
* @library /java/awt/regtesthelpers
57+
* @library /test/lib
58+
* @build PassFailJFrame
59+
* @build jtreg.SkippedException
60+
* @run main/manual CustomPaper 4385157
3261
*/
62+
public class CustomPaper implements Pageable, Printable {
3363

34-
import java.awt.*;
35-
import java.awt.print.*;
36-
import java.awt.geom.*;
37-
38-
public class CustomPaper implements Pageable, Printable{
39-
40-
private static double PIXELS_PER_INCH = 72.0;
41-
42-
private PrinterJob printerJob;
43-
private PageFormat pageFormat;
44-
45-
CustomPaper(){
46-
printerJob = PrinterJob.getPrinterJob();
47-
createPageFormat();
48-
}
49-
50-
private void createPageFormat(){
51-
pageFormat = new PageFormat();
52-
Paper p = new Paper();
53-
double width = 12.0*PIXELS_PER_INCH;
54-
double height = 14.0*PIXELS_PER_INCH;
55-
double ix = PIXELS_PER_INCH;
56-
double iy = PIXELS_PER_INCH;
57-
double iwidth = width - 2.0*PIXELS_PER_INCH;
58-
double iheight = height - 2.0*PIXELS_PER_INCH;
59-
p.setSize(width, height);
60-
p.setImageableArea(ix, iy, iwidth, iheight);
61-
pageFormat.setPaper(p);
62-
}
63-
64-
public Printable getPrintable(int index){
65-
return this;
66-
}
67-
68-
public PageFormat getPageFormat(int index){
69-
return pageFormat;
70-
}
71-
72-
public int getNumberOfPages(){
73-
return 1;
74-
}
75-
76-
public void print(){
77-
if(printerJob.printDialog())
78-
{
79-
try{
80-
printerJob.setPageable(this);
81-
printerJob.print();
82-
}catch(Exception e){e.printStackTrace();}
83-
}
64+
private static final double PIXELS_PER_INCH = 72.0;
65+
66+
private final PrinterJob printerJob;
67+
private PageFormat pageFormat;
8468

85-
}
86-
87-
public int print(Graphics g, PageFormat pf, int pageIndex){
88-
if(pageIndex == 0){
89-
Graphics2D g2 = (Graphics2D)g;
90-
Rectangle2D r = new Rectangle2D.Double(pf.getImageableX(),
91-
pf.getImageableY(),
92-
pf.getImageableWidth(),
93-
pf.getImageableHeight());
94-
g2.setStroke(new BasicStroke(3.0f));
95-
g2.draw(r);
96-
return PAGE_EXISTS;
97-
}else{
98-
return NO_SUCH_PAGE;
69+
CustomPaper() {
70+
printerJob = PrinterJob.getPrinterJob();
71+
createPageFormat();
9972
}
100-
}
101-
102-
public static void main(String[] args){
103-
104-
String[] instructions =
105-
{
106-
"You must have a printer that supports custom paper size of ",
107-
"at least 12 x 14 inches to perform this test. It requires",
108-
"user interaction and you must have a 12 x 14 inch paper available.",
109-
" ",
110-
"To test bug ID 4385157, click OK on print dialog box to print.",
111-
" ",
112-
"To test bug ID 4355514, select the printer in the Print Setup dialog and add a ",
113-
"custom paper size under Printer properties' Paper selection menu. ",
114-
"Set the dimension to width=12 inches and height=14 inches.",
115-
"Select this custom paper size before proceeding to print.",
116-
" ",
117-
"Visual inspection of the one-page printout is needed. A passing",
118-
"test will print a rectangle of the imageable area which is approximately",
119-
"10 x 12 inches.",
120-
};
121-
Sysout.createDialog( );
122-
Sysout.printInstructions( instructions );
12373

124-
CustomPaper pt = new CustomPaper();
125-
pt.print();
126-
//System.exit (0);
127-
}
74+
private void createPageFormat() {
75+
pageFormat = new PageFormat();
76+
Paper p = new Paper();
77+
double width = 12.0 * PIXELS_PER_INCH;
78+
double height = 14.0 * PIXELS_PER_INCH;
79+
double iwidth = width - 2.0 * PIXELS_PER_INCH;
80+
double iheight = height - 2.0 * PIXELS_PER_INCH;
81+
p.setSize(width, height);
82+
p.setImageableArea(PIXELS_PER_INCH, PIXELS_PER_INCH, iwidth, iheight);
83+
pageFormat.setPaper(p);
84+
}
12885

129-
}
86+
@Override
87+
public Printable getPrintable(int index) {
88+
return this;
89+
}
13090

91+
@Override
92+
public PageFormat getPageFormat(int index) {
93+
return pageFormat;
94+
}
13195

132-
class Sysout {
133-
private static TestDialog dialog;
96+
@Override
97+
public int getNumberOfPages() {
98+
return 1;
99+
}
134100

135-
public static void createDialogWithInstructions( String[] instructions )
136-
{
137-
dialog = new TestDialog( new Frame(), "Instructions" );
138-
dialog.printInstructions( instructions );
139-
dialog.show();
140-
println( "Any messages for the tester will display here." );
101+
private void print() throws PrinterException {
102+
if (printerJob.printDialog()) {
103+
printerJob.setPageable(this);
104+
printerJob.print();
105+
} else {
106+
PassFailJFrame.forceFail("Printing canceled by user");
107+
}
141108
}
142109

143-
public static void createDialog( )
144-
{
145-
dialog = new TestDialog( new Frame(), "Instructions" );
146-
String[] defInstr = { "Instructions will appear here. ", "" } ;
147-
dialog.printInstructions( defInstr );
148-
dialog.show();
149-
println( "Any messages for the tester will display here." );
110+
@Override
111+
public int print(Graphics g, PageFormat pf, int pageIndex) {
112+
if (pageIndex == 0) {
113+
Graphics2D g2 = (Graphics2D) g;
114+
Rectangle2D r = new Rectangle2D.Double(pf.getImageableX(),
115+
pf.getImageableY(),
116+
pf.getImageableWidth(),
117+
pf.getImageableHeight());
118+
g2.setStroke(new BasicStroke(3.0f));
119+
g2.draw(r);
120+
return PAGE_EXISTS;
121+
} else {
122+
return NO_SUCH_PAGE;
123+
}
150124
}
151125

126+
private static final String TOP = """
127+
You must have a printer that supports custom paper size of
128+
at least 12 x 14 inches to perform this test. It requires
129+
user interaction and you must have a 12 x 14 inch paper available.
152130
153-
public static void printInstructions( String[] instructions )
154-
{
155-
dialog.printInstructions( instructions );
156-
}
131+
""";
157132

133+
private static final String BOTTOM = """
158134
159-
public static void println( String messageIn )
160-
{
161-
dialog.displayMessage( messageIn );
162-
}
135+
Visual inspection of the one-page printout is needed. A passing
136+
test will print a rectangle of the imageable area which is
137+
approximately 10 x 12 inches.
138+
""";
163139

164-
}// Sysout class
165-
166-
/**
167-
This is part of the standard test machinery. It provides a place for the
168-
test instructions to be displayed, and a place for interactive messages
169-
to the user to be displayed.
170-
To have the test instructions displayed, see Sysout.
171-
To have a message to the user be displayed, see Sysout.
172-
Do not call anything in this dialog directly.
173-
*/
174-
class TestDialog extends Dialog {
175-
176-
TextArea instructionsText;
177-
TextArea messageText;
178-
int maxStringLength = 80;
179-
180-
//DO NOT call this directly, go through Sysout
181-
public TestDialog( Frame frame, String name )
182-
{
183-
super( frame, name );
184-
int scrollBoth = TextArea.SCROLLBARS_BOTH;
185-
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
186-
add( "North", instructionsText );
187-
188-
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
189-
add("Center", messageText);
190-
191-
pack();
192-
193-
show();
194-
}// TestDialog()
195-
196-
//DO NOT call this directly, go through Sysout
197-
public void printInstructions( String[] instructions )
198-
{
199-
//Clear out any current instructions
200-
instructionsText.setText( "" );
201-
202-
//Go down array of instruction strings
203-
204-
String printStr, remainingStr;
205-
for( int i=0; i < instructions.length; i++ )
206-
{
207-
//chop up each into pieces maxSringLength long
208-
remainingStr = instructions[ i ];
209-
while( remainingStr.length() > 0 )
210-
{
211-
//if longer than max then chop off first max chars to print
212-
if( remainingStr.length() >= maxStringLength )
213-
{
214-
//Try to chop on a word boundary
215-
int posOfSpace = remainingStr.
216-
lastIndexOf( ' ', maxStringLength - 1 );
217-
218-
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
219-
220-
printStr = remainingStr.substring( 0, posOfSpace + 1 );
221-
remainingStr = remainingStr.substring( posOfSpace + 1 );
222-
}
223-
//else just print
224-
else
225-
{
226-
printStr = remainingStr;
227-
remainingStr = "";
228-
}
229-
230-
instructionsText.append( printStr + "\n" );
231-
232-
}// while
233-
234-
}// for
235-
236-
}//printInstructions()
237-
238-
//DO NOT call this directly, go through Sysout
239-
public void displayMessage( String messageIn )
240-
{
241-
messageText.append( messageIn + "\n" );
242-
}
140+
private static final String INSTRUCTIONS_4355514 = """
141+
Select the printer in the Print Setup dialog and add a custom
142+
paper size under 'Printer properties' Paper selection menu.
143+
Set the dimension to width=12 inches and height=14 inches.
144+
Select this custom paper size before proceeding to print.
145+
""";
146+
147+
private static final String INSTRUCTIONS_4385157 = """
148+
Click OK on print dialog box to print.
149+
""";
150+
151+
public static void main(String[] args) throws Exception {
152+
String instructions;
153+
154+
if (PrinterJob.lookupPrintServices().length == 0) {
155+
throw new SkippedException("Printer not configured or available."
156+
+ " Test cannot continue.");
157+
}
243158

244-
}// TestDialog class
159+
if (args.length != 1) {
160+
throw new RuntimeException("Select a test by passing 4355514 or 4385157");
161+
}
162+
163+
instructions = switch (args[0]) {
164+
case "4355514" -> TOP + INSTRUCTIONS_4355514 + BOTTOM;
165+
case "4385157" -> TOP + INSTRUCTIONS_4385157 + BOTTOM;
166+
default -> throw new RuntimeException("Unknown bugid " + args[0] + "."
167+
+ "Valid values: 4355514 or 4385157");
168+
};
169+
170+
PassFailJFrame passFailJFrame = new PassFailJFrame.Builder()
171+
.title("CustomPaper Test Instructions")
172+
.instructions(instructions)
173+
.testTimeOut(5)
174+
.rows((int) instructions.lines().count() + 1)
175+
.columns(45)
176+
.build();
177+
178+
CustomPaper pt = new CustomPaper();
179+
pt.print();
180+
passFailJFrame.awaitAndCheck();
181+
}
182+
}

0 commit comments

Comments
 (0)