8324808: Manual printer tests have no Pass/Fail buttons, instructions close set 3

Reviewed-by: tr, achung, aivanov
This commit is contained in:
Renjith Kannath Pariyangad 2024-04-02 04:28:50 +00:00 committed by Phil Race
parent d3fc8df8af
commit af7c6af0cc
6 changed files with 554 additions and 1327 deletions

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -21,209 +21,93 @@
* questions. * questions.
*/ */
/**
* @test
* @bug 4956397
* @key printer
* @run main/manual PageDlgPrnButton
*/
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.* ; import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class PageDlgPrnButton implements Printable import jtreg.SkippedException;
{
public static void main ( String args[] ) {
String[] instructions = /*
{"For non-windows OS, this test PASSes.", * @test
"You must have at least 2 printers available to perform this test.", * @bug 4956397
"This test brings up a native Windows page dialog.", * @key printer
"Click on the Printer... button and change the selected printer. ", * @requires os.family=="windows"
"Test passes if the printout comes from the new selected printer.", * @library /test/lib /java/awt/regtesthelpers
}; * @build PassFailJFrame jtreg.SkippedException
* @run main/manual PageDlgPrnButton
*/
public class PageDlgPrnButton implements Printable {
private static final String INSTRUCTIONS =
"This test brings up a native Windows page dialog.\n" +
"Click on the Printer... button and change the selected printer. \n" +
"Test passes if the printout comes from the new selected printer.";
Sysout.createDialog( ); public static void main(String[] args) throws Exception {
Sysout.printInstructions( instructions ); final int serviceCount = PrinterJob.lookupPrintServices().length;
if (serviceCount == 0) {
PageDlgPrnButton pdpb = new PageDlgPrnButton() ; throw new RuntimeException("Printer not configured or available.");
} }
if (serviceCount < 2) {
public PageDlgPrnButton() throw new SkippedException("The test requires at least 2 printers.");
{
try
{
pageDialogExample();
} }
catch(Exception e)
{e.printStackTrace(System.err);}
}
PassFailJFrame passFailJFrame = PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(45)
.build();
pageDialogExample();
passFailJFrame.awaitAndCheck();
}
// This example just displays the page dialog - you cannot change // This example just displays the page dialog - you cannot change
// the printer (press the "Printer..." button and choose one if you like). // the printer (press the "Printer..." button and choose one if you like).
public void pageDialogExample() throws PrinterException public static void pageDialogExample() throws PrinterException {
{
PrinterJob job = PrinterJob.getPrinterJob(); PrinterJob job = PrinterJob.getPrinterJob();
PageFormat originalPageFormat = job.defaultPage(); PageFormat originalPageFormat = job.defaultPage();
PageFormat pageFormat = job.pageDialog(originalPageFormat); PageFormat pageFormat = job.pageDialog(originalPageFormat);
if(originalPageFormat == pageFormat) return; job.setPrintable(new PageDlgPrnButton(), pageFormat);
if (job.printDialog()) {
job.setPrintable(this,pageFormat); job.print();
job.print(); }
} }
@Override
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
final int boxWidth = 100; final int boxWidth = 100;
final int boxHeight = 100; final int boxHeight = 100;
final Rectangle rect = new Rectangle(0,0,boxWidth,boxHeight); final Rectangle rect = new Rectangle(0, 0, boxWidth, boxHeight);
final double pageH = pageFormat.getImageableHeight(); final double pageH = pageFormat.getImageableHeight();
final double pageW = pageFormat.getImageableWidth(); final double pageW = pageFormat.getImageableWidth();
final Graphics2D g2d = (Graphics2D) g;
if (pageIndex > 0) return (NO_SUCH_PAGE); if (pageIndex > 0) {
return NO_SUCH_PAGE;
final Graphics2D g2d = (Graphics2D)g; }
// Move the (x,y) origin to account for the left-hand and top margins // Move the (x,y) origin to account for the left-hand and top margins
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
// Draw the page bounding box // Draw the page bounding box
g2d.drawRect(0,0,(int)pageW,(int)pageH); g2d.drawRect(0, 0, (int) pageW, (int) pageH);
// Select the smaller scaling factor so that the figure // Select the smaller scaling factor so that the figure
// fits on the page in both dimensions // fits on the page in both dimensions
final double scale = Math.min( (pageW/boxWidth), (pageH/boxHeight) ); final double scale = Math.min((pageW / boxWidth), (pageH / boxHeight));
if(scale < 1.0) g2d.scale(scale, scale); if (scale < 1.0) {
g2d.scale(scale, scale);
}
// Paint the scaled component on the printer // Paint the scaled component on the printer
g2d.fillRect(rect.x, rect.y, rect.width, rect.height); g2d.fillRect(rect.x, rect.y, rect.width, rect.height);
return(PAGE_EXISTS); return PAGE_EXISTS;
} }
} }
class Sysout {
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog {
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
show();
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
}
}// TestDialog class

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -21,226 +21,87 @@
* questions. * questions.
*/ */
/** 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.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
/*
* @test * @test
* @bug 4396835 * @bug 4396835
* @summary Compound font string not printing. * @summary Compound font string not printing.
* @key printer * @key printer
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual PrintCompoundString * @run main/manual PrintCompoundString
*/ */
public class PrintCompoundString implements Printable {
private static final String STR = "Test string compound printing \u2203\u2200\u2211";
private static final String INSTRUCTIONS =
"This test should print following text\n\n" +
STR +"\n\n" +
"If an exception is thrown, or the page doesn't print properly\n" +
"then the test fails";
import java.awt.*; public static void main(String[] args) throws Exception {
import java.awt.event.*; if (PrinterJob.lookupPrintServices().length == 0) {
import java.awt.print.*; throw new RuntimeException("Printer not configured or available.");
import java.text.*; }
public class PrintCompoundString extends Frame implements ActionListener { PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.splitUI(PrintCompoundString::createTestUI)
.rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(45)
.build()
.awaitAndCheck();
}
private TextCanvas c; private static JComponent createTestUI() {
JButton b = new JButton("Print");
public static void main(String args[]) { b.addActionListener((ae) -> {
try {
String[] instructions = PrinterJob job = PrinterJob.getPrinterJob();
{ job.setPrintable(new PrintCompoundString());
"You must have a printer available to perform this test", if (job.printDialog()) {
"This test should print a page which contains the same", job.print();
"text message as in the test window on the screen", }
"You should also monitor the command line to see if any exceptions", } catch (PrinterException ex) {
"were thrown", ex.printStackTrace();
"If an exception is thrown, or the page doesn't print properly", String msg = "PrinterException: " + ex.getMessage();
"then the test fails", JOptionPane.showMessageDialog(b, msg, "Error occurred",
}; JOptionPane.ERROR_MESSAGE);
Sysout.createDialog( ); PassFailJFrame.forceFail(msg);
Sysout.printInstructions( instructions );
PrintCompoundString f = new PrintCompoundString();
f.show();
}
public PrintCompoundString() {
super("JDK 1.2 drawString Printing");
c = new TextCanvas();
add("Center", c);
Button printButton = new Button("Print");
printButton.addActionListener(this);
add("South", printButton);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} }
}); });
pack(); Box main = Box.createHorizontalBox();
} main.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
main.add(Box.createHorizontalGlue());
public void actionPerformed(ActionEvent e) { main.add(b);
main.add(Box.createHorizontalGlue());
PrinterJob pj = PrinterJob.getPrinterJob(); return main;
}
if (pj != null && pj.printDialog()) {
pj.setPrintable(c);
try {
pj.print();
} catch (PrinterException pe) {
} finally {
System.err.println("PRINT RETURNED");
}
}
}
class TextCanvas extends Panel implements Printable {
String nullStr = null;
String emptyStr = new String();
AttributedString nullAttStr = null;
AttributedString emptyAttStr = new AttributedString(emptyStr);
AttributedCharacterIterator nullIterator = null;
AttributedCharacterIterator emptyIterator = emptyAttStr.getIterator();
@Override
public int print(Graphics g, PageFormat pgFmt, int pgIndex) { public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
if (pgIndex > 0) {
return Printable.NO_SUCH_PAGE;
}
if (pgIndex > 0) Graphics2D g2d = (Graphics2D) g;
return Printable.NO_SUCH_PAGE; g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
g2d.drawString(STR, 20, 40);
Graphics2D g2d = (Graphics2D)g; return Printable.PAGE_EXISTS;
g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
paint(g);
return Printable.PAGE_EXISTS;
} }
public void paint(Graphics g1) {
Graphics2D g = (Graphics2D)g1;
String str = "Test string compound printing \u2203\u2200\u2211";
g.drawString(str, 20, 40);
}
public Dimension getPreferredSize() {
return new Dimension(450, 250);
}
}
} }
class Sysout
{
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog {
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
show();
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
}
}// TestDialog class

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -21,276 +21,152 @@
* questions. * questions.
*/ */
/** import java.awt.BorderLayout;
* @test %I %W import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
/*
* @test
* @bug 4298489 * @bug 4298489
* @summary Confirm that output is same as screen. * @summary Confirm that output is same as screen.
* @key printer * @key printer
* @requires os.family=="windows"
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual PrintImage * @run main/manual PrintImage
*/ */
import java.awt.*;
import java.awt.print.*;
import java.awt.event.*;
public class PrintImage extends Frame implements ActionListener { public class PrintImage extends Frame implements ActionListener {
private final PrintImageCanvas printImageCanvas = new PrintImageCanvas();
private final MenuItem print1Menu = new MenuItem("PrintTest1");
private final MenuItem print2Menu = new MenuItem("PrintTest2");
private static final String INSTRUCTIONS =
"Select PrintTest1 in the File menu.\n" +
"Print Dialog will appear.\n" +
"Click OK to start the first print job.\n" +
"\n" +
"Select PrintTest2 in the File menu.\n" +
"Page Setup Dialog will appear.\n" +
"Click OK.\n" +
"Print Dialog will appear.\n" +
"Click OK to start the second print job.\n" +
"\n" +
"The text in the printouts for PrintTest1 and PrintTest2 should be\n" +
"same as that on the screen.\n" +
"Press Pass if they are, otherwise press Fail.";
private PrintImageCanvas printImageCanvas; public static void main(String[] argv) throws Exception {
if (PrinterJob.lookupPrintServices().length == 0) {
private MenuItem print1Menu = new MenuItem("PrintTest1"); throw new RuntimeException("Printer not configured or available.");
private MenuItem print2Menu = new MenuItem("PrintTest2");
private MenuItem exitMenu = new MenuItem("Exit");
public static void main(String[] argv) {
String[] instructions =
{ "You must have a printer available to perform this test,",
"prefererably Canon LaserShot A309GII.",
"Printing must be done in Win 98 Japanese 2nd Edition.",
"",
"Passing test : Output of text image for PrintTest1 and PrintTest2 should be same as that on the screen.",
};
Sysout.createDialog( );
Sysout.printInstructions( instructions );
new PrintImage();
} }
public PrintImage() { PassFailJFrame.builder()
super("PrintImage"); .instructions(INSTRUCTIONS)
initPrintImage(); .testUI(PrintImage::new)
.rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(45)
.build()
.awaitAndCheck();
}
public PrintImage() {
super("PrintImage");
initPrintImage();
}
public void initPrintImage() {
initMenu();
setLayout(new BorderLayout());
add(printImageCanvas, BorderLayout.CENTER);
setSize(500, 300);
}
private void initMenu() {
MenuBar mb = new MenuBar();
Menu me = new Menu("File");
me.add(print1Menu);
me.add(print2Menu);
mb.add(me);
setMenuBar(mb);
print1Menu.addActionListener(this);
print2Menu.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object target = e.getSource();
if (target.equals(print1Menu)) {
printMain1();
} else if (target.equals(print2Menu)) {
printMain2();
} }
}
public void initPrintImage() { private void printMain1() {
PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printerJob.defaultPage();
printImageCanvas = new PrintImageCanvas(this); printerJob.setPrintable(printImageCanvas, pageFormat);
initMenu(); if (printerJob.printDialog()) {
try {
addWindowListener(new WindowAdapter() { printerJob.print();
public void windowClosing(WindowEvent ev) { } catch (PrinterException e) {
dispose(); PassFailJFrame.forceFail("Print Failed");
} e.printStackTrace();
public void windowClosed(WindowEvent ev) { }
System.exit(0); } else {
} printerJob.cancel();
});
setLayout(new BorderLayout());
add(printImageCanvas, BorderLayout.CENTER);
pack();
setSize(500,500);
setVisible(true);
} }
}
private void initMenu() { private void printMain2() {
MenuBar mb = new MenuBar(); PrinterJob printerJob = PrinterJob.getPrinterJob();
Menu me = new Menu("File"); PageFormat pageFormat = printerJob.pageDialog(printerJob.defaultPage());
me.add(print1Menu);
me.add(print2Menu);
me.add("-");
me.add(exitMenu);
mb.add(me);
this.setMenuBar(mb);
print1Menu.addActionListener(this); printerJob.setPrintable(printImageCanvas, pageFormat);
print2Menu.addActionListener(this);
exitMenu.addActionListener(this); if (printerJob.printDialog()) {
} try {
printerJob.print();
public void actionPerformed(ActionEvent e) { } catch (PrinterException e) {
Object target = e.getSource(); PassFailJFrame.forceFail("Print Failed");
if( target.equals(print1Menu) ) { e.printStackTrace();
printMain1(); }
} } else {
else if( target.equals(print2Menu) ) { printerJob.cancel();
printMain2();
}
else if( target.equals(exitMenu) ) {
dispose();
}
}
private void printMain1(){
PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printerJob.defaultPage();
printerJob.setPrintable((Printable)printImageCanvas, pageFormat);
if(printerJob.printDialog()){
try {
printerJob.print();
}
catch(PrinterException p){
}
}
else
printerJob.cancel();
}
private void printMain2(){
PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printerJob.pageDialog(printerJob.defaultPage());
printerJob.setPrintable((Printable)printImageCanvas, pageFormat);
if(printerJob.printDialog()){
try {
printerJob.print();
}
catch(PrinterException p){
}
}
else
printerJob.cancel();
}
}
class PrintImageCanvas extends Canvas implements Printable {
private PrintImage pdsFrame;
public PrintImageCanvas(PrintImage pds) {
pdsFrame = pds;
} }
}
private static class PrintImageCanvas extends Canvas implements Printable {
@Override
public void paint(Graphics g) { public void paint(Graphics g) {
Font drawFont = new Font("MS Mincho",Font.ITALIC,50); Font drawFont = new Font("MS Mincho", Font.ITALIC, 50);
g.setFont(drawFont); g.setFont(drawFont);
g.drawString("PrintSample!",100,150); g.setColor(new Color(0, 0, 0, 200));
g.drawString("PrintSample!", 100, 150);
} }
@Override
public int print(Graphics g, PageFormat pf, int pi) public int print(Graphics g, PageFormat pf, int pi)
throws PrinterException { throws PrinterException {
if (pi > 0) {
if(pi>=1) return NO_SUCH_PAGE;
return NO_SUCH_PAGE; }
else{ paint(g);
Graphics2D g2 = (Graphics2D)g; return PAGE_EXISTS;
g.setColor(new Color(0,0,0,200));
Font drawFont = new Font("MS Mincho",Font.ITALIC,50);
g.setFont(drawFont);
g.drawString("PrintSample!",100,150);
return PAGE_EXISTS;
}
} }
}
} }
class Sysout {
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog {
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
show();
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
}
}// TestDialog class

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -21,308 +21,164 @@
* questions. * questions.
*/ */
/** import java.awt.Button;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import javax.swing.JOptionPane;
/*
* @test * @test
* @bug 4223328 * @bug 4223328
* @summary Printer graphics must behave the same as screen graphics * @summary Printer graphics must behave the same as screen graphics
* @key printer * @key printer
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual PrintNullString * @run main/manual PrintNullString
*/ */
public class PrintNullString extends Frame {
private static final String INSTRUCTIONS =
"This test should print a page which contains the same\n" +
"text messages as in the test window on the screen.\n" +
"\n" +
"The messages should contain only 'OK' and 'expected' messages.\n" +
"Press Pass if it's the case; otherwise press Fail.\n" +
"\n" +
"If the page fails to print, but there were no exceptions\n" +
"then the problem is likely elsewhere (i.e. your printer)";
public static void main(String[] args) throws Exception {
if (PrinterJob.lookupPrintServices().length == 0) {
throw new RuntimeException("Printer not configured or available.");
}
import java.awt.*; PassFailJFrame.builder()
import java.awt.event.*; .instructions(INSTRUCTIONS)
import java.awt.print.*; .testUI(PrintNullString::new)
import java.text.*; .rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(45)
.build()
.awaitAndCheck();
}
public class PrintNullString extends Frame implements ActionListener { public PrintNullString() {
super("PrintNullString");
private TextCanvas c; TextCanvas c = new TextCanvas();
add("Center", c);
public static void main(String args[]) { Button b = new Button("Print");
add("South", b);
String[] instructions = b.addActionListener(e -> {
{ PrinterJob pj = PrinterJob.getPrinterJob();
"You must have a printer available to perform this test", if (pj.printDialog()) {
"This test should print a page which contains the same", pj.setPrintable(c);
"text messages as in the test window on the screen", try {
"The messages should contain only 'OK' and 'expected' messages", pj.print();
"There should be no FAILURE messages.", } catch (PrinterException ex) {
"You should also monitor the command line to see if any exceptions", ex.printStackTrace();
"were thrown", String msg = "PrinterException: " + ex.getMessage();
"If the page fails to print, but there were no exceptions", JOptionPane.showMessageDialog(b, msg, "Error occurred",
"then the problem is likely elsewhere (ie your printer)" JOptionPane.ERROR_MESSAGE);
}; PassFailJFrame.forceFail(msg);
Sysout.createDialog( ); }
Sysout.printInstructions( instructions );
PrintNullString f = new PrintNullString();
f.show();
}
public PrintNullString() {
super("JDK 1.2 drawString Printing");
c = new TextCanvas();
add("Center", c);
Button printButton = new Button("Print");
printButton.addActionListener(this);
add("South", printButton);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} }
}); });
pack();
pack();
}
public void actionPerformed(ActionEvent e) {
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj != null && pj.printDialog()) {
pj.setPrintable(c);
try {
pj.print();
} catch (PrinterException pe) {
} finally {
System.err.println("PRINT RETURNED");
}
}
}
class TextCanvas extends Panel implements Printable {
String nullStr = null;
String emptyStr = new String();
AttributedString nullAttStr = null;
AttributedString emptyAttStr = new AttributedString(emptyStr);
AttributedCharacterIterator nullIterator = null;
AttributedCharacterIterator emptyIterator = emptyAttStr.getIterator();
public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
if (pgIndex > 0)
return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
paint(g);
return Printable.PAGE_EXISTS;
} }
public void paint(Graphics g1) { private static class TextCanvas extends Panel implements Printable {
Graphics2D g = (Graphics2D)g1; private final String nullStr = null;
private final String emptyStr = "";
private final AttributedString emptyAttStr = new AttributedString(emptyStr);
private final AttributedCharacterIterator nullIterator = null;
private final AttributedCharacterIterator emptyIterator = emptyAttStr.getIterator();
// API 1: null & empty drawString(String, int, int); @Override
try { public void paint(Graphics g) {
g.drawString(nullStr, 20, 40); Graphics2D g2d = (Graphics2D) g;
g.drawString("FAILURE: No NPE for null String, int", 20, 40); paint(g2d);
} catch (NullPointerException e) { }
g.drawString("caught expected NPE for null String, int", 20, 40);
}/* catch (Exception e) {
g.drawString("FAILURE: unexpected exception for null String, int",
20, 40);
}*/
//try { @Override
g.drawString(emptyStr, 20, 60); public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
g.drawString("OK for empty String, int", 20, 60); if (pgIndex > 0) {
/*} catch (Exception e) { return NO_SUCH_PAGE;
g.drawString("FAILURE: unexpected exception for empty String, int", }
20, 60);
}*/
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
paint(g2d);
// API 2: null & empty drawString(String, float, float); return PAGE_EXISTS;
try { }
g.drawString(nullStr, 20.0f, 80.0f);
g.drawString("FAILURE: No NPE for null String, float", 20, 80);
} catch (NullPointerException e) {
g.drawString("caught expected NPE for null String, float", 20, 80);
} /*catch (Exception e) {
g.drawString("FAILURE: unexpected exception for null String, float",
20, 80);
}*/
//try {
g.drawString(emptyStr, 20.0f, 100.0f);
g.drawString("OK for empty String, float", 20.0f, 100.f);
/* } catch (Exception e) {
g.drawString("FAILURE: unexpected exception for empty String, float",
20, 100);
}*/
// API 3: null & empty drawString(Iterator, int, int); private void paint(Graphics2D g2d) {
try { // API 1: null & empty drawString(String, int, int);
g.drawString(nullIterator, 20, 120); try {
g.drawString("FAILURE: No NPE for null iterator, float", 20, 120); g2d.drawString(nullStr, 20, 40);
} catch (NullPointerException e) { g2d.drawString("FAILURE: No NPE for null String, int", 20, 40);
g.drawString("caught expected NPE for null iterator, int", 20, 120); } catch (NullPointerException e) {
} /*catch (Exception e) { g2d.drawString("caught expected NPE for null String, int", 20, 40);
g.drawString("FAILURE: unexpected exception for null iterator, int", }
20, 120);
} */
try {
g.drawString(emptyIterator, 20, 140);
g.drawString("FAILURE: No IAE for empty iterator, int",
20, 140);
} catch (IllegalArgumentException e) {
g.drawString("caught expected IAE for empty iterator, int",
20, 140);
} /*catch (Exception e) {
g.drawString("FAILURE: unexpected exception for empty iterator, int",
20, 140);
} */
g2d.drawString(emptyStr, 20, 60);
g2d.drawString("OK for empty String, int", 20, 60);
// API 4: null & empty drawString(Iterator, float, int); // API 2: null & empty drawString(String, float, float);
try { try {
g.drawString(nullIterator, 20.0f, 160.0f); g2d.drawString(nullStr, 20.0f, 80.0f);
g.drawString("FAILURE: No NPE for null iterator, float", 20, 160); g2d.drawString("FAILURE: No NPE for null String, float", 20, 80);
} catch (NullPointerException e) { } catch (NullPointerException e) {
g.drawString("caught expected NPE for null iterator, float", 20, 160); g2d.drawString("caught expected NPE for null String, float", 20, 80);
} /*catch (Exception e) { }
g.drawString("FAILURE: unexpected exception for null iterator, float",
20, 160);
} */
try { g2d.drawString(emptyStr, 20.0f, 100.0f);
g.drawString(emptyIterator, 20, 180); g2d.drawString("OK for empty String, float", 20.0f, 100.f);
g.drawString("FAILURE: No IAE for empty iterator, float",
20, 180); // API 3: null & empty drawString(Iterator, int, int);
} catch (IllegalArgumentException e) { try {
g.drawString("caught expected IAE for empty iterator, float", g2d.drawString(nullIterator, 20, 120);
20, 180); g2d.drawString("FAILURE: No NPE for null iterator, float", 20, 120);
} /*catch (Exception e) { } catch (NullPointerException e) {
g.drawString("FAILURE: unexpected exception for empty iterator, float", g2d.drawString("caught expected NPE for null iterator, int", 20, 120);
20, 180); }
} */
try {
g2d.drawString(emptyIterator, 20, 140);
g2d.drawString("FAILURE: No IAE for empty iterator, int", 20, 140);
} catch (IllegalArgumentException e) {
g2d.drawString("caught expected IAE for empty iterator, int", 20, 140);
}
// API 4: null & empty drawString(Iterator, float, int);
try {
g2d.drawString(nullIterator, 20.0f, 160.0f);
g2d.drawString("FAILURE: No NPE for null iterator, float", 20, 160);
} catch (NullPointerException e) {
g2d.drawString("caught expected NPE for null iterator, float", 20, 160);
}
try {
g2d.drawString(emptyIterator, 20, 180);
g2d.drawString("FAILURE: No IAE for empty iterator, float", 20, 180);
} catch (IllegalArgumentException e) {
g2d.drawString("caught expected IAE for empty iterator, float", 20, 180);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(450, 250);
}
} }
public Dimension getPreferredSize() {
return new Dimension(450, 250);
}
}
} }
class Sysout
{
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog {
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
show();
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
}
}// TestDialog class

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -21,226 +21,87 @@
* questions. * questions.
*/ */
/** 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.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
/*
* @test * @test
* @bug 4399442 * @bug 4399442
* @summary Brackets should be quoted in Postscript output * @summary Brackets should be quoted in Postscript output
* @key printer * @key printer
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual PrintParenString * @run main/manual PrintParenString
*/ */
public class PrintParenString implements Printable {
private static final String STR = "String containing unclosed parenthesis (.";
private static final String INSTRUCTIONS =
"This test should print a page with following text\n\n" +
STR + "\n\n" +
"If an exception is thrown, or the page doesn't print properly\n" +
"then the test fails";
import java.awt.*; public static void main(String[] args) throws Exception {
import java.awt.event.*; if (PrinterJob.lookupPrintServices().length == 0) {
import java.awt.print.*; throw new RuntimeException("Printer not configured or available.");
import java.text.*; }
public class PrintParenString extends Frame implements ActionListener { PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.splitUI(PrintParenString::createTestUI)
.rows((int) INSTRUCTIONS.lines().count() + 1)
.columns(45)
.build()
.awaitAndCheck();
}
private TextCanvas c; private static JComponent createTestUI() {
JButton b = new JButton("Print");
public static void main(String args[]) { b.addActionListener((ae) -> {
try {
String[] instructions = PrinterJob job = PrinterJob.getPrinterJob();
{ job.setPrintable(new PrintParenString());
"You must have a printer available to perform this test", if (job.printDialog()) {
"This test should print a page which contains the same", job.print();
"text message as in the test window on the screen", }
"You should also monitor the command line to see if any exceptions", } catch (PrinterException ex) {
"were thrown", ex.printStackTrace();
"If an exception is thrown, or the page doesn't print properly", String msg = "PrinterException: " + ex.getMessage();
"then the test fails", JOptionPane.showMessageDialog(b, msg, "Error occurred",
}; JOptionPane.ERROR_MESSAGE);
Sysout.createDialog( ); PassFailJFrame.forceFail(msg);
Sysout.printInstructions( instructions );
PrintParenString f = new PrintParenString();
f.show();
}
public PrintParenString() {
super("JDK 1.2 drawString Printing");
c = new TextCanvas();
add("Center", c);
Button printButton = new Button("Print");
printButton.addActionListener(this);
add("South", printButton);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} }
}); });
pack(); Box main = Box.createHorizontalBox();
} main.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
main.add(Box.createHorizontalGlue());
public void actionPerformed(ActionEvent e) { main.add(b);
main.add(Box.createHorizontalGlue());
PrinterJob pj = PrinterJob.getPrinterJob(); return main;
}
if (pj != null && pj.printDialog()) {
pj.setPrintable(c);
try {
pj.print();
} catch (PrinterException pe) {
} finally {
System.err.println("PRINT RETURNED");
}
}
}
class TextCanvas extends Panel implements Printable {
String nullStr = null;
String emptyStr = new String();
AttributedString nullAttStr = null;
AttributedString emptyAttStr = new AttributedString(emptyStr);
AttributedCharacterIterator nullIterator = null;
AttributedCharacterIterator emptyIterator = emptyAttStr.getIterator();
@Override
public int print(Graphics g, PageFormat pgFmt, int pgIndex) { public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
if (pgIndex > 0) {
return Printable.NO_SUCH_PAGE;
}
if (pgIndex > 0) Graphics2D g2d = (Graphics2D) g;
return Printable.NO_SUCH_PAGE; g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
g2d.drawString(STR, 20, 40);
Graphics2D g2d = (Graphics2D)g; return Printable.PAGE_EXISTS;
g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
paint(g);
return Printable.PAGE_EXISTS;
} }
public void paint(Graphics g1) {
Graphics2D g = (Graphics2D)g1;
String str = "String containing unclosed parenthesis (.";
g.drawString(str, 20, 40);
}
public Dimension getPreferredSize() {
return new Dimension(450, 250);
}
}
} }
class Sysout
{
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog {
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
show();
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
}
}// TestDialog class

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -21,237 +21,126 @@
* questions. * questions.
*/ */
/** import java.awt.Button;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.geom.AffineTransform;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JOptionPane;
/*
* @test * @test
* @bug 6359734 * @bug 6359734
* @key printer * @key printer
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @summary Test that fonts with a translation print where they should. * @summary Test that fonts with a translation print where they should.
* @run main/manual PrintTranslatedFont * @run main/manual PrintTranslatedFont
*/ */
public class PrintTranslatedFont extends Frame {
private static final String INSTRUCTIONS =
"This test should print a page which contains the same\n" +
"content as the test window on the screen, in particular the lines\n" +
"should be immediately under the text\n\n" +
"If an exception is thrown, or the page doesn't print properly\n" +
"then the test fails";
public static void main(String[] args) throws Exception {
if (PrinterJob.lookupPrintServices().length == 0) {
throw new RuntimeException("Printer not configured or available.");
}
import java.awt.*; PassFailJFrame.builder()
import java.awt.event.*; .instructions(INSTRUCTIONS)
import java.awt.geom.*; .testUI(PrintTranslatedFont::new)
import java.awt.print.*; .rows((int) INSTRUCTIONS.lines().count() + 1)
import java.text.*; .columns(45)
.build()
.awaitAndCheck();
}
public class PrintTranslatedFont extends Frame implements ActionListener { public PrintTranslatedFont() {
super("PrintTranslatedFont");
private TextCanvas c; TextCanvas c = new TextCanvas();
add("Center", c);
public static void main(String args[]) { Button b = new Button("Print");
add("South", b);
String[] instructions = b.addActionListener(e -> {
{ PrinterJob pj = PrinterJob.getPrinterJob();
"You must have a printer available to perform this test", if (pj.printDialog()) {
"This test should print a page which contains the same", pj.setPrintable(c);
"content as the test window on the screen, in particular the lines", try {
"should be immediately under the text", pj.print();
"You should also monitor the command line to see if any exceptions", } catch (PrinterException ex) {
"were thrown", ex.printStackTrace();
"If an exception is thrown, or the page doesn't print properly", String msg = "PrinterException: " + ex.getMessage();
"then the test fails", JOptionPane.showMessageDialog(b, msg, "Error occurred",
}; JOptionPane.ERROR_MESSAGE);
Sysout.createDialog( ); PassFailJFrame.forceFail(msg);
Sysout.printInstructions( instructions ); }
PrintTranslatedFont f = new PrintTranslatedFont();
f.show();
}
public PrintTranslatedFont() {
super("JDK 1.2 drawString Printing");
c = new TextCanvas();
add("Center", c);
Button printButton = new Button("Print");
printButton.addActionListener(this);
add("South", printButton);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} }
}); });
pack(); pack();
}
public void actionPerformed(ActionEvent e) {
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj != null && pj.printDialog()) {
pj.setPrintable(c);
try {
pj.print();
} catch (PrinterException pe) {
} finally {
System.err.println("PRINT RETURNED");
}
}
}
class TextCanvas extends Panel implements Printable {
public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
if (pgIndex > 0)
return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
paint(g);
return Printable.PAGE_EXISTS;
} }
public void paint(Graphics g1) { private static class TextCanvas extends Panel implements Printable {
Graphics2D g = (Graphics2D)g1; @Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
paint(g2d);
}
Font f = new Font("Dialog", Font.PLAIN, 20); @Override
int tx = 20; public int print(Graphics g, PageFormat pgFmt, int pgIndex) {
int ty = 20; if (pgIndex > 0) {
AffineTransform at = AffineTransform.getTranslateInstance(tx, ty); return Printable.NO_SUCH_PAGE;
f = f.deriveFont(at); }
g.setFont(f);
FontMetrics fm = g.getFontMetrics(); Graphics2D g2d = (Graphics2D) g;
String str = "Basic ascii string"; g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY());
int sw = fm.stringWidth(str); paint(g2d);
int posx = 20, posy = 40; return Printable.PAGE_EXISTS;
g.drawString(str, posx, posy); }
g.drawLine(posx+tx, posy+ty+2, posx+tx+sw, posy+ty+2);
posx = 20; posy = 70; private void paint(Graphics2D g2d) {
str = "Test string compound printing \u2203\u2200"; Font f = new Font("Dialog", Font.PLAIN, 20);
sw = fm.stringWidth(str); int tx = 20;
g.drawString(str, posx, posy); int ty = 20;
g.drawLine(posx+tx, posy+ty+2, posx+tx+sw, posy+ty+2); AffineTransform at = AffineTransform.getTranslateInstance(tx, ty);
f = f.deriveFont(at);
g2d.setFont(f);
FontMetrics fm = g2d.getFontMetrics();
String str = "Basic ascii string";
int sw = fm.stringWidth(str);
int posx = 20;
int posy = 40;
g2d.drawString(str, posx, posy);
g2d.drawLine(posx + tx, posy + ty + 2, posx + tx + sw, posy + ty + 2);
posx = 20;
posy = 70;
str = "Test string compound printing \u2203\u2200";
sw = fm.stringWidth(str);
g2d.drawString(str, posx, posy);
g2d.drawLine(posx + tx, posy + ty + 2, posx + tx + sw, posy + ty + 2);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(450, 250);
}
} }
public Dimension getPreferredSize() {
return new Dimension(450, 250);
}
}
} }
class Sysout
{
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.show();
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog {
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
show();
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
}
}// TestDialog class