Skip to content

Commit 01e9dbd

Browse files
toshiogataPaul Hohensee
authored andcommitted
8328242: Add a log area to the PassFailJFrame
Backport-of: 9bc1b065db238b7c9d0562f9bd55d2f338c6ff3d
1 parent 826184c commit 01e9dbd

File tree

1 file changed

+103
-5
lines changed

1 file changed

+103
-5
lines changed

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

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.concurrent.atomic.AtomicInteger;
5656

5757
import javax.imageio.ImageIO;
58+
import javax.swing.Box;
5859
import javax.swing.JButton;
5960
import javax.swing.JComboBox;
6061
import javax.swing.JComponent;
@@ -157,6 +158,7 @@
157158
* <li>the title of the instruction UI,</li>
158159
* <li>the timeout of the test,</li>
159160
* <li>the size of the instruction UI via rows and columns, and</li>
161+
* <li>to add a log area</li>,
160162
* <li>to enable screenshots.</li>
161163
* </ul>
162164
*/
@@ -206,6 +208,8 @@ public final class PassFailJFrame {
206208

207209
private static Robot robot;
208210

211+
private static JTextArea logArea;
212+
209213
public enum Position {HORIZONTAL, VERTICAL, TOP_LEFT_CORNER}
210214

211215
public PassFailJFrame(String instructions) throws InterruptedException,
@@ -375,6 +379,20 @@ private static void invokeOnEDT(Runnable doRun)
375379
}
376380
}
377381

382+
/**
383+
* Does the same as {@link #invokeOnEDT(Runnable)}, but does not throw
384+
* any checked exceptions.
385+
*
386+
* @param doRun an operation to run on EDT
387+
*/
388+
private static void invokeOnEDTUncheckedException(Runnable doRun) {
389+
try {
390+
invokeOnEDT(doRun);
391+
} catch (InterruptedException | InvocationTargetException e) {
392+
throw new RuntimeException(e);
393+
}
394+
}
395+
378396
private static void createUI(String title, String instructions,
379397
long testTimeOut, int rows, int columns,
380398
boolean enableScreenCapture) {
@@ -386,7 +404,8 @@ private static void createUI(String title, String instructions,
386404
frame.add(createInstructionUIPanel(instructions,
387405
testTimeOut,
388406
rows, columns,
389-
enableScreenCapture),
407+
enableScreenCapture,
408+
false, 0),
390409
BorderLayout.CENTER);
391410
frame.pack();
392411
frame.setLocationRelativeTo(null);
@@ -403,8 +422,9 @@ private static void createUI(Builder builder) {
403422
createInstructionUIPanel(builder.instructions,
404423
builder.testTimeOut,
405424
builder.rows, builder.columns,
406-
builder.screenCapture);
407-
425+
builder.screenCapture,
426+
builder.addLogArea,
427+
builder.logAreaRows);
408428
if (builder.splitUI) {
409429
JSplitPane splitPane = new JSplitPane(
410430
builder.splitUIOrientation,
@@ -423,7 +443,9 @@ private static void createUI(Builder builder) {
423443
private static JComponent createInstructionUIPanel(String instructions,
424444
long testTimeOut,
425445
int rows, int columns,
426-
boolean enableScreenCapture) {
446+
boolean enableScreenCapture,
447+
boolean addLogArea,
448+
int logAreaRows) {
427449
JPanel main = new JPanel(new BorderLayout());
428450
timeoutHandlerPanel = new TimeoutHandlerPanel(testTimeOut);
429451
main.add(timeoutHandlerPanel, BorderLayout.NORTH);
@@ -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;
@@ -1074,13 +1109,45 @@ public static void forceFail(String reason) {
10741109
latch.countDown();
10751110
}
10761111

1112+
/**
1113+
* Adds a {@code message} to the log area, if enabled by
1114+
* {@link Builder#logArea()} or {@link Builder#logArea(int)}.
1115+
*
1116+
* @param message to log
1117+
*/
1118+
public static void log(String message) {
1119+
System.out.println("PassFailJFrame: " + message);
1120+
invokeOnEDTUncheckedException(() -> logArea.append(message + "\n"));
1121+
}
1122+
1123+
/**
1124+
* Clears the log area, if enabled by
1125+
* {@link Builder#logArea()} or {@link Builder#logArea(int)}.
1126+
*/
1127+
public static void logClear() {
1128+
System.out.println("\nPassFailJFrame: log cleared\n");
1129+
invokeOnEDTUncheckedException(() -> logArea.setText(""));
1130+
}
1131+
1132+
/**
1133+
* Replaces the log area content with provided {@code text}, if enabled by
1134+
* {@link Builder#logArea()} or {@link Builder#logArea(int)}.
1135+
* @param text new text for the log area
1136+
*/
1137+
public static void logSet(String text) {
1138+
System.out.println("\nPassFailJFrame: log set to:\n" + text + "\n");
1139+
invokeOnEDTUncheckedException(() -> logArea.setText(text));
1140+
}
1141+
10771142
public static final class Builder {
10781143
private String title;
10791144
private String instructions;
10801145
private long testTimeOut;
10811146
private int rows;
10821147
private int columns;
10831148
private boolean screenCapture;
1149+
private boolean addLogArea;
1150+
private int logAreaRows = 10;
10841151

10851152
private List<? extends Window> testWindows;
10861153
private WindowListCreator windowListCreator;
@@ -1122,6 +1189,37 @@ public Builder screenCapture() {
11221189
return this;
11231190
}
11241191

1192+
/**
1193+
* Adds a log area below the "Pass", "Fail" buttons.
1194+
* <p>
1195+
* The log area can be controlled by {@link #log(String)},
1196+
* {@link #logClear()} and {@link #logSet(String)}.
1197+
*
1198+
* @return this builder
1199+
*/
1200+
public Builder logArea() {
1201+
this.addLogArea = true;
1202+
return this;
1203+
}
1204+
1205+
/**
1206+
* Adds a log area below the "Pass", "Fail" buttons.
1207+
* <p>
1208+
* The log area can be controlled by {@link #log(String)},
1209+
* {@link #logClear()} and {@link #logSet(String)}.
1210+
* <p>
1211+
* The number of columns is taken from the number of
1212+
* columns in the instructional JTextArea.
1213+
*
1214+
* @param rows of the log area
1215+
* @return this builder
1216+
*/
1217+
public Builder logArea(int rows) {
1218+
this.addLogArea = true;
1219+
this.logAreaRows = rows;
1220+
return this;
1221+
}
1222+
11251223
/**
11261224
* Adds a {@code WindowCreator} which the framework will use
11271225
* to create the test UI window.

0 commit comments

Comments
 (0)