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 6529030
  26  * @summary  Java Printing: Print range > Selection gets enabled
  27  * @run main/manual PrintDlgSelectionAttribTest
  28  */
  29 import java.awt.BorderLayout;
  30 import java.awt.FlowLayout;
  31 import java.awt.Graphics;
  32 import java.awt.print.Printable;
  33 import java.awt.print.PageFormat;
  34 import java.awt.print.PrinterException;
  35 import java.awt.print.PrinterJob;
  36 import javax.swing.JButton;
  37 import javax.swing.JDialog;
  38 import javax.swing.JPanel;
  39 import javax.swing.JTextArea;
  40 import javax.swing.SwingUtilities;
  41 
  42 public class PrintDlgSelectionAttribTest {
  43 
  44     private static Thread mainThread;
  45     private static boolean testPassed;
  46     private static boolean testGeneratedInterrupt;
  47     private static PrinterJob printJob;
  48 
  49     public static void print() {
  50 
  51         // Set working printable to print pages
  52         printJob.setPrintable(new Printable() {
  53             public int print(Graphics graphics, PageFormat pageFormat,
  54                     int pageIndex) throws PrinterException {
  55                 return NO_SUCH_PAGE;
  56             }
  57         });
  58 
  59         // Display Print dialog
  60         if (!printJob.printDialog()) {
  61             System.out.println("\tPrinting canceled by user");
  62             return;
  63         }
  64 
  65         try {
  66             printJob.print();
  67         } catch (PrinterException e) {
  68         }
  69     }
  70 
  71     public static void printTest() {
  72         printJob = PrinterJob.getPrinterJob();
  73         System.out.println(" -=- Starting printing #1 -=-");
  74         print();
  75         System.out.println(" -=- Starting printing #2 -=-");
  76         print();
  77     }
  78 
  79     public static void main(String[] args) throws Exception {
  80         SwingUtilities.invokeAndWait(() -> {
  81             doTest(PrintDlgSelectionAttribTest::printTest);
  82         });
  83         mainThread = Thread.currentThread();
  84         try {
  85             Thread.sleep(30000);
  86         } catch (InterruptedException e) {
  87             if (!testPassed && testGeneratedInterrupt) {
  88                 throw new RuntimeException(""
  89                         + "Selection radio button is enabled in print dialog");
  90             }
  91         }
  92         if (!testGeneratedInterrupt) {
  93             throw new RuntimeException("user has not executed the test");
  94         }
  95     }
  96 
  97     public static synchronized void pass() {
  98         testPassed = true;
  99         testGeneratedInterrupt = true;
 100         mainThread.interrupt();
 101     }
 102 
 103     public static synchronized void fail() {
 104         testPassed = false;
 105         testGeneratedInterrupt = true;
 106         mainThread.interrupt();
 107     }
 108 
 109     private static void doTest(Runnable action) {
 110         String description
 111                 = " Visual inspection of print dialog is required.\n"
 112                 + " Initially, a print dialog will be shown.\n "
 113                 + " Please verify Selection radio button is disabled.\n"
 114                 + " Press OK. Then 2nd print dialog will be shown.\n"
 115                 + " Please verify the Selection radio button is disabled\n"
 116                 + " in 2nd print dialog. If disabled, press PASS else press fail";
 117 
 118         final JDialog dialog = new JDialog();
 119         dialog.setTitle("printSelectionTest");
 120         JTextArea textArea = new JTextArea(description);
 121         textArea.setEditable(false);
 122         final JButton testButton = new JButton("Start Test");
 123         final JButton passButton = new JButton("PASS");
 124         passButton.setEnabled(false);
 125         passButton.addActionListener((e) -> {
 126             dialog.dispose();
 127             pass();
 128         });
 129         final JButton failButton = new JButton("FAIL");
 130         failButton.setEnabled(false);
 131         failButton.addActionListener((e) -> {
 132             dialog.dispose();
 133             fail();
 134         });
 135         testButton.addActionListener((e) -> {
 136             testButton.setEnabled(false);
 137             action.run();
 138             passButton.setEnabled(true);
 139             failButton.setEnabled(true);
 140         });
 141         JPanel mainPanel = new JPanel(new BorderLayout());
 142         mainPanel.add(textArea, BorderLayout.CENTER);
 143         JPanel buttonPanel = new JPanel(new FlowLayout());
 144         buttonPanel.add(testButton);
 145         buttonPanel.add(passButton);
 146         buttonPanel.add(failButton);
 147         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
 148         dialog.add(mainPanel);
 149         dialog.pack();
 150         dialog.setVisible(true);
 151     }
 152 }