182 lines
13 KiB
Kotlin
182 lines
13 KiB
Kotlin
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(): EmotionActionIndex {
|
|
val packs=linkedMapOf("neutral" to dynamicPack(97,24,"c".repeat(64)).copy(root="neutral/"),
|
|
"happy" to dynamicPack(49,24,"d".repeat(64)).copy(root="happy/"),
|
|
"sad" to dynamicPack(121,30,"e".repeat(64)).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),480,848,3,125000,List(3) { it*1_000_000L/24 }),
|
|
mouthRuntime=dynamicPack(3,24,"f".repeat(64)).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,480,848,"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() {
|
|
var cases=0
|
|
fun reset() {
|
|
TestScheduler.reset();CodecFixture.reset();GLFixture.reset();BitmapFactory.wrongSize=false;MediaPlayer.created.clear()
|
|
CodecFixture.emitNativeSurface=true
|
|
}
|
|
fun rig(index: EmotionActionIndex=mediaFixtureIndex()): Triple<EmotionActionAvatarView,AvatarMediaBinding,MutableList<VideoPresentation>> {
|
|
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 }
|
|
val binding=AvatarMediaBinding();binding.beginTurn(7)
|
|
val reports=mutableListOf<VideoPresentation>()
|
|
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<ActionStatus>()
|
|
val controller=ActionController(mediaFixtureIndex().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();val (actionView,actionBinding,trace)=rig(index)
|
|
var stopped: ((Boolean)->Unit)?=null
|
|
val statuses=mutableListOf<ActionStatus>()
|
|
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.last().clipId.startsWith("f96_") && trace.last().actual=="action");cases++
|
|
val textures=actionView.children.filterIsInstance<TextureView>()
|
|
fun latch(pts: Long) { val texture=checkNotNull(textures[3].surfaceTexture);texture.timestamp=pts*1000
|
|
textures[3].surfaceTextureListener!!.onSurfaceTextureUpdated(texture) }
|
|
check(textures[3].alpha==0f);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)
|
|
}
|
|
// New idle rows use the actual view/renderer/codec path and reviewed graph.
|
|
reset()
|
|
val idleIndex=mediaFixtureIndex().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 { idleBinding.currentPackageId in setOf("happy","sad") }
|
|
val firstIdle=idleBinding.currentPackageId
|
|
check(idleTrace.any { it.actual=="bridge" } && idleBinding.visualStatus=="neutral_idle_variant");cases++
|
|
TestScheduler.until { idleBinding.currentPackageId=="neutral" }
|
|
TestScheduler.until { idleBinding.currentPackageId in setOf("happy","sad") }
|
|
check(idleBinding.currentPackageId!=firstIdle && idleTrace.none { it.actual=="media_failed" });cases++
|
|
idleBinding.intent(mediaReply(RoleEmotion.NEUTRAL),true)
|
|
TestScheduler.until { 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)).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<ComposedBodyFrame>();var ended=false;val failures=mutableListOf<String>()
|
|
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 actual EmotionActionAvatarView/binding/held GLES renderer/MediaPlayer callback cases: 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")
|
|
}
|