Skip to content

Commit 2a70aaa

Browse files
i556354GoeLin
i556354
authored andcommittedJul 26, 2023
8270331: [TESTBUG] Error: Not a test or directory containing tests: java/awt/print/PrinterJob/InitToBlack.java
Backport-of: 2ef9767aae8e1798661fea615b096833bdb9d985
1 parent 90202ef commit 2a70aaa

File tree

1 file changed

+131
-15
lines changed

1 file changed

+131
-15
lines changed
 

‎test/jdk/java/awt/print/PrinterJob/InitToBlack.java

+131-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2022, 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
@@ -22,21 +22,57 @@
2222
*/
2323

2424
/**
25+
* @test
2526
* @bug 4184565
2627
* @summary Confirm that the default foreground color on a printer
2728
* graphics object is black so that rendering will appear
2829
* without having to execute setColor first.
29-
* @run applet/manual=yesno InitToBlack.html
30+
* @run main/manual InitToBlack
3031
*/
3132

32-
import java.awt.*;
33-
import java.awt.print.*;
34-
import java.applet.Applet;
33+
import java.awt.BorderLayout;
34+
import java.awt.Graphics;
35+
import java.awt.Graphics2D;
36+
import java.awt.print.Book;
37+
import java.awt.print.PageFormat;
38+
import java.awt.print.Printable;
39+
import java.awt.print.PrinterException;
40+
import java.awt.print.PrinterJob;
41+
import java.lang.reflect.InvocationTargetException;
42+
import java.util.concurrent.CountDownLatch;
43+
import java.util.concurrent.TimeUnit;
44+
import javax.swing.JButton;
45+
import javax.swing.JDialog;
46+
import javax.swing.JFrame;
47+
import javax.swing.JLabel;
48+
import javax.swing.JPanel;
49+
import javax.swing.JTextArea;
50+
import javax.swing.SwingUtilities;
3551

36-
public class InitToBlack extends Applet implements Printable {
52+
public class InitToBlack implements Printable {
3753

38-
public void init() {
54+
private static volatile JFrame frame;
55+
private static volatile boolean testResult = false;
56+
private static volatile CountDownLatch printButtonCountDownLatch =
57+
new CountDownLatch(1);
58+
private static volatile CountDownLatch CountDownLatch =
59+
new CountDownLatch(1);
60+
private static volatile String failureReason;
61+
62+
@Override
63+
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
64+
Graphics2D g2d = (Graphics2D) graphics;
65+
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
66+
graphics.drawString("Test Passes", 200, 200);
67+
return PAGE_EXISTS;
68+
}
69+
70+
private void test() {
3971
PrinterJob pjob = PrinterJob.getPrinterJob();
72+
if (pjob.getPrintService() == null) {
73+
System.out.println("There is no printer configured on this system");
74+
return;
75+
}
4076

4177
Book book = new Book();
4278
book.append(this, pjob.defaultPage());
@@ -49,17 +85,97 @@ public void init() {
4985
}
5086
}
5187

52-
public int print(Graphics g, PageFormat pf, int pageIndex) {
53-
Graphics2D g2d = (Graphics2D) g;
54-
g2d.translate(pf.getImageableX(), pf.getImageableY());
88+
private static void createTestUI() {
89+
frame = new JFrame("Test InitToBlack");
90+
String INSTRUCTION = """
91+
Aim: This test checks whether the default foreground color on a printer
92+
graphics object is black so that rendering will appear without having
93+
to execute setColor.
94+
Step:
95+
1) Click on the "Print" button. Check whether page is printed on the printer.
96+
2) Check whether "Test Passes" is printed on the page and it should be in
97+
black color. If yes then press "Pass" button else press "Fail" button.
98+
""";
99+
JTextArea instructionTextArea = new JTextArea(INSTRUCTION, 4, 40);
100+
instructionTextArea.setEditable(false);
55101

56-
g.drawString("Test Passes", 200, 200);
102+
JPanel buttonPanel = new JPanel();
103+
JButton printButton = new JButton("Print");
104+
printButton.addActionListener((ae) -> {
105+
InitToBlack initToBlack = new InitToBlack();
106+
initToBlack.test();
107+
printButtonCountDownLatch.countDown();
108+
});
57109

58-
return PAGE_EXISTS;
110+
JButton passButton = new JButton("Pass");
111+
passButton.addActionListener((ae) -> {
112+
testResult = true;
113+
CountDownLatch.countDown();
114+
frame.dispose();
115+
});
116+
JButton failButton = new JButton("Fail");
117+
failButton.addActionListener((ae) -> {
118+
getFailureReason();
119+
frame.dispose();
120+
});
121+
buttonPanel.add(printButton);
122+
buttonPanel.add(passButton);
123+
buttonPanel.add(failButton);
124+
125+
JPanel panel = new JPanel(new BorderLayout());
126+
panel.add(instructionTextArea, BorderLayout.CENTER);
127+
panel.add(buttonPanel, BorderLayout.SOUTH);
128+
129+
frame.add(panel);
130+
frame.setLocationRelativeTo(null);
131+
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
132+
frame.pack();
133+
frame.setVisible(true);
134+
}
135+
136+
public static void getFailureReason() {
137+
final JDialog dialog = new JDialog();
138+
dialog.setTitle("Read testcase failure reason");
139+
JPanel jPanel = new JPanel(new BorderLayout());
140+
JTextArea jTextArea = new JTextArea(5, 20);
141+
142+
JButton okButton = new JButton("Ok");
143+
okButton.addActionListener((ae) -> {
144+
failureReason = jTextArea.getText();
145+
testResult = false;
146+
CountDownLatch.countDown();
147+
dialog.dispose();
148+
});
149+
150+
jPanel.add(new JLabel("Enter the testcase failed reason below and " +
151+
"click OK button", JLabel.CENTER), BorderLayout.NORTH);
152+
jPanel.add(jTextArea, BorderLayout.CENTER);
153+
154+
JPanel okayBtnPanel = new JPanel();
155+
okayBtnPanel.add(okButton);
156+
157+
jPanel.add(okayBtnPanel, BorderLayout.SOUTH);
158+
dialog.add(jPanel);
159+
dialog.setLocationRelativeTo(null);
160+
dialog.pack();
161+
dialog.setVisible(true);
59162
}
60163

61-
public static void main(String[] args) {
62-
new InitToBlack().init();
63-
System.exit(0);
164+
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
165+
SwingUtilities.invokeAndWait(InitToBlack::createTestUI);
166+
if (!printButtonCountDownLatch.await(2, TimeUnit.MINUTES)) {
167+
throw new RuntimeException("Timeout: User did not perform action " +
168+
"on Print button.");
169+
}
170+
if (!CountDownLatch.await(2, TimeUnit.MINUTES)) {
171+
throw new RuntimeException("Timeout : User did not decide " +
172+
"whether test passed or failed");
173+
}
174+
175+
if (!testResult) {
176+
throw new RuntimeException("Test failed : " + failureReason);
177+
} else {
178+
System.out.println("Test Passed");
179+
}
64180
}
65181
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jul 26, 2023

@openjdk-notifier[bot]
Please sign in to comment.