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 6966350 8160882
  26  * @summary Verifies if Empty pages are printed on Lexmark E352dn PS3
  27  *           with "1200 IQ" setting
  28  * @run main/manual PrintTestLexmarkIQ
  29  */
  30 import java.awt.BorderLayout;
  31 import java.awt.Color;
  32 import java.awt.FlowLayout;
  33 import java.awt.Font;
  34 import java.awt.Graphics;
  35 import java.awt.Graphics2D;
  36 import java.awt.event.WindowAdapter;
  37 import java.awt.event.WindowEvent;
  38 import java.awt.geom.Rectangle2D;
  39 import java.awt.print.PageFormat;
  40 import java.awt.print.Paper;
  41 import java.awt.print.Printable;
  42 import static java.awt.print.Printable.NO_SUCH_PAGE;
  43 import static java.awt.print.Printable.PAGE_EXISTS;
  44 import java.awt.print.PrinterException;
  45 import java.awt.print.PrinterJob;
  46 import javax.swing.JButton;
  47 import javax.swing.JDialog;
  48 import javax.swing.JPanel;
  49 import javax.swing.JTextArea;
  50 import javax.swing.SwingUtilities;
  51 
  52 public class PrintTestLexmarkIQ implements Printable {
  53 
  54     private static Thread mainThread;
  55     private static boolean testPassed;
  56     private static boolean testGeneratedInterrupt;
  57 
  58     public static void main(String[] args) throws Exception {
  59         SwingUtilities.invokeAndWait(() -> {
  60             doTest(PrintTestLexmarkIQ::printTest);
  61         });
  62         mainThread = Thread.currentThread();
  63         try {
  64             Thread.sleep(90000);
  65         } catch (InterruptedException e) {
  66             if (!testPassed && testGeneratedInterrupt) {
  67                 throw new RuntimeException(" Empty pages printed ");
  68             }
  69         }
  70         if (!testGeneratedInterrupt) {
  71             throw new RuntimeException("user has not executed the test");
  72         }
  73     }
  74 
  75     private static void printTest() {
  76         PrinterJob pj = PrinterJob.getPrinterJob();
  77 
  78         PageFormat pf = pj.defaultPage();
  79         Paper paper = new Paper();
  80         double margin = 36; // half inch
  81         paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2,
  82                 paper.getHeight() - margin * 2);
  83         pf.setPaper(paper);
  84 
  85         pj.setPrintable(new PrintTestLexmarkIQ(), pf);
  86         if (pj.printDialog()) {
  87             try {
  88                 pj.print();
  89             } catch (PrinterException e) {
  90                 System.out.println(e);
  91             }
  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 description
 109                 = " Install Lexmark E352dn PS3 or Dell 5310n printer.\n"
 110                 + " A print dialog will be shown.\n"
 111                 + " Select Normal 1200IQ setting in Properties->PrintQuality in Dell 5310n \n"
 112                 + " or for Lexmark E352dn printer, select Normal 1200IQ setting in\n "
 113                 + " Properties -> Advanced -> Graphic -> Print Quality.\n"
 114                 + " Press Print. Verify the print output.\n "
 115                 + " If empty page is printed, press Fail else press Pass.";
 116 
 117         final JDialog dialog = new JDialog();
 118         dialog.setTitle("1200IQTest");
 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             public void windowClosing(WindowEvent e) {
 152                 System.out.println("main dialog closing");
 153                 testGeneratedInterrupt = false;
 154                 mainThread.interrupt();
 155             }
 156         });
 157     }
 158 
 159     public int print(Graphics g, PageFormat pf, int pi)
 160             throws PrinterException {
 161         if (pi != 0) {
 162             return NO_SUCH_PAGE;
 163         }
 164         Graphics2D g2 = (Graphics2D) g;
 165         g2.setFont(new Font("Serif", Font.PLAIN, 36));
 166         g2.setPaint(Color.black);
 167         g2.drawString("Java Source and Support", 100, 100);
 168         Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf
 169                 .getImageableY(), pf.getImageableWidth(), pf
 170                 .getImageableHeight());
 171         g2.draw(outline);
 172         return PAGE_EXISTS;
 173     }
 174 
 175 }
--- EOF ---