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 * @bug 6357887 8165146
25 * @summary  Verifies if selected printertray is used
26 * @requires (os.family == "linux" | os.family == "solaris")
27 * @run main/manual TestMediaTraySelection
28 */
29
30import java.awt.BorderLayout;
31import java.awt.FlowLayout;
32import java.awt.Graphics;
33import java.awt.event.WindowAdapter;
34import java.awt.event.WindowEvent;
35import java.awt.print.PageFormat;
36import java.awt.print.Printable;
37import java.awt.print.PrinterException;
38import java.awt.print.PrinterJob;
39import javax.print.DocFlavor;
40import javax.print.PrintService;
41import javax.print.PrintServiceLookup;
42import javax.print.attribute.HashPrintRequestAttributeSet;
43import javax.print.attribute.PrintRequestAttributeSet;
44import javax.print.attribute.standard.Media;
45import javax.print.attribute.standard.MediaTray;
46import javax.swing.JButton;
47import javax.swing.JDialog;
48import javax.swing.JPanel;
49import javax.swing.JTextArea;
50import javax.swing.SwingUtilities;
51
52public class TestMediaTraySelection implements Printable {
53
54    private static Thread mainThread;
55    private static boolean testPassed;
56    private static boolean testGeneratedInterrupt;
57    private static PrintService prservices;
58
59    public static void main(String[] args)  throws Exception {
60        prservices = PrintServiceLookup.lookupDefaultPrintService();
61        if (prservices == null) {
62            System.out.println("No print service found");
63            return;
64        }
65        System.out.println(" Print service " + prservices);
66        SwingUtilities.invokeAndWait(() -> {
67            doTest(TestMediaTraySelection::printTest);
68        });
69        mainThread = Thread.currentThread();
70        try {
71            Thread.sleep(90000);
72        } catch (InterruptedException e) {
73            if (!testPassed && testGeneratedInterrupt) {
74                throw new RuntimeException("Banner page did not print");
75            }
76        }
77        if (!testGeneratedInterrupt) {
78            throw new RuntimeException("user has not executed the test");
79        }
80    }
81
82    private static void printTest() {
83
84        MediaTray tray = null;
85        //tray = getMediaTray( prservices, "Bypass Tray" );
86        tray = getMediaTray( prservices, "Tray 4" );
87        PrintRequestAttributeSet atrset = new HashPrintRequestAttributeSet();
88        //atrset.add( MediaSizeName.ISO_A4 );
89        atrset.add(tray);
90        PrinterJob pjob = PrinterJob.getPrinterJob();
91        pjob.setPrintable(new TestMediaTraySelection());
92        try {
93            pjob.print(atrset);
94        } catch (PrinterException e) {
95            e.printStackTrace();
96            fail();
97        }
98    }
99
100    static MediaTray getMediaTray( PrintService ps, String name) {
101         Media[] media  = (Media[])ps.getSupportedAttributeValues( Media.class,
102                 DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
103
104        for (Media m : media) {
105            if ( m instanceof MediaTray) {
106                System.out.println("MediaTray=" + m.toString() );
107                if ( m.toString().trim().indexOf( name ) > -1 ) {
108                    return (MediaTray)m;
109                }
110            }
111        }
112        return null;
113    }
114
115    public static synchronized void pass() {
116        testPassed = true;
117        testGeneratedInterrupt = true;
118        mainThread.interrupt();
119    }
120
121    public static synchronized void fail() {
122        testPassed = false;
123        testGeneratedInterrupt = true;
124        mainThread.interrupt();
125    }
126
127    private static void doTest(Runnable action) {
128        String description
129                = " Please verify the \"Tray 4\" of printer is used for printout\n"
130                + " and not standard/auto tray. If yes, press PASS else press FAIL";
131
132        final JDialog dialog = new JDialog();
133        dialog.setTitle("printBannerTest");
134        JTextArea textArea = new JTextArea(description);
135        textArea.setEditable(false);
136        final JButton testButton = new JButton("Start Test");
137        final JButton passButton = new JButton("PASS");
138        passButton.setEnabled(false);
139        passButton.addActionListener((e) -> {
140            dialog.dispose();
141            pass();
142        });
143        final JButton failButton = new JButton("FAIL");
144        failButton.setEnabled(false);
145        failButton.addActionListener((e) -> {
146            dialog.dispose();
147            fail();
148        });
149        testButton.addActionListener((e) -> {
150            testButton.setEnabled(false);
151            action.run();
152            passButton.setEnabled(true);
153            failButton.setEnabled(true);
154        });
155        JPanel mainPanel = new JPanel(new BorderLayout());
156        mainPanel.add(textArea, BorderLayout.CENTER);
157        JPanel buttonPanel = new JPanel(new FlowLayout());
158        buttonPanel.add(testButton);
159        buttonPanel.add(passButton);
160        buttonPanel.add(failButton);
161        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
162        dialog.add(mainPanel);
163        dialog.pack();
164        dialog.setVisible(true);
165        dialog.addWindowListener(new WindowAdapter() {
166            @Override
167            public void windowClosing(WindowEvent e) {
168                System.out.println("main dialog closing");
169                testGeneratedInterrupt = false;
170                mainThread.interrupt();
171            }
172        });
173    }
174
175    @Override
176    public int print(Graphics g, PageFormat pf, int pi) {
177        System.out.println("pi = " + pi);
178        if (pi > 0) {
179            return NO_SUCH_PAGE;
180        }
181        g.drawString("Testing : " , 200, 200);
182        return PAGE_EXISTS;
183    }
184}
185