package qualification; import static qualification.WorkflowProbe.*; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.fileEditor.*; import com.intellij.openapi.project.Project; import com.intellij.openapi.startup.StartupActivity; import com.intellij.openapi.vfs.*; import de.neitzel.fxdesigner.kit.DesignerPane; import java.nio.file.*; import java.util.*; import javafx.scene.control.*; import javafx.scene.image.*; /** Shared actions on the real form inspector in the registered editor. */ abstract class FormWorkflowProbe implements StartupActivity.DumbAware { DesignerPane pane; VirtualFile file; public void runActivity(Project project) { if (!started.compareAndSet(false, true)) return; ApplicationManager.getApplication() .executeOnPooledThread( () -> { try { verify(project); } catch (Throwable ex) { try { var trace = new java.io.StringWriter(); ex.printStackTrace(new java.io.PrintWriter(trace)); if (pane != null) trace.append( fx( () -> pane.lookupAll(".label").stream() .filter(Label.class::isInstance) .map(n -> Objects.toString(((Label) n).getText(), "")) .filter(s -> !s.isEmpty()) .toList() .toString())); Files.writeString(output.resolve("FAILURE.txt"), trace.toString()); } catch (Exception failure) { failure.printStackTrace(); } } }); } void step(String message) throws Exception { Files.writeString( output.resolve("steps.txt"), message + "\n", StandardOpenOption.CREATE, StandardOpenOption.APPEND); } String source() throws Exception { return edt(() -> FileDocumentManager.getInstance().getDocument(file).getText()); } void controls(boolean expanded) throws Exception { fx( () -> { var panel = (TitledPane) pane.lookupAll(".titled-pane").stream() .filter(n -> n instanceof TitledPane t && t.getText().equals("Form controls")) .findFirst() .orElseThrow(); panel.setAnimated(false); panel.setExpanded(expanded); pane.applyCss(); pane.layout(); return null; }); } void choose(String selector, String text) throws Exception { fx( () -> { var combo = (ComboBox) pane.lookup("#" + selector); for (int i = 0; i < combo.getItems().size(); i++) { if (combo.getItems().get(i).toString().equals(text)) { combo.getSelectionModel().select(i); return null; } } for (int i = 0; i < combo.getItems().size(); i++) { String label = combo.getItems().get(i).toString(); if (label.equals(text) || label.contains(text)) { combo.getSelectionModel().select(i); return null; } } throw new AssertionError(selector + " missing " + text + " in " + combo.getItems()); }); } void edited(String before, String expected) throws Exception { await( () -> { String now = source(); String error = fx( () -> { var label = (Label) pane.lookup("#form-message"); return label == null ? "" : label.getText(); }); if (!error.isEmpty()) throw new AssertionError(error); return !before.equals(now) && now.contains(expected) && fx( () -> ((TextArea) pane.lookup("#fxml-source")) == null || ((TextArea) pane.lookup("#fxml-source")).getText().equals(now)); }); // Both the native document and visual selection are refreshed asynchronously. fx(pane::flush).toCompletableFuture().get(10, java.util.concurrent.TimeUnit.SECONDS); } void set(String name, String value, String expected) throws Exception { choose("form-property", name); fx( () -> { ((TextArea) pane.lookup("#form-value")).setText(value); return null; }); String before = source(); click(pane, "Set property"); edited(before, expected); } void awaitTarget(String name) throws Exception { await( () -> { var error = output.resolve("target-error.txt"); if (Files.exists(error)) throw new AssertionError(Files.readString(error)); return Files.exists(output.resolve(name)); }); } void commands(String control, String path, int count) throws Exception { fx( () -> { var group = (TitledPane) pane.lookupAll(".titled-pane").stream() .filter(n -> n instanceof TitledPane t && "Bindings".equals(t.getText())) .findFirst() .orElseThrow(); group.setAnimated(false); group.setExpanded(true); var tabs = (TabPane) group.getContent(); tabs.getSelectionModel() .select( tabs.getTabs().stream() .filter(t -> t.getText().equals("Command bindings")) .findFirst() .orElseThrow()); pane.applyCss(); pane.layout(); var list = (ListView) pane.lookup("#command-bindings"); if (list.getItems().size() != count) throw new AssertionError(list.getItems()); for (int i = 0; i < count; i++) { if (list.getItems().get(i).toString().startsWith(control + " ←")) list.getSelectionModel().select(i); } if (!control.equals(((TextField) pane.lookup("#command-control")).getText()) || !path.equals(((TextField) pane.lookup("#command-path")).getText())) throw new AssertionError(control + " → " + path); return null; }); } abstract void verify(Project project) throws Exception; }