Merge optional Message popups with child notices carried onto the parent.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-30 18:04:39 +01:00
co-authored by Cursor
6 changed files with 356 additions and 3 deletions
@@ -0,0 +1,240 @@
package de.neitzel.fx.view;
import de.neitzel.fx.view.fixture.Person;
import de.neitzel.injection.ApplicationContext;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputControl;
import javafx.stage.Window;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.fail;
class ShowMessagesWhenAskedTest {
@BeforeAll
static void startJavaFx() throws InterruptedException {
Platform.setImplicitExit(false);
CountDownLatch started = new CountDownLatch(1);
try {
Platform.startup(started::countDown);
} catch (IllegalStateException alreadyStarted) {
started.countDown();
}
if (!started.await(10, TimeUnit.SECONDS)) {
fail("JavaFX toolkit did not start");
}
}
@Test
void addingAMessageDoesNotOpenAPopupOrFillAStatusBar() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ShowMessagesWhenAskedTest.class.getResource("name-both-ways.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
Parent root = loaded.getRoot();
Set<Node> nodesBefore = new HashSet<>(root.lookupAll("*"));
Set<Window> windowsBefore = snapshotWindows();
loaded.getViewModel().addMessage(
new Message(Message.Severity.INFO, "saved the person", "form"));
assertEquals(windowsBefore, snapshotWindows());
assertEquals(nodesBefore, new HashSet<>(root.lookupAll("*")));
assertMessageIsNotShown(root, "saved the person");
});
}
@Test
void directBindingShowsTheLatestMessageText() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ShowMessagesWhenAskedTest.class.getResource("latest-message.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
Label messageLabel = (Label) loaded.getRoot().lookup("#messageLabel");
loaded.getViewModel().addMessage(
new Message(Message.Severity.INFO, "saved the person", "form"));
assertEquals("saved the person", messageLabel.getText());
loaded.getViewModel().addMessage(
new Message(Message.Severity.WARN, "name is required", "name"));
assertEquals("name is required", messageLabel.getText());
});
}
@Test
void optedInHelperShowsANewMessageInAPopup() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
URL fxml = ShowMessagesWhenAskedTest.class.getResource("name-both-ways.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
Set<Window> windowsBefore = snapshotWindows();
MessagePopup.showNewMessages(loaded.getViewModel());
assertEquals(windowsBefore, snapshotWindows());
try {
loaded.getViewModel().addMessage(
new Message(Message.Severity.INFO, "saved the person", "form"));
assertNotNull(windowShowing("saved the person", windowsBefore));
assertMessageIsNotShown(loaded.getRoot(), "saved the person");
} finally {
closeWindowsOpenedSince(windowsBefore);
}
});
}
@Test
void updatingABindingFailureDoesNotOpenAnotherPopup() throws Exception {
onFx(() -> {
Person person = new Person("Ada");
person.setAge(42);
URL fxml = ShowMessagesWhenAskedTest.class.getResource("age-both-converted.fxml");
assertNotNull(fxml);
ApplicationContext context = new ApplicationContext("de.neitzel.fx.view.fixture");
LoadedView loaded = ViewLoader.load(fxml, person, context);
TextField ageField = (TextField) loaded.getRoot().lookup("#ageField");
Set<Window> windowsBefore = snapshotWindows();
MessagePopup.showNewMessages(loaded.getViewModel());
try {
ageField.setText("nope");
ViewModel viewModel = loaded.getViewModel();
Message failure = viewModel.getLatestMessage();
assertEquals("not a whole number: nope", failure.getText());
assertEquals(1, windowsOpenedSince(windowsBefore).size());
assertNotNull(windowShowing("not a whole number: nope", windowsBefore));
ageField.setText("still-bad");
assertEquals(1, viewModel.getMessages().size());
assertSame(failure, viewModel.getLatestMessage());
assertEquals("not a whole number: still-bad", failure.getText());
assertEquals(1, windowsOpenedSince(windowsBefore).size());
viewModel.addMessage(new Message(Message.Severity.INFO, "saved the person", "form"));
assertEquals(2, windowsOpenedSince(windowsBefore).size());
assertNotNull(windowShowing("saved the person", windowsBefore));
assertEquals("saved the person", viewModel.getLatestMessage().getText());
} finally {
closeWindowsOpenedSince(windowsBefore);
}
});
}
private static Set<Window> snapshotWindows() {
return new HashSet<>(Window.getWindows());
}
private static Window windowShowing(String text, Set<Window> alreadyOpen) {
for (Window window : Window.getWindows()) {
if (!alreadyOpen.contains(window) && windowShows(window, text)) {
return window;
}
}
return null;
}
private static boolean windowShows(Window window, String text) {
if (window.getScene() == null || !(window.getScene().getRoot() instanceof Parent root)) {
return false;
}
if (root instanceof DialogPane pane && nodeShows(pane.getContent(), text)) {
return true;
}
for (Node node : root.lookupAll("*")) {
if (nodeShows(node, text)) {
return true;
}
}
return false;
}
private static boolean nodeShows(Node node, String text) {
return node instanceof Labeled labeled && text.equals(labeled.getText());
}
private static List<Window> windowsOpenedSince(Set<Window> alreadyOpen) {
List<Window> opened = new ArrayList<>();
for (Window window : Window.getWindows()) {
if (!alreadyOpen.contains(window)) {
opened.add(window);
}
}
return opened;
}
private static void closeWindowsOpenedSince(Set<Window> alreadyOpen) {
List<Window> open = new ArrayList<>(Window.getWindows());
for (Window window : open) {
if (!alreadyOpen.contains(window)) {
window.hide();
}
}
}
private static void assertMessageIsNotShown(Parent root, String text) {
for (Node node : root.lookupAll("*")) {
if (node instanceof Labeled labeled) {
assertNotEquals(text, labeled.getText());
}
if (node instanceof TextInputControl input) {
assertNotEquals(text, input.getText());
}
}
}
private static void onFx(FxAction action) throws Exception {
if (Platform.isFxApplicationThread()) {
action.run();
return;
}
AtomicReference<Throwable> thrown = new AtomicReference<>();
CountDownLatch done = new CountDownLatch(1);
Platform.runLater(() -> {
try {
action.run();
} catch (Throwable failure) {
thrown.set(failure);
} finally {
done.countDown();
}
});
if (!done.await(10, TimeUnit.SECONDS)) {
fail("JavaFX thread did not finish");
}
Throwable failure = thrown.get();
if (failure instanceof Exception exception) {
throw exception;
}
if (failure instanceof Error error) {
throw error;
}
if (failure != null) {
throw new AssertionError(failure);
}
}
@FunctionalInterface
private interface FxAction {
void run() throws Exception;
}
}
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import de.neitzel.fx.view.BindingList?>
<?import de.neitzel.fx.view.DirectBinding?>
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<BindingList>
<DirectBinding control="messageLabel" property="text" path="latestMessage" direction="view"/>
</BindingList>
</fx:define>
<Label fx:id="messageLabel" id="messageLabel"/>
</VBox>