Files
NeitzelFXDesigner/integration-tests/idea/ReaderStatesTargetProbe.java
T

279 lines
12 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package qualification;
import static qualification.DocumentTabsTargetProbe.Probe.*;
import static qualification.FolderActionsTargetProbe.Worker.*;
import java.nio.file.*;
import java.util.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
public final class ReaderStatesTargetProbe {
public static void premain(String dir) {
if (System.getProperty("sun.java.command", "")
.startsWith("de.neitzel.fxdesigner.target.WorkerMain "))
Thread.ofPlatform().daemon().start(() -> Check.run(Path.of(dir)));
}
private static final class Check {
static void run(Path dir) {
try {
var stage = previewStage();
var root =
fx(
() -> {
stage.setWidth(1100);
stage.setHeight(750);
var content = stage.getScene().getRoot();
content.applyCss();
content.layout();
return content;
});
var scenario = Files.readString(dir.resolve("layout-case"));
var result =
switch (scenario) {
case "tag" -> fx(() -> tag(root, dir));
case "attachment-save" -> fx(() -> attachment(root, dir));
case "archive-error" -> archive(root, dir);
default -> throw new AssertionError(scenario);
};
Files.writeString(dir.resolve("result-" + scenario), 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 logging) {
logging.printStackTrace();
}
}
}
private static Button button(Node root, String id) {
return (Button) Objects.requireNonNull(root.lookup("#" + id), id);
}
private static Label label(Node root, String id) {
return (Label) Objects.requireNonNull(root.lookup("#" + id), id);
}
private static String tag(Parent root, Path dir) throws Exception {
var chip = (Region) root.lookup(".tag-chip");
var remove = (Button) chip.lookup(".tag-chip-remove");
var readonly = (CheckBox) root.lookup("#example-tag-readonly");
var busy = (CheckBox) root.lookup("#example-tag-busy");
var status = label(root, "example-tag-status");
require(
chip.getClass().getMethod("getText").invoke(chip).equals("Beleg offen"), "Model text");
require(
chip.getBackground().getFills().stream()
.anyMatch(fill -> Color.web("#f2d58a").equals(fill.getFill())),
"Reopened style");
require(remove.isVisible() && !remove.isDisabled(), "Initial removal enabled");
image(root, dir.resolve("tag-editable.png"));
readonly.fire();
require(!remove.isVisible() && !remove.isManaged() && !chip.isDisabled(), "Read-only");
remove.fire();
require(status.getText().equals("Beleg offen · zugewiesen"), "Read-only action rejected");
image(root, dir.resolve("tag-readonly.png"));
readonly.fire();
busy.fire();
require(chip.isDisabled() && remove.isVisible() && remove.isDisabled(), "Busy");
remove.fire();
require(status.getText().equals("Beleg offen · zugewiesen"), "Busy action rejected");
image(root, dir.resolve("tag-busy.png"));
busy.fire();
remove.fire();
require(
status.getText().equals("Beleg offen · entfernt") && !chip.getParent().isVisible(),
"Removed");
return "DocumentTag · Reopened style and bound text; editable removed the assigned tag."
+ " Read-only and busy retained it.";
}
private static String attachment(Parent root, Path dir) throws Exception {
var pick = (ComboBox<?>) root.lookup("#example-attachments");
var open = button(root, "open-attachment");
var save = button(root, "save-attachment");
var result = label(root, "attachment-result");
require(
open.isDisabled() && save.isDisabled() && result.getUserData() == null, "No selection");
pick.getSelectionModel().selectFirst();
require(!open.isDisabled() && !save.isDisabled(), "Text attachment");
save.fire();
require(
result.getText().equals("Gespeichert · Lieferschein.txt · LS-018 · Drei Archivkartons"),
"Supported attachment can still be saved");
var previous = result.getUserData();
pick.getSelectionModel().select(1);
require(open.isDisabled() && !save.isDisabled(), "Unknown attachment can only be saved");
open.fire();
require(Objects.equals(result.getUserData(), previous), "Open cannot run");
save.fire();
require(result.getUserData() instanceof Path, result.getText());
var saved = (Path) result.getUserData();
require(
saved.getFileName().toString().equals("Messdaten.bin")
&& Arrays.equals(Files.readAllBytes(saved), new byte[] {16, 0, 127, -1}),
"Byte-for-byte export");
image(root, dir.resolve("unknown-attachment.png"));
pick.getSelectionModel().selectFirst();
require(!open.isDisabled() && !save.isDisabled(), "Back to text attachment");
open.fire();
var opened = (Path) result.getUserData();
require(
opened.getFileName().toString().equals("Lieferschein.txt")
&& Files.readString(opened).equals("LS-018 · Drei Archivkartons")
&& result.getText().startsWith("Geöffnet"),
"Correct text attachment opened: " + result.getText());
return "AttachmentToolbar · No selection −/−, text +/+, binary −/+ (Open/Save). Saved"
+ " Messdaten.bin with bytes 10 00 7F FF, then opened Lieferschein.txt (LS-018 ·"
+ " Drei Archivkartons).";
}
private static Object read(Node node, String method) throws Exception {
return node.getClass().getMethod(method).invoke(node);
}
private static boolean failureLabel(Node viewer, Throwable cause) {
return viewer.lookupAll(".label").stream()
.anyMatch(
n ->
n instanceof Label l
&& l.isVisible()
&& Objects.equals(l.getText(), cause.getMessage()));
}
private static void fail(Node invoice, Node viewer, Path dir, boolean first) throws Exception {
fx(
() -> {
button(invoice, "example-html-fail").fire();
require(
(boolean) read(viewer, "isLoading")
&& read(viewer, "getError") == null
&& label(invoice, "example-html-state").getText().equals("HTML wird geladen …")
&& viewer.lookupAll(".progress-indicator").stream().anyMatch(Node::isVisible),
"Loading state visible before completion");
if (first) image(invoice, dir.resolve("html-load-started.png"));
return null;
});
await(
"Viewer error property and visible error",
() -> {
var error = read(viewer, "getError");
return !(Boolean) read(viewer, "isLoading")
&& error instanceof Throwable cause
&& cause.getMessage().contains("32 MiB")
&& failureLabel(viewer, cause)
&& label(invoice, "example-html-state").getText().equals("Laden fehlgeschlagen");
});
fx(
() -> {
require(
button(invoice, "external-media-load").isDisabled(),
"Failed viewer cannot load media");
if (first) image(invoice, dir.resolve("html-load-error.png"));
return null;
});
}
private static String archive(Parent root, Path dir) throws Exception {
var group =
fx(
() ->
root.lookupAll(".tab-pane").stream()
.filter(
n ->
n instanceof TabPane t
&& t.getTabs().stream()
.anyMatch(tab -> tab.getText().equals("Rechnung.html")))
.map(TabPane.class::cast)
.findFirst()
.orElseThrow());
var invoice = select(group, "Rechnung.html");
checkHtml(invoice, "Rechnung RE-042", "Büromaterial · 48,00 €");
fx(
() -> {
requireChips(invoice, "Beleg offen");
((TextField) invoice.lookup("#tag-input")).setText("Beleg geprüft");
button(invoice, "add").fire();
requireChips(invoice, "Beleg offen", "Beleg geprüft");
return null;
});
var other = select(group, "Lieferschein.html");
checkHtml(other, "Lieferschein LS-018", "Wareneingang · 3 Archivkartons");
var deliveryViewer = fx(() -> other.lookup("#html-document"));
fx(
() -> {
requireChips(other, "Beleg geprüft");
require(
!(Boolean) read(deliveryViewer, "isExternalImagesAllowed"),
"Delivery initially offline");
button(other, "external-media-load").fire();
return null;
});
await(
"Delivery media requested",
() -> label(root, "status").getText().matches("Lokale Bildanfragen · [1-9][0-9]*"));
select(group, "Rechnung.html");
var viewer = fx(() -> invoice.lookup("#html-document"));
fx(
() -> {
requireChips(invoice, "Beleg offen", "Beleg geprüft");
return null;
});
fail(invoice, viewer, dir, true);
select(group, "Lieferschein.html");
checkHtml(other, "Lieferschein LS-018", "Wareneingang · 3 Archivkartons");
fx(
() -> {
require(
(Boolean) read(deliveryViewer, "isExternalImagesAllowed")
&& read(deliveryViewer, "getError") == null,
"Other tab unaffected by error");
return null;
});
select(group, "Rechnung.html");
fx(
() -> {
button(invoice, "example-html-recover").fire();
require(read(viewer, "getError") == null, "Recovery clears previous error");
return null;
});
checkHtml(invoice, "Rechnung RE-042", "Büromaterial · 48,00 €");
await(
"Valid document ready",
() ->
!(Boolean) read(viewer, "isLoading")
&& read(viewer, "getError") == null
&& label(invoice, "example-html-state").getText().equals("HTML bereit")
&& viewer.lookupAll(".label").stream()
.noneMatch(
n ->
n instanceof Label l
&& l.isVisible()
&& Objects.toString(l.getText(), "").contains("32 MiB")));
fx(
() -> {
image(invoice, dir.resolve("html-load-recovered.png"));
return null;
});
fail(invoice, viewer, dir, false);
select(group, "Lieferschein.html");
fx(
() -> {
require(
(Boolean) read(deliveryViewer, "isExternalImagesAllowed")
&& read(deliveryViewer, "getError") == null,
"Other tab kept permission across second failure");
return null;
});
return "Rechnung → Lieferschein → Rechnung retained two/one/two chips. HTML viewer reported"
+ " loading → 32 MiB error → valid receipt → repeated error. Delivery kept its own"
+ " media permission.";
}
}
}