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