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 54 // Test UI creation 55 TestUI test = new TestUI(latch); 56 SwingUtilities.invokeAndWait(new Runnable() { 57 @Override 58 public void run() { 59 try { 60 test.createUI(); 61 } catch (Exception e) { 62 63 } 64 } 65 }); 66 67 // PDialogTest creation 68 PDialogTest pDialogTest = new PDialogTest(); 69 // Start a thread for the page setup dialog test 70 Thread T1 = new Thread(new Runnable() { 71 @Override 72 public void run() { 73 try { 74 pDialogTest.do_test(); 75 } catch (PrinterException e) { 76 77 } 78 } 79 }); 80 T1.start(); 81 82 boolean status = latch.await(1, TimeUnit.MINUTES); 83 if (!status) { 84 throw new RuntimeException("Test timed out."); 85 } 86 87 if (test.testResult == false) { 88 throw new RuntimeException("Test Failed."); 89 } 90 91 SwingUtilities.invokeAndWait(() -> { 92 test.disposeUI(); 93 }); 94 } 95 96 public static void do_test() throws PrinterException { 97 // Obtain default page format from the default printer job service 98 PageFormat pageFormat = java.awt.print.PrinterJob.getPrinterJob() 99 .defaultPage(); 100 Paper paper = pageFormat.getPaper(); 101 102 // Set paper attributes with 8.5 inch by 11 inches paper. 103 paper.setSize(8.5 * 72, 11 * 72); 104 pageFormat.setPaper(paper); 105 106 createNewPrintPageSetup(pageFormat); 107 108 setValuesForPrintPageSetup(pageFormat, 2); 109 110 createNewPrintPageSetup(pageFormat); 111 112 setValuesForPrintPageSetup(pageFormat, 3); 113 114 createNewPrintPageSetup(pageFormat); 115 } 116 117 private static void createNewPrintPageSetup(PageFormat pageFormat) 118 throws PrinterException { 119 120 // Display printer default setup dialog 121 java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat); 122 } 123 124 private static void setValuesForPrintPageSetup(PageFormat pageFormat, int 125 marginValue) throws PrinterException { 126 Paper paper = pageFormat.getPaper(); 127 128 // Reset paper attributes to 8.5 inch by 11 inches paper. 129 paper.setSize(8.5 * 72, 11 * 72); 130 double paperHeight = paper.getHeight(); 131 double paperWidth = paper.getWidth(); 132 paper.setImageableArea(72 * marginValue, 72 * marginValue, 133 paperWidth - (72 * 2 * marginValue), 134 paperHeight - (72 * 2 * marginValue)); 135 pageFormat.setPaper(paper); 136 137 java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat); 138 } 139 } 140 141 class TestUI { 142 private static JFrame mainFrame; 143 private static JPanel mainControlPanel; 144 145 private static JTextArea instructionTextArea; 146 147 private static JPanel resultButtonPanel; 148 private static JButton passButton; 149 private static JButton failButton; 150 151 private static JPanel testPanel; 152 private static JButton testButton; 153 private static JLabel buttonPressCountLabel; 154 155 private static GridBagLayout layout; 156 private final CountDownLatch latch; 157 public boolean testResult = false; 158 159 public TestUI(CountDownLatch latch) throws Exception { 160 this.latch = latch; 161 } 162 163 public final void createUI() { 164 mainFrame = new JFrame("PDialogTest"); 165 166 layout = new GridBagLayout(); 167 mainControlPanel = new JPanel(layout); 168 resultButtonPanel = new JPanel(layout); 169 testPanel = new JPanel(layout); 170 171 GridBagConstraints gbc = new GridBagConstraints(); 172 // Create Test instructions 173 String instructions 174 = "This test displays the print setup dialog with margins \n" 175 + "displayed as inches(Use equivalent conversion if different)\n" 176 + "\n" 177 + "Step 1: Verify that margins displays default values for \n" + 178 " left, right, top and bottom margins, then click OK" + 179 ".\n" 180 + "Step 2: Verify that margins displays as 2 for left, right,\n" 181 + " top and bottom margins, then click OK (A new value \n" 182 + " 2 has been set by setting a new imagable area).\n" 183 + "Step 3: Verify that margins dipslays as 2 for left, right,\n" 184 + " top and bottom margins, then click OK (The newly set\n" 185 + " imageable area retains even after closing in the\n" 186 + " previous step).\n" 187 + "Step 4: Verify that margins displays as 3 for left, right,\n" 188 + " top and bottom margins, then click OK (A new value \n" 189 + " 3 has been set by setting a new imagable area).\n" 190 + "Step 5: Verify that margins dipslays as 3 for left, right,\n" 191 + " top and bottom margins, then click OK (The newly set\n" 192 + " imageable area retains even after closing in the\n" 193 + " previous step).\n" 194 + "Click on 'pass' if successful else click on 'fail'\n"; 195 196 instructionTextArea = new JTextArea(); 197 instructionTextArea.setText(instructions); 198 instructionTextArea.setEditable(false); 199 instructionTextArea.setBorder(BorderFactory. 200 createTitledBorder("Test Instructions")); 201 202 gbc.gridx = 0; 203 gbc.gridy = 0; 204 gbc.fill = GridBagConstraints.HORIZONTAL; 205 mainControlPanel.add(instructionTextArea, gbc); 206 207 gbc.gridx = 0; 208 gbc.gridy = 1; 209 testPanel.add(Box.createVerticalStrut(50)); 210 211 mainControlPanel.add(testPanel); 212 213 // Create resultButtonPanel with Pass, Fail buttons 214 passButton = new JButton("Pass"); 215 passButton.setActionCommand("Pass"); 216 passButton.addActionListener((ActionEvent e) -> { 217 System.out.println("Pass Button pressed!"); 218 testResult = true; 219 latch.countDown(); 220 disposeUI(); 221 }); 222 223 failButton = new JButton("Fail"); 224 failButton.setActionCommand("Fail"); 225 failButton.addActionListener((ActionEvent e) -> { 226 System.out.println("Fail Button pressed!"); 227 testResult = false; 228 latch.countDown(); 229 disposeUI(); 230 }); 231 232 gbc.gridx = 0; 233 gbc.gridy = 0; 234 resultButtonPanel.add(passButton, gbc); 235 236 gbc.gridx = 1; 237 gbc.gridy = 0; 238 resultButtonPanel.add(failButton, gbc); 239 240 gbc.gridx = 0; 241 gbc.gridy = 1; 242 mainControlPanel.add(resultButtonPanel, gbc); 243 244 mainFrame.add(mainControlPanel); 245 mainFrame.pack(); 246 mainFrame.setVisible(true); 247 } 248 249 public void disposeUI() { 250 mainFrame.dispose(); 251 } 252 }