package p.studio.app; import com.fasterxml.jackson.databind.ObjectMapper; import javafx.application.Platform; import javafx.scene.control.*; import javafx.scene.image.ImageView; import javafx.scene.input.*; import javafx.stage.Stage; import org.junit.jupiter.api.*; import org.junit.jupiter.api.io.TempDir; import org.springframework.boot.WebApplicationType; import org.springframework.boot.builder.SpringApplicationBuilder; import p.studio.api.execution.*; import p.studio.api.project.*; import p.studio.api.session.ProjectSessionFactory; import p.studio.core.project.*; import p.studio.core.execution.DefaultExecutionControllerFactory; import p.studio.app.runtime.studio.StudioRuntimeGateway; import p.studio.ui.javafx2.window.*; import java.nio.file.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import static org.junit.jupiter.api.Assertions.*; /** Actual new frontend, real compiler/packer and direct installed native process. */ @org.junit.jupiter.api.Tag("integration") class StudioPlayDesktopTest { @TempDir Path temporary; static T fx(Callable action)throws Exception{ var future=new CompletableFuture();Platform.runLater(()->{try{future.complete(action.call());}catch(Throwable f){future.completeExceptionally(f);}}); return future.get(15,TimeUnit.SECONDS); } @BeforeAll static void toolkit()throws Exception{ var ready=new CompletableFuture();Platform.startup(()->{Platform.setImplicitExit(false);ready.complete(null);});ready.get(15,TimeUnit.SECONDS); } @AfterAll static void stopToolkit(){Platform.exit();} private static void await(BooleanSupplier condition,Supplier failure)throws Exception{ long deadline=System.nanoTime()+TimeUnit.SECONDS.toNanos(20); while(System.nanoTime()>(); var nativeGateway=new StudioRuntimeGateway(app.getBean(ObjectMapper.class)); p.studio.core.runtime.RuntimeGateway gateway=new p.studio.core.runtime.RuntimeGateway(){ public boolean interactive(){return true;} public p.studio.core.runtime.RuntimeConnection start(ProjectReference p,ProjectLocalStudioSetup s,Consumer logs)throws Exception{return start(p,s,logs,PlayOptions.PERSISTENT);} public p.studio.core.runtime.RuntimeConnection start(ProjectReference p,ProjectLocalStudioSetup s,Consumer logs,PlayOptions options)throws Exception{ var actual=nativeGateway.start(p,s,logs,options); return new p.studio.core.runtime.RuntimeConnection(){ public CompletableFuture completion(){return actual.completion();} public CompletableFuture startExecution(){return actual.startExecution();} public CompletableFuture pause(){ if(!delayedPause.getAndSet(false))return actual.pause(); var result=new CompletableFuture();oldPause.set(result);return actual.pause().thenCompose(v->result); } public CompletableFuture resume(){return actual.resume();} public CompletableFuture stopAsync(){return actual.stopAsync();} public ExecutionFrame acquireLatestFrame(){return actual.acquireLatestFrame();} public void input(ExecutionInput value){actual.input(value);} public void close(){actual.close();} }; } }; var factory=new DefaultExecutionControllerFactory(app.getBean(p.studio.api.shipper.Shipper.class), gateway,app.getBean(p.studio.api.operation.OperationExecutor.class),access); var known=new KnownProjects(){public List listKnownProjects(){return List.of(ref);}public void remember(ProjectReference r){}public void forget(ProjectReference r){}}; Stage stage=fx(Stage::new); var window=fx(()->new StudioWindowCoordinator2(stage,catalog,known,app.getBean(ProjectSessionFactory.class),factory, (project,setup)->{try(var lease=access.enterManual(project.rootPath())){setups.save(project,setup);}},executor,resources)); try{ fx(()->{window.showLauncher();stage.toFront();stage.requestFocus();assertNotNull(stage.getScene().lookup("#studio-shell"));window.openProject(root);return null;}); await(()->{try{return fx(()->window.projectSession()!=null);}catch(Exception e){throw new RuntimeException(e);}},()->"Project did not open"); Supplier facts=()->{try{capture(stage,"failure.png");return fx(()->"state="+window.projectSession().executionSession().snapshot().state()+" focused="+stage.isFocused()+" frames="+window.viewport().paintedFrames()+"\n"+((Label)stage.getScene().lookup("#studio-notice")).getText()+"\n"+((TextArea)stage.getScene().lookup("#execution-logs")).getText());}catch(Exception e){return e.toString();}}; focus(stage); fire(stage,"play");running(window,stage,facts); await(()->{try{return fx(()->window.viewport().paintedFrames()>3);}catch(Exception e){throw new RuntimeException(e);}},facts); fx(()->{assertTrue(stage.getScene().lookup("#runtime-path").isDisabled());return null;}); assertThrows(p.studio.api.automation.ControlDeniedException.class,()->access.enterManual(root)); fx(()->{ComboBox zoom=(ComboBox)stage.getScene().lookup("#viewport-zoom");zoom.setValue(4); var image=(ImageView)stage.getScene().lookup("#game-image");assertEquals(2560,image.getFitWidth());assertFalse(image.isSmooth()); zoom.setValue(2);assertEquals(1280,image.getFitWidth());zoom.setValue(1);assertEquals(640,image.getFitWidth());return null;}); // Deliver a real key edge through the viewport; Pong leaves its static menu. // Zoom popup/native activation can transfer desktop focus. Establish the // input precondition explicitly; the later focus-loss checks never resume. running(window,stage,facts); long before=imageHash(stage); fx(()->{assertTrue(stage.isFocused(),"Keyboard qualification requires actual foreground focus"); assertEquals(ExecutionState.RUNNING,window.projectSession().executionSession().snapshot().state()); var surface=stage.getScene().lookup("#game-surface");surface.requestFocus(); surface.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED,"a","a",KeyCode.A,false,false,false,false));return null;}); try { await(()->{try{return imageHash(stage)!=before;}catch(Exception e){throw new RuntimeException(e);}}, ()->"Keyboard A must move Pong out of its menu\n"+facts.get()); } finally { fx(()->{stage.getScene().lookup("#game-surface").fireEvent(new KeyEvent(KeyEvent.KEY_RELEASED,"a","a",KeyCode.A,false,false,false,false));return null;}); } fire(stage,"pause");state(window,ExecutionState.PAUSED,facts); long paused=fx(()->window.viewport().lastFrameId());Thread.sleep(200); assertEquals(paused,fx(()->window.viewport().lastFrameId()));fire(stage,"resume");running(window,stage,facts); // A window owned by Studio is not an application switch. Stage owned=fx(()->{var s=new Stage();s.initOwner(stage);s.setScene(new javafx.scene.Scene(new Label("Studio dialog"),240,120));s.show();s.requestFocus();return s;});Thread.sleep(250); assertEquals(ExecutionState.RUNNING,fx(()->window.projectSession().executionSession().snapshot().state())); fx(()->{owned.hide();stage.requestFocus();stage.setIconified(true);return null;});state(window,ExecutionState.PAUSED,facts); fx(()->{stage.setIconified(false);stage.requestFocus();return null;});Thread.sleep(200); assertEquals(ExecutionState.PAUSED,fx(()->window.projectSession().executionSession().snapshot().state()));fire(stage,"resume");running(window,stage,facts); Stage other=fx(()->{var s=new Stage();s.setTitle("Other application focus probe");s.setScene(new javafx.scene.Scene(new Label("Focus probe"),300,150));s.show();s.requestFocus();return s;});state(window,ExecutionState.PAUSED,facts); fx(()->{other.hide();stage.requestFocus();return null;});Thread.sleep(200); assertEquals(ExecutionState.PAUSED,fx(()->window.projectSession().executionSession().snapshot().state()));fire(stage,"resume");running(window,stage,facts); capture(stage,"play.png"); if(Boolean.getBoolean("studio.test.playStress")){ var process=runtime();long runtimeCpu=cpuNanos(process); long javaCpu=cpuNanos(ProcessHandle.current()); long painted=fx(()->window.viewport().paintedFrames()); long heapBefore=java.lang.management.ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); Thread.sleep(30_000); double p95=fx(()->window.viewport().latencyP95Millis());System.out.println("RECEIVE_TO_PAINT_P95_MS="+p95);assertTrue(p95<=33.4,"Idle receive-to-paint p95 "+p95); System.out.println("PAINT_FPS="+(fx(()->window.viewport().paintedFrames())-painted)/30.0); System.out.println("RUNTIME_CPU_SECONDS="+(cpuNanos(process)-runtimeCpu)/1e9); System.out.println("JAVA_CPU_SECONDS="+(cpuNanos(ProcessHandle.current())-javaCpu)/1e9); System.out.println("HEAP_USED_BEFORE_AFTER="+heapBefore+","+java.lang.management.ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed()); assertEquals(ExecutionState.RUNNING,fx(()->window.projectSession().executionSession().snapshot().state()),facts); assertTrue(fx(stage::isFocused),"Idle-host measurement requires the Studio to retain actual focus"); long frame=fx(()->window.viewport().lastFrameId()); fx(()->{Thread.sleep(500);return null;});Thread.sleep(70); long afterFreeze=fx(()->window.viewport().lastFrameId()); System.out.println("FX_FREEZE_FRAME_IDS="+frame+","+afterFreeze); assertTrue(afterFreeze>frame+15,()->"Runtime must advance while FX is blocked: "+frame+" -> "+afterFreeze+"\n"+facts.get()); for(int i=0;i<20;i++){fire(stage,"stop");state(window,ExecutionState.STOPPED,facts);Thread.sleep(130); assertEquals(0,runtimeCount());assertEquals(0,heartbeatThreads()); fire(stage,"play");running(window,stage,facts);Thread.sleep(50); assertEquals(1,runtimeCount());assertEquals(1,heartbeatThreads());} System.out.println("PLAY_STOP_CYCLES=20"); } fire(stage,"stop");state(window,ExecutionState.STOPPED,facts); await(()->{try{return fx(()->!stage.getScene().lookup("#runtime-path").isDisabled());}catch(Exception e){throw new RuntimeException(e);}},facts); try(var lease=access.enterManual(root)){} assertFalse(Files.exists(root.resolve(".codex")),"Play must not prepare an AI client"); assertFalse(Files.exists(root.resolve("build.pmc"))); fire(stage,"play");running(window,stage,facts); delayedPause.set(true);fire(stage,"pause"); await(()->oldPause.get()!=null,facts); fire(stage,"stop");state(window,ExecutionState.STOPPED,facts); fire(stage,"play");running(window,stage,facts); oldPause.get().completeExceptionally(new java.io.IOException("obsolete-pause-result")); Thread.sleep(200); assertFalse(fx(()->((Label)stage.getScene().lookup("#studio-notice")).getText().contains("obsolete-pause-result")), "An old command must not replace a new execution's feedback"); runtime().destroyForcibly();state(window,ExecutionState.RUNTIME_FAILED,facts); await(()->{try{return fx(()->!stage.getScene().lookup("#runtime-path").isDisabled());}catch(Exception e){throw new RuntimeException(e);}},facts); assertTrue(fx(()->((TextArea)stage.getScene().lookup("#execution-logs")).getText().contains("Runtime"))); fire(stage,"play");running(window,stage,facts); fx(()->{window.requestCloseProject();return null;}); await(()->{try{return fx(()->window.projectSession()==null);}catch(Exception e){throw new RuntimeException(e);}},facts); fx(()->{assertTrue(window.launcherStage().isShowing());assertFalse(stage.isShowing());assertNotNull(window.launcherStage().getScene().lookup("#open-project"));return null;});capture(window.launcherStage(),"projects.png"); assertEquals(0,runtimeCount());assertEquals(0,heartbeatThreads()); }finally{fx(window::closeAsync).toCompletableFuture().get(15,TimeUnit.SECONDS);} } } static void fire(Stage stage,String id)throws Exception{fx(()->{Button button=(Button)stage.getScene().lookup("#"+id);assertFalse(button.isDisabled(),id+" disabled");button.fire();return null;});} static void state(StudioWindowCoordinator2 w,ExecutionState expected,Supplier facts)throws Exception{ await(()->{try{return fx(()->w.projectSession().executionSession().snapshot().state()==expected);}catch(Exception e){throw new RuntimeException(e);}},facts); Thread.sleep(130); } static void running(StudioWindowCoordinator2 w,Stage stage,Supplier facts)throws Exception{ focus(stage); await(()->{try{return fx(()->{var state=w.projectSession().executionSession().snapshot().state(); if(state==ExecutionState.PAUSED){stage.requestFocus();((Button)stage.getScene().lookup("#resume")).fire();} return state==ExecutionState.RUNNING && stage.isFocused();});}catch(Exception e){throw new RuntimeException(e);}},facts);Thread.sleep(150); } static void focus(Stage stage)throws Exception{fx(()->{stage.setIconified(false);stage.toFront();stage.requestFocus(); var point=stage.getScene().getRoot().localToScreen(10,10); var robot=new javafx.scene.robot.Robot();robot.mouseMove(point);robot.mouseClick(MouseButton.PRIMARY);return null;});Thread.sleep(150);} static void capture(Stage stage,String name)throws Exception{fx(()->{ var image=stage.getScene().snapshot(null);var output=new java.awt.image.BufferedImage((int)image.getWidth(),(int)image.getHeight(),java.awt.image.BufferedImage.TYPE_INT_ARGB); for(int y=0;y{var image=((ImageView)stage.getScene().lookup("#game-image")).getImage();long hash=1; for(int y=0;y<(int)image.getHeight();y++)for(int x=0;x<(int)image.getWidth();x++)hash=31*hash+image.getPixelReader().getArgb(x,y);return hash;});} static long runtimeCount(){return ProcessHandle.current().children().filter(p->p.isAlive()&&p.info().command().orElse("").endsWith("/prometeu-runtime")).count();} static ProcessHandle runtime(){return ProcessHandle.current().children().filter(p->p.isAlive()&&p.info().command().orElse("").endsWith("/prometeu-runtime")).findFirst().orElseThrow();} static long heartbeatThreads(){return Thread.getAllStackTraces().keySet().stream().filter(t->t.isAlive()&&t.getName().equals("studio-play-heartbeat")).count();} static long cpuNanos(ProcessHandle process)throws Exception{ var cpu=process.info().totalCpuDuration();if(cpu.isPresent())return cpu.get().toNanos(); // HotSpot on this macOS does not expose child CPU duration through ProcessHandle.Info. var query=new ProcessBuilder("/bin/ps","-p",Long.toString(process.pid()),"-o","cputime=").start(); String value=new String(query.getInputStream().readAllBytes(),java.nio.charset.StandardCharsets.UTF_8).trim(); assertTrue(query.waitFor(2,TimeUnit.SECONDS));assertEquals(0,query.exitValue()); double seconds=0;for(String component:value.split(":"))seconds=seconds*60+Double.parseDouble(component); return (long)(seconds*1e9); } @Test void viewportCopiesStablePixelsAndInvertsZoomWithoutClampingOutsideTouches()throws Exception{ var input=new java.util.concurrent.atomic.AtomicReference<>(ExecutionInput.NEUTRAL); byte[] pixels=new byte[4*2*4];for(int i=0;i(new ExecutionFrame(4,2,1,1,pixels,()->{})); ExecutionController controller=new ExecutionController(){ public p.studio.api.operation.OperationHandle play(){throw new UnsupportedOperationException();} public void stop(){}public void close(){}public ExecutionFrame acquireLatestFrame(){return frame.getAndSet(null);} public void input(ExecutionInput value){input.set(value);} }; var viewport=fx(()->new p.studio.ui.javafx2.play.PlaybackViewport(()->controller,()->ExecutionState.RUNNING)); Stage stage=fx(()->{var s=new Stage();s.setScene(new javafx.scene.Scene(viewport,500,300));s.show();return s;}); try{ await(()->{try{return fx(()->viewport.paintedFrames()==1);}catch(Exception e){throw new RuntimeException(e);}},()->"Frame was not painted"); fx(()->{viewport.zoom(4);stage.getScene().getRoot().applyCss();stage.getScene().getRoot().layout(); ImageView image=(ImageView)stage.getScene().lookup("#game-image");assertEquals(0xffff0000,image.getImage().getPixelReader().getArgb(1,1)); assertEquals(16,image.getFitWidth()); pointer(image,MouseEvent.MOUSE_PRESSED,6,6);assertEquals(new ExecutionInput(4096,1,1),input.get()); pointer(image,MouseEvent.MOUSE_DRAGGED,-1,6);assertEquals(0,input.get().buttons());assertEquals(-1,input.get().touchX()); pointer(image,MouseEvent.MOUSE_DRAGGED,6,6);assertEquals(0,input.get().buttons(),"Reentry while held must not recapture"); pointer(image,MouseEvent.MOUSE_PRESSED,14,6);assertEquals(new ExecutionInput(4096,3,1),input.get()); pointer(image,MouseEvent.MOUSE_RELEASED,30,30);assertEquals(0,input.get().buttons()); var surface=stage.getScene().lookup("#game-surface"); surface.fireEvent(new KeyEvent(KeyEvent.KEY_PRESSED,"a","a",KeyCode.A,false,false,false,false));assertEquals(16,input.get().buttons()); viewport.neutralize();assertEquals(0,input.get().buttons());return null;}); }finally{fx(()->{viewport.close();stage.hide();return null;});} } static void pointer(ImageView image,javafx.event.EventType type,double x,double y){ var point=image.localToScene(x,y); image.fireEvent(new MouseEvent(type,point.getX(),point.getY(),0,0,MouseButton.PRIMARY,1, false,false,false,false,type!=MouseEvent.MOUSE_RELEASED,false,false,false,false,false, new PickResult(image,point.getX(),point.getY()))); } }