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 8151590
  26  * @summary  All radio button should be selected when we call
  27  *            setDefaultSelection(JobAttributes.DefaultSelectionType.ALL);
  28  * @run main/manual PrintTest
  29  */
  30 import java.awt.BorderLayout;
  31 import java.awt.Component;
  32 import java.awt.FlowLayout;
  33 import java.awt.JobAttributes;
  34 import java.awt.PageAttributes;
  35 import java.awt.PrintJob;
  36 import java.awt.Toolkit;
  37 import javax.swing.JButton;
  38 import javax.swing.JDialog;
  39 import javax.swing.JFrame;
  40 import javax.swing.JOptionPane;
  41 import javax.swing.JPanel;
  42 import javax.swing.JTextArea;
  43 import javax.swing.SwingUtilities;
  44 
  45 public class PrintTest {
  46 
  47     private static Thread mainThread;
  48     private static boolean testPassed;
  49     private static boolean testGeneratedInterrupt;
  50 
  51     public static void main(String[] args) throws Exception {
  52         SwingUtilities.invokeAndWait(new Runnable() {
  53             @Override
  54             public void run() {
  55                 doTest(PrintTest::printTest);
  56             }
  57         });
  58         mainThread = Thread.currentThread();
  59         try {
  60             mainThread.sleep(30000);
  61         } catch (InterruptedException e) {
  62             if (!testPassed && testGeneratedInterrupt) {
  63                 throw new RuntimeException("All radio button is not selected");
  64             }
  65         }
  66         if (!testGeneratedInterrupt) {
  67             throw new RuntimeException("user has not executed the test");
  68         }
  69     }
  70 
  71     private static void printTest() {
  72         JobAttributes job = new JobAttributes();
  73         PageAttributes page = new PageAttributes();
  74         job.setDialog(JobAttributes.DialogType.NATIVE);
  75         job.setDefaultSelection(JobAttributes.DefaultSelectionType.ALL);
  76         job.setFromPage(2);
  77         job.setToPage(5);
  78         Toolkit tk = Toolkit.getDefaultToolkit();
  79         // seeting this dialog to native pritdialog
  80         if (tk != null) {
  81             PrintJob pj = tk.getPrintJob(new JFrame(), "testing the attribute setting ", job, page);
  82         }
  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 wil lbe shown.\n "
 101                 + " Please verify ALL radio button is selected.\n"
 102                 + " If ALL radio button is selected, 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     }
 138 }