1 /*
   2  * Copyright (c) 2009, 2013, 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 /*
  25  * @test
  26  * @bug 4500750 6848799 8028584
  27  * @summary Tests creating page format from attributes
  28  * @run main PageFormatFromAttributes
  29  */
  30 import java.awt.print.*;
  31 import javax.print.*;
  32 import javax.print.attribute.*;
  33 import javax.print.attribute.standard.*;
  34 
  35 public class PageFormatFromAttributes {
  36 
  37     public static void main(String args[]) {
  38         PrinterJob job = PrinterJob.getPrinterJob();
  39         PrintService service = job.getPrintService();
  40         if (service == null) {
  41             return; // No printers
  42         }
  43         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
  44         test(job, MediaSizeName.ISO_A4, OrientationRequested.PORTRAIT);
  45         test(job, MediaSizeName.ISO_A4, OrientationRequested.LANDSCAPE);
  46         test(job, MediaSizeName.ISO_A4,
  47              OrientationRequested.REVERSE_LANDSCAPE);
  48         test(job, MediaSizeName.ISO_A3, OrientationRequested.PORTRAIT);
  49         test(job, MediaSizeName.NA_LETTER, OrientationRequested.PORTRAIT);
  50         test(job, MediaSizeName.NA_LETTER, OrientationRequested.LANDSCAPE);
  51         test(job, MediaSizeName.NA_LEGAL, OrientationRequested.PORTRAIT);
  52     }
  53 
  54     static void test(PrinterJob job,
  55                      MediaSizeName media, OrientationRequested orient) {
  56 
  57         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
  58         aset.add(media);
  59         aset.add(orient);
  60 
  61         PrintService service = job.getPrintService();
  62         if (!service.isAttributeValueSupported(media, null, aset) ||
  63             !service.isAttributeValueSupported(orient, null, aset)) {
  64             return; // Can't test this case.
  65         }
  66         PageFormat pf = job.getPageFormat(aset);
  67         boolean ok = true;
  68         switch (pf.getOrientation()) {
  69         case PageFormat.PORTRAIT :
  70             ok = orient == OrientationRequested.PORTRAIT;
  71             break;
  72         case PageFormat.LANDSCAPE :
  73             ok = orient == OrientationRequested.LANDSCAPE;
  74             break;
  75         case PageFormat.REVERSE_LANDSCAPE :
  76             ok = orient == OrientationRequested.REVERSE_LANDSCAPE;
  77             break;
  78         }
  79         if (!ok) {
  80             throw new RuntimeException("orientation not as specified");
  81         }
  82         MediaSize mediaSize = MediaSize.getMediaSizeForName(media);
  83         if (mediaSize == null) {
  84             throw new RuntimeException("expected a media size");
  85         }
  86         double units = Size2DSyntax.INCH/72.0;
  87         double w = mediaSize.getX(1) / units;
  88         double h = mediaSize.getY(1) / units;
  89         Paper paper = pf.getPaper();
  90         double pw = paper.getWidth();
  91         double ph = paper.getHeight();
  92         if (Math.round(pw) != Math.round(w) || 
  93             Math.round(ph) != Math.round(h)) {
  94             throw new RuntimeException("size not as specified");
  95         }
  96     }
  97 }