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 6575247 8170579
26 * @summary  Verifies if Banner page is printed
27 * @requires (os.family == "linux" | os.family == "solaris")
28 * @run main/manual BannerTest
29 */
30
31import java.awt.BorderLayout;
32import java.awt.FlowLayout;
33import java.awt.Graphics;
34import java.awt.event.WindowAdapter;
35import java.awt.event.WindowEvent;
36import java.awt.print.PageFormat;
37import java.awt.print.Printable;
38import static java.awt.print.Printable.NO_SUCH_PAGE;
39import static java.awt.print.Printable.PAGE_EXISTS;
40import java.awt.print.PrinterException;
41import java.awt.print.PrinterJob;
42import javax.print.PrintService;
43import javax.print.attribute.standard.JobSheets;
44import javax.print.attribute.standard.SheetCollate;
45import javax.swing.JButton;
46import javax.swing.JDialog;
47import javax.swing.JPanel;
48import javax.swing.JTextArea;
49import javax.swing.SwingUtilities;
50
51
52public class BannerTest implements Printable {
53    private static Thread mainThread;
54    private static boolean testPassed;
55    private static boolean testGeneratedInterrupt;
56    private static volatile PrinterJob job;
57
58    public static void main(String[] args)  throws Exception {
59        job = PrinterJob.getPrinterJob();
60        PrintService prtSrv = job.getPrintService();
61        if (job.getPrintService() == null) {
62            System.out.println("No printers. Test cannot continue");
63            return;
64        }
65        if (!prtSrv.isAttributeCategorySupported(JobSheets.class)) {
66            return;
67        }
68        SwingUtilities.invokeAndWait(() -> {
69            doTest(BannerTest::printTest);
70        });
71        mainThread = Thread.currentThread();
72        try {
73            Thread.sleep(180000);
74        } catch (InterruptedException e) {
75            if (!testPassed && testGeneratedInterrupt) {
76                throw new RuntimeException("Banner page did not print");
77            }
78        }
79        if (!testGeneratedInterrupt) {
80            throw new RuntimeException("user has not executed the test");
81        }
82    }
83
84    private static void printTest() {
85        job.setPrintable(new BannerTest());
86        if(job.printDialog()) {
87            try {
88                job.print();
89            } catch (PrinterException e) {
90                e.printStackTrace();
91                fail();
92            }
93        }
94    }
95
96    public static synchronized void pass() {
97        testPassed = true;
98        testGeneratedInterrupt = true;
99        mainThread.interrupt();
100    }
101
102    public static synchronized void fail() {
103        testPassed = false;
104        testGeneratedInterrupt = true;
105        mainThread.interrupt();
106    }
107
108    private static void doTest(Runnable action) {
109        String description
110                = " A print dialog will be shown.\n"
111                + " Click on the \"Appearance\" tab.\n "
112                + " Select the \"Banner page\" checkbox.\n"
113                + " Click on Print, and check if Banner page is printed.\n "
114                + " If Banner page is printed, press PASS else press FAIL";
115
116        final JDialog dialog = new JDialog();
117        dialog.setTitle("printBannerTest");
118        JTextArea textArea = new JTextArea(description);
119        textArea.setEditable(false);
120        final JButton testButton = new JButton("Start Test");
121        final JButton passButton = new JButton("PASS");
122        passButton.setEnabled(false);
123        passButton.addActionListener((e) -> {
124            dialog.dispose();
125            pass();
126        });
127        final JButton failButton = new JButton("FAIL");
128        failButton.setEnabled(false);
129        failButton.addActionListener((e) -> {
130            dialog.dispose();
131            fail();
132        });
133        testButton.addActionListener((e) -> {
134            testButton.setEnabled(false);
135            action.run();
136            passButton.setEnabled(true);
137            failButton.setEnabled(true);
138        });
139        JPanel mainPanel = new JPanel(new BorderLayout());
140        mainPanel.add(textArea, BorderLayout.CENTER);
141        JPanel buttonPanel = new JPanel(new FlowLayout());
142        buttonPanel.add(testButton);
143        buttonPanel.add(passButton);
144        buttonPanel.add(failButton);
145        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
146        dialog.add(mainPanel);
147        dialog.pack();
148        dialog.setVisible(true);
149        dialog.addWindowListener(new WindowAdapter() {
150           @Override
151            public void windowClosing(WindowEvent e) {
152                System.out.println("main dialog closing");
153                testGeneratedInterrupt = false;
154                mainThread.interrupt();
155            }
156        });
157    }
158
159    @Override
160    public int print(Graphics g, PageFormat pf, int pi)
161            throws PrinterException {
162        System.out.println("pi = " + pi);
163        g.drawString("Testing", 100, 100);
164        if (pi == 1)
165            return NO_SUCH_PAGE;
166        return PAGE_EXISTS;
167    }
168}
169