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 8061258
26  * @summary  PrinterJob's native Print Dialog does not reflect
27  *           specified Copies or Page Ranges
28  * @run main/manual DlgAttrsBug
29  */
30 import java.awt.BorderLayout;
31 import java.awt.FlowLayout;
32 import java.awt.Graphics;
33 import java.awt.print.PageFormat;
34 import java.awt.print.Printable;
35 import java.awt.print.PrinterException;
36 import java.awt.print.PrinterJob;
37 import javax.print.attribute.HashPrintRequestAttributeSet;
38 import javax.print.attribute.PrintRequestAttributeSet;
39 import javax.print.attribute.standard.Copies;
40 import javax.print.attribute.standard.PageRanges;
41 import javax.print.attribute.standard.DialogTypeSelection;
42 import javax.swing.JButton;
43 import javax.swing.JDialog;
44 import javax.swing.JPanel;
45 import javax.swing.JTextArea;
46 import javax.swing.SwingUtilities;
47 
48 
49 public class DlgAttrsBug implements Printable {
50     private static Thread mainThread;
51     private static boolean testPassed;
52     private static boolean testGeneratedInterrupt;
53 
main(String[] args)54     public static void main(String[] args)  throws Exception {
55         SwingUtilities.invokeAndWait(() -> {
56             doTest(DlgAttrsBug::printTest);
57         });
58         mainThread = Thread.currentThread();
59         try {
60             Thread.sleep(30000);
61         } catch (InterruptedException e) {
62             if (!testPassed && testGeneratedInterrupt) {
63                 throw new RuntimeException("Print Dialog does not " +
64                           "reflect Copies or Page Ranges");
65             }
66         }
67         if (!testGeneratedInterrupt) {
68             throw new RuntimeException("user has not executed the test");
69         }
70     }
71 
printTest()72     private static void printTest() {
73         PrinterJob job = PrinterJob.getPrinterJob();
74         if (job.getPrintService() == null) {
75             System.out.println("No printers. Test cannot continue");
76             return;
77         }
78         job.setPrintable(new DlgAttrsBug());
79         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
80         aset.add(new Copies(5));
81         aset.add(new PageRanges(3,4));
82         aset.add(DialogTypeSelection.NATIVE);
83         job.printDialog(aset);
84     }
85 
pass()86     public static synchronized void pass() {
87         testPassed = true;
88         testGeneratedInterrupt = true;
89         mainThread.interrupt();
90     }
91 
fail()92     public static synchronized void fail() {
93         testPassed = false;
94         testGeneratedInterrupt = true;
95         mainThread.interrupt();
96     }
97 
doTest(Runnable action)98     private static void doTest(Runnable action) {
99         String description
100                 = " Visual inspection of print dialog is required.\n"
101                 + " A print dialog will be shown.\n "
102                 + " Please verify Copies 5 is selected.\n"
103                 + " Also verify, Page Range is selected with "
104                 + " from page 3 and to Page 4.\n"
105                 + " If ok, press PASS else press FAIL";
106 
107         final JDialog dialog = new JDialog();
108         dialog.setTitle("printSelectionTest");
109         JTextArea textArea = new JTextArea(description);
110         textArea.setEditable(false);
111         final JButton testButton = new JButton("Start Test");
112         final JButton passButton = new JButton("PASS");
113         passButton.setEnabled(false);
114         passButton.addActionListener((e) -> {
115             dialog.dispose();
116             pass();
117         });
118         final JButton failButton = new JButton("FAIL");
119         failButton.setEnabled(false);
120         failButton.addActionListener((e) -> {
121             dialog.dispose();
122             fail();
123         });
124         testButton.addActionListener((e) -> {
125             testButton.setEnabled(false);
126             action.run();
127             passButton.setEnabled(true);
128             failButton.setEnabled(true);
129         });
130         JPanel mainPanel = new JPanel(new BorderLayout());
131         mainPanel.add(textArea, BorderLayout.CENTER);
132         JPanel buttonPanel = new JPanel(new FlowLayout());
133         buttonPanel.add(testButton);
134         buttonPanel.add(passButton);
135         buttonPanel.add(failButton);
136         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
137         dialog.add(mainPanel);
138         dialog.pack();
139         dialog.setVisible(true);
140     }
141 
print(Graphics g, PageFormat pf, int pi)142     public int print(Graphics g, PageFormat pf, int pi)
143             throws PrinterException {
144         System.out.println("pi = " + pi);
145         if (pi >= 5) {
146             return NO_SUCH_PAGE;
147         }
148         g.drawString("Page : " + (pi+1), 200, 200);
149         return PAGE_EXISTS;
150     }
151 
152 }
153