diff --git a/test/jdk/java/awt/print/Dialog/PrintDlgPageable.java b/test/jdk/java/awt/print/Dialog/PrintDlgPageable.java index eadd3cf9cb6bc..ba13ea9682542 100644 --- a/test/jdk/java/awt/print/Dialog/PrintDlgPageable.java +++ b/test/jdk/java/awt/print/Dialog/PrintDlgPageable.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2024, 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 @@ -25,105 +25,98 @@ * @test * @bug 4869502 4869539 * @key printer - * @summary Confirm that ToPage is populated for argument =2. Range is disabled for argument = 0. - * @run main/manual PrintDlgPageable + * @summary Confirm that ToPage is populated for argument = 2. Range is disabled for argument = 0. + * @library /java/awt/regtesthelpers + * @build PassFailJFrame + * @run main/manual PrintDlgPageable 0 + * @run main/manual PrintDlgPageable 2 */ -import java.awt.*; -import java.awt.print.*; -import java.util.Locale; -import javax.print.*; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.print.PageFormat; +import java.awt.print.Pageable; +import java.awt.print.Printable; +import java.awt.print.PrinterJob; +import java.awt.print.PrinterException; + +public class PrintDlgPageable implements Printable { -class PrintDlgPageable implements Printable { public static int arg; - /** - * Constructor - */ - public PrintDlgPageable() { - super(); + public PrintDlgPageable() { + super(); + } + + public static void main(String[] args) throws Exception { + if (args.length < 1) { + System.out.println("usage: java PrintDlgPageable { 0 | 2}"); + return; } - /** - * Starts the application. - */ - public static void main(java.lang.String[] args) { - if (args.length < 1) { - System.out.println("usage: java PrintDlgPageable { 0 | 2}"); - return; - } - arg = Integer.parseInt(args[0]); - PrintDlgPageable pd = new PrintDlgPageable(); - PrinterJob pj = PrinterJob.getPrinterJob(); - PageableHandler handler = new PageableHandler(); - pj.setPageable(handler); - - System.out.println("open PrintDialog.."); - if (pj.printDialog()) { - try { - System.out.println("About to print the data ..."); - pj.print(); - System.out.println("Printed"); - } - catch (PrinterException pe) { - pe.printStackTrace(); - } - } + arg = Integer.parseInt(args[0]); + String INSTRUCTIONS = " A pagedialog will be shown."; + + if (arg == 0) { + INSTRUCTIONS += "\n Confirm that page range is disabled."; + } else if (arg == 2) { + INSTRUCTIONS += "\n Confirm ToPage is populated with pagerange 2"; } + INSTRUCTIONS += "\nCancel the print dialog. Press PASS if it so seen else press FAIL."; + + PrinterJob pj = PrinterJob.getPrinterJob(); + PageableHandler handler = new PageableHandler(); + pj.setPageable(handler); + + PassFailJFrame passFailJFrame = PassFailJFrame.builder() + .title("Instructions") + .instructions(INSTRUCTIONS) + .columns(35) + .build(); - //printable interface - public int print(Graphics g, PageFormat pf, int pi) throws -PrinterException { - - /*if (pi > 0) { - System.out.println("pi is greater than 0"); - return Printable.NO_SUCH_PAGE; - }*/ - // Simply draw two rectangles - Graphics2D g2 = (Graphics2D)g; - g2.setColor(Color.black); - g2.translate(pf.getImageableX(), pf.getImageableY()); - g2.drawRect(1,1,200,300); - g2.drawRect(1,1,25,25); - System.out.println("print method called "+pi + " Orientation "+pf.getOrientation()); - return Printable.PAGE_EXISTS; + if (pj.printDialog()) { + try { + pj.print(); + } catch (PrinterException pe) { + pe.printStackTrace(); + } } + passFailJFrame.awaitAndCheck(); + } + + //printable interface + public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { + + // Simply draw two rectangles + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.black); + g2.translate(pf.getImageableX(), pf.getImageableY()); + g2.drawRect(1, 1, 200, 300); + g2.drawRect(1, 1, 25, 25); + return Printable.PAGE_EXISTS; + } } class PageableHandler implements Pageable { - PageFormat pf = new PageFormat(); + PageFormat pf = new PageFormat(); - public int getNumberOfPages() { - return PrintDlgPageable.arg; - //return 0; - } + public int getNumberOfPages() { + return PrintDlgPageable.arg; + } - public Printable getPrintable(int pageIndex) { - return new PrintDlgPageable(); - } + public Printable getPrintable(int pageIndex) { + return new PrintDlgPageable(); + } - public PageFormat getPageFormat(int pageIndex) { - System.out.println("getPageFormat called "+pageIndex); - if (pageIndex == 0) { - pf.setOrientation(PageFormat.PORTRAIT); - System.out.println("Orientation returned from Pageable "+findOrientation(pf.getOrientation())); - return pf; - } else { - pf.setOrientation(PageFormat.LANDSCAPE); - System.out.println("Orientation returned from Pageable "+findOrientation(pf.getOrientation())); - return pf; - } + public PageFormat getPageFormat(int pageIndex) { + if (pageIndex == 0) { + pf.setOrientation(PageFormat.PORTRAIT); + return pf; + } else { + pf.setOrientation(PageFormat.LANDSCAPE); + return pf; } + } - public String findOrientation(int orient) { - if (orient == PageFormat.LANDSCAPE) { - return "LANDSCAPE"; - }else if (orient == PageFormat.PORTRAIT) { - return "PORTRAIT"; - } else if (orient == PageFormat.REVERSE_LANDSCAPE) { - return "REVERSE LANDSCAPE"; - } else { - return null; - } - } } diff --git a/test/jdk/javax/print/StreamPrintingOrientation.java b/test/jdk/javax/print/StreamPrintingOrientation.java index d7736aa4f620e..77dd8f4cae04f 100644 --- a/test/jdk/javax/print/StreamPrintingOrientation.java +++ b/test/jdk/javax/print/StreamPrintingOrientation.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2024, 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 @@ -24,95 +24,109 @@ /* * @test * @bug 4904236 - * @summary You would see a cross-platform print dialog being popped up. Check whether orientation is shown as LANDSCAPE. Click 'OK'. 'streamexample.ps' will be created in the same dir where this application was executed. Pass if the orientation in the ps file is landscape. - * @run main/manual StreamPrintingOrientation + * @key printer + * @summary StreamPrintService ignores the PrintReqAttrSet when printing through 2D Printing + * @run main StreamPrintingOrientation */ -import java.awt.*; -import java.awt.print.*; -import javax.print.*; -import javax.print.attribute.standard.*; -import javax.print.attribute.*; -import java.io.FileOutputStream; import java.io.File; -import java.util.Locale; +import java.io.FileOutputStream; +import java.nio.file.Files; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.print.PageFormat; +import java.awt.print.Printable; +import java.awt.print.PrinterException; +import java.awt.print.PrinterJob; +import javax.print.attribute.Attribute; +import javax.print.PrintService; +import javax.print.StreamPrintServiceFactory; +import javax.print.attribute.standard.Copies; +import javax.print.attribute.standard.JobName; +import javax.print.attribute.standard.OrientationRequested; +import javax.print.attribute.HashPrintRequestAttributeSet; + +public class StreamPrintingOrientation implements Printable { + + public static void main(String[] args) throws Exception { + StreamPrintingOrientation pd = new StreamPrintingOrientation(); + PrinterJob pj = PrinterJob.getPrinterJob(); + HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet(); + PrintService service = null; + + FileOutputStream fos = null; + String mType = "application/postscript"; -class StreamPrintingOrientation implements Printable { - /** - * Constructor - */ - public StreamPrintingOrientation() { - super(); + File fl = new File("stream_landscape.ps"); + fl.deleteOnExit(); + fos = new FileOutputStream(fl); + StreamPrintServiceFactory[] factories = PrinterJob.lookupStreamPrintServices(mType); + if (factories.length > 0) { + service = factories[0].getPrintService(fos); } - /** - * Starts the application. - */ - public static void main(java.lang.String[] args) { - StreamPrintingOrientation pd = new StreamPrintingOrientation(); - PrinterJob pj = PrinterJob.getPrinterJob(); - HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet(); - PrintService service = null; - FileOutputStream fos = null; - File f = null, f1 = null; - String mType = "application/postscript"; + if (service != null) { + System.out.println("Stream Print Service " + service); + pj.setPrintService(service); + } else { + throw new RuntimeException("No stream Print Service available."); + } + + pj.setPrintable(pd); + prSet.add(OrientationRequested.LANDSCAPE); + prSet.add(new Copies(1)); + prSet.add(new JobName("orientation test", null)); + System.out.println("open PrintDialog.."); - try { - f = new File("streamexample.ps"); - fos = new FileOutputStream(f); - StreamPrintServiceFactory[] factories = PrinterJob.lookupStreamPrintServices(mType); - if (factories.length > 0) - service = factories[0].getPrintService(fos); + System.out.println("\nValues in attr set passed to print method"); + Attribute attr[] = prSet.toArray(); + for (int x = 0; x < attr.length; x++) { + System.out.println("Name " + attr[x].getName() + " " + attr[x]); + } + System.out.println("About to print the data ..."); + if (service != null) { + System.out.println("TEST: calling Print"); + pj.print(prSet); + System.out.println("TEST: Printed"); + } - if (service != null) { - System.out.println("Stream Print Service "+service); - pj.setPrintService(service); - } else { - throw new RuntimeException("No stream Print Service available."); - } - } catch (Exception e) { - e.printStackTrace(); - } + File fp = new File("stream_portrait.ps"); + fp.deleteOnExit(); + fos = new FileOutputStream(fp); + if (factories.length > 0) { + service = factories[0].getPrintService(fos); + } - pj.setPrintable(pd); - prSet.add(OrientationRequested.LANDSCAPE); - prSet.add(new Copies(3)); - prSet.add(new JobName("orientation test", null)); - System.out.println("open PrintDialog.."); - if (pj.printDialog(prSet)) { - try { - System.out.println("\nValues in attr set passed to print method"); - Attribute attr[] = prSet.toArray(); - for (int x = 0; x < attr.length; x ++) { - System.out.println("Name "+attr[x].getName()+" "+attr[x]); - } - System.out.println("About to print the data ..."); - if (service != null) { - System.out.println("TEST: calling Print"); - pj.print(prSet); - System.out.println("TEST: Printed"); - } - } - catch (PrinterException pe) { - pe.printStackTrace(); - } - } + pj.setPrintService(service); + pj.setPrintable(pd); + prSet.add(OrientationRequested.PORTRAIT); + prSet.add(new Copies(1)); + prSet.add(new JobName("orientation test", null)); + if (service != null) { + pj.print(prSet); + } + if (Files.mismatch(fl.toPath(), fp.toPath()) == -1) { + throw new RuntimeException("Printing stream orientation is same " + + "for both PORTRAIT and LANDSCAPE"); } + } - //printable interface - public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { + //printable interface + public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { - if (pi > 0) { - return Printable.NO_SUCH_PAGE; - } - // Simply draw two rectangles - Graphics2D g2 = (Graphics2D)g; - g2.setColor(Color.black); - g2.translate(pf.getImageableX(), pf.getImageableY()); - System.out.println("StreamPrinting Test Width "+pf.getWidth()+" Height "+pf.getHeight()); - g2.drawRect(1,1,200,300); - g2.drawRect(1,1,25,25); - return Printable.PAGE_EXISTS; + if (pi > 0) { + return Printable.NO_SUCH_PAGE; } + // Simply draw two rectangles + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.black); + g2.translate(pf.getImageableX(), pf.getImageableY()); + System.out.println("StreamPrinting Test Width " + pf.getWidth() + " Height " + pf.getHeight()); + g2.drawRect(1, 1, 200, 300); + g2.drawRect(1, 1, 25, 25); + return Printable.PAGE_EXISTS; + } }