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