package p.studio.app.animation; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.image.ImageView; import javafx.stage.Stage; import org.junit.jupiter.api.*; import org.junit.jupiter.api.io.TempDir; import p.studio.ui.javafx.workspaces.assets.details.animation.AnimationEditorControl; import p.animation.*; import java.nio.file.*; import java.util.concurrent.*; import java.util.function.*; import static org.junit.jupiter.api.Assertions.*; /** Actual JavaFX editing, shared save/preview, drift repair and handoff lifecycle. */ @org.junit.jupiter.api.Tag("integration") class AnimationDesktopTest { @TempDir Path root; @BeforeAll static void toolkit()throws Exception{ var ready=new CompletableFuture(); try{Platform.startup(()->{Platform.setImplicitExit(false);ready.complete(null);});} catch(IllegalStateException running){ready.complete(null);} ready.get(10,TimeUnit.SECONDS); } @AfterAll static void stopToolkit(){Platform.exit();} static T fx(Supplier work)throws Exception{var result=new CompletableFuture();Platform.runLater(()->{try{result.complete(work.get());}catch(Throwable f){result.completeExceptionally(f);}});return result.get(10,TimeUnit.SECONDS);} static void await(BooleanSupplier test)throws Exception{long end=System.nanoTime()+TimeUnit.SECONDS.toNanos(10);while(!test.getAsBoolean()){if(System.nanoTime()>end)fail("Animation editor did not finish");Thread.sleep(10);}} static Button button(AnimationEditorControl editor,String text){return editor.lookupAll(".button").stream().filter(n->n instanceof Button b&&b.getText().equals(text)).map(n->(Button)n).findFirst().orElseThrow();} @Test void authorsPreviewsSavesRepairsAndCloses()throws Exception{ var fixture=new AnimationIntegrationTest();fixture.root=root;fixture.setup(); var pool=Executors.newCachedThreadPool();var stage=new Stage[1];var editor=new AnimationEditorControl[1]; try{ fx(()->{editor[0]=new AnimationEditorControl(fixture.project,fixture.manual,pool);var scroll=new ScrollPane(editor[0]);scroll.setFitToWidth(true);stage[0]=new Stage();stage[0].setTitle("Animation authoring acceptance");stage[0].setScene(new Scene(scroll,1040,940));stage[0].show();editor[0].bind(fixture.uuid);return null;}); await(()->{try{return fx(()->((Label)editor[0].lookup("#animation-status")).getText().equals("New definition"));}catch(Exception e){throw new RuntimeException(e);}}); fx(()->{((TextField)editor[0].lookup("#animation-id")).setText("hero");button(editor[0],"New animator").fire();((ComboBox)editor[0].lookup("#animation-glyph")).getSelectionModel().selectFirst();((TextField)editor[0].lookup("#animation-duration")).setText("2");button(editor[0],"Add frame").fire();return null;}); assertTrue(editor[0].hasUnsavedChanges().toCompletableFuture().get()); editor[0].saveAll().toCompletableFuture().get(10,TimeUnit.SECONDS); assertFalse(editor[0].hasUnsavedChanges().toCompletableFuture().get()); assertEquals(2,fixture.manual.execute(p.studio.api.assets.animation.AnimationService.Request.get(fixture.project,fixture.uuid)).view().document().animators().getFirst().clips().getFirst().frames().getFirst().durationTicks()); fx(()->{button(editor[0],"Preview").fire();return null;}); await(()->{try{return fx(()->((ImageView)editor[0].lookup("#animation-image")).getImage()!=null);}catch(Exception e){throw new RuntimeException(e);}}); fx(()->{var snapshot=stage[0].getScene().snapshot(null);var image=new java.awt.image.BufferedImage((int)snapshot.getWidth(),(int)snapshot.getHeight(),java.awt.image.BufferedImage.TYPE_INT_ARGB);for(int y=0;y{button(editor[0],"Prepare rebind").fire();return null;}); await(()->{try{return fx(()->!button(editor[0],"Apply rebind").isDisabled());}catch(Exception e){throw new RuntimeException(e);}}); fx(()->{button(editor[0],"Apply rebind").fire();return null;}); await(()->fixture.manual.execute(p.studio.api.assets.animation.AnimationService.Request.get(fixture.project,fixture.uuid)).view().diagnostics().isEmpty()); fx(()->{editor[0].openOrigin("/animators/0/clips/0/frames/0");editor[0].close();stage[0].close();return null;}); assertFalse(pool.isShutdown()); }finally{fx(()->{if(editor[0]!=null)editor[0].close();if(stage[0]!=null)stage[0].close();return null;});pool.shutdownNow();fixture.close();} } }