116 lines
4.4 KiB
Java
116 lines
4.4 KiB
Java
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;
|
|
}
|
|
}
|
|
}
|