Resolve generic JavaFX binding targets and verify them in IntelliJ

This commit is contained in:
2026-11-24 23:03:31 +01:00
parent 2d5e6be4b1
commit c980d0c6f5
10 changed files with 585 additions and 61 deletions
@@ -0,0 +1,160 @@
package qualification;
import java.awt.image.BufferedImage;
import java.nio.file.*;
import java.util.concurrent.*;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.stage.*;
import javax.imageio.ImageIO;
/** Reads the visible rows and drives the real import controls in the worker's interactive stage. */
public final class ImportTargetProbe {
public static void premain(String directory) {
if (System.getProperty("sun.java.command", "")
.startsWith("de.neitzel.fxdesigner.target.WorkerMain "))
Thread.ofPlatform().daemon().start(() -> Probe.run(Path.of(directory)));
}
private static final class Probe {
static void run(Path directory) {
try {
while (true) {
Thread.sleep(150);
var task = new FutureTask<>(() -> inspect(directory));
try {
Platform.runLater(task);
} catch (IllegalStateException booting) {
continue;
}
if (task.get(15, TimeUnit.SECONDS)) return;
}
} catch (Throwable failure) {
var trace = new java.io.StringWriter();
failure.printStackTrace(new java.io.PrintWriter(trace));
try {
Files.writeString(directory.resolve("target-error.txt"), trace.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
static boolean inspect(Path directory) throws Exception {
var stage =
Window.getWindows().stream()
.filter(Stage.class::isInstance)
.map(Stage.class::cast)
.filter(
w ->
w.isShowing()
&& w.getTitle() != null
&& w.getTitle().startsWith("Neitzel preview"))
.findFirst();
if (stage.isEmpty()) return false;
Node root = stage.get().getScene().getRoot();
String attempt = Files.readString(directory.resolve("import-attempt"));
String result;
if (attempt.equals("import-grid")) {
var cells =
root.lookupAll("*").stream()
.filter(Labeled.class::isInstance)
.map(n -> ((Labeled) n).getText())
.toList();
if (!cells.contains("Musterrechnung.pdf") || !cells.contains("Lieferbrief.txt"))
return false;
result = "Visible rows: Musterrechnung.pdf, Lieferbrief.txt";
} else {
var form = root.lookup("#form");
if (form == null) return false;
var name = (TextField) form.lookup("#name");
var folders = (ComboBox<?>) form.lookup("#folders");
var free = (Button) form.lookup("#useFreeName");
var target = (Label) form.lookup("#target");
var status = (Label) form.lookup("#status");
if (attempt.equals("import-free")) {
check(
target.getText().equals("Archivwurzel")
&& name.getText().equals("Rechnung.md")
&& !form.isDisabled(),
"Initial free name");
name.setText("Entwurf.md");
folders.getSelectionModel().select(1);
check(
target.getText().equals("Belege")
&& name.getText().equals("Entwurf.md")
&& !free.isVisible()
&& !free.isManaged(),
"Folder/name write-back");
folders.getSelectionModel().select(0);
check(
target.getText().equals("Archivwurzel") && name.getText().equals("Entwurf.md"),
"Return to root");
result = "Edited name Entwurf.md retained across Archivwurzel -> Belege -> Archivwurzel";
} else if (attempt.equals("import-busy")) {
check(
form.isDisabled()
&& name.isDisabled()
&& status.getText().contains("Datei wird abgelegt"),
"Busy draft must block input");
result = "Accepted draft entered busy state and disabled the form";
} else {
check(
attempt.equals("import-collision")
&& name.getText().equals("Rechnung.md")
&& target.getText().equals("Belege")
&& free.isVisible()
&& free.isManaged()
&& status.getText().contains("Dateiname belegt. Vorschlag: Rechnung (2).md"),
"Expected initial collision: "
+ name.getText()
+ " / "
+ target.getText()
+ " / "
+ status.getText());
picture(form, directory.resolve("import-collision.png"));
free.fire();
check(
name.getText().equals("Rechnung (2).md")
&& !free.isVisible()
&& !free.isManaged()
&& !status.getText().contains("belegt"),
"Accept free name");
picture(form, directory.resolve("import-accepted.png"));
result =
"Belege/Rechnung.md -> Rechnung (2).md; collision cleared, button invisible and"
+ " unmanaged";
}
}
Files.writeString(directory.resolve("result-" + attempt), result);
return true;
}
static void check(boolean condition, String message) {
if (!condition) throw new AssertionError(message);
}
static void picture(Node node, Path file) throws Exception {
node.getScene().getRoot().applyCss();
node.getScene().getRoot().layout();
var image = node.snapshot(null, null);
int width = (int) image.getWidth(), height = (int) image.getHeight();
var buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int[] pixels = new int[width * height];
image
.getPixelReader()
.getPixels(
0,
0,
width,
height,
javafx.scene.image.PixelFormat.getIntArgbInstance(),
pixels,
0,
width);
buffer.setRGB(0, 0, width, height, pixels, 0, width);
ImageIO.write(buffer, "png", file.toFile());
}
}
}
@@ -0,0 +1,230 @@
package qualification;
import static qualification.WorkflowProbe.*;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import de.neitzel.fxdesigner.protocol.BindingMetadata.Target;
import java.nio.file.*;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javax.imageio.ImageIO;
/** Exercises generic discovery, persists two bindings, then drives the actual target views. */
public final class ImportWorkflowProbe extends FormWorkflowProbe {
@Override
void verify(Project project) throws Exception {
open(project, "ConnectionForm", 2);
discover();
target("securityChoices", "view", "security", "items", "COMPATIBLE");
target("protocol", "both", "protocol", "value", "COMPATIBLE");
target("security", "both", "security", "value", "COMPATIBLE");
open(project, "FolderTab", 1);
discover();
target("view", "both", "view", "value", "COMPATIBLE");
target("grouping", "both", "grouping", "value", "COMPATIBLE");
target("view", "both", "grouping", "value", "CONVERTER_REQUIRED");
target("rows", "view", "profiles", "items", "INCOMPATIBLE");
target("rows", "view", "clearLayout", "accessibleHelp", "INCOMPATIBLE");
target("view", "both", "untypedPick", "value", "UNRESOLVED");
target("rows", "view", "grid", "items", "COMPATIBLE");
applyAndReopen(project, "FolderTab", 1, "grid", "items", "rows", "view");
preview("archive-folder", "import-grid");
open(project, "ImportForm", 3);
discover();
target("folder", "both", "folders", "value", "COMPATIBLE");
applyAndReopen(project, "ImportForm", 3, "folders", "value", "folder", "both");
preview("import-free", "import-free");
preview("import-collision", "import-collision");
preview("import-busy", "import-busy");
Files.writeString(output.resolve("SUCCESS.txt"), Files.readString(output.resolve("steps.txt")));
}
private void open(Project project, String form, int count) throws Exception {
var path =
Path.of(project.getBasePath(), "src/main/resources/de/neitzel/archiver/ui", form + ".fxml");
file = edt(() -> LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path));
pane = openDesigner(project, file);
fx(pane::flush).toCompletableFuture().get(10, java.util.concurrent.TimeUnit.SECONDS);
click(pane, "Form preview");
click(pane, "Build and Refresh");
await(
() -> fx(() -> ((ComboBox<?>) pane.lookup("#preview-example")).getItems().size() == count));
// The initial native document sync invalidates discovery while the first preview is loading.
// Wait for the displayed revision before selecting a source in the inspector.
await(
() ->
fx(
() -> {
String state = ((Label) pane.lookup("#preview-state")).getText();
if (state.startsWith("Preview failed")
&& form.equals("FolderTab")
&& !((TextArea) pane.lookup("#fxml-source"))
.getText()
.contains("path=\"rows\"")) return true;
if (state.startsWith("Preview failed")
|| state.startsWith("Preview unavailable")
|| state.startsWith("Preview blocked"))
throw new AssertionError(form + " initial preview: " + state);
return ((ImageView) pane.lookup("#full-preview-image")).getImage() != null
&& state.contains("revision");
}));
step("Opened " + form + " with " + count + " examples");
}
private void discover() throws Exception {
fx(
() -> {
var group =
(TitledPane)
pane.lookupAll(".titled-pane").stream()
.filter(n -> n instanceof TitledPane t && t.getText().equals("Bindings"))
.findFirst()
.orElseThrow();
group.setAnimated(false);
group.setExpanded(true);
var tabs = (TabPane) group.getContent();
tabs.getSelectionModel()
.select(
tabs.getTabs().stream()
.filter(t -> t.getText().equals("Discover targets"))
.findFirst()
.orElseThrow());
pane.applyCss();
pane.layout();
return null;
});
click(pane, "Discover bindings");
await(
() ->
fx(
() -> {
if (!((ComboBox<?>) pane.lookup("#binding-path")).getItems().isEmpty())
return true;
var message = ((Label) pane.lookup("#binding-status")).getText();
if (message.startsWith("Document changed")
|| message.startsWith("Build changed")) {
((Button)
pane.lookupAll(".button").stream()
.filter(
n ->
n instanceof Button b
&& b.getText().equals("Discover bindings"))
.findFirst()
.orElseThrow())
.fire();
}
if (message.startsWith("Discovery unavailable"))
throw new AssertionError(message);
return false;
}));
}
@SuppressWarnings("unchecked")
private void target(String path, String dir, String id, String property, String status)
throws Exception {
var prefix = path + " : ";
choose("binding-direction", dir);
await(
() ->
fx(
() ->
((ComboBox<?>) pane.lookup("#binding-path"))
.getItems().stream().anyMatch(x -> x.toString().startsWith(prefix))));
choose("binding-path", prefix);
await(
() ->
fx(
() -> {
var paths = (ComboBox<?>) pane.lookup("#binding-path");
var direction = (ComboBox<?>) pane.lookup("#binding-direction");
if (paths.getValue() == null
|| !paths.getValue().toString().startsWith(prefix)
|| !dir.equals(direction.getValue())) return false;
var table = (TableView<Target>) pane.lookup("#binding-targets");
var found =
table.getItems().stream()
.filter(
t ->
t.control().equals(id)
&& t.property().equals(property)
&& t.status().equals(status))
.findFirst();
found.ifPresent(t -> table.getSelectionModel().select(t));
return found.isPresent();
}));
step(path + " -> " + id + "." + property + " (" + dir + ") " + status);
}
private void applyAndReopen(
Project project, String form, int count, String id, String prop, String path, String dir)
throws Exception {
click(pane, "Use selected target");
if (!fx(
() ->
((TextField) pane.lookup("#direct-path")).getText().equals(path)
&& ((ComboBox<?>) pane.lookup("#direct-direction")).getValue().equals(dir)))
throw new AssertionError("Wrong binding");
click(pane, "Apply binding");
String declaration =
"control=\""
+ id
+ "\" property=\""
+ prop
+ "\" path=\""
+ path
+ "\" direction=\""
+ dir
+ "\"";
await(() -> source().contains(declaration));
click(pane, "Save");
await(() -> Files.readString(Path.of(file.getPath())).contains(declaration));
edt(
() -> {
FileEditorManager.getInstance(project).closeFile(file);
return null;
});
open(project, form, count);
if (!source().contains(declaration)) throw new AssertionError("Binding was lost on reopen");
step("Applied, saved and reopened " + declaration);
}
private void preview(String example, String attempt) throws Exception {
choose("preview-example", example);
String label =
fx(
() ->
((de.neitzel.fxdesigner.workflow.DesignerWorkflow.Example)
((ComboBox<?>) pane.lookup("#preview-example")).getValue())
.label());
await(
() ->
fx(
() -> {
String state = ((Label) pane.lookup("#preview-state")).getText();
if (state.startsWith("Preview failed")
|| state.startsWith("Preview blocked")
|| state.startsWith("Preview unavailable")) throw new AssertionError(state);
return ((ImageView) pane.lookup("#full-preview-image")).getImage() != null
&& state.contains(label);
}));
if (attempt.equals("import-collision"))
fx(
() -> {
ImageIO.write(
SwingFXUtils.fromFXImage(pane.snapshot(null, null), null),
"png",
output.resolve("intellij.png").toFile());
return null;
});
Files.writeString(output.resolve("import-attempt"), attempt);
click(pane, "Interactive preview");
awaitTarget("result-" + attempt);
step(Files.readString(output.resolve("result-" + attempt)));
click(pane, "Stop preview");
}
}