257 lines
9.9 KiB
Java
257 lines
9.9 KiB
Java
package qualification;
|
|
|
|
import java.nio.file.*;
|
|
import java.util.*;
|
|
import java.util.concurrent.*;
|
|
import javafx.application.Platform;
|
|
import javafx.scene.Parent;
|
|
import javafx.scene.control.*;
|
|
import javafx.scene.input.*;
|
|
import javafx.stage.*;
|
|
import org.w3c.dom.Document;
|
|
|
|
/** Observes the target JVM launched by the IDE. No application or WebKit classes enter the IDE. */
|
|
public final class SavedDocumentTargetProbe {
|
|
public static void premain(String directory) {
|
|
if (System.getProperty("sun.java.command", "")
|
|
.startsWith("de.neitzel.fxdesigner.target.WorkerMain "))
|
|
Thread.ofPlatform().daemon().start(() -> new Probe().run(Path.of(directory)));
|
|
}
|
|
|
|
private static final class Probe {
|
|
private Path evidence;
|
|
private Parent root;
|
|
private Object viewer, engine;
|
|
private TextField fixed, dynamic;
|
|
|
|
private <T> T fx(Callable<T> action) throws Exception {
|
|
var task = new FutureTask<>(action);
|
|
Platform.runLater(task);
|
|
return task.get(10, TimeUnit.SECONDS);
|
|
}
|
|
|
|
private void await(Callable<Boolean> check) throws Exception {
|
|
long end = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);
|
|
while (System.nanoTime() < end) {
|
|
if (fx(check)) return;
|
|
Thread.sleep(80);
|
|
}
|
|
throw new AssertionError("Timed out waiting for " + check);
|
|
}
|
|
|
|
private void run(Path directory) {
|
|
evidence = directory;
|
|
try {
|
|
// The same agent is present in discovery and off-screen render workers.
|
|
while (true) {
|
|
try {
|
|
if (fx(
|
|
() -> {
|
|
var stage =
|
|
Window.getWindows().stream()
|
|
.filter(Stage.class::isInstance)
|
|
.map(Stage.class::cast)
|
|
.filter(
|
|
w ->
|
|
w.isShowing()
|
|
&& w.getOpacity() > 0
|
|
&& w.getTitle() != null
|
|
&& w.getTitle().startsWith("Neitzel preview"))
|
|
.findFirst();
|
|
if (stage.isEmpty()) return false;
|
|
stage.get().setWidth(800);
|
|
stage.get().setHeight(580);
|
|
stage.get().requestFocus();
|
|
root = stage.get().getScene().getRoot();
|
|
viewer = root.lookup("#article");
|
|
return viewer != null;
|
|
})) break;
|
|
} catch (IllegalStateException starting) {
|
|
}
|
|
Thread.sleep(120);
|
|
}
|
|
fx(
|
|
() -> {
|
|
var browser = root.lookup("WebView");
|
|
engine = browser.getClass().getMethod("getEngine").invoke(browser);
|
|
fixed = (TextField) root.lookup("#fixed");
|
|
dynamic = (TextField) root.lookup("#dynamic");
|
|
var media = (Button) root.lookup(".html-viewer-media-load");
|
|
require(
|
|
media.getText().equals("Belegbilder laden")
|
|
&& media.getTooltip().getText().equals("Keine externen Belegbilder")
|
|
&& media.isDisabled(),
|
|
"edited offline media labels");
|
|
require(
|
|
"Belegbilder erneut laden".equals(call("getMediaReloadText"))
|
|
&& "Bilder dieses Belegs laden".equals(call("getMediaTooltipText")),
|
|
"remaining media labels");
|
|
require(!Boolean.TRUE.equals(call("isExternalImagesAllowed")), "offline viewer");
|
|
return null;
|
|
});
|
|
body("Rechnung RE-042");
|
|
popup(fixed, "Pr", List.of("Prüfung", "Privat"));
|
|
popup(dynamic, "Re", List.of("Rechnung", "Reise"));
|
|
fx(
|
|
() -> {
|
|
var tags = tagSource();
|
|
dynamic.setText("R");
|
|
tags.add("Register");
|
|
var choices =
|
|
Window.getWindows().stream()
|
|
.filter(w -> w instanceof Popup && w.isShowing())
|
|
.map(w -> (ListView<?>) w.getScene().lookup(".list-view"))
|
|
.filter(Objects::nonNull)
|
|
.findFirst()
|
|
.orElseThrow();
|
|
require(
|
|
choices.getItems().equals(List.of("Rechnung", "Reise", "Register")),
|
|
"addition in open popup");
|
|
tags.remove("Reise");
|
|
require(
|
|
choices.getItems().equals(List.of("Rechnung", "Register")),
|
|
"removal in open popup");
|
|
dynamic.fireEvent(
|
|
new KeyEvent(
|
|
KeyEvent.KEY_PRESSED, "", "", KeyCode.DOWN, false, false, false, false));
|
|
dynamic.fireEvent(
|
|
new KeyEvent(
|
|
KeyEvent.KEY_PRESSED, "", "", KeyCode.ENTER, false, false, false, false));
|
|
require(
|
|
dynamic.getText().equals("Rechnung")
|
|
&& tags.equals(List.of("Rechnung", "Register")),
|
|
"pick leaves catalogue intact");
|
|
dynamic.setText("Re");
|
|
((Button) root.lookup("#next")).fire();
|
|
require(
|
|
Window.getWindows().stream().noneMatch(w -> w instanceof Popup && w.isShowing()),
|
|
"old matches dismissed on replacement");
|
|
return null;
|
|
});
|
|
body("Lieferschein LS-018");
|
|
require(
|
|
fx(() -> tagSource().equals(List.of("Lieferung", "Lieferschein"))),
|
|
"replacement removes old document tags");
|
|
popup(dynamic, "Li", List.of("Lieferung", "Lieferschein"));
|
|
popup(fixed, "Pr", List.of("Prüfung", "Privat"));
|
|
fx(
|
|
() -> {
|
|
((TextArea) root.lookup("#source"))
|
|
.setText("<html><body><h1>Geänderter Beleg</h1></body></html>");
|
|
return null;
|
|
});
|
|
body("Geänderter Beleg");
|
|
fx(
|
|
() -> {
|
|
viewer
|
|
.getClass()
|
|
.getMethod("load", Path.class)
|
|
.invoke(viewer, directory.resolve("missing-preview.html"));
|
|
return null;
|
|
});
|
|
await(() -> !Boolean.TRUE.equals(call("isLoading")) && call("getError") != null);
|
|
fx(
|
|
() -> {
|
|
((Button) root.lookup("#next")).fire();
|
|
return null;
|
|
});
|
|
body("Rechnung RE-042");
|
|
Files.writeString(
|
|
directory.resolve("saved-document-result"),
|
|
"Rechnung RE-042 → Lieferschein LS-018 → Geänderter Beleg; load error and recovery;"
|
|
+ " popup Pr: Prüfung, Privat; Re: Rechnung, Reise; Li: Lieferung, Lieferschein."
|
|
+ " PID "
|
|
+ ProcessHandle.current().pid());
|
|
} catch (Throwable ex) {
|
|
try {
|
|
var trace = new java.io.StringWriter();
|
|
ex.printStackTrace(new java.io.PrintWriter(trace));
|
|
Files.writeString(directory.resolve("target-error.txt"), trace.toString());
|
|
} catch (Exception ignored) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
private Object call(String method) throws Exception {
|
|
return viewer.getClass().getMethod(method).invoke(viewer);
|
|
}
|
|
|
|
private void body(String text) throws Exception {
|
|
await(
|
|
() -> {
|
|
if (call("getError") != null) throw new AssertionError(call("getError"));
|
|
var doc = (Document) engine.getClass().getMethod("getDocument").invoke(engine);
|
|
return !Boolean.TRUE.equals(call("isLoading"))
|
|
&& doc != null
|
|
&& doc.getDocumentElement().getTextContent().contains(text);
|
|
});
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
private List<String> tagSource() throws Exception {
|
|
return (List<String>) dynamic.getClass().getMethod("getSuggestionsSource").invoke(dynamic);
|
|
}
|
|
|
|
private void popup(TextField field, String prefix, List<String> expected) throws Exception {
|
|
await(
|
|
() -> {
|
|
if (field.isFocused()) return true;
|
|
var stage = (Stage) root.getScene().getWindow();
|
|
stage.toFront();
|
|
stage.requestFocus();
|
|
field.requestFocus();
|
|
return field.isFocused();
|
|
});
|
|
fx(
|
|
() -> {
|
|
field.setText("");
|
|
field.setText(prefix);
|
|
return null;
|
|
});
|
|
await(
|
|
() -> {
|
|
for (var window : Window.getWindows()) {
|
|
if (!(window instanceof Popup)
|
|
|| !window.isShowing()
|
|
|| !(window.getScene().lookup(".list-view") instanceof ListView<?> list)
|
|
|| !list.getItems().equals(expected)) continue;
|
|
var image = window.getScene().snapshot(null);
|
|
int w = (int) image.getWidth(), h = (int) image.getHeight();
|
|
var png =
|
|
new java.awt.image.BufferedImage(
|
|
w, h, java.awt.image.BufferedImage.TYPE_INT_ARGB);
|
|
var pixels = new int[w * h];
|
|
image
|
|
.getPixelReader()
|
|
.getPixels(
|
|
0,
|
|
0,
|
|
w,
|
|
h,
|
|
javafx.scene.image.PixelFormat.getIntArgbInstance(),
|
|
pixels,
|
|
0,
|
|
w);
|
|
png.setRGB(0, 0, w, h, pixels, 0, w);
|
|
javax.imageio.ImageIO.write(
|
|
png, "png", evidence.resolve("tags-" + prefix + ".png").toFile());
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
fx(
|
|
() -> {
|
|
field.fireEvent(
|
|
new KeyEvent(
|
|
KeyEvent.KEY_PRESSED, "", "", KeyCode.ESCAPE, false, false, false, false));
|
|
return null;
|
|
});
|
|
}
|
|
|
|
private static void require(boolean condition, String label) {
|
|
if (!condition) throw new AssertionError(label);
|
|
}
|
|
}
|
|
}
|