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.workflow.DesignerWorkflow.Example; import java.nio.file.*; import javafx.scene.control.*; import javafx.scene.image.ImageView; import org.w3c.dom.Element; /** Shared actions for editing declarations and opening the resulting Archiver preview. */ abstract class DeclarationWorkflowProbe extends FormWorkflowProbe { Project project; void open(String name) throws Exception { var path = Path.of(project.getBasePath(), "src/main/resources/de/neitzel/archiver/ui", name + ".fxml"); file = edt(() -> LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path)); pane = openDesigner(project, file); if (!source().contains(" fx( () -> ((ComboBox) pane.lookup("#preview-example")) .getItems().stream().anyMatch(item -> item.toString().contains(id)))); } void interactive(String id, String attempt) throws Exception { choose("preview-example", id); await( () -> fx( () -> { var state = ((Label) pane.lookup("#preview-state")).getText(); if (state.startsWith("Preview failed") || state.startsWith("Preview blocked") || state.startsWith("Preview unavailable")) throw new AssertionError(id + " · " + state); var choice = (Example) ((ComboBox) pane.lookup("#preview-example")).getValue(); return ((ImageView) pane.lookup("#full-preview-image")).getImage() != null && state.contains(choice.label()); })); Files.writeString(output.resolve("layout-case"), attempt); click(pane, "Interactive preview"); String name = "result-" + attempt; awaitTarget(name); step(id + " · " + Files.readString(output.resolve(name))); click(pane, "Stop preview"); } void reopen(String form, String saved) throws Exception { click(pane, "Save"); await(() -> Files.readString(Path.of(file.getPath())).contains(saved)); edt( () -> { FileEditorManager.getInstance(project).closeFile(file); return null; }); open(form); if (!source().contains(saved)) throw new AssertionError("Reopened " + form); } void inspector(boolean expanded) throws Exception { fx( () -> { var panel = (TitledPane) pane.lookupAll(".titled-pane").stream() .filter(n -> n instanceof TitledPane t && t.getText().equals("Bindings")) .findFirst() .orElseThrow(); panel.setAnimated(false); panel.setExpanded(expanded); var tabs = (TabPane) panel.getContent(); tabs.getSelectionModel() .select( tabs.getTabs().stream() .filter(t -> t.getText().equals("DataGrid / Docking")) .findFirst() .orElseThrow()); pane.applyCss(); pane.layout(); return null; }); } void select(String name, String id) throws Exception { fx( () -> { var tree = (TreeView) pane.lookup("#declaration-tree"); for (int i = 0; i < tree.getExpandedItemCount(); i++) { var item = tree.getTreeItem(i); var e = item.getValue(); String tag = e.getTagName(); if ((tag.equals(name) || tag.endsWith("." + name)) && (id.isEmpty() || e.getAttribute("fx:id").equals(id) || e.getAttribute("id").equals(id) || e.getAttribute("destinationId").equals(id))) { tree.getSelectionModel().select(item); return null; } } throw new AssertionError("Missing " + name + " " + id); }); } void combo(String name, String text) throws Exception { fx( () -> { var box = (ComboBox) pane.lookup("#" + name); box.setValue(text); return null; }); } void edit(String button, String expected) throws Exception { String before = source(); click(pane, button); await( () -> { String now = source(); var msg = fx(() -> ((Label) pane.lookup("#declaration-message")).getText()); if (!msg.isEmpty()) throw new AssertionError(msg); return !now.equals(before) && now.contains(expected) && fx( () -> ((TextArea) pane.lookup("#fxml-source")) == null || ((TextArea) pane.lookup("#fxml-source")).getText().equals(now)); }); fx(pane::flush).toCompletableFuture().get(); step(button + " · " + expected); } void applySetting(String field, String value) throws Exception { combo("declaration-property", field); fx( () -> { ((ComboBox) pane.lookup("#declaration-value")).getEditor().setText(value); return null; }); edit("Apply setting", field + "=\"" + value + "\""); } }