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(() -> { 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(() -> { 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 { public Class modelType() { return Person.class; } public PreviewSession 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() {} }; } } }