Skip to content

Commit 385731f

Browse files
author
Andrew Lu
committed
8320303: Allow PassFailJFrame to accept single window creator
Backport-of: 83ffc1ac94b8893532d8663b9058592f1714d337
1 parent 30ead33 commit 385731f

File tree

1 file changed

+146
-27
lines changed

1 file changed

+146
-27
lines changed

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

+146-27
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@
9393
* .awaitAndCheck();
9494
* }
9595
*
96-
* private static List<Window> createTestUI() {
96+
* private static Window createTestUI() {
9797
* JFrame testUI = new JFrame("Test UI");
9898
* testUI.setSize(250, 150);
99-
* return List.of(testUI);
99+
* return testUI;
100100
* }
101101
* }
102102
* }</pre>
@@ -108,6 +108,10 @@
108108
* the provided {@code createTestUI} on the Event Dispatch Thread (EDT),
109109
* and it will automatically position the test UI and make it visible.
110110
* <p>
111+
* The {@code Builder.testUI} methods accept interfaces which create one window
112+
* or a list of windows if the test needs multiple windows,
113+
* or directly a single window, an array of windows or a list of windows.
114+
* <p>
111115
* Alternatively, use one of the {@code PassFailJFrame} constructors to
112116
* create an object, then create secondary test UI, register it
113117
* with {@code PassFailJFrame}, position it and make it visible.
@@ -144,7 +148,7 @@
144148
* <li>to enable screenshots.</li>
145149
* </ul>
146150
*/
147-
public class PassFailJFrame {
151+
public final class PassFailJFrame {
148152

149153
private static final String TITLE = "Test Instruction Frame";
150154
private static final long TEST_TIMEOUT = 5;
@@ -263,12 +267,18 @@ private PassFailJFrame(Builder builder) throws InterruptedException,
263267
this(builder.title, builder.instructions, builder.testTimeOut,
264268
builder.rows, builder.columns, builder.screenCapture);
265269

266-
if (builder.windowCreator != null) {
270+
if (builder.windowListCreator != null) {
267271
invokeOnEDT(() ->
268-
builder.testWindows = builder.windowCreator.createTestUI());
272+
builder.testWindows = builder.windowListCreator.createTestUI());
273+
if (builder.testWindows == null) {
274+
throw new IllegalStateException("Window list creator returned null list");
275+
}
269276
}
270277

271278
if (builder.testWindows != null) {
279+
if (builder.testWindows.isEmpty()) {
280+
throw new IllegalStateException("Window list is empty");
281+
}
272282
addTestWindow(builder.testWindows);
273283
builder.testWindows
274284
.forEach(w -> w.addWindowListener(windowClosingHandler));
@@ -279,17 +289,15 @@ private PassFailJFrame(Builder builder) throws InterruptedException,
279289
builder.positionWindows
280290
.positionTestWindows(unmodifiableList(builder.testWindows),
281291
builder.instructionUIHandler);
282-
283-
windowList.forEach(w -> w.setVisible(true));
284292
});
285293
} else if (builder.testWindows.size() == 1) {
286294
Window window = builder.testWindows.get(0);
287295
positionTestWindow(window, builder.position);
288-
window.setVisible(true);
289296
} else {
290297
positionTestWindow(null, builder.position);
291298
}
292299
}
300+
showAllWindows();
293301
}
294302

295303
/**
@@ -370,7 +378,7 @@ private static void createUI(String title, String instructions,
370378
frame.add(buttonsPanel, BorderLayout.SOUTH);
371379
frame.pack();
372380
frame.setLocationRelativeTo(null);
373-
windowList.add(frame);
381+
addTestWindow(frame);
374382
}
375383

376384
private static JTextComponent configurePlainText(String instructions,
@@ -401,14 +409,27 @@ private static JTextComponent configureHTML(String instructions,
401409

402410

403411
/**
404-
* Creates one or more windows for test UI.
412+
* Creates a test UI window.
405413
*/
406414
@FunctionalInterface
407415
public interface WindowCreator {
416+
/**
417+
* Creates a window for test UI.
418+
* This method is called by the framework on the EDT.
419+
* @return a test UI window
420+
*/
421+
Window createTestUI();
422+
}
423+
424+
/**
425+
* Creates a list of test UI windows.
426+
*/
427+
@FunctionalInterface
428+
public interface WindowListCreator {
408429
/**
409430
* Creates one or more windows for test UI.
410431
* This method is called by the framework on the EDT.
411-
* @return a list of windows.
432+
* @return a list of test UI windows
412433
*/
413434
List<? extends Window> createTestUI();
414435
}
@@ -424,8 +445,13 @@ public interface PositionWindows {
424445
* the instruction UI frame was positioned on the screen.
425446
* <p>
426447
* The list of the test windows contains the windows
427-
* that were passed to the framework via
428-
* {@link Builder#testUI(WindowCreator) testUI} method.
448+
* that were passed to the framework via the
449+
* {@link Builder#testUI(Window...) testUI(Window...)} method or
450+
* that were created with {@code WindowCreator}
451+
* or {@code WindowListCreator} which were passed via
452+
* {@link Builder#testUI(WindowCreator) testUI(WindowCreator)} or
453+
* {@link Builder#testUI(WindowListCreator) testUI(WindowListCreator)}
454+
* correspondingly.
429455
*
430456
* @param testWindows the list of test windows
431457
* @param instructionUI information about the instruction frame
@@ -799,6 +825,29 @@ public static synchronized void addTestWindow(Collection<? extends Window> testW
799825
windowList.addAll(testWindows);
800826
}
801827

828+
/**
829+
* Displays all the windows in {@code windowList}.
830+
*
831+
* @throws InterruptedException if the thread is interrupted while
832+
* waiting for the event dispatch thread to finish running
833+
* the {@link #showUI() showUI}
834+
* @throws InvocationTargetException if an exception is thrown while
835+
* the event dispatch thread executes {@code showUI}
836+
*/
837+
private static void showAllWindows()
838+
throws InterruptedException, InvocationTargetException {
839+
invokeOnEDT(PassFailJFrame::showUI);
840+
}
841+
842+
/**
843+
* Displays all the windows in {@code windowList}; it has to be called on
844+
* the EDT &mdash; use {@link #showAllWindows() showAllWindows} to ensure it.
845+
*/
846+
private static synchronized void showUI() {
847+
windowList.forEach(w -> w.setVisible(true));
848+
}
849+
850+
802851
/**
803852
* Forcibly pass the test.
804853
* <p>The sample usage:
@@ -841,7 +890,7 @@ public static final class Builder {
841890
private boolean screenCapture;
842891

843892
private List<? extends Window> testWindows;
844-
private WindowCreator windowCreator;
893+
private WindowListCreator windowListCreator;
845894
private PositionWindows positionWindows;
846895
private InstructionUI instructionUIHandler;
847896

@@ -877,39 +926,109 @@ public Builder screenCapture() {
877926
return this;
878927
}
879928

929+
/**
930+
* Adds a {@code WindowCreator} which the framework will use
931+
* to create the test UI window.
932+
*
933+
* @param windowCreator a {@code WindowCreator}
934+
* to create the test UI window
935+
* @return this builder
936+
* @throws IllegalArgumentException if {@code windowCreator} is {@code null}
937+
* @throws IllegalStateException if a window creator
938+
* or a list of test windows is already set
939+
*/
940+
public Builder testUI(WindowCreator windowCreator) {
941+
if (windowCreator == null) {
942+
throw new IllegalArgumentException("The window creator can't be null");
943+
}
944+
945+
checkWindowsLists();
946+
947+
this.windowListCreator = () -> List.of(windowCreator.createTestUI());
948+
return this;
949+
}
950+
951+
/**
952+
* Adds a {@code WindowListCreator} which the framework will use
953+
* to create a list of test UI windows.
954+
*
955+
* @param windowListCreator a {@code WindowListCreator}
956+
* to create test UI windows
957+
* @return this builder
958+
* @throws IllegalArgumentException if {@code windowListCreator} is {@code null}
959+
* @throws IllegalStateException if a window creator
960+
* or a list of test windows is already set
961+
*/
962+
public Builder testUI(WindowListCreator windowListCreator) {
963+
if (windowListCreator == null) {
964+
throw new IllegalArgumentException("The window list creator can't be null");
965+
}
966+
967+
checkWindowsLists();
968+
969+
this.windowListCreator = windowListCreator;
970+
return this;
971+
}
972+
973+
/**
974+
* Adds an already created test UI window.
975+
* The window is positioned and shown automatically.
976+
*
977+
* @param window a test UI window
978+
* @return this builder
979+
*/
880980
public Builder testUI(Window window) {
881981
return testUI(List.of(window));
882982
}
883983

984+
/**
985+
* Adds an array of already created test UI windows.
986+
*
987+
* @param windows an array of test UI windows
988+
* @return this builder
989+
*/
884990
public Builder testUI(Window... windows) {
885991
return testUI(List.of(windows));
886992
}
887993

888-
public Builder testUI(List<Window> windows) {
994+
/**
995+
* Adds a list of already created test UI windows.
996+
*
997+
* @param windows a list of test UI windows
998+
* @return this builder
999+
* @throws IllegalArgumentException if {@code windows} is {@code null}
1000+
* or the list contains {@code null}
1001+
* @throws IllegalStateException if a window creator
1002+
* or a list of test windows is already set
1003+
*/
1004+
public Builder testUI(List<? extends Window> windows) {
8891005
if (windows == null) {
8901006
throw new IllegalArgumentException("The list of windows can't be null");
8911007
}
8921008
if (windows.stream()
8931009
.anyMatch(Objects::isNull)) {
894-
throw new IllegalArgumentException("The windows list can't contain null");
1010+
throw new IllegalArgumentException("The list of windows can't contain null");
8951011
}
8961012

897-
if (windowCreator != null) {
898-
throw new IllegalStateException("windowCreator is already set");
899-
}
1013+
checkWindowsLists();
1014+
9001015
this.testWindows = windows;
9011016
return this;
9021017
}
9031018

904-
public Builder testUI(WindowCreator windowCreator) {
905-
if (windowCreator == null) {
906-
throw new IllegalArgumentException("The window creator can't be null");
1019+
/**
1020+
* Verifies the state of window list and window creator.
1021+
*
1022+
* @throws IllegalStateException if a windows list creator
1023+
* or a list of test windows is already set
1024+
*/
1025+
private void checkWindowsLists() {
1026+
if (windowListCreator != null) {
1027+
throw new IllegalStateException("Window list creator is already set");
9071028
}
9081029
if (testWindows != null) {
909-
throw new IllegalStateException("testWindows are already set");
1030+
throw new IllegalStateException("The list of test windows is already set");
9101031
}
911-
this.windowCreator = windowCreator;
912-
return this;
9131032
}
9141033

9151034
public Builder positionTestUI(PositionWindows positionWindows) {
@@ -951,13 +1070,13 @@ private void validate() {
9511070
}
9521071

9531072
if (position == null
954-
&& (testWindows != null || windowCreator != null)) {
1073+
&& (testWindows != null || windowListCreator != null)) {
9551074

9561075
position = Position.HORIZONTAL;
9571076
}
9581077

9591078
if (positionWindows != null) {
960-
if (testWindows == null && windowCreator == null) {
1079+
if (testWindows == null && windowListCreator == null) {
9611080
throw new IllegalStateException("To position windows, "
9621081
+ "provide an a list of windows to the builder");
9631082
}

0 commit comments

Comments
 (0)