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 8061258
  26  * @summary  PrinterJob's native Print Dialog does not reflect 
  27  *           specified Copies or Page Ranges
  28  * @run main/manual DlgAttrsBug
  29  */
  30 import java.awt.BorderLayout;
  31 import java.awt.FlowLayout;
  32 import java.awt.Graphics;
  33 import java.awt.print.PageFormat;
  34 import java.awt.print.Printable;
  35 import java.awt.print.PrinterException;
  36 import java.awt.print.PrinterJob;
  37 import javax.print.attribute.HashPrintRequestAttributeSet;
  38 import javax.print.attribute.PrintRequestAttributeSet;
  39 import javax.print.attribute.standard.Copies;
  40 import javax.print.attribute.standard.PageRanges;
  41 import javax.print.attribute.standard.DialogTypeSelection;
  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 DlgAttrsBug 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(DlgAttrsBug::printTest);
  57         });
  58         mainThread = Thread.currentThread();
  59         try {
  60             Thread.sleep(30000);
  61         } catch (InterruptedException e) {
  62             if (!testPassed && testGeneratedInterrupt) {
  63                 throw new RuntimeException("Print Dialog does not reflect Copies or Page Ranges");
  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. Testt cannot continue");
  75             return;
  76         }
  77         job.setPrintable(new DlgAttrsBug());      
  78         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
  79         aset.add(new Copies(5));
  80         aset.add(new PageRanges(3,4));
  81         aset.add(DialogTypeSelection.NATIVE);
  82         job.printDialog(aset);        
  83     }
  84 
  85     public static synchronized void pass() {
  86         testPassed = true;
  87         testGeneratedInterrupt = true;
  88         mainThread.interrupt();
  89     }
  90 
  91     public static synchronized void fail() {
  92         testPassed = false;
  93         testGeneratedInterrupt = true;
  94         mainThread.interrupt();
  95     }
  96 
  97     private static void doTest(Runnable action) {
  98         String description
  99                 = " Visual inspection of print dialog is required.\n"
 100                 + " A print dialog will be shown.\n "
 101                 + " Please verify Copies 5 is selected.\n"
 102                 + " Also verify, Page Range is selected with "
 103                 + " from page 3 and to Page 4.\n"
 104                 + " If ok, press PASS else press FAIL";
 105 
 106         final JDialog dialog = new JDialog();
 107         dialog.setTitle("printSelectionTest");
 108         JTextArea textArea = new JTextArea(description);
 109         textArea.setEditable(false);
 110         final JButton testButton = new JButton("Start Test");
 111         final JButton passButton = new JButton("PASS");
 112         passButton.setEnabled(false);
 113         passButton.addActionListener((e) -> {
 114             dialog.dispose();
 115             pass();
 116         });
 117         final JButton failButton = new JButton("FAIL");
 118         failButton.setEnabled(false);
 119         failButton.addActionListener((e) -> {
 120             dialog.dispose();
 121             fail();
 122         });
 123         testButton.addActionListener((e) -> {
 124             testButton.setEnabled(false);
 125             action.run();
 126             passButton.setEnabled(true);
 127             failButton.setEnabled(true);
 128         });
 129         JPanel mainPanel = new JPanel(new BorderLayout());
 130         mainPanel.add(textArea, BorderLayout.CENTER);
 131         JPanel buttonPanel = new JPanel(new FlowLayout());
 132         buttonPanel.add(testButton);
 133         buttonPanel.add(passButton);
 134         buttonPanel.add(failButton);
 135         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
 136         dialog.add(mainPanel);
 137         dialog.pack();
 138         dialog.setVisible(true);
 139     }
 140     
 141     public int print(Graphics g, PageFormat pf, int pi) 
 142             throws PrinterException {
 143         System.out.println("pi = " + pi);
 144         if (pi >= 5) {
 145             return NO_SUCH_PAGE;
 146         }
 147         g.drawString("Page : " + (pi+1), 200, 200);
 148         return PAGE_EXISTS;
 149     }
 150     
 151 }