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 6357905
26 * @summary  JobAttributes.getFromPage() and getToPage() always returns 1
27 * @run main/manual JobAttrUpdateTest
28 */
29import java.awt.BorderLayout;
30import java.awt.FlowLayout;
31import java.awt.JobAttributes;
32import java.awt.PrintJob;
33import java.awt.Toolkit;
34import javax.swing.JButton;
35import javax.swing.JDialog;
36import javax.swing.JFrame;
37import javax.swing.JPanel;
38import javax.swing.JTextArea;
39import javax.swing.SwingUtilities;
40
41public class JobAttrUpdateTest {
42
43    private static Thread mainThread;
44    private static boolean testPassed;
45    private static boolean testGeneratedInterrupt;
46
47    public static void main(String[] args) throws Exception {
48        SwingUtilities.invokeAndWait(() -> {
49            doTest(JobAttrUpdateTest::printTest);
50        });
51        mainThread = Thread.currentThread();
52        try {
53            Thread.sleep(30000);
54        } catch (InterruptedException e) {
55            if (!testPassed && testGeneratedInterrupt) {
56                throw new RuntimeException(""
57                        + "JobAttributes.getFromPage(),getToPage() not updated correctly");
58            }
59        }
60        if (!testGeneratedInterrupt) {
61            throw new RuntimeException("user has not executed the test");
62        }
63    }
64
65    private static void printTest() {
66        JobAttributes ja = new JobAttributes();
67
68        Toolkit tk = Toolkit.getDefaultToolkit();
69        // ja.setToPage(4);
70        // ja.setFromPage(3);
71        // show dialog
72        PrintJob pjob = tk.getPrintJob(new JFrame(), "test", ja, null);
73        if (pjob == null) {
74            return;
75        }
76
77
78        if (ja.getDefaultSelection() == JobAttributes.DefaultSelectionType.RANGE) {
79            int fromPage = ja.getFromPage();
80            int toPage = ja.getToPage();
81            if (fromPage != 2 || toPage != 3) {
82                fail();
83            } else {
84                pass();
85            }
86        }
87    }
88
89    public static synchronized void pass() {
90        testPassed = true;
91        testGeneratedInterrupt = true;
92        mainThread.interrupt();
93    }
94
95    public static synchronized void fail() {
96        testPassed = false;
97        testGeneratedInterrupt = true;
98        mainThread.interrupt();
99    }
100
101    private static void doTest(Runnable action) {
102        String description
103                = " A print dialog will be shown.\n "
104                + " Please select Pages within Page-range.\n"
105                + " and enter From 2 and To 3. Then Select OK.";
106
107        final JDialog dialog = new JDialog();
108        dialog.setTitle("JobAttribute Updation Test");
109        JTextArea textArea = new JTextArea(description);
110        textArea.setEditable(false);
111        final JButton testButton = new JButton("Start Test");
112
113        testButton.addActionListener((e) -> {
114            testButton.setEnabled(false);
115            action.run();
116        });
117        JPanel mainPanel = new JPanel(new BorderLayout());
118        mainPanel.add(textArea, BorderLayout.CENTER);
119        JPanel buttonPanel = new JPanel(new FlowLayout());
120        buttonPanel.add(testButton);
121        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
122        dialog.add(mainPanel);
123        dialog.pack();
124        dialog.setVisible(true);
125    }
126}
127