Skip to content

Commit a2f0ddb

Browse files
committed
8325762: Use PassFailJFrame.Builder.splitUI() in PrintLatinCJKTest.java
Backport-of: 41242cbe5d01fc6e19859626dd08f6191e7486fb
1 parent 5013360 commit a2f0ddb

File tree

1 file changed

+56
-49
lines changed

1 file changed

+56
-49
lines changed

test/jdk/java/awt/print/PrinterJob/PrintLatinCJKTest.java

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,59 +31,64 @@
3131
* @run main/manual PrintLatinCJKTest
3232
*/
3333

34-
import java.awt.BorderLayout;
3534
import java.awt.Font;
3635
import java.awt.Graphics;
3736
import java.awt.print.PageFormat;
3837
import java.awt.print.Printable;
3938
import java.awt.print.PrinterException;
4039
import java.awt.print.PrinterJob;
4140
import java.lang.reflect.InvocationTargetException;
42-
import javax.swing.JButton;
43-
import javax.swing.JFrame;
4441

45-
import static javax.swing.SwingUtilities.invokeAndWait;
42+
import javax.swing.BorderFactory;
43+
import javax.swing.Box;
44+
import javax.swing.JButton;
45+
import javax.swing.JComponent;
46+
import javax.swing.JOptionPane;
4647

4748
public class PrintLatinCJKTest implements Printable {
4849

49-
private static PrintLatinCJKTest testInstance = new PrintLatinCJKTest();
50-
private static JFrame frame;
51-
private static final String info = """
52-
You need a printer for this test. If you have none, let
53-
the test pass. If there is a printer, press Print, send
54-
the output to the printer, and examine it. It should have
55-
text looking like this : \u4e00\u4e01\u4e02\u4e03\u4e04English
50+
private static final String TEXT = "\u4e00\u4e01\u4e02\u4e03\u4e04English";
51+
52+
private static final String INFO = """
53+
Press Print, send the output to the printer and examine it.
54+
The printout should have text looking like this:
55+
56+
"""
57+
+ TEXT + """
58+
59+
60+
Press Pass if the text is printed correctly.
61+
If Japanese and English text overlap, press Fail.
62+
5663
5764
To test 8022536, if a remote printer is the system default,
5865
it should show in the dialog as the selected printer.
5966
""";
6067

61-
public static void showFrame() throws InterruptedException, InvocationTargetException {
62-
invokeAndWait( () -> {
63-
frame = new JFrame("Test Frame");
64-
JButton b = new JButton("Print");
65-
b.addActionListener((ae) -> {
66-
try {
67-
PrinterJob job = PrinterJob.getPrinterJob();
68-
job.setPrintable(testInstance);
69-
if (job.printDialog()) {
70-
job.print();
71-
}
72-
} catch (PrinterException ex) {
73-
ex.printStackTrace();
68+
private static JComponent createTestUI() {
69+
JButton b = new JButton("Print");
70+
b.addActionListener((ae) -> {
71+
try {
72+
PrinterJob job = PrinterJob.getPrinterJob();
73+
job.setPrintable(new PrintLatinCJKTest());
74+
if (job.printDialog()) {
75+
job.print();
7476
}
75-
});
76-
frame.getContentPane().add(b, BorderLayout.SOUTH);
77-
frame.pack();
78-
79-
// add the test frame to dispose
80-
PassFailJFrame.addTestWindow(frame);
81-
82-
// Arrange the test instruction frame and test frame side by side
83-
PassFailJFrame.positionTestWindow(frame,
84-
PassFailJFrame.Position.HORIZONTAL);
85-
frame.setVisible(true);
77+
} catch (PrinterException ex) {
78+
ex.printStackTrace();
79+
String msg = "PrinterException: " + ex.getMessage();
80+
JOptionPane.showMessageDialog(b, msg, "Error occurred",
81+
JOptionPane.ERROR_MESSAGE);
82+
PassFailJFrame.forceFail(msg);
83+
}
8684
});
85+
86+
Box main = Box.createHorizontalBox();
87+
main.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
88+
main.add(Box.createHorizontalGlue());
89+
main.add(b);
90+
main.add(Box.createHorizontalGlue());
91+
return main;
8792
}
8893

8994
@Override
@@ -93,22 +98,24 @@ public int print(Graphics g, PageFormat pf, int pageIndex)
9398
return Printable.NO_SUCH_PAGE;
9499
}
95100
g.translate((int) pf.getImageableX(), (int) pf.getImageableY());
96-
g.setFont(new Font("Dialog", Font.PLAIN, 36));
97-
g.drawString("\u4e00\u4e01\u4e02\u4e03\u4e04English", 20, 100);
101+
g.setFont(new Font(Font.DIALOG, Font.PLAIN, 36));
102+
g.drawString(TEXT, 20, 100);
98103
return Printable.PAGE_EXISTS;
99104
}
100105

101106
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
102-
PassFailJFrame passFailJFrame = new PassFailJFrame.Builder()
103-
.title("Test Instructions Frame")
104-
.instructions(info)
105-
.testTimeOut(10)
106-
.rows(10)
107-
.columns(45)
108-
.build();
109-
showFrame();
110-
passFailJFrame.awaitAndCheck();
107+
if (PrinterJob.lookupPrintServices().length == 0) {
108+
throw new RuntimeException("Printer not configured or available.");
109+
}
110+
111+
PassFailJFrame.builder()
112+
.title("Print Latin CJK Test")
113+
.instructions(INFO)
114+
.testTimeOut(10)
115+
.rows(12)
116+
.columns(30)
117+
.splitUI(PrintLatinCJKTest::createTestUI)
118+
.build()
119+
.awaitAndCheck();
111120
}
112121
}
113-
114-

0 commit comments

Comments
 (0)