1/* 2 * Copyright (c) 2017, 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 8167102 26 @summary PrintRequestAttributeSet breaks page size set using PageFormat 27 @ignore Exclude the test until 8167102 is resolved by a new reassessed fix 28 @run main/manual WrongPaperPrintingTest 29 */ 30 31import java.awt.BorderLayout; 32import java.awt.Color; 33import java.awt.FlowLayout; 34import java.awt.Graphics; 35import java.awt.event.WindowAdapter; 36import java.awt.event.WindowEvent; 37import java.awt.print.PageFormat; 38import java.awt.print.Paper; 39import java.awt.print.Printable; 40import java.awt.print.PrinterException; 41import java.awt.print.PrinterJob; 42import java.util.concurrent.CountDownLatch; 43import java.util.concurrent.TimeUnit; 44import javax.print.attribute.HashPrintRequestAttributeSet; 45import javax.print.attribute.PrintRequestAttributeSet; 46import javax.print.attribute.Size2DSyntax; 47import javax.print.attribute.standard.Chromaticity; 48import javax.print.attribute.standard.MediaSize; 49import javax.print.attribute.standard.MediaSizeName; 50import javax.swing.JButton; 51import javax.swing.JDialog; 52import javax.swing.JLabel; 53import javax.swing.JPanel; 54import javax.swing.JTextArea; 55import javax.swing.SwingUtilities; 56import javax.swing.Timer; 57import javax.swing.WindowConstants; 58 59public class WrongPaperPrintingTest implements Printable { 60 private static final CountDownLatch testEndedSignal = new CountDownLatch(1); 61 private static final int testTimeout = 300000; 62 private static volatile String testFailureMsg; 63 private static volatile boolean testPassed; 64 private static volatile boolean testFinished; 65 66 public static void main(String[] args) { 67 SwingUtilities.invokeLater(() -> createAndShowTestDialog()); 68 69 try { 70 if (!testEndedSignal.await(testTimeout, TimeUnit.MILLISECONDS)) { 71 throw new RuntimeException(String.format( 72 "Test timeout '%d ms' elapsed.", testTimeout)); 73 } 74 if (!testPassed) { 75 String failureMsg = testFailureMsg; 76 if ((failureMsg != null) && (!failureMsg.trim().isEmpty())) { 77 throw new RuntimeException(failureMsg); 78 } else { 79 throw new RuntimeException("Test failed."); 80 } 81 } 82 } catch (InterruptedException ie) { 83 throw new RuntimeException(ie); 84 } finally { 85 testFinished = true; 86 } 87 } 88 89 private static void doTest() { 90 PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 91 aset.add(Chromaticity.MONOCHROME); 92 93 MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5); 94 float[] size = isoA5Size.getSize(Size2DSyntax.INCH); 95 Paper paper = new Paper(); 96 paper.setSize(size[0] * 72.0, size[1] * 72.0); 97 paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0); 98 PageFormat pf = new PageFormat(); 99 pf.setPaper(paper); 100 101 PrinterJob job = PrinterJob.getPrinterJob(); 102 job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf)); 103 if (job.printDialog()) { 104 try { 105 job.print(aset); 106 } catch (PrinterException pe) { 107 throw new RuntimeException(pe); 108 } 109 } 110 } 111 112 private static void pass() { 113 testPassed = true; 114 testEndedSignal.countDown(); 115 } 116 117 private static void fail(String failureMsg) { 118 testFailureMsg = failureMsg; 119 testPassed = false; 120 testEndedSignal.countDown(); 121 } 122 123 private static String convertMillisToTimeStr(int millis) { 124 if (millis < 0) { 125 return "00:00:00"; 126 } 127 int hours = millis / 3600000; 128 int minutes = (millis - hours * 3600000) / 60000; 129 int seconds = (millis - hours * 3600000 - minutes * 60000) / 1000; 130 return String.format("%02d:%02d:%02d", hours, minutes, seconds); 131 } 132 133 private static void createAndShowTestDialog() { 134 String description = 135 " To run this test it is required to have a virtual PDF\r\n" + 136 " printer or any other printer supporting A5 paper size.\r\n" + 137 "\r\n" + 138 " 1. Verify that NOT A5 paper size is set as default for the\r\n" + 139 " printer to be used.\r\n" + 140 " 2. Click on \"Start Test\" button.\r\n" + 141 " 3. In the shown print dialog select the printer and click\r\n" + 142 " on \"Print\" button.\r\n" + 143 " 4. Verify that a page with a drawn rectangle is printed on\r\n" + 144 " a paper of A5 size which is (5.8 x 8.3 in) or\r\n" + 145 " (148 x 210 mm).\r\n" + 146 "\r\n" + 147 " If the printed page size is correct, click on \"PASS\"\r\n" + 148 " button, otherwise click on \"FAIL\" button."; 149 150 final JDialog dialog = new JDialog(); 151 dialog.setTitle("WrongPaperPrintingTest"); 152 dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 153 dialog.addWindowListener(new WindowAdapter() { 154 @Override 155 public void windowClosing(WindowEvent e) { 156 dialog.dispose(); 157 fail("Main dialog was closed."); 158 } 159 }); 160 161 final JLabel testTimeoutLabel = new JLabel(String.format( 162 "Test timeout: %s", convertMillisToTimeStr(testTimeout))); 163 final long startTime = System.currentTimeMillis(); 164 final Timer timer = new Timer(0, null); 165 timer.setDelay(1000); 166 timer.addActionListener((e) -> { 167 int leftTime = testTimeout - (int)(System.currentTimeMillis() - startTime); 168 if ((leftTime < 0) || testFinished) { 169 timer.stop(); 170 dialog.dispose(); 171 } 172 testTimeoutLabel.setText(String.format( 173 "Test timeout: %s", convertMillisToTimeStr(leftTime))); 174 }); 175 timer.start(); 176 177 JTextArea textArea = new JTextArea(description); 178 textArea.setEditable(false); 179 180 final JButton testButton = new JButton("Start Test"); 181 final JButton passButton = new JButton("PASS"); 182 final JButton failButton = new JButton("FAIL"); 183 testButton.addActionListener((e) -> { 184 testButton.setEnabled(false); 185 new Thread(() -> { 186 try { 187 doTest(); 188 189 SwingUtilities.invokeLater(() -> { 190 passButton.setEnabled(true); 191 failButton.setEnabled(true); 192 }); 193 } catch (Throwable t) { 194 t.printStackTrace(); 195 dialog.dispose(); 196 fail("Exception occurred in a thread executing the test."); 197 } 198 }).start(); 199 }); 200 passButton.setEnabled(false); 201 passButton.addActionListener((e) -> { 202 dialog.dispose(); 203 pass(); 204 }); 205 failButton.setEnabled(false); 206 failButton.addActionListener((e) -> { 207 dialog.dispose(); 208 fail("Size of a printed page is wrong."); 209 }); 210 211 JPanel mainPanel = new JPanel(new BorderLayout()); 212 JPanel labelPanel = new JPanel(new FlowLayout()); 213 labelPanel.add(testTimeoutLabel); 214 mainPanel.add(labelPanel, BorderLayout.NORTH); 215 mainPanel.add(textArea, BorderLayout.CENTER); 216 JPanel buttonPanel = new JPanel(new FlowLayout()); 217 buttonPanel.add(testButton); 218 buttonPanel.add(passButton); 219 buttonPanel.add(failButton); 220 mainPanel.add(buttonPanel, BorderLayout.SOUTH); 221 dialog.add(mainPanel); 222 223 dialog.pack(); 224 dialog.setVisible(true); 225 } 226 227 @Override 228 public int print(Graphics g, PageFormat pf, int pageIndex) 229 throws PrinterException { 230 if (pageIndex == 0) { 231 g.setColor(Color.RED); 232 g.drawRect((int)pf.getImageableX(), (int)pf.getImageableY(), 233 (int)pf.getImageableWidth(), (int)pf.getImageableHeight()); 234 return Printable.PAGE_EXISTS; 235 } else { 236 return Printable.NO_SUCH_PAGE; 237 } 238 } 239} 240