import com.gorios.lanvoice.avatar.* import com.gorios.lanvoice.dialogue.* import android.os.TestScheduler import android.media.CodecFixture import android.media.MediaPlayer import android.graphics.BitmapFactory import android.opengl.GLFixture import android.view.TextureView fun mediaFixtureIndex(width: Int=480,height: Int=848): EmotionActionIndex { val packs=linkedMapOf("neutral" to dynamicPack(97,24,"c".repeat(64),width,height).copy(root="neutral/"), "happy" to dynamicPack(49,24,"d".repeat(64),width,height).copy(root="happy/"), "sad" to dynamicPack(121,30,"e".repeat(64),width,height).copy(root="sad/")) val visuals=packs.map { (id,p)->TalkPackage(id,RoleEmotion.entries.single { it.wire==id },p.body.sha256,p.body.sha256,p.body.ptsUs,p.body.ptsUs,true) } val bridges=packs.map { (id,p)->ConversationEdge("$id-loop",id,id,p.body.frameCount-1,0,p.body,true) }+ listOf("happy","sad").flatMap { id->listOf("neutral" to id,id to "neutral").map { (from,to)-> ConversationEdge("$from-$to",from,to,packs.getValue(from).body.frameCount-1,0, ContinuousBodyAsset("clips/$from-$to.mp4","f".repeat(64),width,height,3,125000,List(3) { it*1_000_000L/24 }), mouthRuntime=dynamicPack(3,24,"f".repeat(64),width,height).copy(root="bridge-$from-$to/")) } } val musicPts=List(24) { it*1_000_000L/24 } val actions=ReviewedActionPack("a".repeat(64),listOf("asset_01","asset_02").map { ActionAsset(it,1000,ActionMedia("clips/$it.mp4","a".repeat(64),"",musicPts,1_000_000,width,height,"neutral",96,"neutral",0,950_000)) }) return EmotionActionIndex("neutral",packs,visuals,actions,bridges) } fun mediaReply(emotion: RoleEmotion,turn: Long=7,seq: Long=2,intensity: Int=if(emotion==RoleEmotion.NEUTRAL) 0 else 1)= ReplyEmotion(emotion,intensity,EmotionSource.MODEL,turn,seq) fun actualMediaViewCheck(width: Int=480,height: Int=848) { var cases=0 fun reset() { TestScheduler.reset();CodecFixture.reset();GLFixture.reset();BitmapFactory.wrongSize=false;MediaPlayer.created.clear() CodecFixture.emitNativeSurface=true;CodecFixture.width=width;CodecFixture.height=height } fun rig(index: EmotionActionIndex=mediaFixtureIndex(width,height)): Triple> { index.packages.values.forEach { CodecFixture.timelines[it.root+it.body.path]=it.body.ptsUs } index.edges.filterNot { it.nativeLoop }.forEach { CodecFixture.timelines[(it.mouthRuntime?.root ?: index.root)+(it.mouthRuntime?.body?.path ?: it.clip.path)]=it.clip.ptsUs } index.actionPack?.assets?.mapNotNull { it.media }?.forEach { CodecFixture.timelines[it.root+it.path]=it.ptsUs;CodecFixture.audioPaths.add(it.root+it.path) } val binding=AvatarMediaBinding();binding.beginTurn(7) val reports=mutableListOf() val view=EmotionActionAvatarView(android.content.Context(),AvatarPack(fallback="fixture.png",emotionAction=index),binding,reports::add) view.render(true);TestScheduler.advanceBy(1) return Triple(view,binding,reports) } reset();val (view,binding,reports)=rig() check(reports.last().actual=="body_neutral");cases++ val driverField=view.javaClass.getDeclaredField("driver").apply { isAccessible=true } val driver=driverField.get(view) binding.prepare(mediaReply(RoleEmotion.HAPPY,seq=1));TestScheduler.advanceBy(200) check(reports.none { it.actual=="body_happy" });cases++ // Header preloads hidden, never shows. binding.intent(mediaReply(RoleEmotion.HAPPY,intensity=2),true) TestScheduler.until { reports.last().actual=="bridge" || reports.last().actual=="media_failed" } check(reports.last().actual=="bridge") val actualRenderers=view.javaClass.getDeclaredField("renderers").apply { isAccessible=true }.get(view) as Array<*> val bridgeRenderer=actualRenderers.filterNotNull().first { value -> value.javaClass.getDeclaredField("pack").apply { isAccessible=true }.get(value).let { (it as ContinuousAvatarPack).root=="bridge-neutral-happy/" } } check(bridgeRenderer.javaClass.getDeclaredField("mouthEnabled").apply { isAccessible=true }.getBoolean(bridgeRenderer));cases++ check(bridgeRenderer.javaClass.getDeclaredField("sharedMouth").apply { isAccessible=true }.get(bridgeRenderer)!=null);cases++ check(binding.currentPackageId==null && binding.visualStatus=="bridge_own_mouth");cases++ TestScheduler.until { reports.any { it.actual=="body_happy" } || reports.any { it.actual=="media_failed" } } check(reports.none { it.actual=="media_failed" }) { reports.takeLast(6).toString() } val selected=(driver as ConversationMediaDriver).selection check(selected?.selectedIntensity==2 && selected.visualIntensity==1 && selected.selectedEmotion==RoleEmotion.HAPPY);cases++ check(reports.any { it.actual=="bridge" } && reports.none { it.actual=="media_failed" });cases++ TestScheduler.advanceBy(3000) check(reports.any { it.actual=="body_happy" && it.clipId.contains("loop1") });cases++ view.render(true);TestScheduler.advanceBy(100) check(driverField.get(view)===driver);cases++ // Actual persistent view, same settings/foreground path. binding.intent(mediaReply(RoleEmotion.SAD,seq=3),false) TestScheduler.until { reports.last().actual=="bridge" } binding.beginTurn(8);binding.intent(mediaReply(RoleEmotion.SAD,turn=8),true) binding.intent(mediaReply(RoleEmotion.HAPPY,turn=7,seq=99),true) // Retired reply ignored. TestScheduler.until { reports.last().actual=="body_sad" } check((driverField.get(view) as ConversationMediaDriver).selection?.selectedEmotion==RoleEmotion.SAD);cases++ TestScheduler.advanceBy(4500) check(reports.any { it.actual=="body_sad" && it.clipId.contains("loop1") } && reports.none { it.actual=="media_failed" });cases++ val count=reports.size;view.render(false);TestScheduler.advanceBy(1000) check(reports.size==count && driverField.get(view)==null && CodecFixture.releases>=5);cases++ view.dispose() reset();val (mismatchView,mismatchBinding,mismatchTrace)=rig() mismatchBinding.prepare(mediaReply(RoleEmotion.HAPPY,seq=1));TestScheduler.advanceBy(100) mismatchBinding.intent(mediaReply(RoleEmotion.SAD,seq=2),true) mismatchBinding.prepare(null) TestScheduler.until { mismatchTrace.any { it.actual=="body_sad" } } check(mismatchTrace.none { it.actual=="body_happy" || it.actual=="media_failed" });cases++ mismatchView.dispose();TestScheduler.advanceBy(100) for(confirmed in listOf(true,false)) { reset();val (cancelView,cancelBinding,_)=rig() var stop: ((Boolean)->Unit)?=null;var resumed=0 val statuses=mutableListOf() val controller=ActionController(mediaFixtureIndex(width,height).actionPack,cancelBinding,{TestScheduler.now},{stop=it},statuses::add, { active->if(!active) resumed++ }) check(controller.offer(ActionPlan("p".repeat(24),7,"a".repeat(64),15000))) controller.cancel();TestScheduler.advanceBy(100) check(statuses.isEmpty() && controller.active && MediaPlayer.created.isEmpty());cases++ stop!!(confirmed);TestScheduler.advanceBy(100) check(statuses.single().status==if(confirmed) "cancelled" else "failed");cases++ check(MediaPlayer.created.isEmpty() && resumed==(if(confirmed) 1 else 0) && controller.active!=confirmed);cases++ cancelView.dispose();TestScheduler.advanceBy(100) } for(mode in listOf("complete","cancel","failed","background","late_final_frame")) { reset();val index=mediaFixtureIndex(width,height);val (actionView,actionBinding,trace)=rig(index) var stopped: ((Boolean)->Unit)?=null val statuses=mutableListOf() val controller=ActionController(index.actionPack,actionBinding,{TestScheduler.now},{stopped=it},statuses::add) check(controller.offer(ActionPlan("x".repeat(24),7,"a".repeat(64),15000))) TestScheduler.advanceBy(100);check(MediaPlayer.created.isEmpty());cases++ stopped!!(true);val movie=MediaPlayer.created.single() check(!movie.started);movie.prepared!!(movie) TestScheduler.until { movie.started } check(trace.any { it.clipId.startsWith("f96_") && it.actual=="action" } && trace.last().actual=="action_first_frame");cases++ val textures=actionView.children.filterIsInstance() fun latch(pts: Long) { val texture=checkNotNull(textures[3].surfaceTexture);texture.timestamp=pts*1000 textures[3].surfaceTextureListener!!.onSurfaceTextureUpdated(texture) } check(textures[3].alpha==1f);latch(0);check(textures[3].alpha==1f);cases++ when(mode) { "complete","late_final_frame" -> { index.actionPack!!.assets.first().media!!.ptsUs.drop(1).dropLast(if(mode=="late_final_frame") 1 else 0).forEach(::latch) TestScheduler.advanceBy(1000);movie.completion!!(movie) if(mode=="late_final_frame") { check(statuses.last().status=="started");latch(index.actionPack.assets.first().media!!.ptsUs.last()) } TestScheduler.until { statuses.last().status=="completed" } check(movie.released && trace.last().actual=="body_neutral" && trace.last().clipId.startsWith("f0_"));cases++ movie.completion!!(movie);check(statuses.count { it.status=="completed" }==1);cases++ } "cancel" -> { controller.cancel();TestScheduler.until { statuses.last().status=="cancelled" } check(movie.released && !movie.started && !controller.active);cases++ } "failed" -> { movie.error!!(movie,1,2);TestScheduler.until { statuses.last().status=="failed" } check(movie.released && !controller.active);cases++ } "background" -> { controller.cancel();actionView.render(false);TestScheduler.until { !controller.active } check(movie.released && driverField.get(actionView)==null);cases++ } } val before=statuses.size;movie.completion!!(movie);TestScheduler.advanceBy(10) check(statuses.size==before);cases++ actionView.dispose();TestScheduler.advanceBy(100) } // Ten seconds of real production held-renderer callbacks must never steal // the movie TextureView. Native/codec/record ports remain deterministic doubles. for(mode in listOf("complete", "cancel", "failed", "background", "newturn")) { reset() val base=mediaFixtureIndex(width,height) val original=base.actionPack!!.assets.first() val media=original.media!!.copy(ptsUs=List(241) { it*1_000_000L/24 },durationUs=10_041_667, sourceFrame=0,returnFrame=96,audioEndUs=10_000_000) val asset=original.copy(durationMs=10_042,media=media) val index=base.copy(actionPack=base.actionPack.copy(assets=listOf(asset,asset.copy(id="asset_02")))) val (v,b,trace)=rig(index) var confirmation: ((Boolean)->Unit)?=null val statuses=mutableListOf() val controller=ActionController(index.actionPack,b,{TestScheduler.now},{confirmation=it},statuses::add) check(controller.offer(ActionPlan("z".repeat(24),7,"a".repeat(64),25000))) confirmation!!(true);val player=MediaPlayer.created.single();val prepared=player.prepared!! prepared(player);TestScheduler.until { player.started } val textures=v.children.filterIsInstance() fun latch(pts: Long) { val t=textures[3].surfaceTexture!!;t.timestamp=pts*1000;textures[3].surfaceTextureListener!!.onSurfaceTextureUpdated(t) } val owner=driverField.get(v) as ConversationMediaDriver // First source frame was decoded+latched and held before native audio could start. TestScheduler.advanceBy(80);check(owner.layerOwner==ConversationMediaDriver.LayerOwner.ACTION && textures[3].alpha==1f);cases++ latch(0);TestScheduler.advanceBy(21) check(textures[3].alpha==1f && textures.take(3).all { it.alpha==0f } && player.started);cases++ prepared(player) // Duplicated ready cannot start/reclaim again. for(i in 1..240) { TestScheduler.advanceBy(42);latch(media.ptsUs[i]) check(textures[3].alpha==1f && textures.take(3).all { it.alpha==0f } && owner.layerOwner==ConversationMediaDriver.LayerOwner.ACTION);cases++ if(i==120) { v.render(true);check(driverField.get(v)===owner);cases++ } // Settings continuity. } check(statuses.none { it.status=="completed" });cases++ // Time alone is insufficient. when(mode) { "complete" -> { player.completion!!(player);player.completion!!(player) TestScheduler.until { statuses.last().status=="completed" } check(player.released && owner.layerOwner==ConversationMediaDriver.LayerOwner.CONVERSATION && trace.last().clipId.startsWith("f96_"));cases++ } "failed" -> { player.error!!(player,1,2);TestScheduler.until { !controller.active } } "background" -> { controller.cancel();v.render(false);TestScheduler.until { !controller.active } } "newturn" -> { b.beginTurn(8);controller.cancel();TestScheduler.until { !controller.active } } else -> { controller.cancel();TestScheduler.until { !controller.active } } } val before=statuses.size;val alphas=textures.map { it.alpha } prepared(player);player.completion!!(player);player.error!!(player,1,2) check(statuses.size==before && textures.map { it.alpha }==alphas && player.released);cases++ v.dispose();TestScheduler.advanceBy(100) check(owner.layerOwner==ConversationMediaDriver.LayerOwner.STOPPED);cases++ } // New idle rows use the actual view/renderer/codec path and reviewed graph. reset() val idleIndex=mediaFixtureIndex(width,height).let { it.copy(actionPack=null,visuals=it.visuals.map { p->p.copy(emotion=RoleEmotion.NEUTRAL) }) } val (idleView,idleBinding,idleTrace)=rig(idleIndex) TestScheduler.advanceBy(3000) check(idleTrace.all { it.actual=="body_neutral" });cases++ TestScheduler.until(15_000) { idleBinding.currentPackageId in setOf("happy","sad") } val firstIdle=idleBinding.currentPackageId // New turns on the actual displayed variant must retain its legal rotation. val turnBefore=idleTrace.size repeat(4) { i -> idleBinding.beginTurn(8L+i);idleBinding.intent(null,false);TestScheduler.advanceBy(200) } check(idleBinding.currentPackageId==firstIdle && idleTrace.drop(turnBefore).none { it.actual=="body_neutral" });cases++ check(idleTrace.any { it.actual=="bridge" } && idleBinding.visualStatus=="neutral_idle_variant");cases++ TestScheduler.until(15_000) { idleBinding.currentPackageId=="neutral" } TestScheduler.until(15_000) { idleBinding.currentPackageId in setOf("happy","sad") } check(idleBinding.currentPackageId!=firstIdle && idleTrace.none { it.actual=="media_failed" });cases++ idleBinding.intent(mediaReply(RoleEmotion.NEUTRAL,turn=11),true) TestScheduler.until(15_000) { idleBinding.currentPackageId=="neutral" } val speechStart=idleTrace.size;TestScheduler.advanceBy(9000) check(idleTrace.drop(speechStart).all { it.actual=="body_neutral" });cases++ val beforeDispose=idleTrace.size;idleView.dispose();TestScheduler.advanceBy(1000) check(idleTrace.size==beforeDispose && driverField.get(idleView)==null && CodecFixture.releases>=7);cases++ // Real renderer path over a25-frame own-mouth bridge, no loop or endpoint patches. reset() val ownBridge=dynamicPack(25,24,"f".repeat(64),width,height).copy(root="bridge25/") CodecFixture.timelines[ownBridge.root+ownBridge.body.path]=ownBridge.body.ptsUs val clock=ConversationMouthClock();val prior=Any() clock.intent(prior,ownBridge.curves,true,1,1,0);clock.sample(prior,0) TestScheduler.advanceBy(600);val priorPhase=clock.sample(prior,600).phase val composed=mutableListOf();var ended=false;val failures=mutableListOf() val renderer=ContinuousBodyRenderer(android.content.Context(),ownBridge,android.view.Surface(android.graphics.SurfaceTexture(5)),480,848, { frame,_->composed.add(frame) },failures::add,mayLoop=false,sharedMouth=clock,ended={ended=true}) renderer.intent(true,1,2) TestScheduler.until { ended || failures.isNotEmpty() } check(failures.isEmpty() && ended && composed.map { it.key.index }==(0..24).toList());cases++ check(composed.first().mouth.phase==priorPhase && composed.none { it.mouth.phase=="opening" });cases++ check(composed.all { it.patch===ownBridge.patches[it.key.index] && it.key.loopOrdinal==0L && it.selection.textureSlot/5==it.key.index });cases++ check(composed.any { it.selection.opacity==1f } && GLFixture.rendered.any { it.second==1f });cases++ renderer.close();TestScheduler.advanceBy(50) check(GLFixture.deleted>=126 && CodecFixture.releases==1);cases++ CodecFixture.reset();MediaPlayer.created.clear() println("PASS: $cases ${width}x$height actual EmotionActionAvatarView/binding/held GLES renderer/MediaPlayer callback assertions: hidden header/final mismatch, final slight,97→49→121 and24→30fps/native loops, real bridge frames/latest turn, settings continuity, mic-confirm/cancel-pending/failure gate, safe exit/music first+last PTS/native end/release/idle-A, completion order, cancel/error/background/stale callbacks; deterministic Android doubles, device/naturalness not_run") }