package qualification; import static qualification.FolderActionsTargetProbe.Worker.*; import java.nio.file.*; import java.time.*; import java.time.format.*; import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicInteger; import javafx.application.Platform; import javafx.collections.ListChangeListener; import javafx.scene.*; import javafx.scene.control.*; import javafx.stage.Window; /** Checks rendered folder rows and the ordinary modal profile dialogs in the preview worker. */ public final class FolderProfilesTargetProbe { public static void premain(String directory) { if (System.getProperty("sun.java.command", "") .startsWith("de.neitzel.fxdesigner.target.WorkerMain ")) Thread.ofPlatform().daemon().start(() -> Checks.run(Path.of(directory))); } private static final class Checks { private static void run(Path dir) { try { var stage = previewStage(); var root = fx( () -> { stage.setWidth(1360); stage.setHeight(700); var r = stage.getScene().getRoot(); r.applyCss(); r.layout(); return r; }); var attempt = Files.readString(dir.resolve("layout-case")); var result = switch (attempt) { case "profiles", "readonly", "rebound" -> profiles(root, attempt); case "metadata", "renamed", "recovered" -> metadata(root, attempt, dir); default -> throw new AssertionError(attempt); }; 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 error) { error.printStackTrace(); } } } private static DialogPane dialog(String title) { return Window.getWindows().stream() .filter(javafx.stage.Stage.class::isInstance) .map(javafx.stage.Stage.class::cast) .filter(s -> s.isShowing() && title.equals(s.getTitle())) .map(s -> (DialogPane) s.getScene().getRoot()) .findFirst() .orElse(null); } private static Button change(DialogPane d) { return (Button) d.lookupButton( d.getButtonTypes().stream() .filter(t -> t.getButtonData() == ButtonBar.ButtonData.OTHER) .findFirst() .orElseThrow()); } private static void toggle(Node grid, String header) { var menu = (MenuButton) grid.lookup(".data-grid-column-menu"); menu.show(); var item = (CheckMenuItem) menu.getItems().stream() .filter(i -> header.equals(i.getText())) .findFirst() .orElseThrow(); item.setSelected(!item.isSelected()); item.fire(); menu.hide(); } private static String profiles(Parent root, String attempt) throws Exception { var grid = fx(() -> root.lookup("#grid")); var profiles = fx(() -> (ComboBox) root.lookup("#folder-view-profile")); var save = fx(() -> (Button) root.lookup("#save-folder-view-profile")); var clear = fx(() -> (Button) root.lookup("#clear-folder-view-layout")); var manage = fx(() -> (Button) root.lookup("#manage-folder-view-profiles")); boolean readonly = attempt.equals("readonly"), rebound = attempt.equals("rebound"); await( "Initial profile", () -> !manage.isDisabled() && profiles.isDisabled() == readonly && clear.isDisabled()); String name = readonly ? "Metadaten" : "Prüfprofil"; fx( () -> { require( !profiles.getItems().contains("Prüfprofil"), "New session inherited previous profile"); if (!readonly) profiles.getEditor().setText(name); require(readonly == save.isDisabled(), "Save availability"); if (!readonly && !rebound) { save.fire(); require(profiles.getItems().contains(name), "Profile not saved"); } toggle(grid, "Dateityp"); return null; }); await("Changed local layout", () -> !clear.isDisabled() && !visible(grid, "type")); fx( () -> { clear.fire(); return null; }); await("Restored local layout", () -> clear.isDisabled() && visible(grid, "type")); var openings = new AtomicInteger(); String title = "Anzeigeprofile verwalten"; ListChangeListener shown = changes -> { while (changes.next()) for (Window w : changes.getAddedSubList()) if (w instanceof javafx.stage.Stage s && title.equals(s.getTitle())) openings.incrementAndGet(); }; fx( () -> { Window.getWindows().addListener(shown); Platform.runLater((rebound ? save : manage)::fire); return null; }); await("Profile manager opened", () -> dialog(title) != null); fx( () -> { var d = dialog(title); var list = (ListView) d.lookup("#archive-display-profiles"); if (rebound) require(!list.getItems().contains(name), "Old save handler also ran"); else { list.getSelectionModel().select(name); var action = change(d); require(action.isDisabled() == readonly, "Profile deletion availability"); if (!readonly) Platform.runLater(action::fire); } return null; }); if (!readonly && !rebound) { Callable confirm = () -> Window.getWindows().stream() .filter(Window::isShowing) .map(w -> w.getScene().getRoot()) .filter(DialogPane.class::isInstance) .map(DialogPane.class::cast) .filter(d -> d.getButtonTypes().contains(ButtonType.OK)) .findFirst() .orElse(null); await("Delete confirmation", () -> confirm.call() != null); fx( () -> { ((Button) confirm.call().lookupButton(ButtonType.OK)).fire(); return null; }); await("Deleted own profile", () -> !profiles.getItems().contains(name)); } fx( () -> { ((Button) dialog(title).lookupButton(ButtonType.CLOSE)).fire(); return null; }); fx( () -> { Window.getWindows().removeListener(shown); require( openings.get() == 1 && dialog(title) == null, "Expected exactly one profile dialog: " + openings); return null; }); if (rebound) return "Fresh session. Rebound Save opened exactly one management dialog, without saving a" + " profile."; if (readonly) return "Read-only: archive save, selection and deletion disabled. Management and local" + " layout change/reset succeeded."; return "Saved Prüfprofil, hid and restored Dateityp, deleted own profile after confirmation."; } private static Label cell(Node grid, String text) { return grid.lookupAll(".label").stream() .filter(Label.class::isInstance) .map(Label.class::cast) .filter(l -> text.equals(l.getText())) .findFirst() .orElseThrow(() -> new AssertionError(text)); } private static List