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 5080830
  26  * @summary Verify if SheetCollate option is disabled for flavors that do not
  27  *          support SheetCollate
  28  * @run main/manual ServiceDlgSheetCollateTest
  29  */
  30 import java.awt.BorderLayout;
  31 import java.awt.FlowLayout;
  32 import javax.print.DocFlavor;
  33 import javax.print.PrintService;
  34 import javax.print.PrintServiceLookup;
  35 import javax.print.ServiceUI;
  36 import javax.print.attribute.Attribute;
  37 import javax.print.attribute.HashPrintRequestAttributeSet;
  38 import javax.print.attribute.standard.SheetCollate;
  39 import javax.swing.JButton;
  40 import javax.swing.JDialog;
  41 import javax.swing.JPanel;
  42 import javax.swing.JTextArea;
  43 import javax.swing.SwingUtilities;
  44 
  45 public class ServiceDlgSheetCollateTest {
  46     private static Thread mainThread;
  47     private static boolean testPassed;
  48     private static boolean testGeneratedInterrupt;
  49 
  50     /**
  51      * Starts the application.
  52      */
  53     public static void main(java.lang.String[] args) throws Exception {
  54         SwingUtilities.invokeAndWait(() -> {
  55             doTest(ServiceDlgSheetCollateTest::printTest);
  56         });
  57         mainThread = Thread.currentThread();
  58         try {
  59             Thread.sleep(600000);
  60         } catch (InterruptedException e) {
  61             if (!testPassed && testGeneratedInterrupt) {
  62                 throw new RuntimeException("SheetCollate option is not disabled "
  63                         + "for flavors that do not support sheetCollate");
  64             }
  65         }
  66         if (!testGeneratedInterrupt) {
  67             throw new RuntimeException("user has not executed the test");
  68         }
  69     }
  70 
  71     private static void printTest() {
  72         ServiceDlgSheetCollateTest pd = new ServiceDlgSheetCollateTest();
  73         DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
  74         //DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
  75         PrintService defService = null, service[] = null;
  76         defService = PrintServiceLookup.lookupDefaultPrintService();
  77         service = PrintServiceLookup.lookupPrintServices(flavor, null);
  78 
  79         System.out.println("\nDefault print service: " + service );
  80         System.out.println("is flavor: "+flavor+" supported? "+defService.isDocFlavorSupported(flavor));
  81         System.out.println("is SheetCollate category supported? "+defService.isAttributeCategorySupported(SheetCollate.class));
  82         System.out.println("is SheetCollate.COLLATED value supported ? "+defService.isAttributeValueSupported(SheetCollate.COLLATED, flavor, null));
  83 
  84         HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet();
  85         try {
  86             PrintService selService = ServiceUI.printDialog(null, 200, 200, service, defService, flavor, prSet);
  87         } catch (IllegalArgumentException ia) {
  88             System.out.println("Exception thrown : " + ia);
  89         }
  90 
  91         System.out.println("\nSelected Values\n");
  92         Attribute attr[] = prSet.toArray();
  93         for (int x = 0; x < attr.length; x ++) {
  94             System.out.println("Attribute: " + attr[x].getName() + " Value: " + attr[x]);
  95         }
  96 
  97     }
  98 
  99     public static synchronized void pass() {
 100         testPassed = true;
 101         testGeneratedInterrupt = true;
 102         mainThread.interrupt();
 103     }
 104 
 105     public static synchronized void fail() {
 106         testPassed = false;
 107         testGeneratedInterrupt = true;
 108         mainThread.interrupt();
 109     }
 110 
 111     private static void doTest(Runnable action) {
 112         String description
 113                 = " A print dialog would appear.\n"
 114                 + " Increase the no. of copies.\n"
 115                 + " If COLLATE checkbox gets enabled, press FAIL else press PASS.\n"
 116                 + " Press Cancel to close the dialog.";
 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 }
 153