216 lines
7.5 KiB
Java
216 lines
7.5 KiB
Java
package qualification;
|
|
|
|
import java.nio.file.*;
|
|
import java.util.*;
|
|
import java.util.concurrent.*;
|
|
import javafx.application.Platform;
|
|
import javafx.scene.Node;
|
|
import javafx.scene.control.*;
|
|
import javafx.scene.image.PixelFormat;
|
|
import javafx.stage.*;
|
|
import javax.imageio.ImageIO;
|
|
import org.w3c.dom.Document;
|
|
|
|
/**
|
|
* Checks the selected product tab, WebKit DOM and actual popup, including a return to the first
|
|
* tab.
|
|
*/
|
|
public final class DocumentTabsTargetProbe {
|
|
public static void premain(String dir) {
|
|
if (System.getProperty("sun.java.command", "")
|
|
.startsWith("de.neitzel.fxdesigner.target.WorkerMain "))
|
|
Thread.ofPlatform().daemon().start(() -> Probe.run(Path.of(dir)));
|
|
}
|
|
|
|
private static final class Probe {
|
|
private static <T> T fx(Callable<T> task) throws Exception {
|
|
var future = new FutureTask<>(task);
|
|
Platform.runLater(future);
|
|
return future.get(10, TimeUnit.SECONDS);
|
|
}
|
|
|
|
private static void await(String description, Callable<Boolean> check) throws Exception {
|
|
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(45);
|
|
while (System.nanoTime() < deadline) {
|
|
if (fx(check)) return;
|
|
Thread.sleep(100);
|
|
}
|
|
throw new AssertionError(description);
|
|
}
|
|
|
|
private static void run(Path dir) {
|
|
try {
|
|
Stage stage = null;
|
|
while (stage == null) {
|
|
Thread.sleep(120);
|
|
try {
|
|
stage =
|
|
fx(
|
|
() ->
|
|
Window.getWindows().stream()
|
|
.filter(Stage.class::isInstance)
|
|
.map(Stage.class::cast)
|
|
.filter(
|
|
s ->
|
|
s.getOpacity() > 0
|
|
&& s.getTitle() != null
|
|
&& s.getTitle().startsWith("Neitzel preview"))
|
|
.findFirst()
|
|
.orElse(null));
|
|
} catch (IllegalStateException starting) {
|
|
/* Waiting for FX startup. */
|
|
}
|
|
}
|
|
final Stage window = stage;
|
|
TabPane group =
|
|
fx(
|
|
() ->
|
|
window.getScene().getRoot().lookupAll(".tab-pane").stream()
|
|
.map(TabPane.class::cast)
|
|
.filter(
|
|
p ->
|
|
p.getTabs().stream()
|
|
.anyMatch(t -> "Rechnung.html".equals(t.getText())))
|
|
.findFirst()
|
|
.orElseThrow());
|
|
check(
|
|
dir,
|
|
group,
|
|
"Rechnung.html",
|
|
"Rechnung RE-042",
|
|
"Büromaterial · 48,00 €",
|
|
"Beleg offen",
|
|
"Beleg geprüft",
|
|
"rechnung-tab");
|
|
check(
|
|
dir,
|
|
group,
|
|
"Lieferschein.html",
|
|
"Lieferschein LS-018",
|
|
"Wareneingang · 3 Archivkartons",
|
|
"Beleg geprüft",
|
|
"Beleg offen",
|
|
"lieferschein-tab");
|
|
check(
|
|
dir,
|
|
group,
|
|
"Rechnung.html",
|
|
"Rechnung RE-042",
|
|
"Büromaterial · 48,00 €",
|
|
"Beleg offen",
|
|
"Beleg geprüft",
|
|
"rechnung-tab");
|
|
Files.writeString(
|
|
dir.resolve("document-tabs-result"),
|
|
"PASS Rechnung → Lieferschein → Rechnung: HTML content, assigned tag and filtered"
|
|
+ " suggestion in each production tab.\n");
|
|
} catch (Throwable error) {
|
|
var trace = new java.io.StringWriter();
|
|
error.printStackTrace(new java.io.PrintWriter(trace));
|
|
try {
|
|
Files.writeString(dir.resolve("target-error.txt"), trace.toString());
|
|
} catch (Exception ignored) {
|
|
error.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void check(
|
|
Path dir,
|
|
TabPane group,
|
|
String name,
|
|
String heading,
|
|
String body,
|
|
String assigned,
|
|
String suggestion,
|
|
String image)
|
|
throws Exception {
|
|
var tab =
|
|
fx(
|
|
() -> {
|
|
var target =
|
|
group.getTabs().stream()
|
|
.filter(t -> name.equals(t.getText()))
|
|
.findFirst()
|
|
.orElseThrow();
|
|
group.getSelectionModel().select(target);
|
|
return target;
|
|
});
|
|
await(
|
|
"HTML in " + name,
|
|
() -> {
|
|
var content = tab.getContent();
|
|
var viewer = content.lookup("#html-document");
|
|
if (viewer == null
|
|
|| group.getSelectionModel().getSelectedItem() != tab
|
|
|| !content.isVisible()) return false;
|
|
var browser = viewer.lookup("WebView");
|
|
Object engine = browser.getClass().getMethod("getEngine").invoke(browser);
|
|
var doc = (Document) engine.getClass().getMethod("getDocument").invoke(engine);
|
|
return doc != null
|
|
&& doc.getElementsByTagName("h1").getLength() == 1
|
|
&& heading.equals(doc.getElementsByTagName("h1").item(0).getTextContent())
|
|
&& doc.getDocumentElement().getTextContent().contains(body);
|
|
});
|
|
var field = fx(() -> (TextField) tab.getContent().lookup("#tag-input"));
|
|
await(
|
|
"Focus in " + name,
|
|
() -> {
|
|
var stage = (Stage) group.getScene().getWindow();
|
|
if (!field.isFocused()) {
|
|
stage.toFront();
|
|
stage.requestFocus();
|
|
field.requestFocus();
|
|
}
|
|
return field.isFocused();
|
|
});
|
|
fx(
|
|
() -> {
|
|
var chips = tab.getContent().lookupAll(".tag-chip");
|
|
if (chips.size() != 1) throw new AssertionError("Expected one assigned tag in " + name);
|
|
var chip = chips.iterator().next();
|
|
if (!assigned.equals(chip.getClass().getMethod("getText").invoke(chip)))
|
|
throw new AssertionError("Wrong assigned tag in " + name);
|
|
field.setText("");
|
|
field.setText("Be");
|
|
return null;
|
|
});
|
|
await(
|
|
"Popup with " + suggestion,
|
|
() -> {
|
|
for (var window : Window.getWindows()) {
|
|
if (window instanceof Popup popup
|
|
&& popup.isShowing()
|
|
&& popup.getOwnerNode() == field
|
|
&& popup.getScene().lookup(".list-view") instanceof ListView<?> list
|
|
&& list.getItems().equals(List.of(suggestion))) {
|
|
save(list, dir.resolve(image + "-tags.png"));
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
});
|
|
fx(
|
|
() -> {
|
|
save(tab.getContent(), dir.resolve(image + ".png"));
|
|
field.setText("");
|
|
return null;
|
|
});
|
|
}
|
|
|
|
private static void save(Node node, Path file) throws Exception {
|
|
var image = node.snapshot(null, null);
|
|
int width = (int) image.getWidth(), height = (int) image.getHeight();
|
|
var pixels = new int[width * height];
|
|
image
|
|
.getPixelReader()
|
|
.getPixels(0, 0, width, height, PixelFormat.getIntArgbInstance(), pixels, 0, width);
|
|
var png =
|
|
new java.awt.image.BufferedImage(
|
|
width, height, java.awt.image.BufferedImage.TYPE_INT_ARGB);
|
|
png.setRGB(0, 0, width, height, pixels, 0, width);
|
|
ImageIO.write(png, "png", file.toFile());
|
|
}
|
|
}
|
|
}
|