< prev index next >
test/java/awt/print/PageFormat/PDialogTest.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -21,24 +21,232 @@
* questions.
*/
/**
* @test
- * @bug 4855801
+ * @bug 4855801 6949753
+ * @requires (os.family == "Windows") | (os.family == "Linux")
* @summary Changing margins in the page format does not have any effect
* @run main/manual PDialogTest
*/
-import java.awt.print.*;
-import javax.print.attribute.*;
-import javax.print.attribute.standard.*;
+
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.event.ActionEvent;
+import java.awt.print.PageFormat;
+import java.awt.print.Paper;
+import java.awt.print.PrinterException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import javax.swing.BorderFactory;
+import javax.swing.Box;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JTextArea;
+import javax.swing.SwingUtilities;
public class PDialogTest
{
+ public static void main(String args[]) throws Exception {
+ final CountDownLatch latch = new CountDownLatch(1);
+
+ // Test UI creation
+ TestUI test = new TestUI(latch);
+ SwingUtilities.invokeAndWait(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ test.createUI();
+ } catch (Exception e) {
+
+ }
+ }
+ });
+
+ // PDialogTest creation
+ PDialogTest pDialogTest = new PDialogTest();
+ // Start a thread for the page setup dialog test
+ Thread T1 = new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ pDialogTest.do_test();
+ } catch (PrinterException e) {
+
+ }
+ }
+ });
+ T1.start();
+
+ boolean status = latch.await(1, TimeUnit.MINUTES);
+ if (!status) {
+ throw new RuntimeException("Test timed out.");
+ }
+
+ if (test.testResult == false) {
+ throw new RuntimeException("Test Failed.");
+ }
+
+ SwingUtilities.invokeAndWait(() -> {
+ test.disposeUI();
+ });
+ }
+
+ public static void do_test() throws PrinterException {
+ // Obtain default page format from the default printer job service
+ PageFormat pageFormat = java.awt.print.PrinterJob.getPrinterJob()
+ .defaultPage();
+ Paper paper = pageFormat.getPaper();
+
+ // Set paper attributes with 8.5 inch by 11 inches paper.
+ paper.setSize(8.5 * 72, 11 * 72);
+ pageFormat.setPaper(paper);
+
+ createNewPrintPageSetup(pageFormat);
+
+ setValuesForPrintPageSetup(pageFormat, 2);
+
+ createNewPrintPageSetup(pageFormat);
+
+ setValuesForPrintPageSetup(pageFormat, 3);
+
+ createNewPrintPageSetup(pageFormat);
+ }
+
+ private static void createNewPrintPageSetup(PageFormat pageFormat)
+ throws PrinterException {
+
+ // Display printer default setup dialog
+ java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat);
+ }
+
+ private static void setValuesForPrintPageSetup(PageFormat pageFormat, int
+ marginValue) throws PrinterException {
+ Paper paper = pageFormat.getPaper();
+
+ // Reset paper attributes to 8.5 inch by 11 inches paper.
+ paper.setSize(8.5 * 72, 11 * 72);
+ double paperHeight = paper.getHeight();
+ double paperWidth = paper.getWidth();
+ paper.setImageableArea(72 * marginValue, 72 * marginValue,
+ paperWidth - (72 * 2 * marginValue),
+ paperHeight - (72 * 2 * marginValue));
+ pageFormat.setPaper(paper);
+
+ java.awt.print.PrinterJob.getPrinterJob().pageDialog(pageFormat);
+ }
+}
+
+class TestUI {
+ private static JFrame mainFrame;
+ private static JPanel mainControlPanel;
+
+ private static JTextArea instructionTextArea;
+
+ private static JPanel resultButtonPanel;
+ private static JButton passButton;
+ private static JButton failButton;
+
+ private static JPanel testPanel;
+ private static JButton testButton;
+ private static JLabel buttonPressCountLabel;
+
+ private static GridBagLayout layout;
+ private final CountDownLatch latch;
+ public boolean testResult = false;
+
+ public TestUI(CountDownLatch latch) throws Exception {
+ this.latch = latch;
+ }
+
+ public final void createUI() {
+ mainFrame = new JFrame("PDialogTest");
+
+ layout = new GridBagLayout();
+ mainControlPanel = new JPanel(layout);
+ resultButtonPanel = new JPanel(layout);
+ testPanel = new JPanel(layout);
+
+ GridBagConstraints gbc = new GridBagConstraints();
+ // Create Test instructions
+ String instructions
+ = "This test displays the print setup dialog with margins \n"
+ + "displayed as inches(Use equivalent conversion if different)\n"
+ + "\n"
+ + "Step 1: Verify that margins displays default values for \n" +
+ " left, right, top and bottom margins, then click OK" +
+ ".\n"
+ + "Step 2: Verify that margins displays as 2 for left, right,\n"
+ + " top and bottom margins, then click OK (A new value \n"
+ + " 2 has been set by setting a new imagable area).\n"
+ + "Step 3: Verify that margins dipslays as 2 for left, right,\n"
+ + " top and bottom margins, then click OK (The newly set\n"
+ + " imageable area retains even after closing in the\n"
+ + " previous step).\n"
+ + "Step 4: Verify that margins displays as 3 for left, right,\n"
+ + " top and bottom margins, then click OK (A new value \n"
+ + " 3 has been set by setting a new imagable area).\n"
+ + "Step 5: Verify that margins dipslays as 3 for left, right,\n"
+ + " top and bottom margins, then click OK (The newly set\n"
+ + " imageable area retains even after closing in the\n"
+ + " previous step).\n"
+ + "Click on 'pass' if successful else click on 'fail'\n";
+
+ instructionTextArea = new JTextArea();
+ instructionTextArea.setText(instructions);
+ instructionTextArea.setEditable(false);
+ instructionTextArea.setBorder(BorderFactory.
+ createTitledBorder("Test Instructions"));
+
+ gbc.gridx = 0;
+ gbc.gridy = 0;
+ gbc.fill = GridBagConstraints.HORIZONTAL;
+ mainControlPanel.add(instructionTextArea, gbc);
+
+ gbc.gridx = 0;
+ gbc.gridy = 1;
+ testPanel.add(Box.createVerticalStrut(50));
+
+ mainControlPanel.add(testPanel);
+
+ // Create resultButtonPanel with Pass, Fail buttons
+ passButton = new JButton("Pass");
+ passButton.setActionCommand("Pass");
+ passButton.addActionListener((ActionEvent e) -> {
+ System.out.println("Pass Button pressed!");
+ testResult = true;
+ latch.countDown();
+ disposeUI();
+ });
+
+ failButton = new JButton("Fail");
+ failButton.setActionCommand("Fail");
+ failButton.addActionListener((ActionEvent e) -> {
+ System.out.println("Fail Button pressed!");
+ testResult = false;
+ latch.countDown();
+ disposeUI();
+ });
+
+ gbc.gridx = 0;
+ gbc.gridy = 0;
+ resultButtonPanel.add(passButton, gbc);
+
+ gbc.gridx = 1;
+ gbc.gridy = 0;
+ resultButtonPanel.add(failButton, gbc);
+
+ gbc.gridx = 0;
+ gbc.gridy = 1;
+ mainControlPanel.add(resultButtonPanel, gbc);
- public static void main(String[] args) {
- PageFormat page=new PageFormat();
- while(true){
- page=java.awt.print.PrinterJob.getPrinterJob().pageDialog(page);
+ mainFrame.add(mainControlPanel);
+ mainFrame.pack();
+ mainFrame.setVisible(true);
}
+ public void disposeUI() {
+ mainFrame.dispose();
}
}
< prev index next >