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