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 5049012 8163922
26 * @summary Verify if PrintToFile option is disabled for flavors that do not
27 *          support Destination
28 * @requires (os.family == "linux")
29 * @run main/manual ServiceDialogValidateTest
30 */
31import java.awt.BorderLayout;
32import java.awt.FlowLayout;
33import java.awt.event.WindowAdapter;
34import java.awt.event.WindowEvent;
35import java.io.File;
36import javax.print.DocFlavor;
37import javax.print.PrintService;
38import javax.print.PrintServiceLookup;
39import javax.print.ServiceUI;
40import javax.print.attribute.HashPrintRequestAttributeSet;
41import javax.print.attribute.standard.Destination;
42import javax.swing.JButton;
43import javax.swing.JDialog;
44import javax.swing.JPanel;
45import javax.swing.JTextArea;
46import javax.swing.SwingUtilities;
47
48public class ServiceDialogValidateTest {
49    private static Thread mainThread;
50    private static boolean testPassed;
51    private static boolean testGeneratedInterrupt;
52
53    private static void printTest() {
54        PrintService defService = null, service[] = null;
55        HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet();
56        DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
57
58        service = PrintServiceLookup.lookupPrintServices(flavor, null);
59        defService = PrintServiceLookup.lookupDefaultPrintService();
60
61        if ((service == null) || (service.length == 0)) {
62            throw new RuntimeException("No Printer services found");
63        }
64        File f = new File("output.ps");
65        Destination d = new Destination(f.toURI());
66        prSet.add(d);
67        if (defService != null) {
68            System.out.println("isAttrCategory Supported? " +
69                    defService.isAttributeCategorySupported(Destination.class));
70            System.out.println("isAttrValue Supported? " +
71                    defService.isAttributeValueSupported(d, flavor, null));
72        }
73
74        defService = ServiceUI.printDialog(null, 100, 100, service, defService,
75                flavor, prSet);
76
77        ServiceUI.printDialog(null, 100, 100, service, defService,
78                DocFlavor.SERVICE_FORMATTED.PAGEABLE,
79                new HashPrintRequestAttributeSet());
80    }
81
82    /**
83     * Starts the application.
84     */
85    public static void main(java.lang.String[] args) throws Exception {
86        SwingUtilities.invokeAndWait(() -> {
87            doTest(ServiceDialogValidateTest::printTest);
88        });
89        mainThread = Thread.currentThread();
90        try {
91            Thread.sleep(60000);
92        } catch (InterruptedException e) {
93            if (!testPassed && testGeneratedInterrupt) {
94                throw new RuntimeException("PrintToFile option is not disabled "
95                        + "for flavors that do not support destination and/or"
96                        + " disabled for flavors that supports destination");
97            }
98        }
99        if (!testGeneratedInterrupt) {
100            throw new RuntimeException("user has not executed the test");
101        }
102    }
103
104    public static synchronized void pass() {
105        testPassed = true;
106        testGeneratedInterrupt = true;
107        mainThread.interrupt();
108    }
109
110    public static synchronized void fail() {
111        testPassed = false;
112        testGeneratedInterrupt = true;
113        mainThread.interrupt();
114    }
115
116    private static void doTest(Runnable action) {
117        String description
118                = " Visual inspection of print dialog is required.\n"
119                + " 2 print dialog will be shown.\n "
120                + " Please verify Print-To-File option is disabled "
121                + " in the 1st print dialog.\n"
122                + " Press Cancel to close the print dialog.\n"
123                + " Please verify Print-To-File option is enabled "
124                + " in 2nd print dialog\n"
125                + " Press Cancel to close the print dialog.\n"
126                + " If the print dialog's Print-to-File behaves as mentioned, "
127                + " press PASS else press FAIL";
128
129        final JDialog dialog = new JDialog();
130        dialog.setTitle("printSelectionTest");
131        JTextArea textArea = new JTextArea(description);
132        textArea.setEditable(false);
133        final JButton testButton = new JButton("Start Test");
134        final JButton passButton = new JButton("PASS");
135        passButton.setEnabled(false);
136        passButton.addActionListener((e) -> {
137            dialog.dispose();
138            pass();
139        });
140        final JButton failButton = new JButton("FAIL");
141        failButton.setEnabled(false);
142        failButton.addActionListener((e) -> {
143            dialog.dispose();
144            fail();
145        });
146        testButton.addActionListener((e) -> {
147            testButton.setEnabled(false);
148            action.run();
149            passButton.setEnabled(true);
150            failButton.setEnabled(true);
151        });
152        JPanel mainPanel = new JPanel(new BorderLayout());
153        mainPanel.add(textArea, BorderLayout.CENTER);
154        JPanel buttonPanel = new JPanel(new FlowLayout());
155        buttonPanel.add(testButton);
156        buttonPanel.add(passButton);
157        buttonPanel.add(failButton);
158        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
159        dialog.add(mainPanel);
160        dialog.pack();
161        dialog.setVisible(true);
162        dialog.addWindowListener(new WindowAdapter() {
163            @Override
164            public void windowClosing(WindowEvent e) {
165                System.out.println("main dialog closing");
166                testGeneratedInterrupt = false;
167                mainThread.interrupt();
168            }
169        });
170    }
171}
172
173
174