Edit and exercise Archiver CommandBindings in the IntelliJ designer
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package qualification;
|
||||
|
||||
import java.nio.file.*;
|
||||
import java.util.concurrent.*;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.stage.Window;
|
||||
|
||||
/**
|
||||
* Test-only agent. Exercise the actual scene when the plugin opens its interactive target window.
|
||||
*/
|
||||
public final class ArchiveTargetProbe {
|
||||
public static void premain(String directory) {
|
||||
if (System.getProperty("sun.java.command", "")
|
||||
.startsWith("de.neitzel.fxdesigner.target.WorkerMain ")) {
|
||||
Thread.ofPlatform()
|
||||
.daemon()
|
||||
.name("archiver-preview-probe")
|
||||
.start(() -> Probe.observe(Path.of(directory)));
|
||||
}
|
||||
}
|
||||
|
||||
private static class Probe {
|
||||
static void observe(Path directory) {
|
||||
try {
|
||||
while (true) {
|
||||
Thread.sleep(150);
|
||||
var check = new FutureTask<>(() -> inspect(directory));
|
||||
try {
|
||||
Platform.runLater(check);
|
||||
} catch (IllegalStateException toolkitStarting) {
|
||||
continue;
|
||||
}
|
||||
if (check.get(10, TimeUnit.SECONDS)) return;
|
||||
}
|
||||
} catch (Throwable error) {
|
||||
try {
|
||||
Files.writeString(directory.resolve("target-error.txt"), error.toString());
|
||||
} catch (java.io.IOException writeError) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static boolean inspect(Path directory) throws Exception {
|
||||
var stage =
|
||||
Window.getWindows().stream()
|
||||
.filter(Window::isShowing)
|
||||
.filter(w -> w.getScene().lookup("#trash-button") instanceof Button)
|
||||
.findFirst();
|
||||
if (stage.isEmpty()) return false;
|
||||
var root = stage.get().getScene().getRoot();
|
||||
var status = (Label) root.lookup("#status");
|
||||
var trash = (Button) root.lookup("#trash-button");
|
||||
var menu = ((MenuBar) root.lookup(".menu-bar")).getMenus().getFirst();
|
||||
var menuCommands =
|
||||
((MenuBar) root.lookup(".menu-bar"))
|
||||
.getMenus().stream()
|
||||
.flatMap(group -> group.getItems().stream())
|
||||
.filter(item -> !(item instanceof Menu))
|
||||
.filter(item -> item.getOnAction() != null)
|
||||
.count();
|
||||
if (menuCommands != 21 || trash.getOnAction() == null)
|
||||
throw new AssertionError(
|
||||
"Expected 21 menu commands and the trash button, got " + menuCommands);
|
||||
var open =
|
||||
menu.getItems().stream()
|
||||
.filter(m -> "Archiv öffnen …".equals(m.getText()))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
if (!trash.isDisabled() || !status.getText().equals("Vorschau · Bereit"))
|
||||
throw new AssertionError("Expected a fresh, closed example");
|
||||
trash.fire();
|
||||
if (!status.getText().equals("Vorschau · Bereit"))
|
||||
throw new AssertionError("Disabled command ran");
|
||||
if (((TreeView<?>) root.lookup("#folders")).getRoot() == null
|
||||
|| root.lookup("#dockPane").lookupAll(".label").stream()
|
||||
.noneMatch(
|
||||
n -> n instanceof Label l && l.getText().equals("Dokument im Beispielarchiv")))
|
||||
return false;
|
||||
open.fire();
|
||||
String afterOpen = status.getText();
|
||||
if (trash.isDisabled() || !afterOpen.endsWith(" · 1")) throw new AssertionError(afterOpen);
|
||||
trash.fire();
|
||||
String afterTrash = status.getText();
|
||||
if (!afterTrash.equals("Vorschau · Beispielpapierkorb geöffnet · 2"))
|
||||
throw new AssertionError(afterTrash);
|
||||
String run = Files.readString(directory.resolve("attempt")).trim();
|
||||
var image = root.snapshot(null, null);
|
||||
int width = (int) image.getWidth(), height = (int) image.getHeight();
|
||||
int[] argb = new int[width * height];
|
||||
image
|
||||
.getPixelReader()
|
||||
.getPixels(
|
||||
0,
|
||||
0,
|
||||
width,
|
||||
height,
|
||||
javafx.scene.image.PixelFormat.getIntArgbInstance(),
|
||||
argb,
|
||||
0,
|
||||
width);
|
||||
var screenshot =
|
||||
new java.awt.image.BufferedImage(
|
||||
width, height, java.awt.image.BufferedImage.TYPE_INT_ARGB);
|
||||
screenshot.setRGB(0, 0, width, height, argb, 0, width);
|
||||
javax.imageio.ImageIO.write(
|
||||
screenshot, "png", directory.resolve("preview-" + run + ".png").toFile());
|
||||
Files.writeString(
|
||||
directory.resolve("result-" + run),
|
||||
afterOpen + "\n" + afterTrash + "\n" + ProcessHandle.current().pid());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user