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         PageFormat pageFormat = new PageFormat();
  98 
  99         createNewPrintPageSetup(pageFormat);
 100 
 101         setValuesForPrintPageSetup(pageFormat, 2);
 102 
 103         createNewPrintPageSetup(pageFormat);
 104 
 105         setValuesForPrintPageSetup(pageFormat, 3);
 106 
 107         createNewPrintPageSetup(pageFormat);
 108     }
 109 
 110     private static void createNewPrintPageSetup(PageFormat pageFormat)
 111             throws PrinterException {
 112 
 113         // Display printer default setup dialog
 114         java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat);
 115     }
 116 
 117     private static void setValuesForPrintPageSetup(PageFormat pageFormat,  int
 118             marginValue) throws PrinterException {
 119         Paper paper = new Paper();
 120 
 121         double paperHeight = paper.getHeight();
 122         double paperWidth = paper.getWidth();
 123         double paperX = paper.getImageableX();
 124         double paperY = paper.getImageableY();
 125         paper.setImageableArea(paperX * marginValue, paperY * marginValue,
 126                 paperWidth - (paperX * 2 * marginValue),
 127                 paperHeight - (paperY * 2 * marginValue));
 128         pageFormat.setPaper(paper);
 129 
 130         java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat);
 131     }
 132 }
 133 
 134 class TestUI {
 135     private static JFrame mainFrame;
 136     private static JPanel mainControlPanel;
 137 
 138     private static JTextArea instructionTextArea;
 139 
 140     private static JPanel resultButtonPanel;
 141     private static JButton passButton;
 142     private static JButton failButton;
 143 
 144     private static JPanel testPanel;
 145     private static JButton testButton;
 146     private static JLabel buttonPressCountLabel;
 147 
 148     private static GridBagLayout layout;
 149     private final CountDownLatch latch;
 150     public boolean testResult = false;
 151 
 152     public TestUI(CountDownLatch latch) throws Exception {
 153         this.latch = latch;
 154     }
 155 
 156     public final void createUI() {
 157         mainFrame = new JFrame("PDialogTest");
 158 
 159         layout = new GridBagLayout();
 160         mainControlPanel = new JPanel(layout);
 161         resultButtonPanel = new JPanel(layout);
 162         testPanel = new JPanel(layout);
 163 
 164         GridBagConstraints gbc = new GridBagConstraints();
 165         // Create Test instructions
 166         String instructions
 167                 = "This test displays the print setup dialog with margins \n"
 168                 + "displayed as inches(Use equivalent conversion if different)\n"
 169                 + "\n"
 170                 + "Step 1: Verify that margins displays as 1 for left, right,\n"
 171                 + "        top and bottom margins, then click OK.\n"
 172                 + "Step 2: Verify that margins displays as 2 for left, right,\n"
 173                 + "        top and bottom margins, then click OK (A new value \n"
 174                 + "        2 has been set by setting a new imagable area).\n"
 175                 + "Step 3: Verify that margins dipslays as 2 for left, right,\n"
 176                 + "        top and bottom margins, then click OK (The newly set\n"
 177                 + "        imageable area retains even after closing in the\n"
 178                 + "        previous step).\n"
 179                 + "Step 4: Verify that margins displays as 3 for left, right,\n"
 180                 + "        top and bottom margins, then click OK (A new value \n"
 181                 + "        3 has been set by setting a new imagable area).\n"
 182                 + "Step 5: Verify that margins dipslays as 3 for left, right,\n"
 183                 + "        top and bottom margins, then click OK (The newly set\n"
 184                 + "        imageable area retains even after closing in the\n"
 185                 + "        previous step).\n"
 186                 + "Click on 'pass' if successful else click on 'fail'\n";
 187 
 188         instructionTextArea = new JTextArea();
 189         instructionTextArea.setText(instructions);
 190         instructionTextArea.setEditable(false);
 191         instructionTextArea.setBorder(BorderFactory.
 192                 createTitledBorder("Test Instructions"));
 193 
 194         gbc.gridx = 0;
 195         gbc.gridy = 0;
 196         gbc.fill = GridBagConstraints.HORIZONTAL;
 197         mainControlPanel.add(instructionTextArea, gbc);
 198 
 199         gbc.gridx = 0;
 200         gbc.gridy = 1;
 201         testPanel.add(Box.createVerticalStrut(50));
 202 
 203         mainControlPanel.add(testPanel);
 204 
 205         // Create resultButtonPanel with Pass, Fail buttons
 206         passButton = new JButton("Pass");
 207         passButton.setActionCommand("Pass");
 208         passButton.addActionListener((ActionEvent e) -> {
 209             System.out.println("Pass Button pressed!");
 210             testResult = true;
 211             latch.countDown();
 212             disposeUI();
 213         });
 214 
 215         failButton = new JButton("Fail");
 216         failButton.setActionCommand("Fail");
 217         failButton.addActionListener((ActionEvent e) -> {
 218             System.out.println("Fail Button pressed!");
 219             testResult = false;
 220             latch.countDown();
 221             disposeUI();
 222         });
 223 
 224         gbc.gridx = 0;
 225         gbc.gridy = 0;
 226         resultButtonPanel.add(passButton, gbc);
 227 
 228         gbc.gridx = 1;
 229         gbc.gridy = 0;
 230         resultButtonPanel.add(failButton, gbc);
 231 
 232         gbc.gridx = 0;
 233         gbc.gridy = 1;
 234         mainControlPanel.add(resultButtonPanel, gbc);
 235 
 236         mainFrame.add(mainControlPanel);
 237         mainFrame.pack();
 238         mainFrame.setVisible(true);
 239     }
 240 
 241     public void disposeUI() {
 242         mainFrame.dispose();
 243     }
 244 }