Files
NeitzelFXDesigner/integration-tests/idea/ArchiveWorkflowProbe.java
T

409 lines
16 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.concurrent.Callable;
import javafx.scene.control.*;
public final class ArchiveWorkflowProbe implements StartupActivity.DumbAware {
private static final String RESOURCE =
"src/main/resources/de/neitzel/archiver/ui/ArchiveWindow.fxml";
private static DesignerPane pane;
public void runActivity(Project project) {
if (!started.compareAndSet(false, true)) return;
ApplicationManager.getApplication()
.executeOnPooledThread(
() -> {
try {
verify(project);
} catch (Throwable error) {
try {
var trace = new java.io.StringWriter();
error.printStackTrace(new java.io.PrintWriter(trace));
if (pane != null)
trace.append(
fx(
() ->
pane.lookupAll(".label").stream()
.filter(Label.class::isInstance)
.map(n -> ((Label) n).getText())
.toList()
.toString()));
Files.writeString(output.resolve("FAILURE.txt"), trace.toString());
} catch (Exception writeError) {
error.printStackTrace();
}
}
});
}
private static void open(Project project, VirtualFile file) throws Exception {
var editor =
edt(
() -> {
var manager = FileEditorManager.getInstance(project);
var all = manager.openFile(file, true);
manager.setSelectedEditor(file, "neitzel-fx-designer");
return java.util.Arrays.stream(all)
.filter(e -> e.getName().equals("Neitzel Designer"))
.findFirst()
.orElseThrow();
});
await(() -> edt(() -> bridge(editor.getComponent()) != null));
var panel = edt(() -> bridge(editor.getComponent()));
await(() -> fx(() -> panel.getScene() != null));
pane = fx(() -> (DesignerPane) panel.getScene().getRoot());
await(() -> fx(() -> pane.kit().editor().getFxomDocument() != null));
}
private static void tab(String name) throws Exception {
fx(
() -> {
var group =
(TitledPane)
pane.lookupAll(".titled-pane").stream()
.filter(n -> n instanceof TitledPane t && "Bindings".equals(t.getText()))
.findFirst()
.orElseThrow();
group.setExpanded(true);
var tabs = (TabPane) group.getContent();
tabs.getSelectionModel()
.select(
tabs.getTabs().stream()
.filter(t -> name.equals(t.getText()))
.findFirst()
.orElseThrow());
pane.applyCss();
pane.layout();
return null;
});
}
private static void select(String id, String prefix) throws Exception {
fx(
() -> {
var list = (ListView<?>) pane.lookup("#" + id);
for (int i = 0; i < list.getItems().size(); i++) {
if (list.getItems().get(i).toString().startsWith(prefix)) {
list.getSelectionModel().select(i);
return null;
}
}
throw new AssertionError("Missing " + prefix + " in " + list.getItems());
});
}
private static void step(String message) throws Exception {
Files.writeString(
output.resolve("steps.txt"),
message + "\n",
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
}
private static void interactive(String run, String expected) throws Exception {
Files.writeString(output.resolve("attempt"), run);
click(pane, "Interactive preview");
var result = output.resolve("result-" + run);
await(
() -> {
if (Files.exists(output.resolve("target-error.txt")))
throw new AssertionError(Files.readString(output.resolve("target-error.txt")));
return Files.exists(result);
});
if (!Files.readString(result).contains(expected + " · 1"))
throw new AssertionError(Files.readString(result));
step("Interactive " + run + ": " + Files.readString(result));
}
private static void verifyCrossViewPaste(Project project, VirtualFile original, Path other)
throws Exception {
var destination = edt(() -> LocalFileSystem.getInstance().refreshAndFindFileByNioFile(other));
String expected = Files.readString(other);
tab("Structure");
select("structure-nodes", "openExample :");
click(pane, "Copy subtree");
open(project, destination);
click(pane, "Preview last successful");
await(
() ->
fx(
() ->
pane.lookupAll(".label").stream()
.anyMatch(
n ->
n instanceof Label label
&& label.getText() != null
&& label
.getText()
.contains(
"Select a preview example after Build and Refresh"))));
tab("Structure");
select("structure-nodes", "recent :");
click(pane, "Paste subtree");
await(
() ->
fx(
() ->
((Label) pane.lookup("#structure-status"))
.getText()
.contains("Unknown Command path: openReadOnly")));
if (!edt(() -> FileDocumentManager.getInstance().getDocument(destination).getText())
.equals(expected))
throw new AssertionError("Incompatible paste changed the destination document");
edt(
() -> {
FileEditorManager.getInstance(project).closeFile(destination);
return null;
});
open(project, original);
step("Cross-View paste rejected at missing Command getter before writing");
}
private static void verify(Project project) throws Exception {
var path = Path.of(project.getBasePath(), RESOURCE);
var other = path.resolveSibling("ArchivePasteDestination.fxml");
String originalSource = Files.readString(path);
Files.writeString(
other,
originalSource.substring(0, originalSource.indexOf("<BorderPane"))
+ "<BorderPane xmlns:fx='http://javafx.com/fxml/1'><top><MenuBar><menus>"
+ "<Menu fx:id='recent'/></menus></MenuBar></top></BorderPane>");
var file = edt(() -> LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path));
open(project, file);
fx(
() -> {
var root =
(javafx.scene.layout.BorderPane)
pane.kit().editor().getFxomDocument().getSceneGraphRoot();
if (!(root.getTop() instanceof MenuBar)
|| !(root.getCenter() instanceof SplitPane)
|| !new javafx.geometry.Insets(5, 12, 9, 12)
.equals(javafx.scene.layout.BorderPane.getMargin(root.getBottom())))
throw new AssertionError("Incomplete Kit layout or incorrect status bar margin");
return null;
});
step("Registered editor opened ArchiveWindow.fxml");
click(pane, "Build and Refresh");
await(
() ->
fx(
() ->
pane.lookupAll(".combo-box").stream()
.anyMatch(
n ->
n instanceof ComboBox<?> c
&& c.getValue() != null
&& c.getValue().toString().contains("Beispielarchiv"))));
step("Build and Refresh discovered Archiver example");
tab("Command bindings");
await(() -> fx(() -> ((ListView<?>) pane.lookup("#command-bindings")).getItems().size() == 22));
interactive("initial", "Beispielarchiv geöffnet");
click(pane, "Stop preview");
select("command-bindings", "open ←");
fx(
() -> {
((TextField) pane.lookup("#command-path")).setText("openReadOnly");
return null;
});
click(pane, "Apply Command binding");
Callable<String> source =
() -> edt(() -> FileDocumentManager.getInstance().getDocument(file).getText());
await(() -> source.call().contains("control=\"open\" path=\"openReadOnly\""));
click(pane, "Save");
await(() -> Files.readString(path).contains("control=\"open\" path=\"openReadOnly\""));
edt(
() -> {
FileEditorManager.getInstance(project).closeFile(file);
return null;
});
open(project, file);
click(pane, "Build and Refresh");
await(
() ->
fx(
() ->
pane.lookupAll(".combo-box").stream()
.anyMatch(
n ->
n instanceof ComboBox<?> c
&& c.getValue() != null
&& c.getValue().toString().contains("Beispielarchiv"))));
interactive("edited", "Beispielarchiv schreibgeschützt geöffnet");
click(pane, "Stop preview");
tab("Structure");
select("structure-nodes", "open :");
fx(
() -> {
((TextField) pane.lookup("#structure-name")).setText("openExample");
return null;
});
click(pane, "Rename identifier");
String renamed = "control=\"openExample\" path=\"openReadOnly\"";
await(() -> source.call().contains(renamed));
String afterRename = source.call();
if (!afterRename.contains(
"fx:id=\"openExample\" text=\"Archiv öffnen …\" accelerator=\"Shortcut+O\""))
throw new AssertionError("Rename lost the menu accelerator");
click(pane, "Undo");
await(() -> !source.call().contains(renamed));
click(pane, "Redo");
await(() -> source.call().contains(renamed));
select("structure-nodes", "openExample :");
click(pane, "Copy subtree");
select("structure-nodes", "recent :");
click(pane, "Paste subtree");
String duplicate = "control=\"openExampleCopy\" path=\"openReadOnly\"";
await(() -> source.call().contains(duplicate));
String afterPaste = source.call();
click(pane, "Undo");
await(() -> source.call().equals(afterRename));
click(pane, "Redo");
await(() -> source.call().equals(afterPaste));
click(pane, "Undo");
await(() -> source.call().equals(afterRename));
select("structure-nodes", "openExample :");
click(pane, "Delete subtree");
await(() -> !source.call().contains("openExample"));
click(pane, "Undo");
await(() -> source.call().contains(renamed));
click(pane, "Save");
await(() -> Files.readString(path).contains(renamed));
edt(
() -> {
FileEditorManager.getInstance(project).closeFile(file);
return null;
});
open(project, file);
click(pane, "Build and Refresh");
await(
() ->
fx(
() ->
pane.lookupAll(".combo-box").stream()
.anyMatch(
n ->
n instanceof ComboBox<?> c
&& c.getValue() != null
&& c.getValue().toString().contains("Beispielarchiv"))));
tab("Command bindings");
select("command-bindings", "openExample ←");
click(pane, "Remove Command binding");
await(() -> !source.call().contains(renamed));
click(pane, "New Command binding");
fx(
() -> {
((TextField) pane.lookup("#command-control")).setText("openExample");
((TextField) pane.lookup("#command-path")).setText("openReadOnly");
return null;
});
click(pane, "Apply Command binding");
await(() -> source.call().contains(renamed));
click(pane, "Undo");
await(() -> !source.call().contains(renamed));
click(pane, "Undo");
await(() -> source.call().equals(afterRename));
select("command-bindings", "openExample ←");
fx(
() -> {
((TextField) pane.lookup("#command-path")).setText("missingCommand");
return null;
});
click(pane, "Apply Command binding");
await(() -> source.call().contains("missingCommand"));
click(pane, "Discover / Validate Commands");
await(
() ->
fx(
() ->
((Label) pane.lookup("#command-status"))
.getText()
.contains("Unknown Command path: missingCommand")));
var collector = com.intellij.analysis.problemsView.ProblemsCollector.getInstance(project);
await(
() ->
edt(
() ->
collector.getFileProblems(file).stream()
.anyMatch(p -> p.getText().contains("missingCommand"))));
edt(
() -> {
var problem =
(com.intellij.analysis.problemsView.FileProblem)
collector.getFileProblems(file).stream()
.filter(p -> p.getText().contains("missingCommand"))
.findFirst()
.orElseThrow();
var doc = FileDocumentManager.getInstance().getDocument(file);
int offset = doc.getLineStartOffset(problem.getLine()) + problem.getColumn();
if (!doc.getText()
.startsWith(
"<CommandBinding control=\"openExample\" path=\"missingCommand\"", offset))
throw new AssertionError("IntelliJ problem points outside the CommandBinding");
new com.intellij.openapi.fileEditor.OpenFileDescriptor(project, file, offset)
.navigate(true);
if (FileEditorManager.getInstance(project)
.getSelectedTextEditor()
.getCaretModel()
.getOffset()
!= offset) throw new AssertionError("Navigation did not reach the CommandBinding");
FileEditorManager.getInstance(project).setSelectedEditor(file, "neitzel-fx-designer");
return null;
});
click(pane, "Undo");
await(() -> source.call().equals(afterRename));
click(pane, "Discover / Validate Commands");
await(
() ->
edt(
() ->
collector.getFileProblems(file).stream()
.noneMatch(p -> p.getText().contains("missingCommand"))));
verifyCrossViewPaste(project, file, other);
tab("Command bindings");
interactive("restored", "Beispielarchiv schreibgeschützt geöffnet");
fx(
() -> {
javax.imageio.ImageIO.write(
javafx.embed.swing.SwingFXUtils.fromFXImage(pane.snapshot(null, null), null),
"png",
output.resolve("intellij.png").toFile());
return null;
});
click(pane, "Stop preview");
// A valid application wildcard exceeds the canvas projection's import support.
String fallback =
afterRename.replace(
"<?import de.neitzel.fx.view.BindingList?>", "<?import de.neitzel.fx.view.*?>");
edt(
() -> {
var document = FileDocumentManager.getInstance().getDocument(file);
com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction(
project, () -> document.setText(fallback));
return null;
});
await(() -> fx(() -> ((TextArea) pane.lookup("#fxml-source")).getText().equals(fallback)));
interactive("unprojected", "Beispielarchiv schreibgeschützt geöffnet");
long pid = Long.parseLong(Files.readAllLines(output.resolve("result-unprojected")).getLast());
edt(
() -> {
FileEditorManager.getInstance(project).closeFile(file);
return null;
});
await(() -> ProcessHandle.of(pid).map(p -> !p.isAlive()).orElse(true));
Files.writeString(
output.resolve("SUCCESS.txt"),
"PASS Archiver A1–A5, 22 Command bindings, edited and reopened FXML, menu and button"
+ " actions, reference edits, diagnostic recovery, worker exit\n");
}
}