332 lines
13 KiB
Java
332 lines
13 KiB
Java
package qualification;
|
||
|
||
import java.nio.file.*;
|
||
import java.util.*;
|
||
import java.util.concurrent.*;
|
||
import javafx.application.Platform;
|
||
import javafx.geometry.Side;
|
||
import javafx.scene.Parent;
|
||
import javafx.scene.control.*;
|
||
import javafx.stage.*;
|
||
|
||
/** Operates the document tabs' ordinary context menus in the interactive Archiver worker. */
|
||
public final class DockRulesTargetProbe {
|
||
public static void premain(String directory) {
|
||
if (System.getProperty("sun.java.command", "")
|
||
.startsWith("de.neitzel.fxdesigner.target.WorkerMain "))
|
||
Thread.ofPlatform().daemon().start(() -> Worker.run(Path.of(directory)));
|
||
}
|
||
|
||
private static final class Worker {
|
||
private static final String INVOICE = "Rechnung.html", DELIVERY = "Lieferschein.html";
|
||
|
||
static <T> T fx(Callable<T> action) throws Exception {
|
||
var task = new FutureTask<>(action);
|
||
Platform.runLater(task);
|
||
return task.get(10, TimeUnit.SECONDS);
|
||
}
|
||
|
||
static void run(Path dir) {
|
||
try {
|
||
Stage stage = null;
|
||
while (stage == null) {
|
||
Thread.sleep(100);
|
||
try {
|
||
stage =
|
||
fx(
|
||
() ->
|
||
Window.getWindows().stream()
|
||
.filter(Stage.class::isInstance)
|
||
.map(Stage.class::cast)
|
||
.filter(
|
||
s ->
|
||
s.isShowing()
|
||
&& s.getOpacity() > 0
|
||
&& Objects.toString(s.getTitle(), "")
|
||
.startsWith("Neitzel preview"))
|
||
.findFirst()
|
||
.orElse(null));
|
||
} catch (IllegalStateException starting) {
|
||
/* Await JavaFX startup. */
|
||
}
|
||
}
|
||
Stage preview = stage;
|
||
String attempt = Files.readString(dir.resolve("layout-case"));
|
||
Parent root = fx(() -> preview.getScene().getRoot());
|
||
String result;
|
||
if (attempt.equals("defaults")) {
|
||
result = fx(() -> defaults(root));
|
||
} else {
|
||
boolean horizontal = attempt.equals("horizontal");
|
||
require(horizontal || attempt.equals("vertical"), attempt);
|
||
resize(preview, 640, 440);
|
||
String small = fx(() -> checkRules(root, horizontal, false));
|
||
resize(preview, 1340, 760);
|
||
String large = fx(() -> checkRules(root, horizontal, true));
|
||
await(() -> tab(root, INVOICE).getTabPane() != tab(root, DELIVERY).getTabPane());
|
||
String split = fx(() -> inspectSplit(root, horizontal, dir));
|
||
fx(
|
||
() -> {
|
||
move(root, DELIVERY, "details");
|
||
return null;
|
||
});
|
||
await(() -> tab(root, INVOICE).getTabPane().getTabs().size() == 1);
|
||
fx(
|
||
() -> {
|
||
var invoice = tab(root, INVOICE);
|
||
var menu = menu(invoice);
|
||
item(menu.getItems(), "Close", "Schließen").fire();
|
||
return null;
|
||
});
|
||
await(() -> tabs(root).stream().noneMatch(t -> t.getText().equals(INVOICE)));
|
||
String empty = fx(() -> verifyEmpty(root, !horizontal, dir));
|
||
result = small + "\n" + large + "\n" + split + "\n" + empty;
|
||
}
|
||
Files.writeString(dir.resolve("result-" + attempt), result);
|
||
} catch (Throwable failure) {
|
||
var trace = new java.io.StringWriter();
|
||
failure.printStackTrace(new java.io.PrintWriter(trace));
|
||
try {
|
||
Files.writeString(dir.resolve("target-error.txt"), trace.toString());
|
||
} catch (Exception ex) {
|
||
ex.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
static void await(Callable<Boolean> test) throws Exception {
|
||
long end = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
|
||
while (System.nanoTime() < end) {
|
||
if (fx(test)) return;
|
||
Thread.sleep(50);
|
||
}
|
||
throw new AssertionError("Preview state did not settle");
|
||
}
|
||
|
||
static void resize(Stage stage, int width, int height) throws Exception {
|
||
fx(
|
||
() -> {
|
||
stage.setWidth(width);
|
||
stage.setHeight(height);
|
||
return null;
|
||
});
|
||
await(
|
||
() ->
|
||
Math.abs(stage.getScene().getWidth() - width) < 12
|
||
&& stage.getScene().getHeight() > height - 60
|
||
&& stage.getScene().getHeight() <= height);
|
||
fx(
|
||
() -> {
|
||
layout(stage.getScene().getRoot());
|
||
return null;
|
||
});
|
||
}
|
||
|
||
static void layout(Parent root) {
|
||
root.applyCss();
|
||
root.layout();
|
||
}
|
||
|
||
static void require(boolean ok, String reason) {
|
||
if (!ok) throw new AssertionError(reason);
|
||
}
|
||
|
||
static List<Tab> tabs(Parent root) {
|
||
return root.lookupAll(".dock-group").stream()
|
||
.map(TabPane.class::cast)
|
||
.flatMap(p -> p.getTabs().stream())
|
||
.toList();
|
||
}
|
||
|
||
static Tab tab(Parent root, String name) {
|
||
return tabs(root).stream().filter(t -> name.equals(t.getText())).findFirst().orElseThrow();
|
||
}
|
||
|
||
static ContextMenu menu(Tab tab) {
|
||
var m = tab.getContextMenu();
|
||
m.show(tab.getTabPane(), Side.TOP, 0, 0);
|
||
return m;
|
||
}
|
||
|
||
static MenuItem item(List<MenuItem> items, String... labels) {
|
||
return items.stream()
|
||
.filter(
|
||
i ->
|
||
Arrays.stream(labels)
|
||
.anyMatch(
|
||
l ->
|
||
l.equals(i.getText())
|
||
|| i.getText().startsWith(l + " (")
|
||
|| i.getText().startsWith(l + " —")))
|
||
.findFirst()
|
||
.orElseThrow(
|
||
() ->
|
||
new AssertionError(
|
||
"Missing "
|
||
+ Arrays.toString(labels)
|
||
+ " in "
|
||
+ items.stream().map(MenuItem::getText).toList()));
|
||
}
|
||
|
||
static String checkRules(Parent root, boolean horizontal, boolean large) {
|
||
var delivery = tab(root, DELIVERY);
|
||
var group = delivery.getTabPane();
|
||
require(tab(root, INVOICE).getTabPane() == group, "Initial document group");
|
||
require(group.minWidth(-1) == 300 && group.minHeight(-1) == 225, "Effective minima");
|
||
var m = menu(delivery);
|
||
var choices = ((Menu) item(m.getItems(), "Split", "Teilen")).getItems();
|
||
var right = item(choices, "Right", "Rechts");
|
||
var below = item(choices, "Below", "Unten");
|
||
var blocked = horizontal ? below : right;
|
||
var allowed = horizontal ? right : below;
|
||
require(
|
||
blocked.isDisable()
|
||
&& (blocked.getText().contains("direction")
|
||
|| blocked.getText().contains("Richtung")),
|
||
blocked.getText());
|
||
// Exercise the checked operation as well as its disabled presentation.
|
||
blocked.fire();
|
||
require(
|
||
tab(root, INVOICE).getTabPane() == tab(root, DELIVERY).getTabPane(),
|
||
"Forbidden split changed layout");
|
||
String size = Math.round(group.getWidth()) + " × " + Math.round(group.getHeight());
|
||
if (!large) {
|
||
require(
|
||
allowed.isDisable()
|
||
&& (horizontal
|
||
? group.getWidth() < 2 * group.minWidth(-1) + 8
|
||
: group.getHeight() < 2 * group.minHeight(-1) + 8)
|
||
&& !allowed.getText().equals(blocked.getText()),
|
||
size + " · " + allowed.getText());
|
||
allowed.fire();
|
||
require(
|
||
tab(root, INVOICE).getTabPane() == tab(root, DELIVERY).getTabPane(),
|
||
"Small split changed layout");
|
||
} else {
|
||
require(!allowed.isDisable(), size + " · " + allowed.getText());
|
||
allowed.fire();
|
||
}
|
||
m.hide();
|
||
return (large ? "1340 × 760" : "640 × 440")
|
||
+ " · Gruppe "
|
||
+ size
|
||
+ " · "
|
||
+ blocked.getText()
|
||
+ " · "
|
||
+ allowed.getText();
|
||
}
|
||
|
||
static String inspectSplit(Parent root, boolean horizontal, Path dir) throws Exception {
|
||
layout(root);
|
||
var first = tab(root, INVOICE).getTabPane();
|
||
var second = tab(root, DELIVERY).getTabPane();
|
||
for (var group : List.of(first, second)) {
|
||
require(group.getWidth() >= 300 && group.getHeight() >= 225, "Split child below minimum");
|
||
require(
|
||
group.minWidth(-1) == 300 && group.minHeight(-1) == 225,
|
||
"Split did not inherit minima");
|
||
}
|
||
var a = first.localToScene(first.getLayoutBounds());
|
||
var b = second.localToScene(second.getLayoutBounds());
|
||
require(
|
||
horizontal ? b.getMinX() >= a.getMaxX() - 1 : b.getMinY() >= a.getMaxY() - 1,
|
||
"Geometric split direction");
|
||
require(
|
||
first.lookup("#html-document") != null && second.lookup("#html-document") != null,
|
||
"Document viewers");
|
||
image(root, dir.resolve(horizontal ? "dock-horizontal.png" : "dock-vertical.png"));
|
||
return "Rechnung / Lieferschein · "
|
||
+ Math.round(a.getWidth())
|
||
+ " × "
|
||
+ Math.round(a.getHeight())
|
||
+ " und "
|
||
+ Math.round(b.getWidth())
|
||
+ " × "
|
||
+ Math.round(b.getHeight());
|
||
}
|
||
|
||
static void move(Parent root, String document, String destination) {
|
||
var m = menu(tab(root, document));
|
||
var targets = ((Menu) item(m.getItems(), "Move to", "Verschieben nach")).getItems();
|
||
var action = item(targets, destination);
|
||
require(!action.isDisable(), action.getText());
|
||
action.fire();
|
||
m.hide();
|
||
}
|
||
|
||
static String verifyEmpty(Parent root, boolean persistent, Path dir) throws Exception {
|
||
layout(root);
|
||
var m = menu(tab(root, DELIVERY));
|
||
var targets = ((Menu) item(m.getItems(), "Move to", "Verschieben nach")).getItems();
|
||
require(
|
||
targets.stream().anyMatch(i -> i.getText().startsWith("details")),
|
||
"details destination gone");
|
||
var messages = targets.stream().filter(i -> i.getText().equals("messages")).findFirst();
|
||
require(
|
||
messages.isPresent() == persistent,
|
||
"messages target after close: " + targets.stream().map(MenuItem::getText).toList());
|
||
m.hide();
|
||
var empty =
|
||
root.lookupAll(".dock-group").stream()
|
||
.map(TabPane.class::cast)
|
||
.filter(t -> t.getTabs().isEmpty() && t.isVisible() && t.isManaged())
|
||
.toList();
|
||
require(empty.size() == (persistent ? 1 : 0), "Empty groups: " + empty.size());
|
||
image(root, dir.resolve(persistent ? "dock-empty-kept.png" : "dock-empty-removed.png"));
|
||
if (persistent) {
|
||
move(root, DELIVERY, "messages");
|
||
require(tab(root, DELIVERY).getTabPane() == empty.getFirst(), "Return to persistent group");
|
||
}
|
||
return persistent
|
||
? "persistent=true · messages bleibt leer sichtbar und nimmt den Lieferschein wieder auf"
|
||
: "persistent=false · messages nach dem letzten Tab aus Layout und Zielmenü entfernt";
|
||
}
|
||
|
||
static String defaults(Parent root) throws Exception {
|
||
var pane = root.lookup("#dockPane");
|
||
var layout = pane.getClass().getMethod("getInitialLayout").invoke(pane);
|
||
var group = layout.getClass().getMethod("getCenter").invoke(layout);
|
||
var type = group.getClass();
|
||
require(
|
||
Objects.equals(type.getMethod("getMinimumWidth").invoke(group), 80.0)
|
||
&& Objects.equals(type.getMethod("getMinimumHeight").invoke(group), 80.0)
|
||
&& (boolean) type.getMethod("isPersistent").invoke(group)
|
||
&& type.getMethod("getSplitDirections").invoke(group).toString().equals("ALL"),
|
||
"Effective defaults");
|
||
var texts =
|
||
root.lookupAll(".label").stream()
|
||
.filter(Label.class::isInstance)
|
||
.map(Label.class::cast)
|
||
.map(Label::getText)
|
||
.toList();
|
||
require(
|
||
texts.contains("Rechnungen / Musterrechnung.pdf")
|
||
&& texts.contains("Belegnummer RE-2026-042"),
|
||
"Original sample content");
|
||
return "Defaults 80 × 80, true, ALL · Musterrechnung.pdf und Belegnummer RE-2026-042";
|
||
}
|
||
|
||
static void image(Parent root, Path file) throws Exception {
|
||
var image = root.snapshot(null, null);
|
||
var pixels =
|
||
new java.awt.image.BufferedImage(
|
||
(int) image.getWidth(),
|
||
(int) image.getHeight(),
|
||
java.awt.image.BufferedImage.TYPE_INT_ARGB);
|
||
var buffer = ((java.awt.image.DataBufferInt) pixels.getRaster().getDataBuffer()).getData();
|
||
image
|
||
.getPixelReader()
|
||
.getPixels(
|
||
0,
|
||
0,
|
||
pixels.getWidth(),
|
||
pixels.getHeight(),
|
||
javafx.scene.image.PixelFormat.getIntArgbInstance(),
|
||
buffer,
|
||
0,
|
||
pixels.getWidth());
|
||
javax.imageio.ImageIO.write(pixels, "png", file.toFile());
|
||
}
|
||
}
|
||
}
|