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

111 lines
4.3 KiB
Java

package qualification;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
/** Test-only observer of the actual interactive window, inside the target JVM. */
public final class SpringTargetProbe {
private static final String PREVIEW_TITLE = "Neitzel preview — ";
public static void premain(String output) {
var command = System.getProperty("sun.java.command", "").split(" ", 2)[0];
if (command.endsWith(".WorkerMain"))
Thread.ofPlatform().daemon().name("spring-preview-probe").start(() -> Observer.observe(Path.of(output)));
}
private static class Observer {
private static void observe(Path output) {
try {
while (true) {
try {
Platform.runLater(() -> watchOpening(output));
return;
} catch (IllegalStateException toolkitStarting) {
Thread.sleep(10);
}
}
} catch (InterruptedException stopped) {
Thread.currentThread().interrupt();
}
}
private static Optional<Stage> previewWindow() {
return Window.getWindows().stream()
.filter(Stage.class::isInstance)
.map(Stage.class::cast)
.filter(stage -> stage.getOpacity() > 0 && stage.getTitle() != null
&& stage.getTitle().startsWith(PREVIEW_TITLE))
.findFirst();
}
private static void watchOpening(Path output) {
var windows = Window.getWindows();
if (previewWindow().isPresent()) {
reportFailure(output, new AssertionError("Probe attached after the preview opened"));
return;
}
windows.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable ignored) {
previewWindow().ifPresent(stage -> {
windows.removeListener(this);
checkInitialValue(stage, output);
});
}
});
}
private static void checkInitialValue(Stage stage, Path output) {
try {
String attempt = Files.readString(output.resolve("spring-attempt"));
boolean book = attempt.startsWith("book-");
String example = book ? "book" : "card";
if (!stage.getTitle().startsWith(PREVIEW_TITLE + example + " — "))
throw new AssertionError("Unexpected preview window in " + attempt + ": " + stage.getTitle());
if (!(stage.getScene().lookup(book ? "#bookName" : "#entryName") instanceof TextField field))
throw new AssertionError("Missing input field in " + attempt);
// JavaFX adds the window before making its native peer visible.
String initialValue = field.getText();
String expected = book ? "Preview contact book" : "Preview entry";
if (!expected.equals(initialValue))
throw new AssertionError("Expected fresh " + expected + " in " + attempt);
stage.addEventHandler(WindowEvent.WINDOW_SHOWN,
event -> Platform.runLater(() -> checkEditing(stage, field, book, output, attempt, initialValue)));
} catch (Exception | AssertionError failure) {
reportFailure(output, failure);
}
}
private static void checkEditing(Stage stage, TextField field, boolean book, Path output,
String attempt, String initialValue) {
try {
if (!stage.isShowing()) throw new AssertionError("Preview closed before the edit check in " + attempt);
field.setText("Edited in preview");
if (book && !"[Edited in preview]".equals(((Label) stage.getScene().lookup("#convertedName")).getText()))
throw new AssertionError("Spring Converter did not update the live interactive View");
Files.writeString(output.resolve(attempt + ".txt"), initialValue);
} catch (Exception | AssertionError failure) {
reportFailure(output, failure);
}
}
private static void reportFailure(Path output, Throwable failure) {
try {
Files.writeString(output.resolve("target-error.txt"), failure.toString());
} catch (Exception writeFailure) {
failure.addSuppressed(writeFailure);
failure.printStackTrace();
}
}
}
}