package p.studio.ui.javafx2; import javafx.application.Platform; import javafx.animation.AnimationTimer; import javafx.embed.swing.SwingFXUtils; import javafx.event.Event; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.control.skin.ComboBoxListViewSkin; import javafx.scene.image.WritableImage; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; import org.junit.jupiter.api.*; import p.studio.ui.javafx2.appearance.*; import p.studio.ui.javafx2.components.*; import p.studio.ui.javafx2.demo.ComponentGallery; import p.studio.ui.javafx2.i18n.*; import p.studio.ui.javafx2.shell.MinimalShell; import javax.imageio.ImageIO; import java.nio.file.*; import java.util.*; import java.util.concurrent.*; import static org.junit.jupiter.api.Assertions.*; /** Opt-in real toolkit contract tests. No backend, game project, or personal preference writes. */ class FoundationDesktopTest { private static FontResources fonts; private static Messages messages; private static Path captures; private record Window(Stage stage,AppearanceContext context,MinimalShell shell,ComponentGallery gallery) { } @BeforeAll static void startToolkit() throws Exception { CountDownLatch ready=new CountDownLatch(1); Platform.startup(ready::countDown); assertTrue(ready.await(15,TimeUnit.SECONDS)); Platform.setImplicitExit(false); fonts=fx(() -> FontResources.load(s -> fail(s))); messages=Messages.load(s -> fail(s)); captures=Path.of(System.getProperty("studio.ui2.captureDir")); Files.createDirectories(captures); } @AfterAll static void stopToolkit() { Platform.exit(); } @Test void shellGalleryStateThemesLocalesFontsAndLifecycle() throws Exception { Window window=fx(() -> open(true)); try { fx(() -> { assertTrue(window.shell.workspace().getChildren().isEmpty()); assertEquals("Departure Mono",fonts.family(Typeface.DEPARTURE)); assertEquals("Roboto Mono",fonts.family(Typeface.ROBOTO)); assertEquals("São João · 0123",typeAndCopy(window.gallery.nameField(),"São João · 0123")); ((Button)window.stage.getScene().lookup("#apply")).fire(); assertTrue(window.gallery.feedback().message().contains("São João")); window.context.update(window.context.settings().withLanguage(StudioLanguage.PORTUGUESE)); assertTrue(window.gallery.feedback().message().startsWith("Ação")); assertEquals("São João · 0123",window.gallery.nameField().getText()); window.gallery.nameField().setText(""); ((Button)window.stage.getScene().lookup("#apply")).fire(); assertTrue(window.gallery.feedback().message().startsWith("Informe")); assertTrue(window.gallery.nameField().getPseudoClassStates().stream().anyMatch(p -> p.getPseudoClassName().equals("error"))); return null; }); for(Theme theme:Theme.values()) for(StudioLanguage language:StudioLanguage.values()) for(Typeface face:Typeface.values()) { fx(() -> {window.context.update(new PresentationSettings(theme,language,face,1));window.stage.getScene().getRoot().applyCss();window.stage.getScene().getRoot().layout();return null;}); capture(window,theme+"-"+language+"-"+face); } fx(() -> { window.shell.themeSelector().show(); var popup=(Parent)((ComboBoxListViewSkin)window.shell.themeSelector().getSkin()).getPopupContent(); popup.applyCss(); assertTrue(popup.getStyleClass().contains("studio-root")); assertTrue(popup.getStylesheets().stream().anyMatch(value -> value.endsWith("light.css"))); window.context.update(window.context.settings().withLanguage(StudioLanguage.ENGLISH)); popup.applyCss();popup.layout(); assertTrue(popup.lookupAll(".list-cell").stream().filter(node -> node instanceof ListCell).map(node -> ((ListCell)node).getText()).anyMatch("Bananodore · dark"::equals),"An already open popup must translate"); window.shell.themeSelector().hide(); window.shell.themeSelector().requestFocus(); Event.fireEvent(window.shell.themeSelector(),new KeyEvent(KeyEvent.KEY_PRESSED,"","",KeyCode.TAB,false,false,false,false)); assertSame(window.shell.languageSelector(),window.stage.getScene().getFocusOwner(),"Tab must move from theme to language selector"); window.context.update(window.context.settings().withScale(1.5)); assertEquals("Roboto Mono",window.gallery.nameField().getFont().getFamily()); return null; }); long duration=fx(() -> { long begin=System.nanoTime(); for(int i=0;i<20;i++) { window.context.update(new PresentationSettings(i%2==0?Theme.DARK:Theme.LIGHT,i%2==0?StudioLanguage.ENGLISH:StudioLanguage.PORTUGUESE,i%2==0?Typeface.DEPARTURE:Typeface.ROBOTO,1)); window.stage.getScene().getRoot().applyCss();window.stage.getScene().getRoot().layout(); } return System.nanoTime()-begin; }); double averageMillis=duration/20_000_000.; System.out.println("20 appearance changes, average FX CSS/layout ms="+averageMillis); assertTrue(averageMillis<250,"Appearance update exceeds 250ms average budget"); fx(() -> {window.stage.setWidth(680);window.stage.setHeight(480);return null;}); capture(window,"minimum-680x480"); fx(() -> { var scroll=(ScrollPane)window.stage.getScene().getRoot(); assertTrue(scroll.getContent().getLayoutBounds().getWidth() <= scroll.getViewportBounds().getWidth()+1,"Minimum width must not cause horizontal overflow"); return null; }); fx(() -> {((ScrollPane)window.stage.getScene().getRoot()).setVvalue(1);return null;}); capture(window,"fonts-and-licenses"); } finally { fx(() -> {window.stage.close();assertTrue(window.context.isClosed());return null;}); } for(int i=0;i<3;i++) { Window reopened=fx(() -> open(false)); if(i==0){ capture(reopened,"shell-dark-default"); fx(() -> {reopened.stage.setWidth(680);reopened.stage.setHeight(480);return null;}); capture(reopened,"shell-minimum-680x480"); fx(() -> {assertTrue(reopened.shell.workspace().getHeight()>80);return null;}); } fx(() -> {assertTrue(reopened.shell.workspace().getChildren().isEmpty());reopened.stage.close();assertTrue(reopened.context.isClosed());assertThrows(IllegalStateException.class,() -> reopened.context.update(reopened.context.settings().withTheme(Theme.LIGHT)));return null;}); } } private static Window open(boolean gallery) { var context=new AppearanceContext(PresentationSettings.defaults(Locale.ENGLISH),messages,fonts,value -> { }); var shell=new MinimalShell(context); ComponentGallery specimens=gallery?new ComponentGallery(context):null; Stage stage=new Stage(); Parent root=shell; if(gallery) { shell.setMinHeight(340); shell.workspace().setMinHeight(160); VBox content=new VBox(shell,specimens); content.setMinWidth(0); context.attach(content); ScrollPane scroll=new ScrollPane(content);scroll.setFitToWidth(true);context.attach(scroll);root=scroll; } stage.setScene(new Scene(root,1200,800));stage.setMinWidth(680);stage.setMinHeight(480);stage.setTitle("Prometeu Studio — desktop qualification"); stage.setOnHidden(event -> context.close());stage.show();root.applyCss();root.layout(); return new Window(stage,context,shell,specimens); } private static String typeAndCopy(TextField field,String text) { field.requestFocus();field.setText(text);field.selectAll();field.copy();field.clear();field.paste();return field.getText(); } private static void capture(Window window,String name) throws Exception { settleFrames(); record Capture(WritableImage image,String metadata) { } Capture captured=fx(() -> { window.stage.getScene().getRoot().applyCss();window.stage.getScene().getRoot().layout(); WritableImage image=window.stage.getScene().snapshot(null); String metadata="os="+System.getProperty("os.name")+" "+System.getProperty("os.version")+"\njava="+System.getProperty("java.version")+"\noutputScale="+window.stage.getOutputScaleX()+"x"+window.stage.getOutputScaleY()+"\nsettings="+window.context.settings()+"\nscene="+window.stage.getScene().getWidth()+"x"+window.stage.getScene().getHeight()+"\nimage="+image.getWidth()+"x"+image.getHeight()+"\n"; assertEquals(window.stage.getScene().getWidth(),image.getWidth(),1); return new Capture(image,metadata); }); ImageIO.write(SwingFXUtils.fromFXImage(captured.image(),null),"png",captures.resolve(name+".png").toFile()); Files.writeString(captures.resolve(name+".txt"),captured.metadata()); } private static void settleFrames() throws Exception { CountDownLatch settled=new CountDownLatch(1); fx(() -> {new AnimationTimer(){int count;@Override public void handle(long now){if(++count==3){stop();settled.countDown();}}}.start();return null;}); assertTrue(settled.await(5,TimeUnit.SECONDS),"JavaFX pulses did not settle"); } private static T fx(Callable task) throws Exception { FutureTask future=new FutureTask<>(task);Platform.runLater(future);return future.get(30,TimeUnit.SECONDS); } }