Skip to content

Commit 6d12f92

Browse files
toshiogataSendaoYan
authored and
SendaoYan
committed
8328242: Add a log area to the PassFailJFrame
Backport-of: 9bc1b065db238b7c9d0562f9bd55d2f338c6ff3d
1 parent aad0315 commit 6d12f92

File tree

1 file changed

+103
-5
lines changed

1 file changed

+103
-5
lines changed

test/jdk/java/awt/regtesthelpers/PassFailJFrame.java

+103-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.concurrent.atomic.AtomicInteger;
5454

5555
import javax.imageio.ImageIO;
56+
import javax.swing.Box;
5657
import javax.swing.JButton;
5758
import javax.swing.JComboBox;
5859
import javax.swing.JComponent;
@@ -155,6 +156,7 @@
155156
* <li>the title of the instruction UI,</li>
156157
* <li>the timeout of the test,</li>
157158
* <li>the size of the instruction UI via rows and columns, and</li>
159+
* <li>to add a log area</li>,
158160
* <li>to enable screenshots.</li>
159161
* </ul>
160162
*/
@@ -204,6 +206,8 @@ public final class PassFailJFrame {
204206

205207
private static Robot robot;
206208

209+
private static JTextArea logArea;
210+
207211
public enum Position {HORIZONTAL, VERTICAL, TOP_LEFT_CORNER}
208212

209213
public PassFailJFrame(String instructions) throws InterruptedException,
@@ -373,6 +377,20 @@ private static void invokeOnEDT(Runnable doRun)
373377
}
374378
}
375379

380+
/**
381+
* Does the same as {@link #invokeOnEDT(Runnable)}, but does not throw
382+
* any checked exceptions.
383+
*
384+
* @param doRun an operation to run on EDT
385+
*/
386+
private static void invokeOnEDTUncheckedException(Runnable doRun) {
387+
try {
388+
invokeOnEDT(doRun);
389+
} catch (InterruptedException | InvocationTargetException e) {
390+
throw new RuntimeException(e);
391+
}
392+
}
393+
376394
private static void createUI(String title, String instructions,
377395
long testTimeOut, int rows, int columns,
378396
boolean enableScreenCapture) {
@@ -384,7 +402,8 @@ private static void createUI(String title, String instructions,
384402
frame.add(createInstructionUIPanel(instructions,
385403
testTimeOut,
386404
rows, columns,
387-
enableScreenCapture),
405+
enableScreenCapture,
406+
false, 0),
388407
BorderLayout.CENTER);
389408
frame.pack();
390409
frame.setLocationRelativeTo(null);
@@ -401,8 +420,9 @@ private static void createUI(Builder builder) {
401420
createInstructionUIPanel(builder.instructions,
402421
builder.testTimeOut,
403422
builder.rows, builder.columns,
404-
builder.screenCapture);
405-
423+
builder.screenCapture,
424+
builder.addLogArea,
425+
builder.logAreaRows);
406426
if (builder.splitUI) {
407427
JSplitPane splitPane = new JSplitPane(
408428
builder.splitUIOrientation,
@@ -421,7 +441,9 @@ private static void createUI(Builder builder) {
421441
private static JComponent createInstructionUIPanel(String instructions,
422442
long testTimeOut,
423443
int rows, int columns,
424-
boolean enableScreenCapture) {
444+
boolean enableScreenCapture,
445+
boolean addLogArea,
446+
int logAreaRows) {
425447
JPanel main = new JPanel(new BorderLayout());
426448

427449
JLabel testTimeoutLabel = new JLabel("", JLabel.CENTER);
@@ -455,7 +477,20 @@ private static JComponent createInstructionUIPanel(String instructions,
455477
buttonsPanel.add(createCapturePanel());
456478
}
457479

458-
main.add(buttonsPanel, BorderLayout.SOUTH);
480+
if (addLogArea) {
481+
logArea = new JTextArea(logAreaRows, columns);
482+
logArea.setEditable(false);
483+
484+
Box buttonsLogPanel = Box.createVerticalBox();
485+
486+
buttonsLogPanel.add(buttonsPanel);
487+
buttonsLogPanel.add(new JScrollPane(logArea));
488+
489+
main.add(buttonsLogPanel, BorderLayout.SOUTH);
490+
} else {
491+
main.add(buttonsPanel, BorderLayout.SOUTH);
492+
}
493+
459494
main.setMinimumSize(main.getPreferredSize());
460495

461496
return main;
@@ -1039,13 +1074,45 @@ public static void forceFail(String reason) {
10391074
latch.countDown();
10401075
}
10411076

1077+
/**
1078+
* Adds a {@code message} to the log area, if enabled by
1079+
* {@link Builder#logArea()} or {@link Builder#logArea(int)}.
1080+
*
1081+
* @param message to log
1082+
*/
1083+
public static void log(String message) {
1084+
System.out.println("PassFailJFrame: " + message);
1085+
invokeOnEDTUncheckedException(() -> logArea.append(message + "\n"));
1086+
}
1087+
1088+
/**
1089+
* Clears the log area, if enabled by
1090+
* {@link Builder#logArea()} or {@link Builder#logArea(int)}.
1091+
*/
1092+
public static void logClear() {
1093+
System.out.println("\nPassFailJFrame: log cleared\n");
1094+
invokeOnEDTUncheckedException(() -> logArea.setText(""));
1095+
}
1096+
1097+
/**
1098+
* Replaces the log area content with provided {@code text}, if enabled by
1099+
* {@link Builder#logArea()} or {@link Builder#logArea(int)}.
1100+
* @param text new text for the log area
1101+
*/
1102+
public static void logSet(String text) {
1103+
System.out.println("\nPassFailJFrame: log set to:\n" + text + "\n");
1104+
invokeOnEDTUncheckedException(() -> logArea.setText(text));
1105+
}
1106+
10421107
public static final class Builder {
10431108
private String title;
10441109
private String instructions;
10451110
private long testTimeOut;
10461111
private int rows;
10471112
private int columns;
10481113
private boolean screenCapture;
1114+
private boolean addLogArea;
1115+
private int logAreaRows = 10;
10491116

10501117
private List<? extends Window> testWindows;
10511118
private WindowListCreator windowListCreator;
@@ -1087,6 +1154,37 @@ public Builder screenCapture() {
10871154
return this;
10881155
}
10891156

1157+
/**
1158+
* Adds a log area below the "Pass", "Fail" buttons.
1159+
* <p>
1160+
* The log area can be controlled by {@link #log(String)},
1161+
* {@link #logClear()} and {@link #logSet(String)}.
1162+
*
1163+
* @return this builder
1164+
*/
1165+
public Builder logArea() {
1166+
this.addLogArea = true;
1167+
return this;
1168+
}
1169+
1170+
/**
1171+
* Adds a log area below the "Pass", "Fail" buttons.
1172+
* <p>
1173+
* The log area can be controlled by {@link #log(String)},
1174+
* {@link #logClear()} and {@link #logSet(String)}.
1175+
* <p>
1176+
* The number of columns is taken from the number of
1177+
* columns in the instructional JTextArea.
1178+
*
1179+
* @param rows of the log area
1180+
* @return this builder
1181+
*/
1182+
public Builder logArea(int rows) {
1183+
this.addLogArea = true;
1184+
this.logAreaRows = rows;
1185+
return this;
1186+
}
1187+
10901188
/**
10911189
* Adds a {@code WindowCreator} which the framework will use
10921190
* to create the test UI window.

0 commit comments

Comments
 (0)