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 4885375
  26  * @summary  Verifies if PageRanges To Field is populated based on Pageable
  27  *           for COMMON print dialog
  28  * @run main/manual PrintDlgPageable
  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.Pageable;
  37 import java.awt.print.Printable;
  38 import java.awt.print.PrinterException;
  39 import java.awt.print.PrinterJob;
  40 import javax.print.attribute.HashPrintRequestAttributeSet;
  41 import javax.print.attribute.PrintRequestAttributeSet;
  42 import javax.print.attribute.standard.DialogTypeSelection;
  43 import javax.swing.JButton;
  44 import javax.swing.JDialog;
  45 import javax.swing.JPanel;
  46 import javax.swing.JTextArea;
  47 import javax.swing.SwingUtilities;
  48 
  49 public class PrintDlgPageable implements Printable {
  50         
  51     private static Thread mainThread;
  52     private static boolean testPassed;
  53     private static boolean testGeneratedInterrupt;
  54 
  55     public static void main(String[] args)  throws Exception {
  56         SwingUtilities.invokeAndWait(() -> {
  57             doTest(PrintDlgPageable::printTest);
  58         });
  59         mainThread = Thread.currentThread();
  60         try {
  61             Thread.sleep(30000);
  62         } catch (InterruptedException e) {
  63             if (!testPassed && testGeneratedInterrupt) {
  64                 throw new RuntimeException("Print Dialog does not " + 
  65                           "`reflect Copies or Page Ranges");
  66             }
  67         }
  68         if (!testGeneratedInterrupt) {
  69             throw new RuntimeException("user has not executed the test");
  70         }
  71     }
  72 
  73     private static void printTest() {
  74         PrinterJob pj = PrinterJob.getPrinterJob();
  75         PageableHandler handler = new PageableHandler();
  76         pj.setPageable(handler);
  77         
  78         PrintRequestAttributeSet pSet =  new HashPrintRequestAttributeSet();
  79         pSet.add(DialogTypeSelection.COMMON);
  80         pj.printDialog(pSet);
  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                 = " Visual inspection of print dialog is required.\n"
  99                 + " A print dialog will be shown.\n "
 100                 + " Please verify Page Range is populated \n"
 101                 + " with \"From\" 1 and \"To\" 5.\n"
 102                 + " If ok, press PASS else press FAIL";
 103 
 104         final JDialog dialog = new JDialog();
 105         dialog.setTitle("printSelectionTest");
 106         JTextArea textArea = new JTextArea(description);
 107         textArea.setEditable(false);
 108         final JButton testButton = new JButton("Start Test");
 109         final JButton passButton = new JButton("PASS");
 110         passButton.setEnabled(false);
 111         passButton.addActionListener((e) -> {
 112             dialog.dispose();
 113             pass();
 114         });
 115         final JButton failButton = new JButton("FAIL");
 116         failButton.setEnabled(false);
 117         failButton.addActionListener((e) -> {
 118             dialog.dispose();
 119             fail();
 120         });
 121         testButton.addActionListener((e) -> {
 122             testButton.setEnabled(false);
 123             action.run();
 124             passButton.setEnabled(true);
 125             failButton.setEnabled(true);
 126         });
 127         JPanel mainPanel = new JPanel(new BorderLayout());
 128         mainPanel.add(textArea, BorderLayout.CENTER);
 129         JPanel buttonPanel = new JPanel(new FlowLayout());
 130         buttonPanel.add(testButton);
 131         buttonPanel.add(passButton);
 132         buttonPanel.add(failButton);
 133         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
 134         dialog.add(mainPanel);
 135         dialog.pack();
 136         dialog.setVisible(true);
 137         dialog.addWindowListener(new WindowAdapter() {
 138             @Override
 139             public void windowClosing(WindowEvent e) {
 140                 System.out.println("main dialog closing");
 141                 testGeneratedInterrupt = false;
 142                 mainThread.interrupt();
 143             }
 144         });
 145     }
 146     
 147     @Override
 148     public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
 149         return NO_SUCH_PAGE;        
 150     }
 151 }
 152 
 153 class PageableHandler implements Pageable {
 154         
 155     PageFormat pf = new PageFormat();
 156     
 157     @Override
 158     public int getNumberOfPages() {
 159         return 0;
 160     }
 161     
 162     @Override
 163     public Printable getPrintable(int pageIndex) {
 164         return new PrintDlgPageable();
 165     }
 166     
 167     @Override
 168     public PageFormat getPageFormat(int pageIndex) {
 169         return pf;
 170         
 171     }   
 172 }