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