Render WebView content and qualify Archiver document previews in IntelliJ
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
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(
|
||||
() -> {
|
||||
((Button) root.lookup("#next")).fire();
|
||||
return null;
|
||||
});
|
||||
body("Lieferschein LS-018");
|
||||
require(
|
||||
fx(() -> suggestions(dynamic).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);
|
||||
});
|
||||
}
|
||||
|
||||
private List<?> suggestions(TextField field) throws Exception {
|
||||
return (List<?>) field.getClass().getMethod("getSuggestions").invoke(field);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user