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 6357905 8170578
  26  * @summary  JobAttributes.getFromPage() and getToPage() always returns 1
  27  * @run main/manual JobAttrUpdateTest
  28  */
  29 import java.awt.BorderLayout;
  30 import java.awt.FlowLayout;
  31 import java.awt.JobAttributes;
  32 import java.awt.PrintJob;
  33 import java.awt.Toolkit;
  34 import java.awt.print.PrinterJob;
  35 import javax.print.PrintService;
  36 import javax.print.attribute.standard.PageRanges;
  37 import javax.swing.JButton;
  38 import javax.swing.JDialog;
  39 import javax.swing.JFrame;
  40 import javax.swing.JPanel;
  41 import javax.swing.JTextArea;
  42 import javax.swing.SwingUtilities;
  43 
  44 public class JobAttrUpdateTest {
  45 
  46     private static Thread mainThread;
  47     private static boolean testPassed;
  48     private static boolean testGeneratedInterrupt;
  49 
  50     public static void main(String[] args) throws Exception {
  51         PrintService prtSrv = PrinterJob.getPrinterJob().getPrintService();
  52         if (!prtSrv.isAttributeCategorySupported(PageRanges.class)) {
  53             return;
  54         }
  55         SwingUtilities.invokeAndWait(() -> {
  56             doTest(JobAttrUpdateTest::printTest);
  57         });
  58         mainThread = Thread.currentThread();
  59         try {
  60             Thread.sleep(30000);
  61         } catch (InterruptedException e) {
  62             if (!testPassed && testGeneratedInterrupt) {
  63                 throw new RuntimeException(""
  64                         + "JobAttributes.getFromPage(),getToPage() not updated correctly");
  65             }
  66         }
  67         if (!testGeneratedInterrupt) {
  68             throw new RuntimeException("user has not executed the test");
  69         }
  70     }
  71 
  72     private static void printTest() {
  73         JobAttributes ja = new JobAttributes();
  74 
  75         Toolkit tk = Toolkit.getDefaultToolkit();
  76         // ja.setToPage(4);
  77         // ja.setFromPage(3);
  78         // show dialog
  79         PrintJob pjob = tk.getPrintJob(new JFrame(), "test", ja, null);
  80         if (pjob == null) {
  81             return;
  82         }
  83 
  84 
  85         if (ja.getDefaultSelection() == JobAttributes.DefaultSelectionType.RANGE) {
  86             int fromPage = ja.getFromPage();
  87             int toPage = ja.getToPage();
  88             if (fromPage != 2 || toPage != 3) {
  89                 fail();
  90             } else {
  91                 pass();
  92             }
  93         }
  94     }
  95 
  96     public static synchronized void pass() {
  97         testPassed = true;
  98         testGeneratedInterrupt = true;
  99         mainThread.interrupt();
 100     }
 101 
 102     public static synchronized void fail() {
 103         testPassed = false;
 104         testGeneratedInterrupt = true;
 105         mainThread.interrupt();
 106     }
 107 
 108     private static void doTest(Runnable action) {
 109         String description
 110                 = " A print dialog will be shown.\n "
 111                 + " Please select Pages within Page-range.\n"
 112                 + " and enter From 2 and To 3. Then Select OK.";
 113 
 114         final JDialog dialog = new JDialog();
 115         dialog.setTitle("JobAttribute Updation Test");
 116         JTextArea textArea = new JTextArea(description);
 117         textArea.setEditable(false);
 118         final JButton testButton = new JButton("Start Test");
 119 
 120         testButton.addActionListener((e) -> {
 121             testButton.setEnabled(false);
 122             action.run();
 123         });
 124         JPanel mainPanel = new JPanel(new BorderLayout());
 125         mainPanel.add(textArea, BorderLayout.CENTER);
 126         JPanel buttonPanel = new JPanel(new FlowLayout());
 127         buttonPanel.add(testButton);
 128         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
 129         dialog.add(mainPanel);
 130         dialog.pack();
 131         dialog.setVisible(true);
 132     }
 133 }