1 /* 2 * Copyright (c) 2007, 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 /** 25 * @test 26 * @bug 4855801 6949753 27 * @requires (os.family == "Windows") | (os.family == "Linux") 28 * @summary Changing margins in the page format does not have any effect 29 * @run main/manual PDialogTest 30 */ 31 32 import java.awt.GridBagConstraints; 33 import java.awt.GridBagLayout; 34 import java.awt.event.ActionEvent; 35 import java.awt.print.PageFormat; 36 import java.awt.print.Paper; 37 import java.awt.print.PrinterException; 38 import java.util.concurrent.CountDownLatch; 39 import java.util.concurrent.TimeUnit; 40 import javax.swing.BorderFactory; 41 import javax.swing.Box; 42 import javax.swing.JButton; 43 import javax.swing.JFrame; 44 import javax.swing.JLabel; 45 import javax.swing.JPanel; 46 import javax.swing.JTextArea; 47 import javax.swing.SwingUtilities; 48 49 public class PDialogTest 50 { 51 public static void main(String args[]) throws Exception { 52 final CountDownLatch latch = new CountDownLatch(1); 53 TestUI test = new TestUI(latch); 54 55 SwingUtilities.invokeAndWait(() -> { 56 try { 57 test.createUI(); 58 } catch (Exception ex) { 59 throw new RuntimeException("Exception while creating test UI"); 60 } 61 }); 62 63 PageFormat pageFormat = new PageFormat(); 64 65 try { 66 createNewPrintPageSetup(pageFormat); 67 68 setValuesForPrintPageSetup(pageFormat, 2); 69 70 createNewPrintPageSetup(pageFormat); 71 72 setValuesForPrintPageSetup(pageFormat, 3); 73 74 createNewPrintPageSetup(pageFormat); 75 } catch (PrinterException e) { 76 throw new RuntimeException(e); 77 } 78 79 boolean status = latch.await(5, TimeUnit.MINUTES); 80 81 if (!status) { 82 System.out.println("Test timed out."); 83 } 84 85 SwingUtilities.invokeAndWait(() -> { 86 try { 87 test.disposeUI(); 88 } catch (Exception ex) { 89 throw new RuntimeException("Exception while disposing test UI"); 90 } 91 }); 92 93 if (test.testResult == false) { 94 throw new RuntimeException("Test Failed."); 95 } 96 } 97 98 public static void createNewPrintPageSetup(PageFormat pageFormat) 99 throws PrinterException { 100 101 // Display printer default setup dialog 102 java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat); 103 } 104 105 public static void setValuesForPrintPageSetup(PageFormat pageFormat, 106 int marginValue) throws PrinterException { 107 Paper paper = new Paper(); 108 109 double paperHeight = paper.getHeight(); 110 double paperWidth = paper.getWidth(); 111 double paperX = paper.getImageableX(); 112 double paperY = paper.getImageableY(); 113 paper.setImageableArea(paperX * marginValue, paperY * marginValue, 114 paperWidth - (paperX * 2 * marginValue), 115 paperHeight - (paperY * 2 * marginValue)); 116 pageFormat.setPaper(paper); 117 118 java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat); 119 } 120 } 121 122 class TestUI { 123 124 private static JFrame mainFrame; 125 private static JPanel mainControlPanel; 126 127 private static JTextArea instructionTextArea; 128 129 private static JPanel resultButtonPanel; 130 private static JButton passButton; 131 private static JButton failButton; 132 133 private static JPanel testPanel; 134 private static JButton testButton; 135 private static JLabel buttonPressCountLabel; 136 137 private static GridBagLayout layout; 138 private final CountDownLatch latch; 139 public boolean testResult = false; 140 141 public TestUI(CountDownLatch latch) throws Exception { 142 this.latch = latch; 143 } 144 145 public final void createUI() throws Exception { 146 147 mainFrame = new JFrame("PDialogTest"); 148 149 layout = new GridBagLayout(); 150 mainControlPanel = new JPanel(layout); 151 resultButtonPanel = new JPanel(layout); 152 testPanel = new JPanel(layout); 153 154 GridBagConstraints gbc = new GridBagConstraints(); 155 156 // Create Test instructions 157 String instructions 158 = "This test displays the print setup dialog with margins \n" 159 + "displayed as inches(Use equivalent conversion if different)\n" 160 + "\n" 161 + "Step 1: Verify that margins displays as 1 for left, right,\n" 162 + " top and bottom margins, then click OK.\n" 163 + "Step 2: Verify that margins displays as 2 for left, right,\n" 164 + " top and bottom margins, then click OK (A new value \n" 165 + " 2 has been set by setting a new imagable area).\n" 166 + "Step 3: Verify that margins dipslays as 2 for left, right,\n" 167 + " top and bottom margins, then click OK (The newly set\n" 168 + " imageable area retains even after closing in the\n" 169 + " previous step).\n" 170 + "Step 4: Verify that margins displays as 3 for left, right,\n" 171 + " top and bottom margins, then click OK (A new value \n" 172 + " 3 has been set by setting a new imagable area).\n" 173 + "Step 5: Verify that margins dipslays as 3 for left, right,\n" 174 + " top and bottom margins, then click OK (The newly set\n" 175 + " imageable area retains even after closing in the\n" 176 + " previous step).\n" 177 + "Click on 'pass' if successful else click on 'fail'\n"; 178 179 instructionTextArea = new JTextArea(); 180 instructionTextArea.setText(instructions); 181 instructionTextArea.setEditable(false); 182 instructionTextArea.setBorder(BorderFactory. 183 createTitledBorder("Test Instructions")); 184 185 gbc.gridx = 0; 186 gbc.gridy = 0; 187 gbc.fill = GridBagConstraints.HORIZONTAL; 188 mainControlPanel.add(instructionTextArea, gbc); 189 190 gbc.gridx = 0; 191 gbc.gridy = 1; 192 testPanel.add(Box.createVerticalStrut(50)); 193 194 mainControlPanel.add(testPanel); 195 196 // Create resultButtonPanel with Pass, Fail buttons 197 passButton = new JButton("Pass"); 198 passButton.setActionCommand("Pass"); 199 passButton.addActionListener((ActionEvent e) -> { 200 System.out.println("Pass Button pressed!"); 201 testResult = true; 202 latch.countDown(); 203 204 }); 205 206 failButton = new JButton("Fail"); 207 failButton.setActionCommand("Fail"); 208 failButton.addActionListener((ActionEvent e) -> { 209 System.out.println("Fail Button pressed!"); 210 testResult = false; 211 latch.countDown(); 212 }); 213 214 gbc.gridx = 0; 215 gbc.gridy = 0; 216 resultButtonPanel.add(passButton, gbc); 217 gbc.gridx = 1; 218 gbc.gridy = 0; 219 resultButtonPanel.add(failButton, gbc); 220 221 gbc.gridx = 0; 222 gbc.gridy = 1; 223 mainControlPanel.add(resultButtonPanel, gbc); 224 225 mainFrame.add(mainControlPanel); 226 227 mainFrame.pack(); 228 mainFrame.setVisible(true); 229 } 230 231 public void disposeUI() { 232 mainFrame.setVisible(false); 233 mainFrame.dispose(); 234 } 235 }