Run Designer previews with application-owned DI adapters

This commit is contained in:
2026-11-26 16:01:19 +01:00
parent e49d586916
commit 6126967fdc
56 changed files with 2131 additions and 50 deletions
@@ -0,0 +1,101 @@
import de.neitzel.fx.example.DeskApp;
import de.neitzel.fx.example.model.Person;
import de.neitzel.fxdesigner.preview.ExampleDescriptor;
import de.neitzel.fxdesigner.preview.PreviewProvider;
import de.neitzel.fxdesigner.preview.PreviewSession;
import de.neitzel.fxdesigner.target.ExampleCatalog;
import de.neitzel.fxdesigner.target.PreviewFailure;
import de.neitzel.fxdesigner.target.TargetPreview;
import de.neitzel.injection.ApplicationContext;
import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import javafx.application.Platform;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
/** Exercises actual Person Desk preview providers against the split FX artifacts. */
public final class NeitzelAdapterPreviewCheck {
public static void main(String[] args) throws Exception {
var loader = NeitzelAdapterPreviewCheck.class.getClassLoader();
String scenario = args[0];
if (scenario.equals("application")) {
application();
return;
}
String example = scenario.equals("row") ? "row" : "desk";
var descriptor = ExampleCatalog.discover(loader).stream()
.filter(entry -> entry.id().equals(example)).findFirst().orElseThrow();
if (scenario.equals("raw-context"))
descriptor = new ExampleDescriptor("raw-context", descriptor.label(), descriptor.modelType(),
LegacyProvider.class.getName(), descriptor.viewResource(), descriptor.componentPackage());
Platform.startup(() -> Platform.setImplicitExit(false));
try (var preview = TargetPreview.open(descriptor, loader, Duration.ofSeconds(25))) {
try {
preview.ready().toCompletableFuture().get(25, TimeUnit.SECONDS);
if (scenario.equals("raw-context")) throw new AssertionError("Raw ApplicationContext was accepted");
var check = new FutureTask<Void>(() -> {
var view = preview.loadedView();
if (scenario.equals("row")) {
((Button) view.getRoot().lookup(".button")).fire();
} else if (!"Ada Lovelace".equals(((TextField) view.getRoot().lookup("#nameField")).getText())) {
throw new AssertionError("The Person Desk Model was not bound");
}
if (preview.render(500, 380).length != 500 * 380 * 4)
throw new AssertionError("Unexpected render size");
return null;
});
Platform.runLater(check);
check.get(15, TimeUnit.SECONDS);
} catch (ExecutionException failure) {
if (!scenario.equals("raw-context")
|| !(failure.getCause() instanceof PreviewFailure diagnostic)
|| diagnostic.code() != PreviewFailure.Code.CONTEXT_TYPE
|| !diagnostic.getMessage().contains("fx-injection")) throw failure;
}
preview.close();
preview.cleanup().toCompletableFuture().get(10, TimeUnit.SECONDS);
System.out.println("PASS " + scenario);
} finally {
Platform.exit();
}
}
private static void application() throws Exception {
var check = new FutureTask<Void>(() -> {
var window = new Stage();
try {
new DeskApp().start(window);
if (!window.isShowing()
|| !"Ada Lovelace".equals(((TextField) window.getScene().lookup("#nameField")).getText()))
throw new AssertionError("The migrated Person Desk did not open");
} finally {
window.close();
}
return null;
});
Platform.startup(check);
try {
check.get(15, TimeUnit.SECONDS);
System.out.println("PASS application");
} finally {
Platform.exit();
}
}
public static final class LegacyProvider implements PreviewProvider<Person, ApplicationContext> {
public Class<Person> modelType() { return Person.class; }
public PreviewSession<Person, ApplicationContext> open(String example) {
var person = DeskApp.sample();
var context = DeskApp.dependencies(person).context();
return new PreviewSession<>() {
public Person model() { return person; }
public ApplicationContext context() { return context; }
public void close() {}
};
}
}
}
@@ -0,0 +1,66 @@
package qualification;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import javafx.application.Platform;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.Window;
/** Test-only observer of the actual interactive window, inside the target JVM. */
public final class SpringTargetProbe {
public static void premain(String output) {
var command = System.getProperty("sun.java.command", "").split(" ", 2)[0];
if (command.endsWith(".WorkerMain"))
Thread.ofPlatform().daemon().name("spring-preview-probe").start(() -> Observer.observe(Path.of(output)));
}
private static class Observer {
private static void observe(Path output) {
try {
while (true) {
Thread.sleep(100);
var inspect = new FutureTask<>(() -> inspect(output));
try {
Platform.runLater(inspect);
} catch (IllegalStateException toolkitStarting) {
continue;
}
if (inspect.get(10, TimeUnit.SECONDS)) return;
}
} catch (InterruptedException stopped) {
Thread.currentThread().interrupt();
} catch (Throwable error) {
try {
Files.writeString(output.resolve("target-error.txt"), error.toString());
} catch (Exception writeFailure) {
error.addSuppressed(writeFailure);
error.printStackTrace();
}
}
}
private static boolean inspect(Path output) throws Exception {
var stage = Window.getWindows().stream()
.filter(w -> w instanceof Stage window && window.isShowing() && window.getOpacity() > 0
&& window.getTitle() != null && window.getTitle().startsWith("Neitzel preview"))
.findFirst();
if (stage.isEmpty()) return false;
String attempt = Files.readString(output.resolve("spring-attempt"));
boolean book = attempt.startsWith("book-");
var scene = stage.get().getScene();
var field = (TextField) scene.lookup(book ? "#bookName" : "#entryName");
String expected = book ? "Preview contact book" : "Preview entry";
if (field == null || !expected.equals(field.getText()))
throw new AssertionError("Expected fresh " + expected + " in " + attempt);
field.setText("Edited in preview");
if (book && !"[Edited in preview]".equals(((Label) scene.lookup("#convertedName")).getText()))
throw new AssertionError("Spring Converter did not update the live interactive View");
Files.writeString(output.resolve(attempt + ".txt"), expected);
return true;
}
}
}
@@ -0,0 +1,44 @@
package qualification;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.vfs.LocalFileSystem;
import de.neitzel.fxdesigner.kit.SpringPreviewHostChecks;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import static qualification.WorkflowProbe.edt;
import static qualification.WorkflowProbe.openDesigner;
import static qualification.WorkflowProbe.output;
public final class SpringWorkflowProbe implements StartupActivity.DumbAware {
public void runActivity(Project project) {
if (!WorkflowProbe.started.compareAndSet(false, true)) return;
ApplicationManager.getApplication().executeOnPooledThread(() -> {
try {
var path = Path.of(project.getBasePath(), "app/src/main/resources/fixture/spring/ui");
verify(project, path.resolve("Book.fxml"), "Spring contact book", true);
verify(project, path.resolve("EntryCard.fxml"), "Spring entry", false);
Files.writeString(output.resolve("SUCCESS.txt"),
"PASS Spring canvas, form and interactive previews in the registered IntelliJ editor");
} catch (Throwable failure) {
var trace = new StringWriter();
failure.printStackTrace(new PrintWriter(trace));
try {
Files.writeString(output.resolve("FAILURE.txt"), trace.toString());
} catch (Exception writeFailure) {
failure.addSuppressed(writeFailure);
failure.printStackTrace();
}
}
});
}
private static void verify(Project project, Path path, String label, boolean book) throws Exception {
var file = edt(() -> LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path));
SpringPreviewHostChecks.verify(openDesigner(project, file), label, book, output);
}
}