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 6360339
27  * @summary Test for fp error in paper size calculations.
28  * @run main/manual PaperSizeError
29  */
30 
31 import java.awt.print.*;
32 import javax.print.*;
33 import javax.print.attribute.*;
34 import javax.print.attribute.standard.*;
35 
36 public class PaperSizeError {
37 
38   static String[] instructions = {
39      "This test assumes and requires that you have a printer installed",
40      "Two page dialogs will appear. You must press 'OK' on both.",
41      "If the test fails, it will throw an Exception.",
42      ""
43   };
44 
main(String[] args)45   public static void main(String[] args) {
46 
47       for (int i=0;i<instructions.length;i++) {
48          System.out.println(instructions[i]);
49       }
50 
51       /* First find out if we have a valid test environment:
52        * ie print service exists and supports A4.
53        */
54       PrinterJob job = PrinterJob.getPrinterJob();
55       PrintService service = job.getPrintService();
56       if (service == null ||
57           !service.isAttributeValueSupported(MediaSizeName.ISO_A4,
58                                              null, null)) {
59          return;
60       }
61 
62       // Create A4 sized PageFormat.
63       MediaSize a4 = MediaSize.ISO.A4;
64       double a4w = Math.rint((a4.getX(1) * 72.0) / Size2DSyntax.INCH);
65       double a4h = Math.rint((a4.getY(1) * 72.0) / Size2DSyntax.INCH);
66       System.out.println("Units = 1/72\" size=" + a4w + "x" + a4h);
67       Paper paper = new Paper();
68       paper.setSize(a4w, a4h);
69       PageFormat pf = new PageFormat();
70       pf.setPaper(paper);
71 
72       // Test dialog with PF argument
73       PageFormat newPF = job.pageDialog(pf);
74       if (newPF == null) {
75           return; // user cancelled the dialog (and hence the test).
76       } else {
77           verifyPaper(newPF, a4w, a4h);
78       }
79 
80       PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
81       aset.add(OrientationRequested.PORTRAIT);
82       aset.add(MediaSizeName.ISO_A4);
83 
84       // Test dialog with AttributeSet argument
85       newPF = job.pageDialog(aset);
86       if (newPF == null) {
87           return; // user cancelled the dialog (and hence the test).
88       } else {
89           verifyPaper(newPF, a4w, a4h);
90       }
91   }
92 
verifyPaper(PageFormat pf , double a4w, double a4h)93   static void verifyPaper(PageFormat pf , double a4w, double a4h) {
94 
95       double dw1 = pf.getWidth();
96       double dh1 = pf.getHeight();
97       float fwMM = (float)((dw1 * 25.4) / 72.0);
98       float fhMM = (float)((dh1 * 25.4) / 72.0);
99       MediaSizeName msn = MediaSize.findMedia(fwMM, fhMM, Size2DSyntax.MM);
100       System.out.println("Units = 1/72\" new size=" + dw1 + "x" + dh1);
101       System.out.println("Units = MM new size=" + fwMM + "x" + fhMM);
102       System.out.println("Media = " + msn);
103       if (a4w != Math.rint(dw1) || a4h != Math.rint(dh1)) {
104          System.out.println("Got " + Math.rint(dw1) + "x" + Math.rint(dh1) +
105                             ". Expected " + a4w + "x" + a4h);
106          throw new RuntimeException("Size is not close enough to A4 size");
107       }
108       // So far as I know, there's no other standard size that is A4.
109       // So we should match the right one.
110       if (msn != MediaSizeName.ISO_A4) {
111           throw new RuntimeException("MediaSizeName is not A4: " + msn);
112       }
113   }
114 }
115