Run Designer previews with application-owned DI adapters
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package de.neitzel.fxdesigner.kit;
|
||||
|
||||
import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance;
|
||||
import com.oracle.javafx.scenebuilder.kit.fxom.FXOMFxIdIndex;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.image.ImageView;
|
||||
|
||||
/** Runs the same UI actions in the standalone and the registered IntelliJ editor. */
|
||||
public final class SpringPreviewHostChecks {
|
||||
private SpringPreviewHostChecks() {}
|
||||
|
||||
public static void verify(DesignerPane pane, String label, boolean book, Path output) throws Exception {
|
||||
try {
|
||||
await(() -> pane.kit().editor().getFxomDocument() != null, "canvas");
|
||||
click(pane, "Build and Refresh");
|
||||
if (book) {
|
||||
await(() -> {
|
||||
var component = new FXOMFxIdIndex(pane.kit().editor().getFxomDocument()).lookup("featured");
|
||||
return component instanceof FXOMInstance instance
|
||||
&& instance.getSceneGraphObject() instanceof SnapshotRegion region
|
||||
&& region.image() != null && region.statusText().equals("Target runtime");
|
||||
}, "Spring Component canvas pixels");
|
||||
}
|
||||
click(pane, "Form preview");
|
||||
await(() -> pane.lookup("#full-preview-image") instanceof ImageView view && view.getImage() != null
|
||||
&& ((Label) pane.lookup("#preview-state")).getText().startsWith(label + " · "),
|
||||
label + " form preview");
|
||||
for (int run = 1; run <= 2; run++) {
|
||||
String attempt = (book ? "book-" : "card-") + run;
|
||||
System.out.println(label + " interactive preview " + run);
|
||||
Files.writeString(output.resolve("spring-attempt"), attempt);
|
||||
click(pane, "Interactive preview");
|
||||
await(() -> {
|
||||
var error = output.resolve("target-error.txt");
|
||||
if (Files.exists(error)) throw new AssertionError(Files.readString(error));
|
||||
if (hasLabel(pane, "Interactive preview failed; use Interactive preview to retry"))
|
||||
throw new AssertionError("Interactive preview failed");
|
||||
var result = output.resolve(attempt + ".txt");
|
||||
return Files.exists(result) && Files.readString(result)
|
||||
.equals(book ? "Preview contact book" : "Preview entry");
|
||||
}, label + " interactive preview " + run);
|
||||
click(pane, "Stop preview");
|
||||
}
|
||||
} catch (Exception | AssertionError failure) {
|
||||
var details = fx(() -> Stream.concat(pane.lookupAll(".label").stream(), pane.lookupAll(".text-area").stream())
|
||||
.map(node -> switch (node) {
|
||||
case Label control -> control.getText();
|
||||
case TextArea control -> control.getText();
|
||||
default -> "";
|
||||
})
|
||||
.filter(text -> text != null && !text.isBlank())
|
||||
.distinct().toList());
|
||||
throw new AssertionError(details.toString(), failure);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasLabel(DesignerPane pane, String text) {
|
||||
return pane.lookupAll(".label").stream().anyMatch(node ->
|
||||
node instanceof Label label && text.equals(label.getText()));
|
||||
}
|
||||
|
||||
private static void click(DesignerPane pane, String text) throws Exception {
|
||||
fx(() -> {
|
||||
var button = pane.lookupAll(".button").stream()
|
||||
.filter(node -> node instanceof Button control && text.equals(control.getText()))
|
||||
.map(Button.class::cast)
|
||||
.findFirst().orElseThrow();
|
||||
button.fire();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private static void await(Callable<Boolean> condition, String description) throws Exception {
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(100);
|
||||
while (System.nanoTime() < deadline) {
|
||||
if (fx(condition)) return;
|
||||
Thread.sleep(100);
|
||||
}
|
||||
throw new AssertionError("Timed out waiting for " + description);
|
||||
}
|
||||
|
||||
public static <T> T fx(Callable<T> action) throws Exception {
|
||||
var task = new FutureTask<T>(action);
|
||||
Platform.runLater(task);
|
||||
return task.get(15, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user