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