6357887: selected printertray is ignored under linux
authorpsadhukhan
Tue, 30 Aug 2016 11:07:58 +0530
changeset 40729 aae3aa5fd024
parent 40728 1ed235a29653
child 40730 0d19d6d18c0b
6357887: selected printertray is ignored under linux Reviewed-by: prr, vadim
jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java
jdk/src/java.desktop/unix/classes/sun/print/UnixPrintJob.java
jdk/test/java/awt/print/PrinterJob/TestMediaTraySelection.java
--- a/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java	Mon Aug 29 08:53:32 2016 -0700
+++ b/jdk/src/java.desktop/share/classes/sun/print/PSPrinterJob.java	Tue Aug 30 11:07:58 2016 +0530
@@ -33,11 +33,8 @@
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.HeadlessException;
-import java.awt.Rectangle;
 import java.awt.Shape;
 
-import java.awt.image.BufferedImage;
-
 import java.awt.font.FontRenderContext;
 
 import java.awt.geom.AffineTransform;
@@ -46,7 +43,6 @@
 
 import java.awt.image.BufferedImage;
 
-import java.awt.peer.FontPeer;
 import java.awt.print.Pageable;
 import java.awt.print.PageFormat;
 import java.awt.print.Paper;
@@ -55,14 +51,12 @@
 import java.awt.print.PrinterIOException;
 import java.awt.print.PrinterJob;
 
-import javax.print.DocFlavor;
 import javax.print.PrintService;
 import javax.print.StreamPrintService;
 import javax.print.attribute.HashPrintRequestAttributeSet;
 import javax.print.attribute.PrintRequestAttributeSet;
 import javax.print.attribute.PrintServiceAttributeSet;
 import javax.print.attribute.standard.PrinterName;
-import javax.print.attribute.standard.Chromaticity;
 import javax.print.attribute.standard.Copies;
 import javax.print.attribute.standard.Destination;
 import javax.print.attribute.standard.DialogTypeSelection;
@@ -72,7 +66,6 @@
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
-import java.io.CharConversionException;
 import java.io.File;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -85,17 +78,14 @@
 import java.io.StringWriter;
 
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.Locale;
 import java.util.Properties;
 
 import sun.awt.CharsetString;
 import sun.awt.FontConfiguration;
-import sun.awt.FontDescriptor;
 import sun.awt.PlatformFont;
 import sun.awt.SunToolkit;
 import sun.font.FontAccess;
-import sun.font.FontManagerFactory;
 import sun.font.FontUtilities;
 
 import java.nio.charset.*;
@@ -105,7 +95,9 @@
 
 //REMIND: Remove use of this class when IPPPrintService is moved to share directory.
 import java.lang.reflect.Method;
+import javax.print.attribute.Attribute;
 import javax.print.attribute.standard.JobSheets;
+import javax.print.attribute.standard.Media;
 
 /**
  * A class which initiates and executes a PostScript printer job.
@@ -489,6 +481,23 @@
         return doPrint;
     }
 
+    @Override
+    protected void setAttributes(PrintRequestAttributeSet attributes)
+                                 throws PrinterException {
+        super.setAttributes(attributes);
+        if (attributes == null) {
+            return; // now always use attributes, so this shouldn't happen.
+        }
+        Attribute attr = attributes.get(Media.class);
+        if (attr instanceof CustomMediaTray) {
+            CustomMediaTray customTray = (CustomMediaTray)attr;
+            String choice = customTray.getChoiceName();
+            if (choice != null) {
+                mOptions = " InputSlot="+ choice;
+            }
+        }
+    }
+
     /**
      * Invoked by the RasterPrinterJob super class
      * this method is called to mark the start of a
@@ -1629,7 +1638,7 @@
                 execCmd[n++] = "-o job-sheets=standard";
             }
             if ((pFlags & OPTIONS) != 0) {
-                execCmd[n++] = new String(options);
+                execCmd[n++] = "-o" + options;
             }
         } else {
             ncomps+=1; //add 1 arg for lp
--- a/jdk/src/java.desktop/unix/classes/sun/print/UnixPrintJob.java	Mon Aug 29 08:53:32 2016 -0700
+++ b/jdk/src/java.desktop/unix/classes/sun/print/UnixPrintJob.java	Tue Aug 30 11:07:58 2016 +0530
@@ -41,14 +41,12 @@
 import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.StringWriter;
-import java.io.UnsupportedEncodingException;
 import java.nio.file.Files;
 import java.util.Vector;
 
 import javax.print.CancelablePrintJob;
 import javax.print.Doc;
 import javax.print.DocFlavor;
-import javax.print.DocPrintJob;
 import javax.print.PrintService;
 import javax.print.PrintException;
 import javax.print.event.PrintJobEvent;
@@ -56,7 +54,6 @@
 import javax.print.event.PrintJobAttributeListener;
 
 import javax.print.attribute.Attribute;
-import javax.print.attribute.AttributeSet;
 import javax.print.attribute.AttributeSetUtilities;
 import javax.print.attribute.DocAttributeSet;
 import javax.print.attribute.HashPrintJobAttributeSet;
@@ -65,7 +62,6 @@
 import javax.print.attribute.PrintJobAttributeSet;
 import javax.print.attribute.PrintRequestAttribute;
 import javax.print.attribute.PrintRequestAttributeSet;
-import javax.print.attribute.PrintServiceAttributeSet;
 import javax.print.attribute.standard.Copies;
 import javax.print.attribute.standard.Destination;
 import javax.print.attribute.standard.DocumentName;
@@ -77,13 +73,17 @@
 import javax.print.attribute.standard.MediaSize;
 import javax.print.attribute.standard.MediaSizeName;
 import javax.print.attribute.standard.OrientationRequested;
-import javax.print.attribute.standard.PrinterName;
 import javax.print.attribute.standard.RequestingUserName;
 import javax.print.attribute.standard.NumberUp;
 import javax.print.attribute.standard.Sides;
 import javax.print.attribute.standard.PrinterIsAcceptingJobs;
 
-import java.awt.print.*;
+import java.awt.print.PageFormat;
+import java.awt.print.PrinterJob;
+import java.awt.print.Pageable;
+import java.awt.print.Paper;
+import java.awt.print.Printable;
+import java.awt.print.PrinterException;
 
 
 
@@ -370,7 +370,7 @@
                  customTray instanceof CustomMediaTray) {
                  String choice = customTray.getChoiceName();
                  if (choice != null) {
-                     mOptions += " media="+choice;
+                     mOptions += " InputSlot="+choice;
                  }
              }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/print/PrinterJob/TestMediaTraySelection.java	Tue Aug 30 11:07:58 2016 +0530
@@ -0,0 +1,185 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/*
+ * @test
+ * @bug 6357887
+ * @summary  Verifies if selected printertray is used
+ * @requires (os.family == "linux" | os.family == "solaris")
+ * @run main/manual TestMediaTraySelection
+ */
+
+import java.awt.BorderLayout;
+import java.awt.FlowLayout;
+import java.awt.Graphics;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.awt.print.PageFormat;
+import java.awt.print.Printable;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterJob;
+import javax.print.DocFlavor;
+import javax.print.PrintService;
+import javax.print.PrintServiceLookup;
+import javax.print.attribute.HashPrintRequestAttributeSet;
+import javax.print.attribute.PrintRequestAttributeSet;
+import javax.print.attribute.standard.Media;
+import javax.print.attribute.standard.MediaTray;
+import javax.swing.JButton;
+import javax.swing.JDialog;
+import javax.swing.JPanel;
+import javax.swing.JTextArea;
+import javax.swing.SwingUtilities;
+
+public class TestMediaTraySelection implements Printable {
+
+    private static Thread mainThread;
+    private static boolean testPassed;
+    private static boolean testGeneratedInterrupt;
+    private static PrintService prservices;
+
+    public static void main(String[] args)  throws Exception {
+        prservices = PrintServiceLookup.lookupDefaultPrintService();
+        if (prservices == null) {
+            System.out.println("No print service found");
+            return;
+        }
+        System.out.println(" Print service " + prservices);
+        SwingUtilities.invokeAndWait(() -> {
+            doTest(TestMediaTraySelection::printTest);
+        });
+        mainThread = Thread.currentThread();
+        try {
+            Thread.sleep(90000);
+        } catch (InterruptedException e) {
+            if (!testPassed && testGeneratedInterrupt) {
+                throw new RuntimeException("Banner page did not print");
+            }
+        }
+        if (!testGeneratedInterrupt) {
+            throw new RuntimeException("user has not executed the test");
+        }
+    }
+
+    private static void printTest() {
+
+        MediaTray tray = null;
+        //tray = getMediaTray( prservices, "Bypass Tray" );
+        tray = getMediaTray( prservices, "Tray 4" );
+        PrintRequestAttributeSet atrset = new HashPrintRequestAttributeSet();
+        //atrset.add( MediaSizeName.ISO_A4 );
+        atrset.add(tray);
+        PrinterJob pjob = PrinterJob.getPrinterJob();
+        pjob.setPrintable(new TestMediaTraySelection());
+        try {
+            pjob.print(atrset);
+        } catch (PrinterException e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+
+    static MediaTray getMediaTray( PrintService ps, String name) {
+         Media[] media  = (Media[])ps.getSupportedAttributeValues( Media.class,
+                 DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
+
+        for (Media m : media) {
+            if ( m instanceof MediaTray) {
+                System.out.println("MediaTray=" + m.toString() );
+                if ( m.toString().trim().indexOf( name ) > -1 ) {
+                    return (MediaTray)m;
+                }
+            }
+        }
+        return null;
+    }
+
+    public static synchronized void pass() {
+        testPassed = true;
+        testGeneratedInterrupt = true;
+        mainThread.interrupt();
+    }
+
+    public static synchronized void fail() {
+        testPassed = false;
+        testGeneratedInterrupt = true;
+        mainThread.interrupt();
+    }
+
+    private static void doTest(Runnable action) {
+        String description
+                = " Please verify the \"Tray 4\" of printer is used for printout\n"
+                + " and not standard/auto tray. If yes, press PASS else press FAIL";
+
+        final JDialog dialog = new JDialog();
+        dialog.setTitle("printBannerTest");
+        JTextArea textArea = new JTextArea(description);
+        textArea.setEditable(false);
+        final JButton testButton = new JButton("Start Test");
+        final JButton passButton = new JButton("PASS");
+        passButton.setEnabled(false);
+        passButton.addActionListener((e) -> {
+            dialog.dispose();
+            pass();
+        });
+        final JButton failButton = new JButton("FAIL");
+        failButton.setEnabled(false);
+        failButton.addActionListener((e) -> {
+            dialog.dispose();
+            fail();
+        });
+        testButton.addActionListener((e) -> {
+            testButton.setEnabled(false);
+            action.run();
+            passButton.setEnabled(true);
+            failButton.setEnabled(true);
+        });
+        JPanel mainPanel = new JPanel(new BorderLayout());
+        mainPanel.add(textArea, BorderLayout.CENTER);
+        JPanel buttonPanel = new JPanel(new FlowLayout());
+        buttonPanel.add(testButton);
+        buttonPanel.add(passButton);
+        buttonPanel.add(failButton);
+        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
+        dialog.add(mainPanel);
+        dialog.pack();
+        dialog.setVisible(true);
+        dialog.addWindowListener(new WindowAdapter() {
+            @Override
+            public void windowClosing(WindowEvent e) {
+                System.out.println("main dialog closing");
+                testGeneratedInterrupt = false;
+                mainThread.interrupt();
+            }
+        });
+    }
+
+    @Override
+    public int print(Graphics g, PageFormat pf, int pi) {
+        System.out.println("pi = " + pi);
+        if (pi > 0) {
+            return NO_SUCH_PAGE;
+        }
+        g.drawString("Testing : " , 200, 200);
+        return PAGE_EXISTS;
+    }
+}