Skip to content

Commit

Permalink
8328242: Add a log area to the PassFailJFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
azvegint committed Mar 15, 2024
1 parent f6390e5 commit a0fc617
Showing 1 changed file with 111 additions and 5 deletions.
116 changes: 111 additions & 5 deletions test/jdk/java/awt/regtesthelpers/PassFailJFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.concurrent.atomic.AtomicInteger;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
Expand Down Expand Up @@ -205,6 +206,8 @@ public final class PassFailJFrame {

private static Robot robot;

private static JTextArea logArea;

public enum Position {HORIZONTAL, VERTICAL, TOP_LEFT_CORNER}

public PassFailJFrame(String instructions) throws InterruptedException,
Expand Down Expand Up @@ -374,6 +377,20 @@ private static void invokeOnEDT(Runnable doRun)
}
}

/**
* Does the same as {@link #invokeOnEDT(Runnable)}, but does not throw
* any checked exceptions.
*
* @param doRun an operation to run on EDT
*/
private static void invokeOnEDTUncheckedException(Runnable doRun) {
try {
invokeOnEDT(doRun);
} catch (InterruptedException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

private static void createUI(String title, String instructions,
long testTimeOut, int rows, int columns,
boolean enableScreenCapture) {
Expand All @@ -385,7 +402,8 @@ private static void createUI(String title, String instructions,
frame.add(createInstructionUIPanel(instructions,
testTimeOut,
rows, columns,
enableScreenCapture),
enableScreenCapture,
false, 0),
BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
Expand All @@ -402,8 +420,9 @@ private static void createUI(Builder builder) {
createInstructionUIPanel(builder.instructions,
builder.testTimeOut,
builder.rows, builder.columns,
builder.screenCapture);

builder.screenCapture,
builder.addLogArea,
builder.logAreaRows);
if (builder.splitUI) {
JSplitPane splitPane = new JSplitPane(
builder.splitUIOrientation,
Expand All @@ -422,7 +441,9 @@ private static void createUI(Builder builder) {
private static JComponent createInstructionUIPanel(String instructions,
long testTimeOut,
int rows, int columns,
boolean enableScreenCapture) {
boolean enableScreenCapture,
boolean addLogArea,
int logAreaRows) {
JPanel main = new JPanel(new BorderLayout());

JLabel testTimeoutLabel = new JLabel("", JLabel.CENTER);
Expand Down Expand Up @@ -456,7 +477,22 @@ private static JComponent createInstructionUIPanel(String instructions,
buttonsPanel.add(createCapturePanel());
}

main.add(buttonsPanel, BorderLayout.SOUTH);
if (addLogArea) {
logArea = new JTextArea(logAreaRows, columns);
logArea.setEditable(false);

JPanel buttonsLogPanel = new JPanel();
BoxLayout layout = new BoxLayout(buttonsLogPanel, BoxLayout.Y_AXIS);
buttonsLogPanel.setLayout(layout);

buttonsLogPanel.add(buttonsPanel);
buttonsLogPanel.add(new JScrollPane(logArea));

main.add(buttonsLogPanel, BorderLayout.SOUTH);
} else {
main.add(buttonsPanel, BorderLayout.SOUTH);
}

main.setMinimumSize(main.getPreferredSize());

return main;
Expand Down Expand Up @@ -1041,13 +1077,54 @@ public static void forceFail(String reason) {
latch.countDown();
}

/**
* Adds a {@code message} to the log area, if enabled by
* {@link Builder#logArea()} or {@link Builder#logArea(int)}.
*
* @param message to log
*/
public static void log(String message) {
invokeOnEDTUncheckedException(() -> {
if (logArea != null) {
logArea.append(message + System.lineSeparator());
}
});
}

/**
* Clears the log area, if enabled by
* {@link Builder#logArea()} or {@link Builder#logArea(int)}.
*/
public static void logClear() {
invokeOnEDTUncheckedException(() -> {
if (logArea != null) {
logArea.setText("");
}
});
}

/**
* Replaces the log area content with provided {@code text}, if enabled by
* {@link Builder#logArea()} or {@link Builder#logArea(int)}.
* @param text replacement
*/
public static void logSet(String text) {
invokeOnEDTUncheckedException(() -> {
if (logArea != null) {
logArea.setText(text);
}
});
}

public static final class Builder {
private String title;
private String instructions;
private long testTimeOut;
private int rows;
private int columns;
private boolean screenCapture;
private boolean addLogArea;
private int logAreaRows = 10;

private List<? extends Window> testWindows;
private WindowListCreator windowListCreator;
Expand Down Expand Up @@ -1097,6 +1174,35 @@ public Builder screenCapture() {
return this;
}

/**
* Adds a log area below the "Pass", "Fail" buttons.
* <p> The log area can be controlled by {@link #log(String)},
* {@link #logClear()} and {@link #logSet(String)}.
*
* @return this builder
*/
public Builder logArea() {
this.addLogArea = true;
return this;
}

/**
* Adds a log area below the "Pass", "Fail" buttons.
* <p> The log area can be controlled by {@link #log(String)},
* {@link #logClear()} and {@link #logSet(String)}.
*
* <p> The number of columns is taken from the number of
* columns in the instructional JTextArea.
*
* @param rows of the log area
* @return this builder
*/
public Builder logArea(int rows) {
this.addLogArea = true;
this.logAreaRows = rows;
return this;
}

/**
* Adds a {@code WindowCreator} which the framework will use
* to create the test UI window.
Expand Down

0 comments on commit a0fc617

Please sign in to comment.