1/*
2 * Copyright (c) 2016, 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 * @test
25 * @bug 6509729
26 * @summary  Verifies pageDialog margin validation is correct
27 * @run main/manual PageDialogMarginValidation
28 */
29import java.awt.BorderLayout;
30import java.awt.FlowLayout;
31import java.awt.print.PageFormat;
32import java.awt.print.PrinterJob;
33import javax.print.attribute.HashPrintRequestAttributeSet;
34import javax.swing.JButton;
35import javax.swing.JDialog;
36import javax.swing.JPanel;
37import javax.swing.JTextArea;
38import javax.swing.SwingUtilities;
39
40public class PageDialogMarginValidation {
41
42    private static Thread mainThread;
43    private static boolean testPassed;
44    private static boolean testGeneratedInterrupt;
45    private static PrinterJob printJob;
46
47    public static void pageMarginTest() {
48        PrinterJob pj = PrinterJob.getPrinterJob();
49        HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
50        PageFormat pf;
51        pf = pj.pageDialog(aset);
52    }
53
54    public static void main(String[] args) throws Exception {
55        SwingUtilities.invokeAndWait(() -> {
56            doTest(PageDialogMarginValidation::pageMarginTest);
57        });
58        mainThread = Thread.currentThread();
59        try {
60            Thread.sleep(30000);
61        } catch (InterruptedException e) {
62            if (!testPassed && testGeneratedInterrupt) {
63                throw new RuntimeException(""
64                        + "Margin validation is not correct");
65            }
66        }
67        if (!testGeneratedInterrupt) {
68            throw new RuntimeException("user has not executed the test");
69        }
70    }
71
72    public static synchronized void pass() {
73        testPassed = true;
74        testGeneratedInterrupt = true;
75        mainThread.interrupt();
76    }
77
78    public static synchronized void fail() {
79        testPassed = false;
80        testGeneratedInterrupt = true;
81        mainThread.interrupt();
82    }
83
84    private static void doTest(Runnable action) {
85        String description
86                = " Initially, a page dialog will be shown.\n "
87                + " The left, right, bottom, right margin will have value 1.0.\n"
88                + " Modify right margin from 1.0 to 0.0 and press tab.\n"
89                + " Please verify the right margin changes back to 1.0. \n"
90                + " Please do the same for bottom margin.\n"
91                + " If right and bottom margin changes back to 1.0, press PASS else press fail";
92
93        final JDialog dialog = new JDialog();
94        dialog.setTitle("printSelectionTest");
95        JTextArea textArea = new JTextArea(description);
96        textArea.setEditable(false);
97        final JButton testButton = new JButton("Start Test");
98        final JButton passButton = new JButton("PASS");
99        passButton.setEnabled(false);
100        passButton.addActionListener((e) -> {
101            dialog.dispose();
102            pass();
103        });
104        final JButton failButton = new JButton("FAIL");
105        failButton.setEnabled(false);
106        failButton.addActionListener((e) -> {
107            dialog.dispose();
108            fail();
109        });
110        testButton.addActionListener((e) -> {
111            testButton.setEnabled(false);
112            action.run();
113            passButton.setEnabled(true);
114            failButton.setEnabled(true);
115        });
116        JPanel mainPanel = new JPanel(new BorderLayout());
117        mainPanel.add(textArea, BorderLayout.CENTER);
118        JPanel buttonPanel = new JPanel(new FlowLayout());
119        buttonPanel.add(testButton);
120        buttonPanel.add(passButton);
121        buttonPanel.add(failButton);
122        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
123        dialog.add(mainPanel);
124        dialog.pack();
125        dialog.setVisible(true);
126    }
127}
128