8283803: Remove jtreg tag manual=yesno for java/awt/print/PrinterJob/PrintGlyphVectorTest.java and fix test

8284898: Enhance PassFailJFrame

Reviewed-by: prr, aivanov
This commit is contained in:
lawrence.andrews 2022-04-19 21:07:38 +00:00 committed by Alexey Ivanov
parent 5df8bd6b4e
commit ed23033dc6
2 changed files with 156 additions and 94 deletions
test/jdk/java/awt
print/PrinterJob
regtesthelpers

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2022, 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,19 +24,46 @@
/*
* @test
* @bug 8029204
* @library ../../regtesthelpers
* @build PassFailJFrame
* @summary Tests GlyphVector is printed in the correct location
* @run main/manual=yesno PrintGlyphVectorTest
* @run main/manual PrintGlyphVectorTest
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class PrintGlyphVectorTest extends Component implements Printable {
private static final String INSTRUCTIONS = """
Note: You must have a printer installed for this test.
If printer is not available, the test passes automatically.
Press the PRINT button on the 'Test PrintGlyphVector' frame
and press OK/print button on the print dialog.
Retrieve the output and compare the printed and on-screen
text to confirm that in both cases the text is aligned and
the boxes are around the text, not offset from the text.
""";
public void drawGVs(Graphics g) {
String testString = "0123456789abcdefghijklm";
@ -46,9 +73,8 @@ public class PrintGlyphVectorTest extends Component implements Printable {
FontRenderContext frc = g2d.getFontRenderContext();
GlyphVector v = font.createGlyphVector(frc, testString);
float x = 50f,
y = 50f;
float x = 50f;
float y = 50f;
g2d.drawGlyphVector(v, x, y);
Rectangle2D r = v.getVisualBounds();
@ -69,16 +95,13 @@ public class PrintGlyphVectorTest extends Component implements Printable {
r = v.getVisualBounds();
r.setRect(r.getX()+x, r.getY()+y, r.getWidth(), r.getHeight());
g2d.draw(r);
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,getSize().width, getSize().height);
drawGVs(g);
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width, getSize().height);
drawGVs(g);
}
public Dimension getPreferredSize() {
return new Dimension(600,200);
@ -97,54 +120,48 @@ public class PrintGlyphVectorTest extends Component implements Printable {
return Printable.PAGE_EXISTS;
}
private static void createTestUI() {
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj.getPrintService() == null) {
System.out.println("Printer not configured or available."
+ " Test cannot continue.");
PassFailJFrame.forcePass();
}
public static void main(String arg[]) throws Exception {
Frame f = new Frame("Test PrintGlyphVector");
PrintGlyphVectorTest pvt = new PrintGlyphVectorTest();
f.add(pvt, BorderLayout.CENTER);
Frame f = new Frame();
PrintGlyphVectorTest pvt = new PrintGlyphVectorTest();
f.add("Center", pvt);
f.add("South", new PrintInstructions());
f.pack();
f.show();
Button printButton = new Button("PRINT");
printButton.addActionListener((e) -> {
pj.setPrintable(new PrintGlyphVectorTest());
if (pj.printDialog()) {
try {
pj.print();
} catch (PrinterException ex) {
throw new RuntimeException(ex.getMessage());
}
} else {
throw new RuntimeException("Test failed : "
+ "User selected 'Cancel' button on the print dialog");
}
});
f.add(printButton, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
// add the test frame to dispose
PassFailJFrame.addTestFrame(f);
// Arrange the test instruction frame and test frame side by side
PassFailJFrame.positionTestFrame(f, PassFailJFrame.Position.HORIZONTAL);
}
public static void main(String[] arg) throws Exception {
PassFailJFrame passFailJFrame = new PassFailJFrame(INSTRUCTIONS);
createTestUI();
passFailJFrame.awaitAndCheck();
}
}
class PrintInstructions extends Panel implements ActionListener {
static final String INSTRUCTIONS =
"You must have a printer installed for this test.\n" +
"Press the PRINT button below and OK the print dialog\n" +
"Retrieve the output and compare the printed and on-screen text\n" +
" to confirm that in both cases the text is aligned and the boxes\n" +
"are around the text, not offset from the text.";
PrintInstructions() {
setLayout(new GridLayout(2,1));
TextArea t = new TextArea(INSTRUCTIONS, 8, 80);
add(t);
Button b = new Button("PRINT");
b.setFont(new Font("Dialog", Font.BOLD, 30));
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent e) {
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj == null ||
pj.getPrintService() == null ||
!pj.printDialog()) {
return;
}
pj.setPrintable(new PrintGlyphVectorTest());
try {
pj.print();
} catch (PrinterException ex) {
System.err.println(ex);
}
}
}

@ -24,7 +24,6 @@
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
@ -47,16 +46,38 @@ import static javax.swing.SwingUtilities.isEventDispatchThread;
public class PassFailJFrame {
private final static CountDownLatch latch = new CountDownLatch(1);
private static final String TITLE = "Test Instruction Frame";
private static final long TEST_TIMEOUT = 5;
private static final int ROWS = 10;
private static final int COLUMNS = 40;
private static final List<Frame> frameList = new ArrayList<>();
private static final Timer timer = new Timer(0, null);
private static final CountDownLatch latch = new CountDownLatch(1);
private static volatile boolean failed;
private static volatile boolean timeout;
private static volatile String testFailedReason;
private static JFrame frame;
private static final List<Frame> frameList = new ArrayList<>();
private static final Timer timer = new Timer(0, null);
public enum Position {HORIZONTAL, VERTICAL}
public PassFailJFrame(String instructions) throws InterruptedException,
InvocationTargetException {
this(instructions, TEST_TIMEOUT);
}
public PassFailJFrame(String instructions, long testTimeOut) throws
InterruptedException, InvocationTargetException {
this(TITLE, instructions, testTimeOut);
}
public PassFailJFrame(String title, String instructions,
long testTimeOut) throws InterruptedException,
InvocationTargetException {
this(title, instructions, testTimeOut, ROWS, COLUMNS);
}
/**
* Constructs a JFrame with a given title & serves as test instructional
* frame where the user follows the specified test instruction in order
@ -65,48 +86,48 @@ public class PassFailJFrame {
* on the 'Fail' button and the reason for the failure should be
* specified in the JDialog JTextArea.
*
* @param title title of the Frame.
* @param instructions specified instruction that user should follow.
* @param rows number of visible rows of the JTextArea where the
* instruction is show.
* @param columns Number of columns of the instructional
* JTextArea
* @param testTimeOutInMinutes timeout of the test where time is specified in
* minutes.
* @throws HeadlessException HeadlessException
* @throws InterruptedException exception thrown for invokeAndWait
* @throws InvocationTargetException exception thrown for invokeAndWait
* @param title title of the Frame.
* @param instructions the instruction for the tester on how to test
* and what is expected (pass) and what is not
* expected (fail).
* @param testTimeOut test timeout where time is specified in minutes.
* @param rows number of visible rows of the JTextArea where the
* instruction is show.
* @param columns Number of columns of the instructional
* JTextArea
* @throws InterruptedException exception thrown when thread is
* interrupted
* @throws InvocationTargetException if an exception is thrown while
* creating the test instruction frame on
* EDT
*/
public PassFailJFrame(String title, String instructions,
int rows, int columns,
int testTimeOutInMinutes) throws HeadlessException,
InterruptedException, InvocationTargetException {
public PassFailJFrame(String title, String instructions, long testTimeOut,
int rows, int columns) throws InterruptedException,
InvocationTargetException {
if (isEventDispatchThread()) {
createUI(title, instructions, rows, columns, testTimeOutInMinutes);
createUI(title, instructions, testTimeOut, rows, columns);
} else {
invokeAndWait(() -> createUI(title, instructions, rows, columns,
testTimeOutInMinutes));
invokeAndWait(() -> createUI(title, instructions, testTimeOut,
rows, columns));
}
}
private static void createUI(String title, String instructions,
int rows, int columns,
int timeoutInMinutes) {
long testTimeOut, int rows, int columns) {
frame = new JFrame(title);
frame.setLayout(new BorderLayout());
JTextArea instructionsText = new JTextArea(instructions, rows, columns);
instructionsText.setEditable(false);
instructionsText.setLineWrap(true);
long testTimeout = TimeUnit.MINUTES.toMillis(timeoutInMinutes);
long tTimeout = TimeUnit.MINUTES.toMillis(testTimeOut);
final JLabel testTimeoutLabel = new JLabel(String.format("Test " +
"timeout: %s", convertMillisToTimeStr(testTimeout)), JLabel.CENTER);
"timeout: %s", convertMillisToTimeStr(tTimeout)), JLabel.CENTER);
final long startTime = System.currentTimeMillis();
timer.setDelay(1000);
timer.addActionListener((e) -> {
long leftTime = testTimeout - (System.currentTimeMillis() - startTime);
long leftTime = tTimeout - (System.currentTimeMillis() - startTime);
if ((leftTime < 0) || failed) {
timer.stop();
testFailedReason = "Failure Reason:\n"
@ -151,7 +172,6 @@ public class PassFailJFrame {
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frameList.add(frame);
}
@ -171,8 +191,10 @@ public class PassFailJFrame {
* the specified timeoutMinutes period and the test gets timeout.
* Note: This method should be called from main() thread
*
* @throws InterruptedException if the thread is interrupted
* @throws InvocationTargetException exception thrown for invokeAndWait
* @throws InterruptedException exception thrown when thread is
* interrupted
* @throws InvocationTargetException if an exception is thrown while
* disposing of frames on EDT
*/
public void awaitAndCheck() throws InterruptedException, InvocationTargetException {
if (isEventDispatchThread()) {
@ -272,5 +294,28 @@ public class PassFailJFrame {
public static synchronized void addTestFrame(Frame testFrame) {
frameList.add(testFrame);
}
/**
* Forcibly pass the test.
* <p>The sample usage:
* <pre><code>
* PrinterJob pj = PrinterJob.getPrinterJob();
* if (pj == null || pj.getPrintService() == null) {
* System.out.println(""Printer not configured or available.");
* PassFailJFrame.forcePass();
* }
* </code></pre>
*/
public static void forcePass() {
latch.countDown();
}
/**
* Forcibly fail the test.
*/
public static void forceFail() {
failed = true;
latch.countDown();
}
}