1 /*
2  * Copyright (c) 2016, 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  * @test
25  * @bug 6789262
26  * @key printer
27  * @summary  Verifies if getPageFormat returns correct mediaprintable value
28  * @run main TestPgfmtSetMPA
29  */
30 import java.awt.print.PageFormat;
31 import java.awt.print.PrinterJob;
32 import javax.print.attribute.HashPrintRequestAttributeSet;
33 import javax.print.attribute.PrintRequestAttributeSet;
34 import javax.print.attribute.standard.MediaPrintableArea;
35 
36 public class TestPgfmtSetMPA {
37 
main(String args[])38     public static void main(String args[]) {
39         PrinterJob job;
40 
41         job = PrinterJob.getPrinterJob();
42         if (job.getPrintService() == null) {
43             System.out.println("No printers. Test cannot continue");
44             return;
45         }
46 
47         PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
48 
49         // Here you could see the PageFormat with an empty PrintRequestAttributeSet
50         PageFormat pf2 = job.getPageFormat(pras);
51         System.out.println((pf2.getImageableX() / 72f) + " "
52                 + (pf2.getImageableY() / 72f) + " "
53                 + (pf2.getImageableWidth() / 72f) + " "
54                 + (pf2.getImageableHeight() / 72f)
55         );
56 
57         // Set left margin to 2.0
58         pras.add(new MediaPrintableArea(2.0f,
59                 (float)(pf2.getImageableY() / 72f),
60                 (float) ((pf2.getImageableWidth() / 72f) - 1.0f),
61                 (float) (pf2.getImageableHeight() / 72f),
62                 MediaPrintableArea.INCH));
63 
64         pf2 = job.getPageFormat(pras);
65         System.out.println((pf2.getImageableX() / 72f) + " "
66                 + (pf2.getImageableY() / 72f) + " "
67                 + (pf2.getImageableWidth() / 72f) + " "
68                 + (pf2.getImageableHeight() / 72f)
69         );
70 
71         // check if returned left margin of imageable area is 2.0 as set earlier
72         if (pf2.getImageableX() / 72f != 2.0f) {
73             throw new RuntimeException("getPageFormat doesn't apply specified "
74                     + "MediaPrintableArea attribute");
75         }
76     }
77 }
78