author | prr |
Fri, 24 May 2019 09:10:10 -0700 | |
changeset 55180 | f13dba72a5ea |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
46151
5fa789776f7d
8185500: [TESTBUG] Add keywords headful/printer in java/awt and javax tests.
goetz
parents:
42209
diff
changeset
|
2 |
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
55180
f13dba72a5ea
8224705: Tests that need to be problem-listed or have printer resources
prr
parents:
47216
diff
changeset
|
26 |
* @key headful printer |
42209
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
27 |
* @bug 6543815 6601097 8160888 |
2 | 28 |
* @summary Image should be sent to printer, no exceptions thrown. |
39528
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
29 |
* The 3 printouts should have a rectangle which is the minimum |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
30 |
* possible margins ie, the margins should be hardware margins |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
31 |
* and not java default 1 inch margins. |
42209
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
32 |
* @run main Margins |
2 | 33 |
*/ |
34 |
||
39528
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
35 |
import java.awt.print.PrinterJob; |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
36 |
import java.awt.print.Printable; |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
37 |
import java.awt.print.PageFormat; |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
38 |
import java.awt.print.Paper; |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
39 |
import java.awt.print.PrinterException; |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
40 |
import java.awt.Graphics; |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
41 |
import java.awt.Graphics2D; |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
42 |
import java.awt.Color; |
42209
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
43 |
import java.awt.Robot; |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
44 |
import java.awt.event.KeyEvent; |
2 | 45 |
|
46 |
public class Margins implements Printable { |
|
47 |
||
42209
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
48 |
public static void main(String args[]) throws Exception { |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
49 |
Robot robot = new Robot(); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
50 |
Thread t = new Thread (() -> { |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
51 |
robot.waitForIdle(); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
52 |
robot.delay(5000); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
53 |
robot.keyPress(KeyEvent.VK_ENTER); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
54 |
robot.keyRelease(KeyEvent.VK_ENTER); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
55 |
robot.waitForIdle(); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
56 |
robot.delay(5000); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
57 |
robot.keyPress(KeyEvent.VK_ENTER); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
58 |
robot.keyRelease(KeyEvent.VK_ENTER); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
59 |
robot.waitForIdle(); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
60 |
robot.delay(5000); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
61 |
robot.keyPress(KeyEvent.VK_ENTER); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
62 |
robot.keyRelease(KeyEvent.VK_ENTER); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
63 |
}); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
64 |
|
2 | 65 |
PrinterJob job = PrinterJob.getPrinterJob(); |
66 |
PageFormat pageFormat = job.defaultPage(); |
|
67 |
Paper paper = pageFormat.getPaper(); |
|
68 |
double wid = paper.getWidth(); |
|
69 |
double hgt = paper.getHeight(); |
|
70 |
paper.setImageableArea(0, -10, wid, hgt); |
|
42209
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
71 |
t.start(); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
72 |
|
2 | 73 |
pageFormat = job.pageDialog(pageFormat); |
74 |
pageFormat.setPaper(paper); |
|
75 |
job.setPrintable(new Margins(), pageFormat); |
|
76 |
try { |
|
77 |
job.print(); |
|
78 |
} catch (PrinterException e) { |
|
79 |
} |
|
80 |
||
42209
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
81 |
paper.setImageableArea(0, 0, wid, hgt + 72); |
2 | 82 |
pageFormat = job.pageDialog(pageFormat); |
83 |
pageFormat.setPaper(paper); |
|
84 |
||
85 |
job.setPrintable(new Margins(), pageFormat); |
|
86 |
try { |
|
87 |
job.print(); |
|
88 |
} catch (PrinterException e) { |
|
89 |
} |
|
39528
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
90 |
|
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
91 |
pageFormat = job.defaultPage(); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
92 |
paper = pageFormat.getPaper(); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
93 |
wid = paper.getWidth(); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
94 |
hgt = paper.getHeight(); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
95 |
|
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
96 |
paper.setImageableArea(0, -10, -wid, hgt); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
97 |
pageFormat = job.pageDialog(pageFormat); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
98 |
pageFormat.setPaper(paper); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
99 |
|
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
100 |
job.setPrintable(new Margins(), pageFormat); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
101 |
try { |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
102 |
job.print(); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
103 |
} catch (PrinterException e) { |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
104 |
} |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
105 |
} |
2 | 106 |
|
107 |
public int print(Graphics g, PageFormat pf, int page) |
|
108 |
throws PrinterException { |
|
109 |
||
110 |
if (page > 0) { |
|
111 |
return NO_SUCH_PAGE; |
|
112 |
} |
|
113 |
int ix = (int)pf.getImageableX(); |
|
114 |
int iy = (int)pf.getImageableY(); |
|
115 |
int iw = (int)pf.getImageableWidth(); |
|
116 |
int ih = (int)pf.getImageableHeight(); |
|
117 |
System.out.println("ix="+ix+" iy="+iy+" iw="+iw+" ih="+ih); |
|
118 |
if ((ix < 0) || (iy < 0)) { |
|
119 |
throw new RuntimeException("Imageable x or y is a negative value."); |
|
120 |
} |
|
121 |
||
39528
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
122 |
|
2 | 123 |
Paper paper = pf.getPaper(); |
42209
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
124 |
int wid = (int)paper.getWidth(); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
125 |
int hgt = (int)paper.getHeight(); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
126 |
System.out.println("wid="+wid+" hgt="+hgt); |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
127 |
/* |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
128 |
* If imageable width/height is -ve, then print was done with 1" margin |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
129 |
* e.g. ix=72 iy=72 iw=451 ih=697 and paper wid=595 |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
130 |
* but with fix, we get print with hardware margin e.g. |
39528
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
131 |
* ix=12, iy=12, iw=571, ih=817 |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
132 |
*/ |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
133 |
if ((wid - iw > 72) || (hgt - ih > 72)) { |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
134 |
throw new RuntimeException("Imageable width or height is negative value"); |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
135 |
} |
2 | 136 |
if ((ix+iw > wid) || (iy+ih > hgt)) { |
39528
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
137 |
throw new RuntimeException("Printable width or height " |
0544281c5bd3
6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents:
5506
diff
changeset
|
138 |
+ "exceeds paper width or height."); |
2 | 139 |
} |
42209
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
140 |
// runtime checking to see if the margins/printable area |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
141 |
// correspond to the entire size of the paper, for now, make it pass |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
142 |
// as for linux, the hwmargin is not taken into account - bug6574279 |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
143 |
if (ix == 0 && iy == 0 && (ix+iw == wid) && (iy+ih == hgt)) { |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
144 |
return PAGE_EXISTS; |
2b05a23bc5ee
8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents:
39528
diff
changeset
|
145 |
} |
2 | 146 |
|
147 |
Graphics2D g2d = (Graphics2D)g; |
|
148 |
g2d.translate(ix, iy); |
|
149 |
g2d.setColor(Color.black); |
|
150 |
g2d.drawRect(1, 1, iw-2, ih-2); |
|
151 |
||
152 |
return PAGE_EXISTS; |
|
153 |
} |
|
154 |
} |