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 
  31 import java.awt.BorderLayout;
  32 import java.awt.FlowLayout;
  33 import java.awt.Graphics;
  34 import java.awt.event.WindowAdapter;
  35 import java.awt.event.WindowEvent;
  36 import java.awt.print.PageFormat;
  37 import java.awt.print.Printable;
  38 import static java.awt.print.Printable.NO_SUCH_PAGE;
  39 import static java.awt.print.Printable.PAGE_EXISTS;
  40 import java.awt.print.PrinterException;
  41 import java.awt.print.PrinterJob;
  42 import javax.print.PrintService;
  43 import javax.print.attribute.standard.JobSheets;
  44 import javax.print.attribute.standard.SheetCollate;
  45 import javax.swing.JButton;
  46 import javax.swing.JDialog;
  47 import javax.swing.JPanel;
  48 import javax.swing.JTextArea;
  49 import javax.swing.SwingUtilities;
  50 
  51 
  52 public 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         
  86         job.setPrintable(new BannerTest());
  87         if(job.printDialog()) {
  88             try {
  89                 job.print();
  90             } catch (PrinterException e) {
  91                 e.printStackTrace();
  92                 fail();
  93             }
  94         }
  95     }
  96 
  97     public static synchronized void pass() {
  98         testPassed = true;
  99         testGeneratedInterrupt = true;
 100         mainThread.interrupt();
 101     }
 102 
 103     public static synchronized void fail() {
 104         testPassed = false;
 105         testGeneratedInterrupt = true;
 106         mainThread.interrupt();
 107     }
 108 
 109     private static void doTest(Runnable action) {
 110         String description
 111                 = " A print dialog will be shown.\n"
 112                 + " Click on the \"Appearance\" tab.\n "
 113                 + " Select the \"Banner page\" checkbox.\n"
 114                 + " Click on Print, and check if Banner page is printed.\n "
 115                 + " If Banner page is printed, press PASS else press FAIL";
 116 
 117         final JDialog dialog = new JDialog();
 118         dialog.setTitle("printBannerTest");
 119         JTextArea textArea = new JTextArea(description);
 120         textArea.setEditable(false);
 121         final JButton testButton = new JButton("Start Test");
 122         final JButton passButton = new JButton("PASS");
 123         passButton.setEnabled(false);
 124         passButton.addActionListener((e) -> {
 125             dialog.dispose();
 126             pass();
 127         });
 128         final JButton failButton = new JButton("FAIL");
 129         failButton.setEnabled(false);
 130         failButton.addActionListener((e) -> {
 131             dialog.dispose();
 132             fail();
 133         });
 134         testButton.addActionListener((e) -> {
 135             testButton.setEnabled(false);
 136             action.run();
 137             passButton.setEnabled(true);
 138             failButton.setEnabled(true);
 139         });
 140         JPanel mainPanel = new JPanel(new BorderLayout());
 141         mainPanel.add(textArea, BorderLayout.CENTER);
 142         JPanel buttonPanel = new JPanel(new FlowLayout());
 143         buttonPanel.add(testButton);
 144         buttonPanel.add(passButton);
 145         buttonPanel.add(failButton);
 146         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
 147         dialog.add(mainPanel);
 148         dialog.pack();
 149         dialog.setVisible(true);
 150         dialog.addWindowListener(new WindowAdapter() {
 151            @Override
 152             public void windowClosing(WindowEvent e) {
 153                 System.out.println("main dialog closing");
 154                 testGeneratedInterrupt = false;
 155                 mainThread.interrupt();
 156             }
 157         });
 158     }
 159 
 160     @Override
 161     public int print(Graphics g, PageFormat pf, int pi)
 162             throws PrinterException {
 163         System.out.println("pi = " + pi);
 164         g.drawString("Testing", 100, 100);
 165         if (pi == 1)
 166             return NO_SUCH_PAGE;
 167         return PAGE_EXISTS;
 168     }
 169 }