Merge remote-tracking branch 'upstream/main' into wallentx/game-debug-logging

This commit is contained in:
wallentx committed 2026-10-18 12:37:08 -05:00
commit cbd3d8c313
264 files changed
+24276 -1133

No files matched your search

@@ -0,0 +1,66 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
import java.util.Locale
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import com.droiddeck.launcher.R
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28])
class CalibrationDistanceCheckTest {
private val context get() = RuntimeEnvironment.getApplication()
private fun board(z: Float)=List(24) { i -> SpatialVector((i%4-1.5f)*.03f,(i/4-2.5f)*.03f,z) }
@Test
@Config(qualifiers = "fr")
fun rejectedCaptureExplainsTheProblemInTheSelectedLanguage() {
val result = CalibrationDistanceCheck.evaluate(context, .3048f, 3, null)
assertFalse(result.accepted)
assertEquals(context.getString(R.string.spatial_distance_corners, 3), result.message)
assertFalse(result.message.contains("checkerboard corners detected"))
assertTrue(result.message.contains("3 / 24"))
}
@Test fun missingAndPartialBoardsExplainVisibilityRatherThanDistance() {
for (count in listOf(0,12,23)) {
val result=CalibrationDistanceCheck.evaluate(context,.508f,count,null)
assertFalse(result.accepted)
assertTrue(result.message.contains("$count / 24"))
}
}
@Test fun fullBoardWithRejectedStereoGeometryCannotPass() {
assertFalse(CalibrationDistanceCheck.evaluate(context,.508f,24,null).accepted)
assertFalse(CalibrationDistanceCheck.evaluate(context,.508f,24,board(.508f).dropLast(1)).accepted)
assertFalse(CalibrationDistanceCheck.evaluate(context,.508f,24,board(.508f).toMutableList().also {
it[0]=SpatialVector(z=Float.NaN)
}).accepted)
}
@Test fun preservesFiveCentimeterToleranceAndReportsBothUnits() {
val locale=Locale.getDefault()
try {
Locale.setDefault(Locale.US)
assertTrue(CalibrationDistanceCheck.evaluate(context,.508f,24,board(.508f)).accepted)
assertTrue(CalibrationDistanceCheck.evaluate(context,.3048f,24,board(.3048f)).accepted)
assertFalse(CalibrationDistanceCheck.evaluate(context,.508f,24,board(.3048f)).accepted)
assertTrue(CalibrationDistanceCheck.evaluate(context,.508f,24,board(.557f)).accepted)
val result=CalibrationDistanceCheck.evaluate(context,.508f,24,board(.60f))
assertFalse(result.accepted)
assertTrue(result.message.contains("60.0 cm (23.62 in)"))
assertTrue(result.message.contains("50.8 cm (20.00 in)"))
} finally { Locale.setDefault(locale) }
}
@Test fun checksCameraToCenterDistanceRatherThanOnlyForwardDepth() {
val points=board(.4f).map { it+SpatialVector(x=.3f) }
assertTrue(CalibrationDistanceCheck.evaluate(context,.5f,24,points).accepted)
assertFalse(CalibrationDistanceCheck.evaluate(context,.4f,24,points).accepted)
}
@Test fun rejectsInvalidRequestedDistances() {
for(distance in listOf(0f,Float.NaN,Float.POSITIVE_INFINITY)) {
assertThrows(IllegalArgumentException::class.java) { CalibrationDistanceCheck.evaluate(context,distance,24,board(.5f)) }
}
}
}
@@ -0,0 +1,47 @@
package com.droiddeck.launcher.glasses
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk=[28],manifest=Config.NONE)
class CalibrationObservationDataTest {
private fun view(count: Int=24) = JSONObject().put("objects",JSONArray((0 until count).map {
JSONArray(listOf((it%4+1)*0.03,(it/4+1)*0.03,0.0))
})).put("left",JSONArray((0 until count).map { JSONArray(listOf(100.0+it,200.0)) }))
.put("right",JSONArray((0 until count).map { JSONArray(listOf(80.0+it,200.0)) }))
.put("head",JSONArray(listOf(0.1,0.2,0.3,1.0,0.0,0.0,0.0)))
@Test fun preservesPartialBoardCorrespondencesAndHeadPose() {
for(count in listOf(12,20,24)) {
val restored=CalibrationObservationData.parse(JSONObject(view(count).toString()))
assertEquals(count,restored.objects.size)
assertEquals(20.0,restored.left.last()[0]-restored.right.last()[0],0.0)
assertEquals(SpatialVector(.1f,.2f,.3f),restored.head.position)
assertEquals(SpatialRotation(),restored.head.rotation)
}
}
@Test fun rejectsTruncatedOrMismatchedCorrespondences() {
assertThrows(IllegalArgumentException::class.java) { CalibrationObservationData.parse(view(11)) }
assertThrows(IllegalArgumentException::class.java) { CalibrationObservationData.parse(view(25)) }
assertThrows(IllegalArgumentException::class.java) {
CalibrationObservationData.parse(view().put("right",view(20).getJSONArray("right")))
}
assertThrows(IllegalArgumentException::class.java) {
CalibrationObservationData.parse(view().also { it.getJSONArray("left").put(0,JSONArray(listOf(1.0))) })
}
}
@Test fun rejectsInvalidHeadRotationAndFloatOverflow() {
assertThrows(IllegalStateException::class.java) {
CalibrationObservationData.parse(view().put("head",JSONArray(listOf(0,0,0,0,0,0,0))))
}
assertThrows(IllegalStateException::class.java) {
CalibrationObservationData.parse(view().put("head",JSONArray(listOf(1e100,0,0,1,0,0,0))))
}
}
}
@@ -0,0 +1,450 @@
package com.droiddeck.launcher.glasses
import android.content.Context
import android.os.Handler
import android.os.Looper
import kotlinx.coroutines.flow.MutableStateFlow
import org.junit.Assert.*
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
@LooperMode(LooperMode.Mode.PAUSED)
class Glasses3dRoutingTest {
private val handler = Handler(Looper.getMainLooper())
private val commands = mutableListOf<Pair<Int, Boolean>>()
private val hardwareMessages = mutableListOf<Int>()
private lateinit var stateBefore: GlassesState
private lateinit var settingsBefore: GlassesSettings
private val supportedModels: List<GlassesModel> =
listOf("One", "Lite", "Pro", "Pro 2", "Luma", "Luma Pro", "Luma Cyber", "Luma Ultra", "Beast")
.map { VitureProtocol.sdkModel(it)!! } + listOf(
RayNeoModel.model(0x1bbb, 0xaf50, "RayNeo Air 4 Pro")!!,
RayNeoModel.model(0x3941, 0xaf50, "RayNeo GT")!!,
RayNeoModel.model(0x3941, 0xaf50, "RayNeo GT MAX")!!,
XrealModel,
)
private fun field(name: String) = Glasses::class.java.getDeclaredField(name).apply { isAccessible = true }
@Before fun prepare() {
stateBefore = Glasses.state.value
settingsBefore = Glasses.settings.value
val context = RuntimeEnvironment.getApplication()
context.getSharedPreferences("glasses", Context.MODE_PRIVATE).edit().clear().commit()
field("app").set(null, context)
field("handler").set(null, handler)
field("model").set(null, VitureProtocol.model(0x1019, null))
@Suppress("UNCHECKED_CAST")
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
state.value = GlassesState(connected = true, controllable = true,
displayId = 2, width = 1920, height = 1080)
@Suppress("UNCHECKED_CAST")
val settings = field("_settings").get(null) as MutableStateFlow<GlassesSettings>
settings.value = GlassesSettings(gameOnGlasses = false)
field("link").set(null, object : GlassesLink {
override val hasImu = false
override fun send(msgId: Int, payload: ByteArray): Boolean {
hardwareMessages += msgId
if (msgId == VitureProtocol.MSG_DISPLAY)
commands += (payload[0].toInt() and 255) to Glasses.settings.value.gameOnGlasses
return true
}
override fun close() {}
})
}
@After fun cleanUp() {
handler.removeCallbacksAndMessages(null)
field("link").set(null, null)
(field("modeState").get(null) as GlassesModeState).apply { manual(); sent(false, 0) }
@Suppress("UNCHECKED_CAST")
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
state.value = stateBefore
@Suppress("UNCHECKED_CAST")
val settings = field("_settings").get(null) as MutableStateFlow<GlassesSettings>
settings.value = settingsBefore
}
@Test fun lumaJoyConsDefaultOffPersistAndExcludeBareHands() {
assertFalse(GlassesSettings().joyConTracking)
@Suppress("UNCHECKED_CAST")
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
state.value = state.value.copy(supportsSpatial = true)
@Suppress("UNCHECKED_CAST")
val settings = field("_settings").get(null) as MutableStateFlow<GlassesSettings>
Glasses.setJoyConTracking(true)
shadowOf(Looper.getMainLooper()).idle()
assertFalse(Glasses.settings.value.joyConTracking)
settings.value = settings.value.copy(positionalTracking = true, handControllers = true)
Glasses.setJoyConTracking(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.settings.value.joyConTracking)
assertFalse(Glasses.settings.value.handControllers)
val prefs = RuntimeEnvironment.getApplication().getSharedPreferences("glasses", Context.MODE_PRIVATE)
assertTrue(prefs.getBoolean("joyConTracking", false))
val loaded = Glasses::class.java.getDeclaredMethod("load").apply { isAccessible = true }.invoke(Glasses) as GlassesSettings
assertTrue(loaded.joyConTracking)
Glasses.setHandControllers(true)
shadowOf(Looper.getMainLooper()).idle()
assertFalse(Glasses.settings.value.joyConTracking)
assertTrue(Glasses.settings.value.handControllers)
SpatialTracking.setControllers(false, false)
}
@Test fun otherGlassesRejectSpatialEnableAndRestartWithoutClosingTheirLink() {
val before = Glasses.settings.value
val link = field("link").get(null)
Glasses.setPositionalTracking(true)
Glasses.restartSpatialTracking()
shadowOf(Looper.getMainLooper()).idle()
assertEquals(before, Glasses.settings.value)
assertSame(link, field("link").get(null))
assertTrue(hardwareMessages.isEmpty())
}
@Test fun manual3dSelectsAndSavesGlassesOutputBeforeSwitchingTheDisplay() {
Glasses.set3d(true)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x32 to true), commands)
assertTrue(Glasses.settings.value.gameOnGlasses)
val prefs = RuntimeEnvironment.getApplication().getSharedPreferences("glasses", Context.MODE_PRIVATE)
assertTrue(prefs.getBoolean("gameOnGlasses", false))
}
@Test fun nativeThirtyHzIsSavedWithoutSendingAFabricatedHardwareMode() {
@Suppress("UNCHECKED_CAST")
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
Glasses.setGameOnGlasses(true)
shadowOf(Looper.getMainLooper()).idle()
state.value = state.value.copy(supports30Hz = true)
Glasses.setRefreshHz(30)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(30, Glasses.settings.value.refreshHz)
assertTrue(hardwareMessages.isEmpty())
state.value = state.value.copy(sbs = true, width = 3840)
Glasses.setSbsHz(30)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(30, Glasses.settings.value.sbsHz)
assertTrue(hardwareMessages.isEmpty())
val saved = Glasses::class.java.getDeclaredMethod("load").apply { isAccessible = true }
.invoke(Glasses) as GlassesSettings
assertEquals(30, saved.refreshHz)
assertEquals(30, saved.sbsHz)
}
@Test fun unsupportedThirtyHzRequestsLeavePreferencesAndHardwareUnchanged() {
val before = Glasses.settings.value
Glasses.setRefreshHz(30)
Glasses.setSbsHz(30)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(before, Glasses.settings.value)
assertTrue(hardwareMessages.isEmpty())
}
@Test fun automaticSteamVr3dAlsoSelectsGlassesOutput() {
Glasses.set3dForVr(true)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x32 to true), commands)
assertTrue(Glasses.settings.value.gameOnGlasses)
}
@Test fun returningTo2dKeepsTheGameOnGlasses() {
Glasses.set3d(true)
shadowOf(Looper.getMainLooper()).idle()
Glasses.set3d(false)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x32 to true, 0x34 to true), commands)
assertTrue(Glasses.settings.value.gameOnGlasses)
}
@Test fun turningOff3dDoesNotEnableGlassesOutput() {
Glasses.set3d(false)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x34 to false), commands)
assertFalse(Glasses.settings.value.gameOnGlasses)
}
@Test fun turningOffGlassesOutputRestores2dOnEverySupportedModel() {
for (model in supportedModels) {
selectModel(model)
Glasses.set3dof(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.settings.value.screen3dof)
@Suppress("UNCHECKED_CAST")
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
state.value = state.value.copy(sbs = true, width = 3840)
commands.clear()
Glasses.setGameOnGlasses(false)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(model.mode(120, false) to true), commands)
assertFalse(Glasses.settings.value.gameOnGlasses)
assertFalse(Glasses.settings.value.screen3dof)
assertEquals(GlassesSettings.PICTURE_AUTO, Glasses.settings.value.picture3d)
val prefs = RuntimeEnvironment.getApplication().getSharedPreferences("glasses", Context.MODE_PRIVATE)
assertFalse(prefs.getBoolean("gameOnGlasses", true))
}
}
@Test fun turningOffGlassesOutputCancelsAnUnconfirmedStereoSwitch() {
Glasses.set3d(true)
shadowOf(Looper.getMainLooper()).idle()
assertFalse(Glasses.state.value.sbs)
assertTrue(Glasses.state.value.switching3d)
Glasses.setGameOnGlasses(false)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x32 to true, 0x34 to true), commands)
assertFalse(Glasses.state.value.switching3d)
assertFalse(Glasses.settings.value.gameOnGlasses)
}
@Test fun turningOffGlassesOutputTakesOwnershipFromAutomaticVr() {
Glasses.set3dForVr(true)
shadowOf(Looper.getMainLooper()).idle()
Glasses.setGameOnGlasses(false)
shadowOf(Looper.getMainLooper()).idle()
Glasses.set3dForVr(false)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x32 to true, 0x34 to true), commands)
assertFalse(Glasses.state.value.switching3d)
assertFalse(Glasses.settings.value.gameOnGlasses)
}
@Test fun togglingGlassesOutputIn2dDoesNotSendDisplayCommands() {
Glasses.setGameOnGlasses(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.settings.value.gameOnGlasses)
Glasses.setGameOnGlasses(false)
shadowOf(Looper.getMainLooper()).idle()
assertFalse(Glasses.settings.value.gameOnGlasses)
assertTrue(hardwareMessages.isEmpty())
}
@Test fun turningOffGlassesOutputDisablesVirtualScreenAndSavesTheChoice() {
selectModel(supportedModels.first())
Glasses.set3dof(true)
shadowOf(Looper.getMainLooper()).idle()
Glasses.setGameOnGlasses(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.settings.value.screen3dof)
Glasses.setGameOnGlasses(false)
shadowOf(Looper.getMainLooper()).idle()
assertFalse(Glasses.settings.value.screen3dof)
assertEquals(GlassesSettings.PICTURE_AUTO, Glasses.settings.value.picture3d)
val saved = Glasses::class.java.getDeclaredMethod("load").apply { isAccessible = true }
.invoke(Glasses) as GlassesSettings
assertFalse(saved.gameOnGlasses)
assertFalse(saved.screen3dof)
assertEquals(GlassesSettings.PICTURE_AUTO, saved.picture3d)
Glasses.setGameOnGlasses(true)
shadowOf(Looper.getMainLooper()).idle()
assertFalse(Glasses.settings.value.screen3dof)
assertTrue(hardwareMessages.isEmpty())
}
@Test fun turningOffGlassesOutputDisablesVirtualScreenWithoutDeviceControl() {
selectModel(supportedModels.first())
Glasses.set3dof(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.settings.value.screen3dof)
@Suppress("UNCHECKED_CAST")
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
state.value = state.value.copy(connected = false, supportsScreen3dof = false)
Glasses.setGameOnGlasses(false)
shadowOf(Looper.getMainLooper()).idle()
assertFalse(Glasses.settings.value.gameOnGlasses)
assertFalse(Glasses.settings.value.screen3dof)
assertEquals(GlassesSettings.PICTURE_AUTO, Glasses.settings.value.picture3d)
assertTrue(hardwareMessages.isEmpty())
}
@Test fun enabling3dofRoutesFlatContentWithoutSendingAStereoCommandAndSurvivesReload() {
for (model in supportedModels) {
selectModel(model)
Glasses.set3dof(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(hardwareMessages.isEmpty())
assertFalse(Glasses.state.value.sbs)
assertFalse(Glasses.state.value.switching3d)
assertTrue(Glasses.settings.value.screen3dof)
assertTrue(Glasses.settings.value.gameOnGlasses)
val saved = Glasses::class.java.getDeclaredMethod("load").apply { isAccessible = true }
.invoke(Glasses) as GlassesSettings
assertTrue(saved.screen3dof)
assertTrue(saved.gameOnGlasses)
}
}
@Test fun stereoAnd3dofCanBeToggledIndependentlyOnEverySupportedModel() {
for (model in supportedModels) {
selectModel(model)
Glasses.set3dof(true)
Glasses.set3d(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.settings.value.screen3dof)
Glasses.set3d(false)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.settings.value.screen3dof)
assertEquals(listOf(model.mode(60, true) to true, model.mode(120, false) to true), commands)
commands.clear()
hardwareMessages.clear()
Glasses.set3dof(false)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(hardwareMessages.isEmpty())
assertFalse(Glasses.settings.value.screen3dof)
assertEquals(GlassesSettings.PICTURE_AUTO, Glasses.settings.value.picture3d)
assertTrue(Glasses.settings.value.gameOnGlasses)
}
}
@Test fun enabling3dofWithoutHeadTrackingSupportLeavesSettingsUnchanged() {
val before = Glasses.settings.value
Glasses.set3dof(true)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(before, Glasses.settings.value)
assertTrue(hardwareMessages.isEmpty())
}
@Test fun resolutionsPersistIndependentlyAndOnlyTheActiveProfileChangesHardware() {
selectModel(VitureProtocol.sdkModel("Luma Ultra")!!)
Glasses.setResolution(GlassesDisplayProfile.THREE_D, "1920x1080")
Glasses.setResolution(GlassesDisplayProfile.VR, "1920x1200")
shadowOf(Looper.getMainLooper()).idle()
assertTrue(hardwareMessages.isEmpty())
Glasses.setResolution(GlassesDisplayProfile.TWO_D, "1920x1080")
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x34 to false), commands)
assertEquals(120, Glasses.settings.value.refreshHz)
assertEquals(60, Glasses.settings.value.sbsHz)
val saved = Glasses::class.java.getDeclaredMethod("load").apply { isAccessible = true }
.invoke(Glasses) as GlassesSettings
assertEquals("1920x1080", saved.resolution2d)
assertEquals("1920x1080", saved.resolution3d)
assertEquals("1920x1200", saved.resolutionVr)
assertEquals(listOf(VitureProtocol.MSG_DISPLAY), hardwareMessages)
}
@Test fun vrResolutionIsAppliedAndTheOrdinaryResolutionIsRestoredAfterStopping() {
selectModel(VitureProtocol.sdkModel("Luma Ultra")!!)
Glasses.setResolution(GlassesDisplayProfile.TWO_D, "1920x1080")
Glasses.setResolution(GlassesDisplayProfile.VR, "1920x1200")
shadowOf(Looper.getMainLooper()).idle()
commands.clear()
Glasses.setVrActive(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.state.value.vrActive)
assertEquals(listOf(0x44 to false), commands)
Glasses.setVrActive(false)
shadowOf(Looper.getMainLooper()).idle()
assertFalse(Glasses.state.value.vrActive)
assertEquals(listOf(0x44 to false, 0x34 to false), commands)
}
@Test fun vrResolutionUsesStereoCommandsAndRestoresThePreVrLayoutOnce() {
selectModel(VitureProtocol.sdkModel("Luma Ultra")!!)
Glasses.setGameOnGlasses(true)
Glasses.setAuto3d(true)
Glasses.setResolution(GlassesDisplayProfile.VR, "1920x1080")
shadowOf(Looper.getMainLooper()).idle()
Glasses.setVrActive(true)
Glasses.set3dForVr(true)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x32 to true), commands)
@Suppress("UNCHECKED_CAST")
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
state.value = state.value.copy(sbs = true, width = 3840, switching3d = false)
(field("modeState").get(null) as GlassesModeState).observe(true, 0)
Glasses.setSbsHz(90)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(0x35 to true, commands.last())
Glasses.setVrActive(false)
Glasses.set3dForVr(false)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x32 to true, 0x35 to true, 0x44 to true), commands)
}
@Test fun invalidResolutionCannotChangePreferencesOrSendDeviceCommands() {
selectModel(VitureProtocol.sdkModel("Pro")!!)
val before = Glasses.settings.value
for (value in listOf("1920x1200", "3840x1080", "-1x1080", "unknown"))
Glasses.setResolution(GlassesDisplayProfile.TWO_D, value)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(before, Glasses.settings.value)
assertTrue(hardwareMessages.isEmpty())
}
@Test fun startingVrDuringAStereoSwitchDoesNotSendAFlatMode() {
selectModel(VitureProtocol.sdkModel("Luma Ultra")!!)
Glasses.setResolution(GlassesDisplayProfile.VR, "1920x1080")
Glasses.set3d(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.state.value.switching3d)
Glasses.setVrActive(true)
shadowOf(Looper.getMainLooper()).idle()
assertEquals(listOf(0x42 to true, 0x32 to true), commands)
}
@Test fun reconnectRestoresASavedResolutionEvenWhenRefreshAlreadyMatches() {
selectModel(VitureProtocol.sdkModel("Luma Ultra")!!)
Glasses.setResolution(GlassesDisplayProfile.TWO_D, "1920x1080")
shadowOf(Looper.getMainLooper()).idle()
@Suppress("UNCHECKED_CAST")
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
state.value = state.value.copy(width = 1920, height = 1200, hz = 120f)
field("glassesMode").setInt(null, 0)
field("autoModeSent").setBoolean(null, false)
field("lastModeSentAt").setLong(null, -10_000L)
commands.clear()
Glasses::class.java.getDeclaredMethod("applyMode", Boolean::class.javaPrimitiveType)
.apply { isAccessible = true }.invoke(Glasses, false)
assertEquals(listOf(0x34 to false), commands)
}
@Test fun defaultVrProfileDoesNotSendExtraDisplayCommands() {
selectModel(VitureProtocol.sdkModel("Luma Ultra")!!)
Glasses.setVrActive(true)
Glasses.setVrActive(false)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(hardwareMessages.isEmpty())
}
private fun selectModel(model: GlassesModel) {
field("model").set(null, model)
@Suppress("UNCHECKED_CAST")
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
state.value = state.value.copy(model = model.name, supportsScreen3dof = true, resolutions = model.resolutions)
@Suppress("UNCHECKED_CAST")
val settings = field("_settings").get(null) as MutableStateFlow<GlassesSettings>
settings.value = GlassesSettings(gameOnGlasses = false, headTracking = false)
assertFalse(settings.value.screen3dof)
}
}
@@ -0,0 +1,62 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class GlassesDisplayModesTest {
private val fullHd = GlassesResolution(1920, 1080)
private val tall = GlassesResolution(1920, 1200)
private val ultra = VitureProtocol.sdkModel("Luma Ultra")!!
private val state = GlassesState(resolutions = ultra.resolutions)
@Test fun separateProfilesRetainTheirOwnResolutions() {
val settings = GlassesSettings(resolution2d = fullHd.key, resolution3d = tall.key, resolutionVr = fullHd.key)
assertEquals(fullHd, GlassesDisplayModes.selected(settings, state))
assertEquals(tall, GlassesDisplayModes.selected(settings, state.copy(sbs = true)))
for (stereo in listOf(false, true))
assertEquals(fullHd, GlassesDisplayModes.selected(settings, state.copy(sbs = stereo, vrActive = true)))
}
@Test fun automaticVrInheritsTheActiveLayoutAndDefaultsKeepNativeBehavior() {
val settings = GlassesSettings(resolution2d = fullHd.key, resolution3d = tall.key)
assertEquals(fullHd, GlassesDisplayModes.selected(settings, state.copy(vrActive = true)))
assertEquals(tall, GlassesDisplayModes.selected(settings, state.copy(sbs = true, vrActive = true)))
assertNull(GlassesDisplayModes.selected(GlassesSettings(), state))
assertNull(GlassesDisplayModes.selected(GlassesSettings(), state.copy(sbs = true, vrActive = true)))
}
@Test fun anotherHeadsetDoesNotReceiveAnUnsupportedSavedResolution() {
val settings = GlassesSettings(resolution2d = tall.key, resolutionVr = tall.key)
assertNull(GlassesDisplayModes.selected(settings, state.copy(resolutions = listOf(fullHd))))
assertNull(GlassesDisplayModes.selected(settings, state.copy(resolutions = emptyList(), vrActive = true)))
assertEquals(tall, GlassesDisplayModes.selected(settings, state))
}
@Test fun ultraResolutionsMapToVendorCommandsAtEverySupportedRefresh() {
for ((hz, code) in listOf(60 to 0x31, 90 to 0x33, 120 to 0x34)) {
assertEquals(code, ultra.mode(hz, false, fullHd))
assertEquals(code + 0x10, ultra.mode(hz, false, tall))
}
for ((hz, code) in listOf(60 to 0x32, 90 to 0x35)) {
assertEquals(code, ultra.mode(hz, true, fullHd))
assertEquals(code + 0x10, ultra.mode(hz, true, tall))
}
assertEquals(0x41, ultra.mode(30, false, tall))
assertEquals(0x42, ultra.mode(30, true, tall))
}
@Test fun olderModelsKeepTheirNativeResolutionAndStereoRateLimits() {
for (name in listOf("One", "Pro", "Pro 2")) {
val model = VitureProtocol.sdkModel(name)!!
assertEquals(listOf(fullHd), model.resolutions)
assertEquals(0x34, model.mode(120, false, tall))
assertEquals(0x32, model.mode(90, true, tall))
}
for (name in listOf("Luma", "Luma Pro", "Luma Ultra", "Beast")) {
val model = VitureProtocol.sdkModel(name)!!
assertEquals(listOf(fullHd, tall), model.resolutions)
assertEquals(0x44, model.mode(120, false))
assertEquals(if (model.sbs90) 0x45 else 0x42, model.mode(90, true))
}
}
}
@@ -0,0 +1,149 @@
package com.droiddeck.launcher.glasses
import android.app.Activity
import android.app.Application
import android.app.Dialog
import android.app.Presentation
import android.graphics.SurfaceTexture
import android.hardware.display.DisplayManager
import android.view.Choreographer
import android.view.Surface
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.android.controller.ActivityController
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import org.robolectric.shadows.ShadowDisplayManager
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [29], manifest = Config.NONE, application = Application::class)
@LooperMode(LooperMode.Mode.PAUSED)
class GlassesMenuCaptureTest {
private lateinit var controller: ActivityController<Activity>
private lateinit var activity: Activity
private val dialogs = mutableListOf<Dialog>()
@Before fun setUp() {
controller = Robolectric.buildActivity(Activity::class.java).setup().visible()
activity = controller.get()
activity.window.decorView.layout(0, 0, 1920, 1080)
}
@After fun tearDown() {
dialogs.forEach { it.dismiss() }
controller.pause().stop().destroy()
}
@Test fun onlyTheActivityAndItsOwnDialogAreIncludedOnce() {
val own = dialog(activity)
val otherController = Robolectric.buildActivity(Activity::class.java).setup().visible()
val other = otherController.get()
val otherDialog = dialog(other)
try {
val decor = activity.window.decorView
val roots = GlassesMenuCapture.captureRoots(activity,
listOf(other.window.decorView, own, otherDialog, decor, own))
assertEquals(listOf(decor, own), roots)
} finally {
dialogs.removeAll { if (it.ownerActivity === other) { it.dismiss(); true } else false }
otherController.pause().stop().destroy()
}
}
@Test fun secureActivityProducesNoRootsAndSecureDialogsAreExcluded() {
val secure = dialog(activity)
val params = secure.layoutParams as WindowManager.LayoutParams
params.flags = params.flags or WindowManager.LayoutParams.FLAG_SECURE
assertEquals(listOf(activity.window.decorView), GlassesMenuCapture.captureRoots(activity, listOf(secure)))
activity.window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
assertTrue(GlassesMenuCapture.captureRoots(activity, listOf(secure)).isEmpty())
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
assertEquals(listOf(activity.window.decorView), GlassesMenuCapture.captureRoots(activity, listOf(secure)))
}
@Test fun systemWindowsAndPresentationsOnOtherDisplaysAreExcluded() {
val own = dialog(activity)
(own.layoutParams as WindowManager.LayoutParams).type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
val displayId = ShadowDisplayManager.addDisplay("w1920dp-h1080dp")
val display = activity.getSystemService(DisplayManager::class.java).getDisplay(displayId)
val presentation = Presentation(activity, display).apply {
setContentView(FrameLayout(context))
show()
}.also(dialogs::add)
try {
assertEquals(listOf(activity.window.decorView), GlassesMenuCapture.captureRoots(activity,
listOf(own, presentation.window!!.decorView)))
} finally {
presentation.dismiss()
dialogs.remove(presentation)
ShadowDisplayManager.removeDisplay(displayId)
}
}
@Test
@Config(sdk = [26])
fun olderAndroidCapturesOnlyTheActivityDecor() {
val own = dialog(activity)
assertEquals(listOf(activity.window.decorView), GlassesMenuCapture.captureRoots(activity, listOf(own)))
}
@Test fun captureSizesPreserveOrientationAndNeverUpscale() {
assertEquals(1920 to 1080, GlassesMenuCapture.captureSize(3840, 2160))
assertEquals(1080 to 1920, GlassesMenuCapture.captureSize(2160, 3840))
assertEquals(1200 to 1800, GlassesMenuCapture.captureSize(1200, 1800))
val fold = GlassesMenuCapture.captureSize(2152, 1812)!!
assertEquals(1920, fold.first)
assertEquals(2152.0 / 1812.0, fold.first.toDouble() / fold.second, 0.001)
assertNull(GlassesMenuCapture.captureSize(0, 1000))
assertNull(GlassesMenuCapture.captureSize(1000, -1))
}
@Test fun repeatedSurfaceFailureStopsOnceAndDoesNotRetryAfterStop() {
var sizes = 0
var failures = 0
val capture = GlassesMenuCapture(activity, { _, _ -> sizes++ }, { failures++ })
val input = SurfaceTexture(0)
val target = Surface(input)
target.release()
try {
capture.attach(input, target)
capture.start()
for (time in listOf(1L, 33_333_335L, 66_666_669L, 100_000_003L)) {
Choreographer.getInstance().removeFrameCallback(capture)
capture.doFrame(time)
}
assertEquals(1, failures)
assertEquals(0, sizes)
assertFalse(pending(capture))
} finally {
capture.stop()
input.release()
}
}
@Test fun stoppingRejectsAnAlreadyDispatchedFrameWithoutRescheduling() {
val capture = GlassesMenuCapture(activity, { _, _ -> fail("Stopped capture changed its size") })
capture.start()
capture.stop()
capture.doFrame(1L)
assertFalse(pending(capture))
}
private fun dialog(owner: Activity): View = Dialog(owner).apply {
setOwnerActivity(owner)
setContentView(FrameLayout(context))
show()
}.also(dialogs::add).window!!.decorView
private fun pending(capture: GlassesMenuCapture): Boolean =
GlassesMenuCapture::class.java.getDeclaredField("framePending").apply { isAccessible = true }.getBoolean(capture)
}
@@ -0,0 +1,340 @@
package com.droiddeck.launcher.glasses
import android.app.Activity
import android.app.Application
import android.hardware.display.DisplayManager
import android.view.Display
import android.view.WindowManager
import com.droiddeck.launcher.QuestXrActivity
import com.droiddeck.launcher.SessionActivity
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.android.controller.ActivityController
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import org.robolectric.shadows.ShadowDisplayManager
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class, manifest = Config.NONE)
@LooperMode(LooperMode.Mode.PAUSED)
class GlassesMenuControllerTest {
private lateinit var owner: Activity
private lateinit var ownerLifecycle: ActivityController<Activity>
private lateinit var external: Display
private lateinit var menus: GlassesMenuController
private val displays = mutableListOf<Int>()
private val outputs = mutableListOf<FakeOutput>()
private val subscriptions = mutableListOf<Boolean>()
private val scheduled = ArrayDeque<() -> Unit>()
private var target: Display? = null
private var creationError: RuntimeException? = null
private var onCreateOutput: (FakeOutput) -> Unit = {}
private val saved = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN,
headTracking = true, screenWidthDm = 35, screenDistanceDm = 45)
private lateinit var glasses: GlassesState
@Before fun setUp() {
ownerLifecycle = Robolectric.buildActivity(Activity::class.java).setup()
owner = ownerLifecycle.get()
external = addDisplay()
target = external
glasses = GlassesState(present = true, connected = true, controllable = true,
supportsScreen3dof = true, displayId = external.displayId, width = 1920, height = 1080)
menus = GlassesMenuController(GlassesMenuPose(spatialEnabled = { false }),
display = { target }, observe = { subscriptions += it },
create = { activity, display, failure ->
creationError?.let { throw it }
FakeOutput(activity, display, failure).also {
outputs += it
onCreateOutput(it)
}
}, schedule = { scheduled.addLast(it) })
menus.update(glasses, saved)
}
@After fun tearDown() {
menus.pause(owner)
ownerLifecycle.pause().stop().destroy()
displays.forEach(ShadowDisplayManager::removeDisplay)
}
@Test fun menusStartOnlyForSupportedControlledExternalOutputWithTrackingSelected() {
val invalid = listOf(
glasses.copy(connected = false) to saved,
glasses.copy(controllable = false) to saved,
glasses.copy(supportsScreen3dof = false) to saved,
glasses.copy(noSignal = true) to saved,
glasses.copy(displayId = Display.INVALID_DISPLAY) to saved,
glasses to saved.copy(gameOnGlasses = false),
glasses to saved.copy(headTracking = false),
glasses to saved.copy(picture3d = GlassesSettings.PICTURE_AUTO),
)
for ((state, preferences) in invalid) {
menus.update(state, preferences)
menus.resume(owner)
assertTrue(outputs.isEmpty())
assertTrue(subscriptions.isEmpty())
}
menus.update(glasses, saved)
assertEquals(1, outputs.size)
assertEquals(listOf(true), subscriptions)
assertEquals(1, outputs.single().shows)
assertEquals(saved, outputs.single().settings)
}
@Test fun absentDisplayAndThePhoneDisplayNeverCreateAnOutput() {
target = null
menus.resume(owner)
target = owner.windowManager.defaultDisplay
menus.update(glasses, saved)
assertTrue(outputs.isEmpty())
assertTrue(subscriptions.isEmpty())
target = external
menus.update(glasses, saved)
assertEquals(1, outputs.size)
}
@Test fun sessionContentIsExcludedUntilItsMenuIsVisibleAndStalePauseCannotCloseNewOwner() {
menus.resume(owner)
val first = outputs.single()
val session = Robolectric.buildActivity(SessionActivity::class.java).get()
menus.resume(session)
assertEquals(1, first.closes)
assertEquals(1, outputs.size)
menus.setSessionMenuVisible(session, true)
val overlay = outputs.last()
assertSame(session, overlay.owner)
assertEquals(2, outputs.size)
menus.setSessionMenuVisible(session, false)
assertEquals(1, overlay.closes)
menus.resume(owner)
val latest = outputs.last()
menus.pause(session)
menus.setSessionMenuVisible(session, true)
assertEquals(0, latest.closes)
assertEquals(3, outputs.size)
}
@Test fun pauseReleasesSubscriptionAndLateFailureCannotCloseAReplacement() {
menus.resume(owner)
val old = outputs.single()
menus.pause(owner)
menus.pause(owner)
assertEquals(1, old.closes)
assertEquals(listOf(true, false), subscriptions)
menus.resume(owner)
val current = outputs.last()
old.fail(IllegalStateException("late GL callback"))
assertEquals(0, current.closes)
assertEquals(2, outputs.size)
menus.pause(owner)
assertEquals(1, current.closes)
assertFalse(subscriptions.last())
}
@Test fun destroyedActivityCannotRecreateItsOutputAfterCleanup() {
val lifecycle = Robolectric.buildActivity(Activity::class.java).setup()
val destroyed = lifecycle.get()
menus.resume(destroyed)
val output = outputs.single()
lifecycle.pause().stop().destroy()
menus.pause(destroyed)
menus.resume(destroyed)
assertEquals(1, output.closes)
assertEquals(1, outputs.size)
assertFalse(subscriptions.last())
menus.pause(destroyed)
}
@Test fun secureFinishingAndImmersiveQuestActivitiesCannotBeCaptured() {
owner.window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
menus.resume(owner)
assertTrue(outputs.isEmpty())
owner.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
val quest = Robolectric.buildActivity(QuestXrActivity::class.java).get()
menus.resume(quest)
assertTrue(outputs.isEmpty())
owner.finish()
menus.resume(owner)
assertTrue(outputs.isEmpty())
}
@Test fun settingsReconfigureTheSameOutputAndDisablingTrackingClosesIt() {
menus.resume(owner)
val output = outputs.single()
val changed = saved.copy(screenWidthDm = 50, screenDistanceDm = 60, positionalTracking = true)
menus.update(glasses.copy(sbs = true), changed)
assertEquals(1, outputs.size)
assertEquals(2, output.configurations)
assertEquals(changed, output.settings)
assertTrue(output.state!!.sbs)
assertEquals(listOf(true), subscriptions)
menus.update(glasses, changed.copy(headTracking = false))
assertEquals(1, output.closes)
assertFalse(subscriptions.last())
menus.update(glasses, changed)
assertEquals(2, outputs.size)
assertTrue(subscriptions.last())
}
@Test fun displayRemovalAndReplacementCloseOldOutputAndRebindOnlyTheCurrentDisplay() {
menus.resume(owner)
val old = outputs.single()
target = null
menus.update(glasses.copy(displayId = Display.INVALID_DISPLAY), saved)
assertEquals(1, old.closes)
assertFalse(subscriptions.last())
target = addDisplay()
menus.update(glasses.copy(displayId = target!!.displayId), saved)
assertEquals(2, outputs.size)
assertSame(target, outputs.last().display)
old.fail(IllegalStateException("removed display"))
assertEquals(0, outputs.last().closes)
}
@Test fun creationFailureIsLatchedUntilPreferencesChangeWithoutSubscribing() {
creationError = IllegalStateException("no EGL display")
menus.resume(owner)
assertTrue(outputs.isEmpty())
assertTrue(subscriptions.isEmpty())
creationError = null
menus.update(glasses, saved)
assertTrue(outputs.isEmpty())
menus.update(glasses, saved.copy(screenWidthDm = 40))
assertEquals(1, outputs.size)
assertTrue(subscriptions.last())
}
@Test fun configureAndShowExceptionsReleaseOutputAndDoNotEscapeUpdates() {
onCreateOutput = { it.showError = IllegalStateException("window removed") }
menus.resume(owner)
assertEquals(1, outputs.single().closes)
assertEquals(listOf(true, false), subscriptions)
onCreateOutput = {}
menus.update(glasses, saved.copy(screenWidthDm = 40))
val next = outputs.last()
next.onConfigure = { throw IllegalStateException("surface lost") }
menus.update(glasses, saved)
assertEquals(1, next.closes)
assertFalse(subscriptions.last())
menus.update(glasses, saved)
assertEquals(2, outputs.size)
}
@Test fun synchronousFactoryOrConfigureFailureCannotShowOrSubscribeAClosedOutput() {
onCreateOutput = { it.fail(IllegalStateException("creation callback")) }
menus.resume(owner)
assertEquals(0, outputs.single().shows)
assertEquals(1, outputs.single().closes)
assertFalse(subscriptions.contains(true))
onCreateOutput = { output -> output.onConfigure = { output.fail(IllegalStateException("configure callback")) } }
menus.update(glasses, saved.copy(screenWidthDm = 40))
assertEquals(2, outputs.size)
assertEquals(0, outputs.last().shows)
assertEquals(1, outputs.last().closes)
assertFalse(subscriptions.contains(true))
}
@Test fun closeFailureStillReleasesObserverAndDoesNotBlockTheNextResume() {
menus.resume(owner)
outputs.single().closeError = IllegalStateException("already destroyed surface")
menus.pause(owner)
assertFalse(subscriptions.last())
menus.resume(owner)
assertEquals(2, outputs.size)
assertEquals(1, outputs.last().shows)
}
@Test fun unexpectedDismissalGetsOneScheduledRetryAndStopsAfterASecondFailure() {
menus.resume(owner)
val old = outputs.single()
old.fail(GlassesMenuDisplayStopped())
assertEquals(1, old.closes)
assertFalse(subscriptions.last())
assertEquals(1, scheduled.size)
assertEquals(1, outputs.size)
scheduled.removeFirst().invoke()
assertEquals(2, outputs.size)
assertTrue(subscriptions.last())
outputs.last().fail(GlassesMenuDisplayStopped())
assertTrue(scheduled.isEmpty())
assertFalse(subscriptions.last())
menus.update(glasses, saved)
assertEquals(2, outputs.size)
menus.update(glasses, saved.copy(screenWidthDm = 40))
assertEquals(3, outputs.size)
outputs.last().fail(GlassesMenuDisplayStopped())
assertEquals(1, scheduled.size)
}
@Test fun scheduledDismissalRecoveryCannotReopenAfterPauseOrReplaceANewerOutput() {
menus.resume(owner)
outputs.single().fail(GlassesMenuDisplayStopped())
menus.pause(owner)
scheduled.removeFirst().invoke()
assertEquals(1, outputs.size)
assertFalse(subscriptions.last())
menus.resume(owner)
outputs.last().fail(GlassesMenuDisplayStopped())
menus.pause(owner)
menus.resume(owner)
val replacement = outputs.last()
scheduled.removeFirst().invoke()
assertEquals(3, outputs.size)
assertEquals(0, replacement.closes)
assertTrue(subscriptions.last())
}
@Test fun displayResizeAllowsRetryAfterARealRendererFailureWithoutRetryingEveryStateUpdate() {
menus.resume(owner)
outputs.single().fail(IllegalStateException("old surface size"))
assertTrue(scheduled.isEmpty())
menus.update(glasses.copy(tracking = true), saved)
assertEquals(1, outputs.size)
menus.update(glasses.copy(width = 3840, sbs = true), saved)
assertEquals(2, outputs.size)
assertEquals(3840, outputs.last().state!!.width)
assertTrue(subscriptions.last())
}
private fun addDisplay(): Display {
val id = ShadowDisplayManager.addDisplay("w1920dp-h1080dp")
displays += id
return RuntimeEnvironment.getApplication().getSystemService(DisplayManager::class.java).getDisplay(id)
}
private class FakeOutput(val owner: Activity, val display: Display, val fail: (Throwable) -> Unit) : GlassesMenuController.Output {
var configurations = 0
var shows = 0
var closes = 0
var state: GlassesState? = null
var settings: GlassesSettings? = null
var onConfigure: () -> Unit = {}
var showError: RuntimeException? = null
var closeError: RuntimeException? = null
override fun configure(state: GlassesState, settings: GlassesSettings) {
configurations++
this.state = state
this.settings = settings
onConfigure()
}
override fun show() { shows++; showError?.let { throw it } }
override fun close() { closes++; closeError?.let { throw it } }
}
}
@@ -0,0 +1,142 @@
package com.droiddeck.launcher.glasses
import com.droiddeck.launcher.gpu.HeadPose
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class GlassesMenuPoseTest {
private val time = 1_000_000_000L
@Test fun firstFreshGlassesPoseCentersTheScreenInAllThreeAxes() {
val pose = GlassesMenuPose(spatialEnabled = { false })
pose.onPose(0.2f, -0.3f, 0.1f, 0.9f, time)
val frame = pose.snapshot(time)
assertTrue(frame.valid)
assertArrayEquals(HeadPose.IDENTITY, frame.rotation, 1e-5f)
assertArrayEquals(FloatArray(3), frame.position, 0f)
}
@Test fun pageConfigurationPreservesTheThreeDofAnchorUntilExplicitRecenter() {
val pose = GlassesMenuPose(spatialEnabled = { false })
pose.onPose(0f, 0f, 0f, 1f, time)
pose.snapshot(time)
pose.onPose(0f, 0f, 0.1f, 0.995f, time+1)
val moved = pose.snapshot(time+1)
pose.configure(GlassesSettings(screenWidthDm = 40))
assertArrayEquals(moved.rotation, pose.snapshot(time+2).rotation, 0f)
assertTrue(moved.rotation.zip(HeadPose.IDENTITY.toList()).any { (a, b) -> kotlin.math.abs(a-b) > 0.1f })
pose.recenter()
assertArrayEquals(HeadPose.IDENTITY, pose.snapshot(time+3).rotation, 1e-5f)
}
@Test fun invalidOldAndFutureSamplesCannotReplaceTheLastSafeView() {
val pose = GlassesMenuPose(spatialEnabled = { false })
pose.onPose(0f, 0f, 0f, 1f, time)
pose.snapshot(time)
pose.onPose(0f, 0.1f, 0f, 0.995f, time+1)
val valid = pose.snapshot(time+1)
pose.onPose(Float.NaN, 0f, 0f, 1f, time+2)
pose.onPose(0f, 0f, 0f, 0f, time+3)
pose.onPose(0.1f, 0f, 0f, 0.995f, time)
val stale = pose.snapshot(time+GlassesMenuPose.MAX_AGE_NS+2)
assertFalse(stale.valid)
assertArrayEquals(valid.rotation, stale.rotation, 0f)
pose.onPose(0.1f, 0f, 0f, 0.995f, time+1_000_000_000L)
val future = pose.snapshot(time+20)
assertFalse(future.valid)
assertArrayEquals(valid.rotation, future.rotation, 0f)
}
@Test fun detachResetDiscardsOldOriginAndHeldPose() {
val pose = GlassesMenuPose(spatialEnabled = { false })
pose.onPose(0f, 0f, 0f, 1f, time)
pose.snapshot(time)
pose.onPose(0f, 0.2f, 0f, 0.98f, time+1)
pose.snapshot(time+1)
pose.reset()
assertFalse(pose.snapshot(time+2).valid)
assertArrayEquals(HeadPose.IDENTITY, pose.snapshot(time+2).rotation, 0f)
pose.onPose(0.2f, 0f, 0f, 0.98f, time+3)
assertArrayEquals(HeadPose.IDENTITY, pose.snapshot(time+3).rotation, 1e-5f)
}
@Test fun spatialScreenPoseKeepsMetresAndHoldsDuringTrackingLoss() {
var current: SpatialPose? = SpatialPose(SpatialVector(0.2f, -0.1f, 0.3f), SpatialRotation(y = 0.1f, w = 0.995f))
var recenters = 0
val pose = GlassesMenuPose({ true }, { current }, { recenters++ })
pose.configure(GlassesSettings(positionalTracking = true))
val valid = pose.snapshot(time)
assertTrue(valid.spatial)
assertTrue(valid.valid)
assertArrayEquals(floatArrayOf(0.2f, -0.1f, 0.3f), valid.position, 0f)
current = null
val lost = pose.snapshot(time+1)
assertFalse(lost.valid)
assertArrayEquals(valid.position, lost.position, 0f)
assertArrayEquals(valid.rotation, lost.rotation, 0f)
pose.recenter()
assertEquals(1, recenters)
}
@Test fun disabledTrackingReturnsAnUntranslatedCenteredView() {
val pose = GlassesMenuPose({ true }, { SpatialPose(SpatialVector(x = 0.2f)) }, {})
pose.configure(GlassesSettings(positionalTracking = true))
pose.snapshot(time)
pose.configure(GlassesSettings(headTracking = false, positionalTracking = true))
val frame = pose.snapshot(time+1)
assertFalse(frame.valid)
assertArrayEquals(HeadPose.IDENTITY, frame.rotation, 0f)
assertArrayEquals(FloatArray(3), frame.position, 0f)
}
@Test fun menuRecenterChangesTheScreenOriginWithoutMovingTheVrFloorOrigin() {
val session = SpatialSession()
session.reset(1, 1.6f)
session.accept(SpatialSample(SpatialPose(SpatialVector(1f, 2f, 3f)), time, 1, true))
session.accept(SpatialSample(SpatialPose(SpatialVector(1.2f, 2.1f, 3f)), time+1, 1, true))
val pose = GlassesMenuPose({ true }, { now -> session.view(true).takeIf { session.valid(now) } },
{ session.recenter(true, time+1) })
pose.configure(GlassesSettings(positionalTracking = true))
val vrBefore = session.view(false)!!
assertArrayEquals(floatArrayOf(0.2f, 0.1f, 0f), pose.snapshot(time+1).position, 1e-5f)
pose.recenter()
assertArrayEquals(FloatArray(3), pose.snapshot(time+1).position, 1e-5f)
assertEquals(vrBefore, session.view(false))
}
@Test fun malformedSpatialPoseCannotReplaceTheHeldView() {
var current = SpatialPose(SpatialVector(x = 0.1f))
val pose = GlassesMenuPose({ true }, { current }, {})
pose.configure(GlassesSettings(positionalTracking = true))
val valid = pose.snapshot(time)
current = SpatialPose(SpatialVector(x = Float.NaN))
assertFalse(pose.snapshot(time+1).valid)
assertArrayEquals(valid.position, pose.snapshot(time+1).position, 0f)
current = SpatialPose(rotation = SpatialRotation(w = 0f))
assertFalse(pose.snapshot(time+2).valid)
assertArrayEquals(valid.rotation, pose.snapshot(time+2).rotation, 0f)
}
@Test fun snapshotsDoNotExposeTheHeldArraysForMutation() {
val pose = GlassesMenuPose(spatialEnabled = { false })
pose.onPose(0f, 0f, 0f, 1f, time)
pose.snapshot(time).rotation.fill(Float.NaN)
assertArrayEquals(HeadPose.IDENTITY, pose.snapshot(time+GlassesMenuPose.MAX_AGE_NS+1).rotation, 1e-5f)
}
}
@@ -0,0 +1,82 @@
package com.droiddeck.launcher.glasses
import com.droiddeck.launcher.gpu.HeadPose
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
class GlassesMenuProjectionTest {
private fun frame(rotation: FloatArray = HeadPose.IDENTITY, position: FloatArray = FloatArray(3)) =
GlassesMenuPose.Frame(rotation, position, true, false)
private fun hit(pose: GlassesMenuPose.Frame = frame(), x: Float = 0f, y: Float = 0f, eye: Float = 0f,
aspect: Float = 2f) = GlassesMenuProjection.intersect(pose, x, y, eye, 1f, 3f, aspect)
@Test fun centeredForwardRaySamplesTheTextureCenter() {
assertEquals(GlassesMenuProjection.Uv(0.5f, 0.5f), hit())
}
@Test fun screenTopAndRightRaysUseTopLeftTextureCoordinates() {
val topRight = hit(x = 1f/3f, y = -1f/6f)!!
assertEquals(1f, topRight.x, 1e-5f)
assertEquals(0f, topRight.y, 1e-5f)
}
@Test fun eyeOriginsShiftByPhysicalIpdWhileMonoUsesTheCenter() {
val left = hit(eye = -GlassesMenuSurface.IPD_METERS/2)!!
val right = hit(eye = GlassesMenuSurface.IPD_METERS/2)!!
assertEquals(0.484f, left.x, 1e-5f)
assertEquals(0.516f, right.x, 1e-5f)
assertEquals(0.5f, hit()!!.x, 0f)
}
@Test fun translatedHeadUsesMetresInTheAnchoredScreenFrame() {
val translated = hit(frame(position = floatArrayOf(0.2f, 0.1f, 0f)))!!
assertEquals(0.6f, translated.x, 1e-5f)
assertEquals(0.4f, translated.y, 1e-5f)
}
@Test fun rowMajorPositiveYawTurnsTheForwardRayTowardNegativeX() {
val angle = Math.toRadians(10.0)
val rotation = SpatialRotation(y = kotlin.math.sin(angle/2).toFloat(), w = kotlin.math.cos(angle/2).toFloat()).matrix()
assertEquals(0.5f - 1.5f*kotlin.math.tan(angle).toFloat(), hit(frame(rotation))!!.x, 1e-5f)
}
@Test fun aRayBehindTheHeadOrOutsideThePlaneHasNoUiHit() {
assertNull(hit(frame(rotation = SpatialRotation(y = 1f, w = 0f).matrix())))
assertNull(hit(x = 1f))
assertNull(hit(frame(position = floatArrayOf(0f, 0f, -4f))))
}
@Test fun sourceAspectChangesThePlaneHeightWithoutStretchingItsWidth() {
assertNotNull(hit(y = 0.2f, aspect = 1f))
assertNull(hit(y = 0.2f, aspect = 2f))
}
@Test fun nonFiniteInputsCannotCreateTextureCoordinates() {
assertNull(hit(x = Float.NaN))
assertNull(hit(frame(position = floatArrayOf(Float.POSITIVE_INFINITY, 0f, 0f))))
}
}
private object GlassesMenuProjection {
data class Uv(val x: Float, val y: Float)
// Row-major head-to-anchor rotation; +Y is up, -Z is forward, output UV starts at the top left.
fun intersect(frame: GlassesMenuPose.Frame, rayX: Float, rayY: Float, eye: Float,
halfWidth: Float, distance: Float, aspect: Float): Uv? {
if (frame.rotation.size != 9 || frame.position.size != 3 || frame.rotation.any { !it.isFinite() } ||
frame.position.any { !it.isFinite() } || listOf(rayX, rayY, eye, halfWidth, distance, aspect).any { !it.isFinite() } ||
halfWidth <= 0f || distance <= 0f || aspect <= 0f) return null
val r = frame.rotation
val d = FloatArray(3) { row -> r[row*3]*rayX - r[row*3+1]*rayY - r[row*3+2] }
val o = FloatArray(3) { row -> frame.position[row] + r[row*3]*eye }
if (d[2] >= -0.0001f) return null
val t = (-distance-o[2]) / d[2]
if (t <= 0f) return null
val u = (o[0]+d[0]*t) / halfWidth * 0.5f + 0.5f
val v = -(o[1]+d[1]*t) * aspect / halfWidth * 0.5f + 0.5f
return Uv(u, v).takeIf { u in 0f..1f && v in 0f..1f }
}
}
@@ -11,6 +11,25 @@ class GlassesRenderingTest {
)
private val base = Cardboard(ipdMm = 67)
@Test fun thirtyHzRequiresANativeModeAndDedicatedGlassesOutput() {
val settings = GlassesSettings(refreshHz = 30, sbsHz = 30)
for (stereo in listOf(false, true)) for (sbs90 in listOf(false, true)) {
val state = ultra.copy(sbs = stereo, sbs90 = sbs90, supports30Hz = true)
assertTrue(GlassesRendering.refreshRates(settings, state).contains(30))
assertEquals(30, GlassesRendering.outputHz(settings, state))
assertEquals(60, GlassesRendering.mirrorRefreshLimit(settings, state.copy(hz = 60f), false))
assertEquals(30, GlassesRendering.mirrorRefreshLimit(settings, state.copy(hz = 29.97f), false))
for (unavailable in listOf(state.copy(supports30Hz = false), state.copy(noSignal = true), state.copy(displayId = -1))) {
assertFalse(GlassesRendering.refreshRates(settings, unavailable).contains(30))
assertEquals(60, GlassesRendering.outputHz(settings, unavailable))
}
val mirrored = settings.copy(gameOnGlasses = false)
assertFalse(GlassesRendering.refreshRates(mirrored, state).contains(30))
assertEquals(60, GlassesRendering.outputHz(mirrored, state))
assertEquals(60, GlassesRendering.mirrorRefreshLimit(mirrored, state, false))
}
}
@Test fun mirroredRefreshUsesTheSelectedGlassesRateEvenBeforeTheDisplaySwitches() {
val settings = GlassesSettings(gameOnGlasses = false, refreshHz = 60)
assertEquals(60, GlassesRendering.mirrorRefreshLimit(settings, ultra.copy(hz = 120f), false))
@@ -75,27 +94,82 @@ class GlassesRenderingTest {
assertFalse(changed.headTracking)
}
@Test fun automatic3dGamesUseAVirtualScreenButSteamVrUsesDirectStereo() {
@Test fun automaticSbsDuplicatesTheWholeFrameWithoutEnablingHeadTracking() {
val settings = GlassesSettings()
val stereo = ultra.copy(sbs = true, width = 3840)
assertEquals(GlassesRendering.Picture.VIRTUAL_SCREEN, GlassesRendering.picture(settings, true, false))
assertEquals(Cardboard.SOURCE_SCREEN, render(settings, stereo).source)
assertEquals(GlassesRendering.Picture.DUPLICATED, GlassesRendering.picture(settings, true, false))
assertEquals(Cardboard.SOURCE_FLAT, render(settings, stereo).source)
assertFalse(render(settings, stereo).headTracking)
assertFalse(render(settings, stereo).mono)
assertEquals(GlassesRendering.Picture.STEREO, GlassesRendering.picture(settings, true, true))
assertEquals(Cardboard.SOURCE_SBS, render(settings, stereo, steamVr = true).source)
assertTrue(render(settings, stereo, steamVr = true).headTracking)
}
@Test fun explicitPictureChoiceOverridesAutomaticResolution() {
@Test fun flatSbsDoesNotDependOnTheTrackingPreferenceOrUsbControl() {
for (tracking in listOf(false, true)) for (connected in listOf(false, true)) {
for (height in listOf(1080, 1200)) {
val state = ultra.copy(sbs = true, width = 3840, height = height, connected = connected)
val settings = GlassesSettings(headTracking = tracking)
val flat = render(settings, state)
assertTrue(flat.enabled)
assertFalse(flat.mono)
assertFalse(flat.headTracking)
assertEquals(Cardboard.SOURCE_FLAT, flat.source)
assertEquals(Cardboard.SOURCE_SBS, render(settings.copy(picture3d = GlassesSettings.PICTURE_SBS), state).source)
}
}
}
@Test fun virtualScreenTracksFlatContentIndependentlyOfTheHardwareSbsMode() {
val settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN)
assertTrue(settings.screen3dof)
assertFalse(settings.copy(headTracking = false).screen3dof)
for (height in listOf(1080, 1200)) {
for (sbs in listOf(false, true)) {
val projection = render(settings, ultra.copy(width = if (sbs) 3840 else 1920, height = height, sbs = sbs))
assertTrue(projection.enabled)
assertTrue(projection.headTracking)
assertEquals(Cardboard.SOURCE_SCREEN, projection.source)
assertEquals(!sbs, projection.mono)
}
}
}
@Test fun explicitPictureChoiceOverridesAutomaticResolutionOutsideSteamVr() {
val virtual = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN)
val direct = GlassesSettings(picture3d = GlassesSettings.PICTURE_SBS)
for (stereo in listOf(false, true)) {
for (vr in listOf(false, true)) {
assertEquals(GlassesRendering.Picture.VIRTUAL_SCREEN, GlassesRendering.picture(virtual, stereo, vr))
assertEquals(GlassesRendering.Picture.STEREO, GlassesRendering.picture(direct, stereo, vr))
assertEquals(GlassesRendering.Picture.VIRTUAL_SCREEN, GlassesRendering.picture(virtual, stereo, false))
assertEquals(GlassesRendering.Picture.STEREO, GlassesRendering.picture(direct, stereo, false))
}
}
@Test fun steamVrSplitsTheEyePairRegardlessOfTheSavedFlatGamePicture() {
for (picture in listOf(GlassesSettings.PICTURE_AUTO, GlassesSettings.PICTURE_SCREEN, GlassesSettings.PICTURE_SBS)) {
val settings = GlassesSettings(picture3d = picture, auto3d = true)
for (stereo in listOf(false, true)) for (outputActive in listOf(false, true)) {
val state = ultra.copy(sbs = stereo, width = if (stereo) 3840 else 1920)
val projection = GlassesRendering.cardboard(base, settings, state, outputActive, true)
assertEquals(GlassesRendering.Picture.STEREO, GlassesRendering.picture(settings, stereo, true))
assertEquals(Cardboard.SOURCE_SBS, projection.source)
assertEquals(!stereo, projection.mono)
assertTrue(projection.enabled)
assertTrue(projection.headTracking)
}
}
}
@Test fun stoppingSteamVrRestoresTheSavedVirtualScreen() {
val settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN, auto3d = true,
screenWidthDm = 18, screenDistanceDm = 30)
val before = render(settings)
assertEquals(Cardboard.SOURCE_SCREEN, before.source)
assertEquals(Cardboard.SOURCE_SBS, render(settings, steamVr = true).source)
assertEquals(before, render(settings))
assertEquals(GlassesSettings.PICTURE_SCREEN, settings.picture3d)
}
@Test fun mirrorModeKeepsThePhoneProjectionInsteadOfApplyingDormantGlassesSettings() {
val phone = base.copy(enabled = true, screenWidthDm = 60, screenDistanceDm = 20)
val settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN,
@@ -4,6 +4,8 @@ import android.hardware.display.DisplayManager
import android.view.Display
import android.view.SurfaceHolder
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@@ -21,6 +23,40 @@ import org.robolectric.shadows.ShadowDisplayManager
@Config(sdk = [31, 34], manifest = Config.NONE, shadows = [GlassesScreenTest.ModeDisplay::class])
@LooperMode(LooperMode.Mode.PAUSED)
class GlassesScreenTest {
@Test fun recoveryCoversTheExistingRenderSurfaceWithoutReplacingIt() {
val flat=mode(1,1920,1200,60f)
val (screen,_)=screen(flat,arrayOf(flat),60)
val holder=screen.holder
val root=screen.surfaceView.parent as android.view.ViewGroup
assertTrue(root.indexOfChild(screen.trackingRecovery)>root.indexOfChild(screen.surfaceView))
screen.trackingRecovery.update(TrackingRecovery.LOST,false)
assertEquals(android.view.View.VISIBLE,screen.trackingRecovery.visibility)
screen.trackingRecovery.update(null,false)
assertEquals(android.view.View.GONE,screen.trackingRecovery.visibility)
assertTrue(holder===screen.holder)
}
@Test fun nativeThirtyHzMustMatchBothDimensionsOfTheCurrentEyeLayout() {
val flat60 = mode(1, 1920, 1200, 60f)
val flat30 = mode(2, 1920, 1200, 29.97f)
val stereo60 = mode(3, 3840, 1200, 60f)
val stereo30 = mode(4, 3840, 1200, 30f)
val short30 = mode(5, 3840, 1080, 30f)
val (screen, panel) = screen(flat60, arrayOf(flat60, flat30, stereo60, short30), 30)
assertTrue(Glasses.supports30Hz(screen.display))
assertEquals(flat30.modeId, screen.window!!.attributes.preferredDisplayModeId)
panel.current = stereo60
screen.prefer(30)
assertFalse(Glasses.supports30Hz(screen.display))
assertEquals(stereo60.modeId, screen.window!!.attributes.preferredDisplayModeId)
panel.supported += stereo30
screen.prefer(30)
assertTrue(Glasses.supports30Hz(screen.display))
assertEquals(stereo30.modeId, screen.window!!.attributes.preferredDisplayModeId)
}
@Test fun switchingBetween2dAndStereoRefreshesTheModePreferenceEvenAtTheSameHz() {
val flat = mode(1, 1920, 1200, 60f)
val stereo = mode(2, 3840, 1200, 60f)
@@ -54,6 +90,30 @@ class GlassesScreenTest {
assertEquals(stereo90.modeId, screen.window!!.attributes.preferredDisplayModeId)
}
@Test fun explicitResolutionChangesBothDimensionsWithoutSwitchingEyeLayout() {
val flat1080 = mode(1, 1920, 1080, 90f)
val flat1200 = mode(2, 1920, 1200, 90f)
val stereo1080 = mode(3, 3840, 1080, 90f)
val stereo1200 = mode(4, 3840, 1200, 90f)
val (screen, panel) = screen(flat1200, arrayOf(flat1080, flat1200, stereo1080, stereo1200), 90)
screen.prefer(90, GlassesResolution(1920, 1080))
assertEquals(flat1080.modeId, screen.window!!.attributes.preferredDisplayModeId)
panel.current = stereo1200
screen.prefer(90, GlassesResolution(1920, 1080))
assertEquals(stereo1080.modeId, screen.window!!.attributes.preferredDisplayModeId)
screen.prefer(90, GlassesResolution(1920, 1200))
assertEquals(stereo1200.modeId, screen.window!!.attributes.preferredDisplayModeId)
}
@Test fun unavailableResolutionKeepsTheCurrentModeUntilHardwareConfirmsTheChange() {
val stereo60 = mode(1, 3840, 1200, 60f)
val stereo90 = mode(2, 3840, 1200, 90f)
val flat1080 = mode(3, 1920, 1080, 90f)
val (screen, _) = screen(stereo60, arrayOf(stereo60, stereo90, flat1080), 60)
screen.prefer(90, GlassesResolution(1920, 1080))
assertEquals(stereo90.modeId, screen.window!!.attributes.preferredDisplayModeId)
}
private fun mode(id: Int, width: Int, height: Int, hz: Float): Display.Mode =
ShadowDisplayManager.ModeBuilder.modeBuilder(id)
.setWidth(width).setHeight(height).setRefreshRate(hz).build()
@@ -0,0 +1,152 @@
package com.droiddeck.launcher.glasses
import android.app.Application
import android.content.Intent
import android.hardware.display.DisplayManager
import android.view.Display
import androidx.compose.runtime.MutableState
import com.droiddeck.launcher.SessionActivity
import com.droiddeck.launcher.gpu.Cardboard
import com.droiddeck.launcher.gpu.SteamVr
import com.droiddeck.launcher.runtime.FlatpakManager
import com.droiddeck.launcher.session.LoadingState
import com.droiddeck.launcher.session.SessionService
import com.droiddeck.launcher.session.SessionState
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import org.robolectric.shadows.ShadowDisplayManager
import org.robolectric.util.ReflectionHelpers
import org.robolectric.util.ReflectionHelpers.ClassParameter
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class, manifest = Config.NONE)
@LooperMode(LooperMode.Mode.PAUSED)
class GlassesSessionModesTest {
private data class Launch(val name: String, val mode: String, val program: String? = null, val args: Array<String>? = null)
private val launches = listOf(
Launch("Steam and store games", SessionService.MODE_STEAM),
Launch("Desktop", SessionService.MODE_DESKTOP),
Launch("Standalone", SessionService.MODE_RUN, "/usr/bin/example-game"),
Launch("Emulator", SessionService.MODE_RUN, "/usr/bin/retroarch", arrayOf("/games/example.rom")),
Launch("Flatpak", SessionService.MODE_RUN, FlatpakManager.LAUNCHER, arrayOf("org.example.Game")),
)
private lateinit var display: Display
private lateinit var restore: () -> Unit
private val saved = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN,
headTracking = true, screenWidthDm = 35, screenDistanceDm = 45)
private val phone = Cardboard(enabled = true, source = Cardboard.SOURCE_SCREEN, ipdMm = 67,
screenWidthDm = 20, screenDistanceDm = 30, headTracking = false)
@Before fun setUp() {
val running = SessionState.running
val steamVr = SessionState.steamVr
val mode = SessionState.mode
val fields = listOf("context", "publisherAttached").associateWith {
SpatialTracking::class.java.getDeclaredField(it).apply { isAccessible = true }
}
val values = fields.mapValues { it.value.get(null) }
fields.getValue("context").set(null, null)
fields.getValue("publisherAttached").set(null, false)
restore = {
fields.forEach { (name, field) -> field.set(null, values[name]) }
SessionState.running = running
SessionState.steamVr = steamVr
SessionState.mode = mode
}
val id = ShadowDisplayManager.addDisplay("w1920dp-h1080dp")
display = RuntimeEnvironment.getApplication().getSystemService(DisplayManager::class.java).getDisplay(id)
SessionState.running = true
SessionState.steamVr = false
}
@After fun tearDown() {
SpatialTracking.useLegacyTracking()
SpatialTracking.disconnect()
ShadowDisplayManager.removeDisplay(display.displayId)
restore()
}
@Test fun flatScreenSizeDistanceAndTrackingReachEveryGuestSessionWithoutSteamVr() {
for (launch in launches) for (spatial in listOf(false, true)) {
val activity = activity(launch, saved.copy(positionalTracking = spatial), output = true)
SpatialTracking.connect(0, 101, null, spatial, 1.6f, false)
val projection = projection(activity)
assertTrue(launch.name, projection.enabled)
assertTrue(launch.name, projection.headTracking)
assertTrue(launch.name, projection.mono)
assertEquals(launch.name, Cardboard.SOURCE_SCREEN, projection.source)
assertEquals(launch.name, Cardboard.Viewer.VITURE.id, projection.viewer)
assertEquals(launch.name, 35, projection.screenWidthDm)
assertEquals(launch.name, 45, projection.screenDistanceDm)
assertEquals(launch.name, phone.ipdMm, projection.ipdMm)
assertEquals(launch.name, if (spatial) TrackingRecovery.LOST else null, recovery(activity))
}
}
@Test fun positionalRecoverySurvivesUsbLossInEveryGuestSessionAndHonorsTrackingOff() {
for (launch in launches) {
val activity = activity(launch, saved.copy(positionalTracking = true), output = true)
SpatialTracking.connect(0, 102, null, true, 1.6f, false)
assertEquals(launch.name, TrackingRecovery.LOST, recovery(activity))
SpatialTracking.disconnect()
state(activity, "glasses", glasses().copy(connected = false, supportsSpatial = false))
assertEquals(launch.name, TrackingRecovery.DISCONNECTED, recovery(activity))
state(activity, "glassesSettings", saved.copy(positionalTracking = true, headTracking = false))
assertFalse(launch.name, projection(activity).headTracking)
assertNull(launch.name, recovery(activity))
}
}
@Test fun savedGlassesTrackingDoesNotChangePhoneRenderingOrRecoveryInAnySessionMode() {
for (launch in launches) {
val activity = activity(launch, saved.copy(positionalTracking = true), output = false)
SpatialTracking.connect(0, 103, null, true, 1.6f, false)
assertSame(launch.name, phone, projection(activity))
assertNull(launch.name, recovery(activity))
}
}
private fun activity(launch: Launch, settings: GlassesSettings, output: Boolean): SessionActivity {
val intent = Intent(RuntimeEnvironment.getApplication(), SessionActivity::class.java)
.putExtra(SessionService.EXTRA_MODE, launch.mode)
.putExtra(SessionService.EXTRA_PROGRAM, launch.program)
.putExtra(SessionService.EXTRA_PROGRAM_ARGS, launch.args)
SessionState.mode = launch.mode
return Robolectric.buildActivity(SessionActivity::class.java, intent).get().also {
ReflectionHelpers.setField(it, "loading", LoadingState(it).apply { visible = false })
ReflectionHelpers.setField(it, "started", true)
if (output) ReflectionHelpers.setField(it, "glassesScreen", GlassesScreen(it, display, 60, it))
state(it, "glassesOutputActive", output)
state(it, "glasses", glasses())
state(it, "glassesSettings", settings)
state(it, "steamVrStatus", SteamVr.Status.READY)
}
}
private fun glasses() = GlassesState(present = true, connected = true, controllable = true,
supportsSpatial = true, supportsScreen3dof = true, displayId = display.displayId, width = 1920, height = 1080)
private fun projection(activity: SessionActivity): Cardboard = ReflectionHelpers.callInstanceMethod(
activity, "effectiveCardboard", ClassParameter.from(Cardboard::class.java, phone))
private fun recovery(activity: SessionActivity): TrackingRecovery? {
ReflectionHelpers.callInstanceMethod<Void>(activity, "updateTrackingRecovery")
return ReflectionHelpers.getField<MutableState<TrackingRecovery?>>(activity, "trackingRecovery\$delegate").value
}
private fun <T> state(activity: SessionActivity, name: String, value: T) {
ReflectionHelpers.getField<MutableState<T>>(activity, name + "\$delegate").value = value
}
}
@@ -0,0 +1,33 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class GlassesSpatialAvailabilityTest {
private val ultra = GlassesState(connected = true, controllable = true, supportsSpatial = true)
private val selected = GlassesSettings(positionalTracking = true, handControllers = true)
@Test fun sdkConfirmedConnectedUltraOwnsSpatialControllerInput() {
assertTrue(ultra.spatialConnected)
assertTrue(ultra.usesSpatialControllers(selected))
assertTrue(ultra.usesSpatialControllers(selected.copy(handControllers = false, joyConTracking = true)))
assertFalse(ultra.usesSpatialControllers(selected.copy(positionalTracking = false)))
assertFalse(ultra.usesSpatialControllers(selected.copy(handControllers = false)))
}
@Test fun savedUltraPreferencesDoNotSelectSpatialInputForOtherGlasses() {
for (name in listOf("VITURE Luma Ultra", "VITURE Luma Pro", "VITURE Pro", "XREAL", "RayNeo")) {
val other = ultra.copy(model = name, supportsSpatial = false)
assertFalse(name, other.spatialConnected)
assertFalse(name, other.usesSpatialControllers(selected))
assertFalse(name, other.usesSpatialControllers(selected.copy(handControllers = false, joyConTracking = true)))
}
}
@Test fun disconnectedOrUncontrollableUltraCannotClaimSpatialInput() {
for (state in listOf(ultra.copy(connected = false), ultra.copy(controllable = false), GlassesState())) {
assertFalse(state.spatialConnected)
assertFalse(state.usesSpatialControllers(selected))
}
}
}
@@ -4,6 +4,7 @@ import android.app.Application
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import com.droiddeck.launcher.SessionActivity
import com.droiddeck.launcher.gpu.SteamVr
import com.droiddeck.launcher.session.LoadingState
@@ -24,6 +25,7 @@ import org.robolectric.annotation.LooperMode
import java.util.concurrent.CountDownLatch
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import java.time.Duration
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class)
@@ -33,8 +35,10 @@ class GlassesSteamVrTest {
private val handler = Handler(Looper.getMainLooper())
private lateinit var restore: () -> Unit
private fun field(name: String) = Glasses::class.java.getDeclaredField(name).apply { isAccessible = true }
private val spatialGate get() = field("steamVrSpatialGate").get(null) as SteamVrSpatialGate
@Before fun prepare() {
spatialGate.cancel()
val fields = listOf("handler", "app", "model").associateWith { field(it).get(null) }
@Suppress("UNCHECKED_CAST")
val flows = listOf("_state", "_settings").map { field(it).get(null) as MutableStateFlow<Any> }
@@ -64,6 +68,7 @@ class GlassesSteamVrTest {
@After fun cleanUp() {
handler.removeCallbacksAndMessages(null)
spatialGate.cancel()
field("link").set(null, null)
(field("modeState").get(null) as GlassesModeState).apply { manual(); sent(false, 0) }
SessionState.steamVr = false
@@ -133,6 +138,7 @@ class GlassesSteamVrTest {
.apply { isAccessible = true }.invoke(activity, name)
action("start")
assertTrue("A manual start must keep status polling alive", SessionState.steamVr)
shadowOf(Looper.getMainLooper()).idle()
val worker = SessionActivity::class.java.getDeclaredField("steamVrWorker")
.apply { isAccessible = true }.get(null) as ThreadPoolExecutor
assertEquals(1, worker.queue.size)
@@ -146,6 +152,90 @@ class GlassesSteamVrTest {
}
}
@Test fun preparationCallbackRunsAfterSuspensionAndCancelWorksBeforeRunning() {
@Suppress("UNCHECKED_CAST")
val settings = field("_settings").get(null) as MutableStateFlow<GlassesSettings>
settings.value = GlassesSettings(positionalTracking = true, handControllers = true)
val saved = settings.value
var prepared = 0
repeat(2) {
Glasses.prepareSteamVrStart {
assertFalse(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
prepared++
}
}
shadowOf(Looper.getMainLooper()).idle()
assertEquals(2, prepared)
Glasses.setVrActive(false)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(3))
assertFalse(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
Glasses.cancelSteamVrStart()
shadowOf(Looper.getMainLooper()).idle()
assertTrue(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
assertEquals(saved, settings.value)
}
@Test fun rapidRestartDoesNotLaunchTheCancelledPreparedRequest() = withLaunch(auto3d = false) { activity ->
SessionState.steamVr = false
val action = SessionActivity::class.java.getDeclaredMethod("steamVrAction", String::class.java)
.apply { isAccessible = true }
action.invoke(activity, "start")
action.invoke(activity, "stop")
action.invoke(activity, "start")
shadowOf(Looper.getMainLooper()).idle()
val worker = SessionActivity::class.java.getDeclaredField("steamVrWorker")
.apply { isAccessible = true }.get(null) as ThreadPoolExecutor
assertTrue(SessionState.steamVr)
assertEquals("Only stop and the latest start should reach the worker", 2, worker.queue.size)
}
@Test fun duplicateRunningKeepsDeadlineAndStoppingCancelsThePendingResume() {
Glasses.prepareSteamVrStart()
Glasses.setVrActive(true)
shadowOf(Looper.getMainLooper()).idle()
val deadline = spatialGate.deadline!!
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1_999))
var prepared = false
Glasses.prepareSteamVrStart { prepared = true }
Glasses.setVrActive(true)
shadowOf(Looper.getMainLooper()).idle()
assertTrue(prepared)
assertEquals(deadline, spatialGate.deadline)
assertFalse(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1))
assertTrue(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
Glasses.setVrActive(false)
Glasses.prepareSteamVrStart()
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(3))
assertFalse(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
assertNull(spatialGate.deadline)
}
@Test fun restartWhileDisplayStillReportsRunningGetsANewTrackingDeadline() {
Glasses.setVrActive(true)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(3))
assertTrue(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
Glasses.cancelSteamVrStart()
Glasses.prepareSteamVrStart()
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.state.value.vrActive)
assertFalse(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
Glasses.setVrActive(true)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1_999))
assertFalse(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(1))
assertTrue(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
}
@Test fun endedSessionIgnoresLateRunningStatus() = withLaunch(auto3d = true) { activity ->
SessionActivity::class.java.getDeclaredField("ended").apply { isAccessible = true }.set(activity, true)
status(activity, SteamVr.Status.RUNNING)
assertFalse(Glasses.state.value.vrActive)
assertNull(spatialGate.deadline)
assertTrue(spatialGate.spatialEnabled(true, SystemClock.uptimeMillis()))
assertTrue(commands.isEmpty())
}
private fun status(activity: SessionActivity, status: SteamVr.Status) {
SessionActivity::class.java.getDeclaredMethod("onSteamVrStatus", SteamVr.Status::class.java)
.apply { isAccessible = true }.invoke(activity, status)
@@ -0,0 +1,192 @@
package com.droiddeck.launcher.glasses
import android.app.Application
import android.os.HandlerThread
import android.view.Display
import androidx.compose.runtime.MutableState
import com.droiddeck.launcher.SessionActivity
import com.droiddeck.launcher.gpu.Cardboard
import com.droiddeck.launcher.gpu.SteamVr
import com.droiddeck.launcher.input.ControllerPrefs
import com.droiddeck.launcher.input.JoyConVr
import com.droiddeck.launcher.session.LoadingState
import com.droiddeck.launcher.session.SessionState
import java.io.File
import java.nio.ByteBuffer
import java.nio.ByteOrder
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import org.robolectric.util.ReflectionHelpers
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class, manifest = Config.NONE)
@LooperMode(LooperMode.Mode.PAUSED)
class GlassesTrackingRoutingTest {
private lateinit var activity: SessionActivity
private lateinit var restore: () -> Unit
private val ultra = GlassesState(present = true, connected = true, controllable = true,
supportsSpatial = true, displayId = Display.DEFAULT_DISPLAY + 1, width = 1920, height = 1080)
private val saved = GlassesSettings(positionalTracking = true, handControllers = true)
@Before fun setUp() {
val running = SessionState.running
val steamVr = SessionState.steamVr
val fields = listOf("context", "publisherAttached").associateWith {
SpatialTracking::class.java.getDeclaredField(it).apply { isAccessible = true }
}
val values = fields.mapValues { it.value.get(null) }
fields.getValue("context").set(null, null)
fields.getValue("publisherAttached").set(null, false)
restore = {
fields.forEach { (name, field) -> field.set(null, values[name]) }
SessionState.running = running
SessionState.steamVr = steamVr
}
activity = Robolectric.buildActivity(SessionActivity::class.java).get()
ReflectionHelpers.setField(activity, "loading", LoadingState(activity).apply { visible = false })
ReflectionHelpers.setField(activity, "started", true)
ReflectionHelpers.setField(activity, "activeCardboard", Cardboard.OFF)
state("glasses", ultra)
state("glassesSettings", saved)
state("steamVrStatus", SteamVr.Status.RUNNING)
SessionState.running = true
SessionState.steamVr = true
SpatialTracking.setOutputUsesGlasses(true)
SpatialTracking.connect(0, 81, null, true, 1.6f, true)
}
@After fun tearDown() {
SpatialTracking.useLegacyTracking()
SpatialTracking.disconnect()
SpatialTracking.setOutputUsesGlasses(true)
restore()
}
@Test fun usbLossKeepsSpatialOwnershipAndShowsRecoveryOnTheGlassesDisplay() {
SpatialTracking.disconnect()
state("glasses", ultra.copy(connected = false, supportsSpatial = false))
updateRouting()
assertEquals(3, modes())
assertTrue(SpatialTracking.selected)
assertEquals(TrackingRecovery.DISCONNECTED, recovery())
}
@Test fun phoneHandoffClearsPublishedModesWithoutErasingTheGlassesSelection() {
for (joyCons in listOf(false, true)) {
val selected = saved.copy(handControllers = !joyCons, joyConTracking = joyCons)
state("glassesSettings", selected)
SpatialTracking.connect(0, if (joyCons) 83 else 82, null, true, 1.6f, !joyCons, joyCons)
SpatialTracking.disconnect()
state("glasses", GlassesState())
updateRouting()
assertEquals(0, modes())
assertTrue(SpatialTracking.selected)
assertEquals(selected, stateValue<GlassesSettings>("glassesSettings"))
assertNull(recovery())
SpatialTracking.setControllers(!joyCons, joyCons)
assertEquals("Later controller updates must not reclaim the phone output", 0, modes())
SpatialTracking.connect(0, if (joyCons) 85 else 84, null, true, 1.6f, !joyCons, joyCons)
assertEquals("Reconnecting waits for the output handoff", 0, modes())
state("glasses", ultra)
updateRouting()
assertEquals(if (joyCons) 5 else 3, modes())
}
}
@Test fun endingTheSessionReleasesSpatialInputEvenWhileTheGlassesRemainConnected() {
ReflectionHelpers.setField(activity, "ended", true)
updateRouting()
assertEquals(0, modes())
assertTrue(SpatialTracking.selected)
assertEquals(saved, stateValue<GlassesSettings>("glassesSettings"))
assertNull(recovery())
}
@Test @Config(sdk = [31]) fun displayLossReleasesSpatialControllerOverridesWhileUsbRemainsConnected() {
val previousJoyConVr = ControllerPrefs.joyConVr(activity)
ControllerPrefs.setJoyConVr(activity, false)
val joyCon = JoyConVr(activity, File(activity.cacheDir, "routing-joycons")) { _, _ -> }
ReflectionHelpers.setField(activity, "joyConVr", joyCon)
val worker = ReflectionHelpers.getField<HandlerThread>(joyCon, "thread")
fun updateInputs(): Boolean {
ReflectionHelpers.callInstanceMethod<Void>(activity, "updateJoyConTracking")
shadowOf(worker.looper).idle()
return ReflectionHelpers.getField(joyCon, "wanted")
}
try {
for (joyCons in listOf(false, true)) {
val selected = saved.copy(handControllers = !joyCons, joyConTracking = joyCons)
state("glassesSettings", selected)
for (displayLost in listOf(ultra.copy(noSignal = true), ultra.copy(displayId = Display.INVALID_DISPLAY))) {
state("glasses", ultra)
assertTrue(ReflectionHelpers.callInstanceMethod(activity, "usesSpatialControllers"))
assertEquals(joyCons, updateInputs())
state("glasses", displayLost)
updateRouting()
assertTrue(displayLost.spatialConnected)
assertEquals(0, modes())
assertFalse("The phone camera must not be suppressed by a missing display",
ReflectionHelpers.callInstanceMethod(activity, "usesSpatialControllers"))
assertFalse("Saved glasses Joy-Cons must not override the phone preference", updateInputs())
assertEquals(selected, stateValue<GlassesSettings>("glassesSettings"))
ControllerPrefs.setJoyConVr(activity, true)
assertTrue("The phone Joy-Con preference must still apply", updateInputs())
ControllerPrefs.setJoyConVr(activity, false)
state("glasses", ultra)
updateRouting()
assertTrue(ReflectionHelpers.callInstanceMethod(activity, "usesSpatialControllers"))
assertEquals(joyCons, updateInputs())
}
}
} finally {
ReflectionHelpers.setField(activity, "joyConVr", null)
joyCon.close()
ControllerPrefs.setJoyConVr(activity, previousJoyConVr)
}
}
@Test fun anotherGlassesModelDoesNotActivateSavedUltraTracking() {
SpatialTracking.disconnect()
SpatialTracking.useLegacyTracking()
state("glasses", ultra.copy(model = "XREAL", supportsSpatial = false))
updateRouting()
assertEquals(0, modes())
assertFalse(SpatialTracking.selected)
assertEquals(saved, stateValue<GlassesSettings>("glassesSettings"))
assertNull(recovery())
}
private fun updateRouting() {
ReflectionHelpers.callInstanceMethod<Void>(activity, "updateHeadTracker")
ReflectionHelpers.callInstanceMethod<Void>(activity, "updateTrackingRecovery")
}
private fun modes(): Int {
val field = SpatialTracking::class.java.getDeclaredField("buffer").apply { isAccessible = true }
return (field.get(null) as ByteBuffer).duplicate().order(ByteOrder.LITTLE_ENDIAN).getInt(32)
}
private fun recovery(): TrackingRecovery? = stateValue("trackingRecovery")
private fun <T> state(name: String, value: T) {
ReflectionHelpers.getField<MutableState<T>>(activity, name + "\$delegate").value = value
}
private fun <T> stateValue(name: String): T =
ReflectionHelpers.getField<MutableState<T>>(activity, name + "\$delegate").value
}
@@ -165,6 +165,36 @@ class GlassesUsbTest {
assertEquals(listOf(gt), manager.requests)
}
@Test fun screen3dofAvailabilityFollowsTheTrackingTransportAcrossBrands() {
val devices = listOf(device(0x1011), device(0x1019), device(0x1131),
device(0xaf50, 0x1bbb, "RayNeo Air 4 Pro"),
device(0xaf50, 0x3941, "RayNeo GT MAX"),
device(0x043e, 0x3318, "XREAL 1S"))
try {
for (device in devices) for (imu in listOf(false, true)) {
manager.devices.clear()
manager.devices[device.deviceName] = device
manager.granted.add(device.deviceName)
field("linkDevice").set(null, device)
field("link").set(null, object : GlassesLink {
override val hasImu = imu
override fun send(msgId: Int, payload: ByteArray) = true
override fun close() {}
})
refresh()
assertTrue(state.value.canControl)
assertEquals(imu, state.value.supportsScreen3dof)
}
manager.devices.clear()
refresh()
assertFalse(state.value.supportsScreen3dof)
} finally {
field("link").set(null, null)
field("linkDevice").set(null, null)
(field("handler").get(null) as Handler).removeCallbacksAndMessages(null)
}
}
private fun refresh(ask: Boolean = false) {
Glasses::class.java.getDeclaredMethod("refresh", Boolean::class.javaPrimitiveType)
.apply { isAccessible = true }.invoke(Glasses, ask)
@@ -0,0 +1,192 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class HandGesturesTest {
private fun openHand(): MutableList<SpatialVector> {
val p=MutableList(21) { SpatialVector(z=.5f) }
p[0]=SpatialVector(0f,0f,.5f)
p[4]=SpatialVector(.07f,-.05f,.5f)
for((base,x) in listOf(5 to .035f,9 to .01f,13 to -.015f,17 to -.04f)) {
for(j in 0..3)p[base+j]=SpatialVector(x,-.06f-.025f*j,.5f)
}
return p
}
@Test fun triangulatedMetersAreTransformedIntoTheHeadWorldFrame() {
val h=HandGestures(rightHand=false).estimate(openHand(),SpatialPose(SpatialVector(1f,2f,3f)),1_000_000_000)!!
assertEquals(3.5f,h.pose.position.z,.0001f)
assertTrue(h.pose.rotation.normalized()!=null);assertFalse(h.grip);assertEquals(0f,h.trigger,.0001f)
}
@Test fun mirroredHandsUseTheSamePalmNormalAndFingerDirection() {
val left=openHand().apply { this[9]=SpatialVector(0f,-.06f,.5f) }
val right=left.map { SpatialVector(-it.x,it.y,it.z) }
val world=SpatialPose(SpatialVector(1f,2f,3f),SpatialRotation(.2f,-.3f,.1f,.9f).normalized()!!)
for(turn in listOf(SpatialRotation(),SpatialRotation(y=1f,w=0f),
SpatialRotation(.3f,.1f,-.2f,.9f).normalized()!!)) {
val hands=listOf(left,right).mapIndexed { side,points ->
HandGestures(rightHand=side==1).estimate(points.map(turn::apply),world,1_000_000_000)!!
}
for(hand in hands) {
val rotation=hand.pose.rotation
assertVector(world.rotation.apply(turn.apply(SpatialVector(z=-1f))),rotation.apply(SpatialVector(y=1f)))
assertVector(world.rotation.apply(turn.apply(SpatialVector(y=-1f))),rotation.apply(SpatialVector(z=-1f)))
val x=rotation.apply(SpatialVector(x=1f));val y=rotation.apply(SpatialVector(y=1f))
assertVector(rotation.apply(SpatialVector(z=1f)),x.cross(y))
}
}
}
@Test fun rightHandOrientationDoesNotChangePinchOrGrip() {
val points=openHand()
points[4]=points[8]
for((tip,base) in listOf(12 to 9,16 to 13,20 to 17)) points[tip]=points[base]
val left=HandGestures(rightHand=false).estimate(points,SpatialPose(),1_000_000_000)!!
val right=HandGestures(rightHand=true).estimate(points.map { SpatialVector(-it.x,it.y,it.z) },SpatialPose(),1_000_000_000)!!
assertEquals(left.flags(),right.flags());assertEquals(1f,right.trigger,0f);assertTrue(right.grip)
assertEquals(left.timeNs,right.timeNs)
assertVector(SpatialVector(-left.pose.position.x,left.pose.position.y,left.pose.position.z),right.pose.position)
}
private fun assertVector(expected: SpatialVector,actual: SpatialVector) {
assertEquals(expected.x,actual.x,.0001f);assertEquals(expected.y,actual.y,.0001f);assertEquals(expected.z,actual.z,.0001f)
}
@Test fun pinchAndFingerClosureProduceTriggerAndGrip() {
val detector=HandGestures(rightHand=false);val p=openHand()
detector.estimate(p,SpatialPose(),1_000_000_000)
p[4]=p[8]
for((tip,base) in listOf(12 to 9,16 to 13,20 to 17)) p[tip]=p[base]
val pressed=detector.estimate(p,SpatialPose(),1_040_000_000)!!
assertEquals(1f,pressed.trigger,.0001f);assertTrue(pressed.grip)
val released=detector.estimate(openHand(),SpatialPose(),1_080_000_000)!!
assertEquals(0f,released.trigger,.0001f);assertFalse(released.grip)
}
@Test fun invalidGeometryAndImplausibleTemporalJumpAreRejected() {
val detector=HandGestures(rightHand=false)
assertNull(detector.estimate(emptyList(),SpatialPose(),1))
assertEquals(HandObservationOutcome.LANDMARK_COUNT_INVALID,detector.lastOutcome)
assertNull(detector.estimate(List(21) { SpatialVector() },SpatialPose(),1))
assertEquals(HandObservationOutcome.PALM_WIDTH_INVALID,detector.lastOutcome)
detector.estimate(openHand(),SpatialPose(),1_000_000_000)
assertNull(detector.estimate(openHand(),SpatialPose(SpatialVector(2f,0f,0f)),1_040_000_000))
assertEquals(HandObservationOutcome.PALM_MOTION_REJECTED,detector.lastOutcome)
detector.reset()
assertNotNull(detector.estimate(openHand(),SpatialPose(SpatialVector(2f,0f,0f)),1_080_000_000))
assertEquals(HandObservationOutcome.ACCEPTED,detector.lastOutcome)
}
@Test fun anUnreliableUnusedJointDoesNotHideThePalmOrDisableGestures() {
val points=openHand().map { it as SpatialVector? }.toMutableList()
points[6]=null
val hand=HandGestures(rightHand=false).estimate(points,SpatialPose(),1_000_000_000)!!
assertTrue(hand.valid);assertTrue(hand.triggerValid);assertTrue(hand.gripValid)
assertEquals(7,hand.flags())
}
@Test fun repeatedMotionRejectionsPreserveTheLastPoseAndGripHysteresis() {
val detector=HandGestures(false)
val closed=openHand().apply {
for((tip,base) in listOf(12 to 9,16 to 13,20 to 17)) this[tip]=this[base]
}
assertTrue(detector.estimate(closed,SpatialPose(),1_000_000_000)!!.grip)
for(time in listOf(1_040_000_000L,1_080_000_000L,1_120_000_000L)) {
assertNull(detector.estimate(openHand(),SpatialPose(SpatialVector(2f,0f,0f)),time))
assertEquals(HandObservationOutcome.PALM_MOTION_REJECTED,detector.lastOutcome)
detector.resetAfterRejection(detector.lastOutcome)
}
val partlyClosed=openHand().apply { this[12]=this[9];this[16]=this[13] }
val recovered=detector.estimate(partlyClosed,SpatialPose(),1_160_000_000)!!
assertTrue(recovered.grip)
assertEquals(1_160_000_000L,recovered.timeNs)
}
@Test fun motionRejectionDoesNotPreventReacquisitionAfterTheMotionWindow() {
val detector=HandGestures(false)
detector.estimate(openHand(),SpatialPose(),1_000_000_000)
val moved=SpatialPose(SpatialVector(2f,0f,0f))
for(time in listOf(1_040_000_000L,1_160_000_000L)) {
assertNull(detector.estimate(openHand(),moved,time))
detector.resetAfterRejection(detector.lastOutcome)
}
val reacquired=detector.estimate(openHand(),moved,1_200_000_000)!!
assertEquals(HandObservationOutcome.ACCEPTED,detector.lastOutcome)
assertEquals(1_200_000_000L,reacquired.timeNs)
}
@Test fun actualDetectionLossClearsMotionHistoryAndGripEvenAfterAMotionRejection() {
val detector=HandGestures(false)
val closed=openHand().apply {
for((tip,base) in listOf(12 to 9,16 to 13,20 to 17)) this[tip]=this[base]
}
assertTrue(detector.estimate(closed,SpatialPose(),1_000_000_000)!!.grip)
val moved=SpatialPose(SpatialVector(2f,0f,0f))
assertNull(detector.estimate(closed,moved,1_040_000_000))
detector.resetAfterRejection(detector.lastOutcome)
detector.resetAfterRejection(HandObservationOutcome.NO_MATCHING_DETECTION)
val partlyClosed=openHand().apply { this[12]=this[9];this[16]=this[13] }
val reacquired=detector.estimate(partlyClosed,moved,1_080_000_000)!!
assertFalse(reacquired.grip)
}
@Test fun occludedPinchKeepsThePalmAndIndependentGrip() {
val detector=HandGestures(rightHand=false)
val points=openHand().map { it as SpatialVector? }.toMutableList()
points[4]=points[8]
for((tip,base) in listOf(12 to 9,16 to 13,20 to 17)) points[tip]=points[base]
assertEquals(1f,detector.estimate(points,SpatialPose(),1_000_000_000)!!.trigger,0f)
points[4]=null
val hand=detector.estimate(points,SpatialPose(),1_080_000_000)!!
assertTrue(hand.valid);assertFalse(hand.triggerValid);assertEquals(0f,hand.trigger,0f)
assertTrue(hand.gripValid);assertTrue(hand.grip);assertEquals(5,hand.flags())
}
@Test fun occludedGripKeepsThePalmAndIndependentPinch() {
val points=openHand().map { it as SpatialVector? }.toMutableList()
points[4]=points[8];points[16]=SpatialVector(Float.NaN,0f,.5f)
val hand=HandGestures(rightHand=false).estimate(points,SpatialPose(),1_000_000_000)!!
assertTrue(hand.valid);assertTrue(hand.triggerValid);assertEquals(1f,hand.trigger,0f)
assertFalse(hand.gripValid);assertFalse(hand.grip);assertEquals(3,hand.flags())
}
@Test fun missingPalmStillRejectsTheController() {
for(index in listOf(0,5,9,13,17)) {
val points=openHand().map { it as SpatialVector? }.toMutableList();points[index]=null
val detector=HandGestures(rightHand=false)
assertNull(detector.estimate(points,SpatialPose(),1_000_000_000))
assertEquals(HandObservationOutcome.PALM_LANDMARK_MISSING,detector.lastOutcome)
assertNotNull(detector.estimate(openHand(),SpatialPose(),1_040_000_000))
assertEquals(HandObservationOutcome.ACCEPTED,detector.lastOutcome)
}
assertEquals(0,SpatialHand(triggerValid=true,gripValid=true).flags())
}
@Test fun degeneratePalmAxesAreDistinguishedFromMissingStereoLandmarks() {
val detector=HandGestures(rightHand=false)
for(middle in listOf(SpatialVector(0f,0f,.5f),SpatialVector(.04f,0f,.5f))) {
val points=openHand().apply { this[9]=middle }
assertNull(detector.estimate(points,SpatialPose(),1_000_000_000))
assertEquals(HandObservationOutcome.PALM_BASIS_INVALID,detector.lastOutcome)
}
assertNotNull(detector.estimate(openHand(),SpatialPose(),1_040_000_000))
assertEquals(HandObservationOutcome.ACCEPTED,detector.lastOutcome)
}
@Test fun agreeingImageCurlsAndPinchSurviveMissingTriangulatedFingertips() {
val image=openHand().apply {
this[4]=this[8]
for((tip,base) in listOf(12 to 9,16 to 13,20 to 17)) this[tip]=this[base]
}.map { SpatialVector(320f+it.x*1000f,240f+it.y*1000f) }
val points=openHand().map { it as SpatialVector? }.toMutableList()
for(index in listOf(4,8,12,16,20)) points[index]=null
val hand=HandGestures(false).estimate(points,SpatialPose(),1_000_000_000,image,image)!!
assertTrue(hand.valid);assertTrue(hand.gripValid);assertTrue(hand.grip)
assertTrue(hand.triggerValid);assertEquals(1f,hand.trigger,0f)
assertVector(HandGestures(false).estimate(openHand(),SpatialPose(),1_000_000_000)!!.pose.position,hand.pose.position)
}
@Test fun imageDisagreementInvalidatesGestureInsteadOfFallingBackToPressedDepth() {
val closed=openHand().apply {
this[4]=this[8]
for((tip,base) in listOf(12 to 9,16 to 13,20 to 17)) this[tip]=this[base]
}
fun image(points: List<SpatialVector>)=points.map { SpatialVector(it.x*1000f,it.y*1000f) }
val detector=HandGestures(false)
assertTrue(detector.estimate(closed,SpatialPose(),1_000_000_000,image(closed),image(closed))!!.grip)
val conflict=detector.estimate(closed,SpatialPose(),1_080_000_000,image(closed),image(openHand()))!!
assertTrue(conflict.valid);assertFalse(conflict.triggerValid);assertFalse(conflict.gripValid)
assertFalse(conflict.grip);assertEquals(0f,conflict.trigger,0f)
val missingView=detector.estimate(closed,SpatialPose(),1_120_000_000,image(closed),null)!!
assertTrue(missingView.valid);assertFalse(missingView.triggerValid);assertFalse(missingView.gripValid)
val released=detector.estimate(closed,SpatialPose(),1_160_000_000,image(openHand()),image(openHand()))!!
assertTrue(released.triggerValid);assertTrue(released.gripValid)
assertFalse(released.grip);assertEquals(0f,released.trigger,0f)
}
}
@@ -0,0 +1,90 @@
package com.droiddeck.launcher.glasses
import android.graphics.Bitmap
import android.graphics.Color
import com.google.mediapipe.framework.image.ByteBufferExtractor
import com.google.mediapipe.framework.image.MPImage
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk=[28],manifest=Config.NONE)
class HandImageBufferTest {
private fun bitmap(vararg values: Int) = Bitmap.createBitmap(values.size, 1, Bitmap.Config.ARGB_8888).apply {
setPixels(values.map { Color.rgb(it, it, it) }.toIntArray(), 0, values.size, 0, 0, values.size, 1)
}
private fun values(buffer: HandImageBuffer, bitmap: Bitmap): List<Int> = values(buffer.image(bitmap))
private fun values(image: MPImage): List<Int> = image.use {
assertEquals(MPImage.IMAGE_FORMAT_RGB, image.containedImageProperties.single().imageFormat)
val pixels = ByteBufferExtractor.extract(image)
assertTrue(pixels.isDirect)
assertEquals(image.width * image.height * 3, pixels.remaining())
List(image.width * image.height) {
val red = pixels.get().toInt() and 255
assertEquals(red, pixels.get().toInt() and 255)
assertEquals(red, pixels.get().toInt() and 255)
red
}
}
@Test fun liveDefaultUsesFullModelAndPreservesCameraPixelCoordinatesAndExposure() {
assertEquals(HandModel.FULL, HandModel.DEFAULT)
val input = byteArrayOf(32, 64, 96, 128.toByte(), 160.toByte(), 192.toByte())
val original = input.copyOf()
val image = HandImageBuffer().image(input, 3, 2)
assertEquals(3, image.width); assertEquals(2, image.height)
assertEquals(listOf(32, 64, 96, 128, 160, 192), values(image))
assertArrayEquals(original, input)
}
@Test fun directCameraAndRawBitmapInputsMatchAcrossEveryGrayValueAndFrameSize() {
val buffer = HandImageBuffer()
for ((width, height) in listOf(16 to 16, 3 to 2, 2 to 3, 1 to 1)) {
val input = ByteArray(width * height) { ((it * 73 + 32) and 255).toByte() }
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).apply {
setPixels(input.map { (it.toInt() and 255).let { value -> Color.rgb(value, value, value) } }.toIntArray(),
0, width, 0, 0, width, height)
}
try {
val expected = input.map { it.toInt() and 255 }
assertEquals(expected, values(buffer.image(input, width, height)))
assertEquals(expected, values(buffer.image(bitmap)))
} finally { bitmap.recycle() }
}
}
@Test(expected = IllegalArgumentException::class)
fun cameraDimensionsMustMatchTheGrayscaleFrame() {
HandImageBuffer().image(ByteArray(5), 3, 2)
}
@Test fun resizedAndSuccessiveFramesDoNotRetainOldPixelsOrExposure() {
val buffer = HandImageBuffer()
for (input in listOf(intArrayOf(32, 96, 160), intArrayOf(64, 128, 192), intArrayOf(0, 255))) {
val bitmap = bitmap(*input)
try {
assertEquals(input.toList(), values(buffer, bitmap))
} finally { bitmap.recycle() }
}
}
@Test fun closingInferenceImagesDoesNotRecycleReusableCameraBitmap() {
val bitmap=Bitmap.createBitmap(2,2,Bitmap.Config.ARGB_8888)
val buffer=HandImageBuffer()
repeat(3) { frame ->
bitmap.eraseColor(if(frame%2==0) Color.WHITE else Color.BLACK)
buffer.image(bitmap).use { image ->
assertEquals(2,image.width);assertEquals(2,image.height)
val pixels=ByteBufferExtractor.extract(image)
assertEquals(if(frame%2==0) 255 else 0,pixels.get(0).toInt() and 255)
}
assertFalse(bitmap.isRecycled)
}
bitmap.recycle()
}
}
@@ -0,0 +1,128 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class HandInferenceScheduleTest {
private fun frame(schedule: HandInferenceSchedule, time: Long, left: List<Int> = listOf(0),
right: List<Int> = left, rawLeft: Int = left.size, rawRight: Int = right.size): Int {
val maximum = schedule.select(time)
schedule.observe(time, left, right, rawLeft, rawRight)
return maximum
}
private fun stable(schedule: HandInferenceSchedule, side: Int = 0) {
for (time in listOf(0L, 80L, 160L)) assertEquals(2, frame(schedule, time, listOf(side)))
}
@Test fun requiresThreeStereoConsistentFramesBeforeSingleHandTracking() {
val schedule = HandInferenceSchedule()
stable(schedule)
assertEquals(1, frame(schedule, 240))
assertEquals(1, frame(schedule, 320))
assertEquals(2, frame(schedule, 400))
assertEquals(1, frame(schedule, 480))
}
@Test fun discoversSecondHandAtBothCameraInputCadences() {
for (cadence in listOf(40L, 80L)) {
val schedule = HandInferenceSchedule()
val searches = ArrayList<Long>()
for (time in 0L..1200L step cadence) {
if (frame(schedule, time) == 2) searches.add(time)
}
assertTrue(searches.size >= 5)
assertTrue(searches.zipWithNext().all { (first, second) -> second - first <= 240 })
}
}
@Test fun delayedFrameStillSearchesAtFirstFrameAfterDiscoveryDeadline() {
val schedule = HandInferenceSchedule()
stable(schedule)
assertEquals(1, frame(schedule, 240))
assertEquals(1, frame(schedule, 399))
assertEquals(2, frame(schedule, 479))
assertEquals(1, frame(schedule, 559))
}
@Test fun secondHandDiscoveredBySearchKeepsBothHandDetectorsSelected() {
val schedule = HandInferenceSchedule()
stable(schedule)
assertEquals(1, frame(schedule, 240))
assertEquals(2, frame(schedule, 400, listOf(0, 1)))
for (time in listOf(480L, 560L, 640L)) assertEquals(2, frame(schedule, time, listOf(1, 0)))
for (time in listOf(720L, 800L, 880L)) assertEquals(2, frame(schedule, time))
assertEquals(1, frame(schedule, 960))
}
@Test fun anyCameraLossImmediatelyReturnsToFullSearch() {
for ((left, right) in listOf(emptyList<Int>() to listOf(0), listOf(0) to emptyList())) {
val schedule = HandInferenceSchedule()
stable(schedule)
assertEquals(1, frame(schedule, 240, left, right))
assertEquals(2, frame(schedule, 320))
}
}
@Test fun ambiguousOrFilteredDetectionsNeverMaintainSingleHandMode() {
val invalid = listOf(
listOf(0) to listOf(1),
listOf(0, 0) to listOf(0),
listOf(0) to listOf(0, 0),
listOf(-1) to listOf(-1),
listOf(2) to listOf(2)
)
for ((left, right) in invalid) {
val schedule = HandInferenceSchedule()
stable(schedule)
frame(schedule, 240, left, right)
assertEquals(2, frame(schedule, 320))
}
for ((rawLeft, rawRight) in listOf(2 to 1, 1 to 2, 0 to 1, -1 to 1)) {
val schedule = HandInferenceSchedule()
stable(schedule)
frame(schedule, 240, rawLeft = rawLeft, rawRight = rawRight)
assertEquals(2, frame(schedule, 320))
}
}
@Test fun changingSidesRequiresFreshFullSearchStability() {
val schedule = HandInferenceSchedule()
stable(schedule)
assertEquals(1, frame(schedule, 240, listOf(1)))
for (time in listOf(320L, 400L, 480L)) assertEquals(2, frame(schedule, time, listOf(1)))
assertEquals(1, frame(schedule, 560, listOf(1)))
}
@Test fun inconsistentInitialSidesCannotAccumulateStability() {
val schedule = HandInferenceSchedule()
for (index in 0..8) assertEquals(2, frame(schedule, index * 80L, listOf(index % 2)))
}
@Test fun resetDropsSessionState() {
val schedule = HandInferenceSchedule()
stable(schedule)
frame(schedule, 240)
schedule.reset()
stable(schedule, 1)
assertEquals(1, frame(schedule, 240, listOf(1)))
}
@Test fun longGapAndNonmonotonicTimeRequireFreshStability() {
for (time in listOf(160L, 120L, 561L, -1L)) {
val schedule = HandInferenceSchedule()
stable(schedule)
assertEquals(2, frame(schedule, time))
assertEquals(2, frame(schedule, maxOf(0, time) + 80))
}
}
@Test fun unobservedInferenceAndMismatchedObservationResetSchedule() {
val schedule = HandInferenceSchedule()
stable(schedule)
assertEquals(1, schedule.select(240))
assertEquals(2, frame(schedule, 320))
schedule.observe(240, listOf(0), listOf(0))
assertEquals(2, frame(schedule, 400))
}
}
@@ -0,0 +1,65 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class HandObservationTest {
private val time=1_000_000_000L
private fun session()=SpatialSession().apply {
reset(7,1.6f)
accept(SpatialSample(SpatialPose(),time,7,true))
}
private fun advance(session: SpatialSession, ms: Long, valid: Boolean=true, x: Float=.1f) {
session.accept(SpatialSample(SpatialPose(SpatialVector(x=x)),time+ms*1_000_000,7,valid))
}
@Test fun lateCameraConfirmationUsesTheCapturePoseInsteadOfTheLatestPose() {
val session=session()
val observation=HandObservation(time,7,session.trackingFrameGeneration)
assertNull(observation.headPose(session,false,time))
advance(session,150)
assertEquals(SpatialPose(),observation.headPose(session,true,time+150_000_000))
assertEquals(time,observation.timeNs)
}
@Test fun missingConfirmationOrExpiredObservationCannotPublish() {
val session=session()
val observation=HandObservation(time,7,session.trackingFrameGeneration)
advance(session,180)
assertNull(observation.headPose(session,false,time+180_000_000))
assertNotNull(observation.headPose(session,true,time+200_000_000))
assertNull(observation.headPose(session,true,time+200_000_001))
assertNull(observation.headPose(session,true,time-1))
}
@Test fun longerHandWindowDoesNotExtendHeadFreshness() {
val session=session()
val observation=HandObservation(time,7,session.trackingFrameGeneration)
assertNotNull(observation.headPose(session,true,time+100_000_000))
assertNull(observation.headPose(session,true,time+100_000_001))
advance(session,150)
assertNotNull(observation.headPose(session,true,time+150_000_000))
}
@Test fun trackingLossAndRecoveredDiscontinuityInvalidateInFlightInference() {
val session=session()
val observation=HandObservation(time,7,session.trackingFrameGeneration)
advance(session,20,valid=false)
assertNull(observation.headPose(session,true,time+20_000_000))
advance(session,30,x=5f)
assertNull(observation.headPose(session,true,time+30_000_000))
session.setFloor(1.6f,time+30_000_000)
assertTrue(session.valid(time+30_000_000))
assertNull(observation.headPose(session,true,time+30_000_000))
}
@Test fun reconnectAndMissingHistoryCannotBorrowAnotherPose() {
val session=session()
val observation=HandObservation(time,7,session.trackingFrameGeneration)
advance(session,40)
assertNull(observation.copy(timeNs=time+20_000_000).headPose(session,true,time+40_000_000))
session.reset(8,1.6f)
session.accept(SpatialSample(SpatialPose(),time+50_000_000,8,true))
assertNull(observation.headPose(session,true,time+50_000_000))
}
}
@@ -0,0 +1,119 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class HandProcessingRateTest {
private fun sample(rate: HandProcessingRate, processingMs: Long = 35, ageMs: Long = 65,
healthy: Boolean = true): Int =
rate.observe(processingMs * 1_000_000, ageMs * 1_000_000, healthy)
private fun fastest(rate: HandProcessingRate) {
repeat(16) { sample(rate) }
assertEquals(800, rate.permille)
}
@Test fun beginsConservativelyAndRequiresEightHealthySamplesPerIncrease() {
val rate = HandProcessingRate()
assertEquals(500, rate.permille)
repeat(7) { assertEquals(500, sample(rate)) }
assertEquals(600, sample(rate))
repeat(7) { assertEquals(600, sample(rate)) }
assertEquals(800, sample(rate))
repeat(20) { assertEquals(800, sample(rate)) }
}
@Test fun onlySelectsFifteenHzWhenTwentyHzHasInsufficientHeadroom() {
val rate = HandProcessingRate()
repeat(8) { sample(rate, 50, 80) }
assertEquals(600, rate.permille)
repeat(30) { assertEquals(600, sample(rate, 50, 80)) }
}
@Test fun slowProcessingStaysAtTwelvePointFiveHz() {
val rate = HandProcessingRate()
repeat(30) { assertEquals(500, sample(rate, 60, 100)) }
}
@Test fun sustainedProcessingPastTheCurrentPeriodReducesRate() {
val rate = HandProcessingRate()
fastest(rate)
repeat(2) { assertEquals(800, sample(rate, 70)) }
assertEquals(600, sample(rate, 70))
repeat(7) { assertEquals(600, sample(rate, 70)) }
assertEquals(500, sample(rate, 70))
repeat(7) { assertEquals(500, sample(rate)) }
}
@Test fun largeProcessingSpikeSkipsDirectlyToConservativeRate() {
val rate = HandProcessingRate()
fastest(rate)
assertEquals(500, sample(rate, 80, 100))
}
@Test fun ageLimitReducesRateWithoutWaitingForProcessingAverage() {
val rate = HandProcessingRate()
fastest(rate)
assertEquals(500, sample(rate, ageMs = 140))
repeat(7) { assertEquals(500, sample(rate)) }
assertEquals(600, sample(rate))
}
@Test fun agingFramesCannotRaiseTheRate() {
val rate = HandProcessingRate()
repeat(30) { assertEquals(500, sample(rate, ageMs = 111)) }
repeat(8) { sample(rate, ageMs = 100) }
assertEquals(600, rate.permille)
repeat(30) { assertEquals(600, sample(rate, ageMs = 91)) }
}
@Test fun marginalHeadroomInterruptsConsecutiveUpshiftSamples() {
val rate = HandProcessingRate()
repeat(7) { sample(rate, 55) }
assertEquals(500, sample(rate, 60))
repeat(7) { assertEquals(500, sample(rate)) }
assertEquals(600, sample(rate))
}
@Test fun recoveryMustAlsoBringAverageProcessingBelowUpshiftThreshold() {
val rate = HandProcessingRate()
sample(rate, 100)
repeat(8) { assertEquals(500, sample(rate)) }
repeat(8) { sample(rate) }
assertEquals(600, rate.permille)
}
@Test fun trackingLossAndInvalidTimingResetTheRateAndHistory() {
for (invalid in listOf(Triple(35_000_000L, 65_000_000L, false),
Triple(0L, 65_000_000L, true), Triple(-1L, 65_000_000L, true),
Triple(35_000_000L, -1L, true))) {
val rate = HandProcessingRate()
fastest(rate)
assertEquals(500, rate.observe(invalid.first, invalid.second, invalid.third))
repeat(7) { assertEquals(500, sample(rate)) }
assertEquals(600, sample(rate))
}
}
@Test fun lifecycleResetDropsOldHeadroom() {
val rate = HandProcessingRate()
fastest(rate)
assertEquals(500, rate.reset())
repeat(7) { assertEquals(500, sample(rate)) }
assertEquals(600, sample(rate))
}
@Test fun periodicDiscoverySearchesPermitFifteenHzButNotTwentyHz() {
val rate = HandProcessingRate()
repeat(8) { sample(rate, if (it % 3 == 2) 74 else 36, 90) }
assertEquals(600, rate.permille)
for (frame in 8 until 90) {
assertEquals(600, sample(rate, if (frame % 3 == 2) 74 else 36, 90))
}
}
@Test fun continuouslySlowDiscoveryCannotRaiseRate() {
val rate = HandProcessingRate()
repeat(30) { assertEquals(500, sample(rate, 70, 100)) }
}
}
@@ -0,0 +1,119 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class HandTrackingMetricsTest {
private val missing = HandObservationDiagnostic(HandObservationOutcome.NO_MATCHING_DETECTION)
private fun accepted(trigger: Float = 0f, grip: Boolean = false) = HandObservationDiagnostic(
HandObservationOutcome.ACCEPTED, listOf(0,5,9,13,17), trigger, true, grip, true)
@Test fun cameraSequencesCountOnceAndSkippedPairsAreNotInvented() {
val metrics = HandTrackingMetrics()
metrics.observe(0, true, accepted(), accepted())
metrics.observe(10, true, accepted(), missing)
metrics.observe(10, true, missing, accepted())
metrics.observe(9, true, missing, accepted())
metrics.observe(15, true, accepted(), missing)
val result = metrics.snapshot()
assertEquals(2L, result.processedPairs)
assertEquals(2L, result.inferencePairs)
assertEquals(2L, result.left.accepted)
assertEquals(0L, result.right.accepted)
assertEquals(2L, result.right.observations)
assertEquals(0L, result.left.trackingLosses)
}
@Test fun setupAndUnavailableFramesRemainDistinctFromInference() {
val metrics = HandTrackingMetrics()
val setup = HandObservationDiagnostic(HandObservationOutcome.CALIBRATION_REQUIRED)
val unavailable = HandObservationDiagnostic(HandObservationOutcome.INFERENCE_UNAVAILABLE)
metrics.observe(1, false, setup, setup)
metrics.observe(2, false, unavailable, unavailable)
metrics.observe(3, true, accepted(), missing)
val result = metrics.snapshot()
assertEquals(3L, result.processedPairs)
assertEquals(1L, result.inferencePairs)
assertEquals(3L, result.left.observations)
assertEquals(1L, result.left.accepted)
assertEquals(1L, result.left.outcomes[HandObservationOutcome.CALIBRATION_REQUIRED])
assertEquals(1L, result.left.outcomes[HandObservationOutcome.INFERENCE_UNAVAILABLE])
assertEquals(3L, result.right.maximumConsecutiveRejected)
}
@Test fun countsGestureLossesIndependentlyFromPalmLossAndOtherHand() {
val metrics = HandTrackingMetrics()
metrics.observe(1, true, accepted(), accepted())
metrics.observe(2, true, accepted().copy(triggerGeometryValid = false), accepted())
metrics.observe(3, true, accepted().copy(gripGeometryValid = false), accepted())
metrics.observe(4, true, missing, accepted())
metrics.observe(5, true, missing, accepted())
val result = metrics.snapshot()
assertEquals(3L, result.left.accepted)
assertEquals(1L, result.left.trackingLosses)
assertEquals(2L, result.left.triggerLosses)
assertEquals(1L, result.left.gripLosses)
assertEquals(2L, result.left.maximumConsecutiveRejected)
assertEquals(0L, result.right.trackingLosses)
assertEquals(0L, result.right.triggerLosses)
assertEquals(0L, result.right.gripLosses)
}
@Test fun rejectedObservationRetainsRawEstimatesButCannotCountUsableInputs() {
val metrics = HandTrackingMetrics()
val stale = accepted(1f, true).copy(outcome = HandObservationOutcome.STALE_OBSERVATION)
assertEquals(1f, stale.triggerEstimate, 0f)
assertTrue(stale.gripEstimate)
assertTrue(stale.triggerGeometryValid)
assertFalse(stale.accepted)
assertFalse(stale.triggerValid)
assertFalse(stale.gripValid)
metrics.observe(1, true, stale, stale)
val side = metrics.snapshot().left
assertEquals(0L, side.accepted)
assertEquals(0L, side.triggerValid)
assertEquals(0L, side.gripValid)
assertEquals(0L, side.triggerAboveClickThreshold)
assertEquals(0L, side.gripPressed)
}
@Test fun thresholdCountsDescribeValidEstimatesWithoutClaimingDriverPresses() {
val metrics = HandTrackingMetrics()
for ((index, trigger) in listOf(0f, .25f, .65f, 1f).withIndex())
metrics.observe(index + 1L, true, accepted(trigger, index == 3), missing)
metrics.observe(5, true, accepted(1f, true).copy(triggerGeometryValid = false, gripGeometryValid = false), missing)
val side = metrics.snapshot().left
assertEquals(5L, side.accepted)
assertEquals(4L, side.triggerValid)
assertEquals(4L, side.gripValid)
assertEquals(1L, side.triggerAboveClickThreshold)
assertEquals(1L, side.triggerReleased)
assertEquals(1L, side.gripPressed)
}
@Test fun snapshotsRetainTheirCountsWhenLaterFramesArrive() {
val metrics = HandTrackingMetrics()
metrics.observe(1, true, accepted(), missing)
val first = metrics.snapshot()
metrics.observe(2, true, missing, accepted())
assertEquals(1L, first.processedPairs)
assertEquals(mapOf(HandObservationOutcome.ACCEPTED to 1L), first.left.outcomes)
assertEquals(0L, first.left.trackingLosses)
assertEquals(2L, metrics.snapshot().processedPairs)
assertEquals(1L, metrics.snapshot().left.trackingLosses)
}
@Test fun aNewTrackingConnectionStartsCountsAndSequenceFromZero() {
val old = HandTrackingMetrics()
old.observe(500, true, accepted(), missing)
old.observe(501, true, missing, missing)
val current = HandTrackingMetrics()
assertEquals(0L, current.snapshot().processedPairs)
current.observe(1, true, accepted(), accepted())
val result = current.snapshot()
assertEquals(1L, result.processedPairs)
assertEquals(1L, result.left.accepted)
assertEquals(0L, result.left.trackingLosses)
assertEquals(0L, result.right.maximumConsecutiveRejected)
}
}
@@ -0,0 +1,23 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class JoyConFlowGateTest {
@Test fun missingDetectionsCannotExtendTheSeedLifetime() {
val seed = 1_000_000_000L
assertTrue(JoyConFlowGate.fresh(seed, seed + 640_000_000, seed + 720_000_000))
assertFalse(JoyConFlowGate.fresh(seed, seed + 720_000_000, seed + 800_000_000))
assertFalse(JoyConFlowGate.fresh(seed, seed, seed + 200_000_001))
assertFalse(JoyConFlowGate.fresh(seed, seed, seed))
}
@Test fun featureMustBeVisibleAndReturnToItsOriginalLocation() {
assertTrue(JoyConFlowGate.point(110.0,105.0,100.5,100.5,100.0,100.0,5f,true,true,640,480))
assertFalse(JoyConFlowGate.point(110.0,105.0,103.0,100.0,100.0,100.0,5f,true,true,640,480))
assertFalse(JoyConFlowGate.point(110.0,105.0,100.0,100.0,100.0,100.0,30f,true,true,640,480))
assertFalse(JoyConFlowGate.point(110.0,105.0,100.0,100.0,100.0,100.0,5f,false,true,640,480))
assertFalse(JoyConFlowGate.point(Double.NaN,105.0,100.0,100.0,100.0,100.0,5f,true,true,640,480))
assertFalse(JoyConFlowGate.point(640.0,105.0,100.0,100.0,100.0,100.0,5f,true,true,640,480))
}
}
@@ -0,0 +1,69 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class JoyConWristPositionTest {
private fun points(depth: Float = .4f): MutableList<SpatialVector?> = MutableList<SpatialVector?>(21) { null }.apply {
this[0] = SpatialVector(0f, 0f, depth)
this[5] = SpatialVector(.035f, -.06f, depth)
this[9] = SpatialVector(0f, -.065f, depth)
this[13] = SpatialVector(-.02f, -.06f, depth)
this[17] = SpatialVector(-.045f, -.05f, depth)
}
@Test fun grippingControllerNeedsOnlyTheWristAndNeverGeneratesFingerGestures() {
val estimator = JoyConWristPosition()
val full = estimator.estimate(points(), SpatialPose(), 1_000_000_000)!!
val occluded = MutableList<SpatialVector?>(21) { null }.apply { this[0] = points()[0] }
val grip = estimator.estimate(occluded, SpatialPose(), 1_080_000_000)!!
assertEquals(occluded[0], grip.pose.position)
assertEquals(full.pose.position, grip.pose.position)
assertTrue(grip.valid)
assertFalse(grip.triggerValid)
assertFalse(grip.gripValid)
assertNull(HandGestures(false).estimate(occluded, SpatialPose(), 1_080_000_000))
}
@Test fun erroneousKnucklesCannotMoveOrRejectTheWrist() {
val malformed = points().apply {
this[5] = SpatialVector(Float.NaN, 0f, .4f)
this[9] = SpatialVector(4f, 5f, 6f)
this[13] = this[0]
this[17] = null
}
val grip = JoyConWristPosition().estimate(malformed, SpatialPose(), 1)!!
assertEquals(malformed[0], grip.pose.position)
}
@Test fun measuredExtensionMovesTheWorldPositionByTheSameDistance() {
val estimator = JoyConWristPosition()
val world = SpatialPose(SpatialVector(1f, 2f, 3f), SpatialRotation(y = 1f, w = 0f))
val near = estimator.estimate(points(.3f).take(1), world, 1_000_000_000)!!
val far = estimator.estimate(points(.7f).take(1), world, 1_300_000_000)!!
assertEquals(SpatialVector(1f, 2f, 2.7f), near.pose.position)
assertEquals(-.4f, far.pose.position.z - near.pose.position.z, .0001f)
assertEquals(near.pose.position.x, far.pose.position.x, .0001f)
assertEquals(1_300_000_000, far.timeNs)
}
@Test fun missingOrNonfiniteWristIsRejectedEvenWithVisibleKnuckles() {
for (wrist in listOf(null, SpatialVector(Float.NaN, 0f, .4f), SpatialVector(0f, 0f, Float.POSITIVE_INFINITY))) {
val estimator = JoyConWristPosition()
assertNull(estimator.estimate(points().apply { this[0] = wrist }, SpatialPose(), 1))
assertEquals(HandObservationOutcome.WRIST_LANDMARK_MISSING, estimator.lastOutcome)
}
assertNull(JoyConWristPosition().estimate(emptyList(), SpatialPose(), 1))
}
@Test fun implausibleJumpsAndRepeatedFramesStayRejectedWithoutPoisoningTheLastPosition() {
val estimator = JoyConWristPosition()
assertNotNull(estimator.estimate(points(), SpatialPose(), 1_000_000_000))
assertNull(estimator.estimate(points(1.2f), SpatialPose(), 1_080_000_000))
assertEquals(HandObservationOutcome.WRIST_MOTION_REJECTED, estimator.lastOutcome)
assertNull(estimator.estimate(points(), SpatialPose(), 1_000_000_000))
assertNotNull(estimator.estimate(points(.45f).take(1), SpatialPose(), 1_090_000_000))
estimator.reset()
assertNotNull(estimator.estimate(points(1.2f).take(1), SpatialPose(), 1_100_000_000))
}
}
@@ -0,0 +1,73 @@
package com.droiddeck.launcher.glasses
import android.app.Application
import android.os.SystemClock
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.json.JSONObject
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.io.File
@RunWith(RobolectricTestRunner::class)
@Config(application = Application::class)
class LumaRgbDiagnosticTest {
private val context get() = RuntimeEnvironment.getApplication()
private val statusFile get() = File(context.filesDir, "spatial/rgb-diagnostic.json")
@Before fun reset() { statusFile.delete(); statusFile.parentFile?.mkdirs() }
@Test fun aDeadWorkerOrPreviousBootCannotLeaveCapturePermanentlyBusy() {
for (started in listOf(SystemClock.elapsedRealtime() - 16_000, SystemClock.elapsedRealtime() + 5_000)) {
statusFile.writeText("""{"state":"capturing","startedMs":$started}""")
assertEquals("failed", LumaRgbDiagnostic.status(context).getString("state"))
}
}
@Test fun onlyPendingCapturesExpire() {
statusFile.writeText("""{"state":"completed","path":"saved-capture"}""")
assertEquals("completed", LumaRgbDiagnostic.status(context).getString("state"))
statusFile.writeText("""{"state":"capturing","startedMs":${SystemClock.elapsedRealtime()}}""")
assertEquals("capturing", LumaRgbDiagnostic.status(context).getString("state"))
}
@Test fun longerReliabilityCaptureRemainsBusyUntilItsBoundedDeadline() {
val started = SystemClock.elapsedRealtime() - 20_000
statusFile.writeText("""{"state":"capturing","startedMs":$started,"timeoutMs":45000}""")
assertEquals("capturing", LumaRgbDiagnostic.status(context).getString("state"))
statusFile.writeText("""{"state":"capturing","startedMs":${started-30_000},"timeoutMs":999999}""")
assertEquals("failed", LumaRgbDiagnostic.status(context).getString("state"))
}
@Test fun terminalResultWaitsForWorkerExitBeforeAnotherRequest() {
for (result in listOf("completed","failed")) {
val record=JSONObject().put("state",result).put("workerPid",123).put("startedMs",1000)
val finishing=LumaRgbDiagnostic.resolveStatus(JSONObject(record.toString()),2000,true)
assertEquals("capturing",finishing.getString("state"))
assertEquals("finishing",finishing.getString("phase"))
assertEquals(result,finishing.getString("resultState"))
assertTrue(finishing.getBoolean("busy"))
val exited=LumaRgbDiagnostic.resolveStatus(record,2000,false)
assertEquals(result,exited.getString("state"))
assertFalse(exited.getBoolean("busy"))
}
}
@Test fun deadWorkerFailsImmediatelyAndExpiredWorkerRemainsBusyOnlyUntilExit() {
val record=JSONObject().put("state","capturing").put("workerPid",123).put("startedMs",1000)
val dead=LumaRgbDiagnostic.resolveStatus(JSONObject(record.toString()),2000,false)
assertEquals("failed",dead.getString("state"))
assertFalse(dead.getBoolean("busy"))
val expired=LumaRgbDiagnostic.resolveStatus(JSONObject(record.toString()),17000,true)
assertEquals("failed",expired.getString("state"))
assertTrue(expired.getBoolean("busy"))
val exited=LumaRgbDiagnostic.resolveStatus(record,17000,false)
assertEquals("failed",exited.getString("state"))
assertFalse(exited.getBoolean("busy"))
}
}
@@ -0,0 +1,77 @@
package com.droiddeck.launcher.glasses
import java.nio.ByteBuffer
import java.nio.ByteOrder
import org.junit.After
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class SpatialControllerModeTest {
private fun snapshot(): ByteBuffer = (SpatialTracking::class.java.getDeclaredField("buffer")
.apply { isAccessible = true }.get(null) as ByteBuffer).duplicate().order(ByteOrder.LITTLE_ENDIAN)
@After fun cleanUp() {
SpatialTracking.setSteadyHandAim(false)
SpatialTracking.setControllers(false, false)
SpatialTracking.disconnect()
}
@Test fun joyConModeStartsStereoWithoutSelectingGestureControllersAndPersistsAcrossLoss() {
SpatialTracking.connect(0, 1, null, true, 1.6f, false, true)
assertTrue(SpatialTracking.handsEnabled)
assertEquals(4, snapshot().getInt(4))
assertEquals(5, snapshot().getInt(32))
SpatialTracking.disconnect()
assertFalse(SpatialTracking.handsEnabled)
assertEquals(5, snapshot().getInt(32))
assertEquals(0, snapshot().getInt(76))
SpatialTracking.setControllers(false, false)
assertEquals(1, snapshot().getInt(32))
}
@Test fun steadyAimIsOptInAndOnlyAppliesToBareHands() {
assertFalse(GlassesSettings().steadyHandAim)
SpatialTracking.connect(0, 4, null, true, 1.6f, true)
assertEquals(3, snapshot().getInt(32))
SpatialTracking.setSteadyHandAim(true)
assertEquals(11, snapshot().getInt(32))
SpatialTracking.setControllers(false, true)
assertEquals(5, snapshot().getInt(32))
SpatialTracking.setControllers(true, false)
assertEquals(11, snapshot().getInt(32))
SpatialTracking.setSteadyHandAim(false)
assertEquals(3, snapshot().getInt(32))
SpatialTracking.connect(0, 5, null, true, 1.6f, true, steadyAim = true)
assertEquals(11, snapshot().getInt(32))
SpatialTracking.useLegacyTracking()
assertEquals(0, snapshot().getInt(32))
}
@Test fun openingOtherGlassesClearsTheRetainedUltraMode() {
SpatialTracking.connect(0, 3, null, true, 1.6f, false, true)
SpatialTracking.disconnect()
assertEquals(5, snapshot().getInt(32))
SpatialTracking.useLegacyTracking()
assertEquals(0, snapshot().getInt(32))
assertFalse(SpatialTracking.enabled)
assertFalse(SpatialTracking.handsEnabled)
}
@Test fun switchingControllerModesClearsOldStereoObservations() {
SpatialTracking.connect(0, 2, null, true, 1.6f, true)
assertEquals(3, snapshot().getInt(32))
SpatialTracking.setControllers(false, true)
assertTrue(SpatialTracking.handsEnabled)
assertEquals(5, snapshot().getInt(32))
SpatialTracking.setControllers(true, false)
assertEquals(3, snapshot().getInt(32))
SpatialTracking.setControllers(false, false)
assertFalse(SpatialTracking.handsEnabled)
assertEquals(1, snapshot().getInt(32))
}
}
@@ -0,0 +1,25 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
import java.util.Locale
class SpatialMeasurementsTest {
@Test fun validationKeepsExactImperialAndMetricMeasurements() {
assertEquals(0.3048f, CalibrationDistance.INCHES_12.meters, 0.000001f)
assertEquals(0.3f, CalibrationDistance.CENTIMETERS_30.meters, 0.000001f)
assertEquals(0.508f, CalibrationDistance.INCHES_20.meters, 0.000001f)
assertEquals(0.5f, CalibrationDistance.CENTIMETERS_50.meters, 0.000001f)
}
@Test fun heightCarriesRoundedInchesIntoFeetAndAlwaysShowsBothUnits() {
val previous = Locale.getDefault()
try {
Locale.setDefault(Locale.US)
assertEquals("160 cm (5 ft 3.0 in)", SpatialMeasurements.height(160))
assertEquals("183 cm (6 ft 0.0 in)", SpatialMeasurements.height(183))
assertEquals("1.0 m (3.3 ft)", SpatialMeasurements.distance(1f))
assertEquals("50.8 cm (20.00 in)", SpatialMeasurements.validation(0.508f))
} finally { Locale.setDefault(previous) }
}
}
@@ -0,0 +1,310 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
import kotlin.math.sqrt
class SpatialPoseTest {
private val t=1_000_000_000L
private fun sample(x: Float=0f,y: Float=0f,z: Float=0f,time: Long=t,generation: Long=7,valid: Boolean=true,
rotation: SpatialRotation=SpatialRotation())=SpatialSample(SpatialPose(SpatialVector(x,y,z),rotation),time,generation,valid)
private fun session()=SpatialSession().apply { reset(7,1.6f);accept(sample()) }
private fun near(expected: Float,actual: Float)=assertEquals(expected,actual,0.0001f)
@Test fun sdkPoseKeepsMetersAndCanonicalAxes() {
val p=SpatialPose.fromSdk(floatArrayOf(1f,2f,-3f,1.1f,0f,0f,0f))!!
assertEquals(SpatialVector(1f,2f,-3f),p.position);near(1f,p.rotation.w)
assertNull(SpatialPose.fromSdk(floatArrayOf(0f,0f,0f,0f,0f,0f,0f)))
assertNull(SpatialPose.fromSdk(floatArrayOf(Float.NaN,0f,0f,1f,0f,0f,0f)))
}
@Test fun quaternionRotationAndInverseAgree() {
val q=SpatialRotation(y=sqrt(0.5f),w=sqrt(0.5f))
val p=SpatialPose(SpatialVector(1f,2f,3f),q)
val front=q.apply(SpatialVector(z=-1f));near(-1f,front.x);near(0f,front.z)
val inverse=p.inverse()*p;near(0f,inverse.position.length());near(1f,inverse.rotation.w)
}
@Test fun floorSurvivesCrouchedRecenter() {
val s=session();s.accept(sample(x=.2f,y=-.3f,time=t+10));s.recenter(false,t+10)
near(1.3f,s.view(false)!!.position.y);near(0f,s.view(false)!!.position.x)
s.accept(sample(x=.2f,time=t+20));near(1.6f,s.view(false)!!.position.y)
}
@Test fun screenAndVrOriginsStayIndependent() {
val s=session();s.accept(sample(x=.2f,time=t+10));s.recenter(true,t+10)
near(0f,s.view(true)!!.position.x);near(.2f,s.view(false)!!.position.x)
s.accept(sample(x=.3f,time=t+20));s.recenter(false,t+20)
near(.1f,s.view(true)!!.position.x);near(0f,s.view(false)!!.position.x)
}
@Test fun screenRecenterPreservesRecliningRotation() {
val s=SpatialSession();s.reset(7,1.6f)
val tilt=SpatialRotation(x=sqrt(.5f),w=sqrt(.5f))
s.accept(sample(rotation=tilt));near(1f,s.view(true)!!.rotation.w)
near(tilt.x,s.view(false)!!.rotation.x)
}
@Test fun staleHeadFreezesViewAndRejectsOldConnection() {
val s=session();s.accept(sample(x=.1f,time=t+10));val last=s.view(false)
assertFalse(s.accept(sample(x=99f,time=t+20,generation=6)))
s.accept(sample(time=t+20,valid=false));assertEquals(last,s.view(false));assertFalse(s.valid(t+21))
s.accept(sample(x=.1f,time=t+30));assertFalse(s.valid(t+100_000_031))
assertFalse(s.accept(sample(x=Float.NaN,time=t+40)));assertEquals(last,s.view(false))
}
@Test fun discontinuityRequiresExplicitRecovery() {
val s=session();assertFalse(s.accept(sample(x=5f,time=t+10)))
assertTrue(s.needsRecenter);assertFalse(s.valid(t+10));near(0f,s.view(false)!!.position.x)
s.recenter(false,t+10);assertTrue(s.recenterPending);assertFalse(s.valid(t+10))
for (i in 1..6) s.accept(sample(x=5f,time=t+i*50_000_000L))
assertTrue(s.valid(t+300_000_000));near(0f,s.view(false)!!.position.x)
s.reset(8,1.6f,true);s.accept(sample(generation=8,time=t+400_000_000));assertFalse(s.valid(t+400_000_000))
s.recenter(false,t+400_000_000)
for (i in 1..6) s.accept(sample(generation=8,time=t+400_000_000+i*50_000_000L))
assertTrue(s.valid(t+700_000_000))
}
@Test fun recenterDuringLostTrackingWaitsForSteadySamplesInsteadOfDroppingTheRequest() {
val s=session();s.accept(sample(time=t+10,valid=false));val frozen=s.view(false)
s.recenter(false,t+20);assertTrue(s.recenterPending)
s.accept(sample(x=.2f,time=t+50_000_000))
assertFalse(s.valid(t+50_000_000));assertEquals(frozen,s.view(false))
for(i in 2..6) s.accept(sample(x=.2f,time=t+i*50_000_000L))
assertFalse(s.recenterPending);assertTrue(s.valid(t+300_000_000));near(0f,s.view(false)!!.position.x)
near(1.6f,s.view(false)!!.position.y)
}
@Test fun staleRecenterWaitsAndRepeatedTapsDoNotRestartTheWait() {
val s=session();s.recenter(true,t+200_000_000);assertTrue(s.recenterPending)
s.accept(sample(x=.2f,time=t+250_000_000))
s.recenter(true,t+250_000_000)
for(i in 1..5) s.accept(sample(x=.2f,time=t+250_000_000+i*50_000_000L))
assertFalse(s.recenterPending);near(0f,s.view(true)!!.position.x)
near(.2f,s.view(false)!!.position.x)
}
@Test fun unstableOrInterruptedRecoveryCannotAcceptANewOrigin() {
val s=session();s.accept(sample(x=5f,time=t+10));s.recenter(false,t+10)
for(i in 1..8) s.accept(sample(x=5f+(i%2)*.1f,time=t+i*50_000_000L))
assertTrue(s.recenterPending);assertTrue(s.needsRecenter)
s.accept(sample(x=5f,time=t+450_000_000))
s.accept(sample(x=5f,time=t+500_000_000,valid=false))
s.accept(sample(x=5f,time=t+550_000_000))
assertTrue(s.recenterPending)
for(i in 1..5) s.accept(sample(x=5f,time=t+550_000_000+i*50_000_000L))
assertFalse(s.recenterPending);assertTrue(s.valid(t+800_000_000))
}
@Test fun disconnectCancelsQueuedRecenterAndOldSamplesCannotCompleteIt() {
val s=session();s.recenter(false,t+200_000_000);assertTrue(s.recenterPending)
s.reset(8,1.6f,true);assertFalse(s.recenterPending)
for(i in 1..10) s.accept(sample(time=t+i*50_000_000L))
assertTrue(s.needsRecenter);assertNull(s.latest())
s.accept(sample(generation=8,time=t+600_000_000));assertFalse(s.valid(t+600_000_000))
}
@Test fun recoverableQuaternionScaleDoesNotCreateFalseDiscontinuities() {
val s=session()
for(i in 1..4) assertTrue(s.accept(sample(time=t+i,rotation=SpatialRotation(w=.8f))))
assertFalse(s.needsRecenter);near(1f,s.latest()!!.pose.rotation.w)
}
@Test fun calibrationFrameChangesOnlyWhenTheTrackingFrameChanges() {
val s=session();val frame=s.trackingFrameGeneration
s.recenter(false,t);assertEquals(frame,s.trackingFrameGeneration)
s.accept(sample(x=5f,time=t+10));assertEquals(frame+1,s.trackingFrameGeneration)
s.accept(sample(x=5f,time=t+20));assertEquals(frame+1,s.trackingFrameGeneration)
s.reset(8,1.6f,true);assertEquals(frame+2,s.trackingFrameGeneration)
}
@Test fun captureRemainsValidWhenHeadPollingAdvancesDuringBoardDetection() {
val s=session();val frame=s.trackingFrameGeneration
val detectionStarted=t+1_000_000L
s.accept(sample(x=.01f,time=t+20_000_000L))
assertFalse(s.valid(detectionStarted))
assertTrue(s.isCurrentTrackingFrame(7,frame) { t+25_000_000L })
}
@Test fun captureCompletionStillRejectsStaleChangedAndDisconnectedTracking() {
val s=session();val frame=s.trackingFrameGeneration
assertFalse(s.isCurrentTrackingFrame(7,frame) { t+100_000_001L })
s.accept(sample(x=5f,time=t+10));assertFalse(s.isCurrentTrackingFrame(7,frame) { t+20 })
s.recenter(false,t+20)
for(i in 1..6) s.accept(sample(x=5f,time=t+i*50_000_000L))
assertTrue(s.valid(t+300_000_000L))
assertFalse(s.isCurrentTrackingFrame(7,frame) { t+300_000_000L })
s.reset(8,1.6f);s.accept(sample(generation=8,time=t+400_000_000L))
assertFalse(s.isCurrentTrackingFrame(7,frame) { t+400_000_000L })
}
@Test fun newerHeadDoesNotRefreshOlderHand() {
val s=session();s.setHands(7,SpatialHand(SpatialPose(),t,true,1f,true),SpatialHand())
s.accept(sample(time=t+80_000_000))
s.accept(sample(time=t+150_000_000));assertTrue(s.hand(0,t+200_000_000).valid)
assertEquals(t,s.hand(0,t+200_000_000).timeNs)
assertFalse(s.hand(0,t+200_000_001).valid)
s.recenter(false,t+200_000_001);assertFalse(s.hand(0,t+200_000_001).valid)
s.reset(8,1.6f);s.setHands(7,SpatialHand(timeNs=t,valid=true),SpatialHand());assertFalse(s.hand(0,t).valid)
}
private fun heldHand(time: Long = t) = SpatialHand(timeNs=time,valid=true,trigger=1f,grip=true)
private fun epochs(session: SpatialSession) = List(4) { (session.inputEpochs() ushr (it*8)) and 0xff }
@Test fun overwrittenHandLossRemainsVisibleAfterHeldGestureReturns() {
val s=session();s.setHands(7,heldHand(),heldHand())
s.setHands(7,SpatialHand(),heldHand(t+10))
s.setHands(7,heldHand(t+20),heldHand(t+20))
assertEquals(listOf(1,1,0,0),epochs(s))
assertTrue(s.hand(0,t+20).valid);assertTrue(s.hand(0,t+20).grip)
assertEquals(listOf(1,1,0,0),epochs(s))
}
@Test fun triggerAndGripLossEpochsAreIndependentForBothHands() {
val s=session();s.setHands(7,heldHand(),heldHand())
s.setHands(7,heldHand(t+10).copy(triggerValid=false),heldHand(t+10))
assertEquals(listOf(1,0,0,0),epochs(s))
assertTrue(s.hand(0,t+10).gripValid)
s.setHands(7,heldHand(t+20).copy(gripValid=false),heldHand(t+20).copy(triggerValid=false))
assertEquals(listOf(1,1,1,0),epochs(s))
s.setHands(7,heldHand(t+30).copy(gripValid=false),heldHand(t+30).copy(triggerValid=false))
assertEquals(listOf(1,1,1,0),epochs(s))
assertFalse(s.hand(0,t+30).gripValid);assertTrue(s.hand(0,t+30).triggerValid)
}
@Test fun overwrittenHeadLossDoesNotLoseTheInputReset() {
val s=session();s.setHands(7,heldHand(),heldHand())
s.accept(sample(time=t+10,valid=false))
s.accept(sample(time=t+20))
s.setHands(7,heldHand(t+20),heldHand(t+20))
assertEquals(listOf(1,1,1,1),epochs(s))
assertTrue(s.hand(0,t+20).triggerValid)
}
@Test fun headGapClearsCachedHandsEvenWithoutAnInvalidSnapshot() {
val s=session();s.setHands(7,heldHand(),heldHand())
s.accept(sample(time=t+100_000_001))
assertFalse(s.hand(0,t+100_000_001).valid)
assertEquals(listOf(1,1,1,1),epochs(s))
s.setHands(7,heldHand(t+110_000_000),heldHand(t+110_000_000))
assertTrue(s.hand(0,t+110_000_000).valid)
assertEquals(listOf(1,1,1,1),epochs(s))
}
@Test fun latePreLossCaptureCannotRearmAnInputAfterHeadRecovery() {
val s=session();s.setHands(7,heldHand(),heldHand())
s.accept(sample(time=t+20,valid=false));s.accept(sample(time=t+30))
val released=heldHand(t+10).copy(trigger=0f,grip=false)
s.setHands(7,released,released)
val late=s.hand(0,t+30)
assertTrue(late.valid);assertFalse(late.triggerValid);assertFalse(late.gripValid)
assertEquals(listOf(1,1,1,1),epochs(s))
s.setHands(7,released.copy(timeNs=t+40),released.copy(timeNs=t+40))
assertTrue(s.hand(0,t+40).triggerValid);assertTrue(s.hand(0,t+40).gripValid)
assertEquals(listOf(1,1,1,1),epochs(s))
}
@Test fun freshReplacementAfterLongHandGapStillAdvancesResetEpoch() {
val s=session();s.setHands(7,heldHand(),heldHand())
for(i in 1..5) s.accept(sample(time=t+i*50_000_000))
s.setHands(7,heldHand(t+250_000_000),heldHand(t+250_000_000))
assertTrue(s.hand(0,t+250_000_000).valid)
assertEquals(listOf(1,1,1,1),epochs(s))
}
@Test fun handExpiryAndRepeatedInvalidPollsOnlyIncrementOnce() {
val s=session();s.setHands(7,heldHand(),SpatialHand())
for(i in 1..4) s.accept(sample(time=t+i*50_000_000))
assertTrue(s.hand(0,t+200_000_000).valid)
repeat(4) { assertFalse(s.hand(0,t+200_000_001+it).valid) }
assertEquals(listOf(1,1,0,0),epochs(s))
s.setHands(7,heldHand(t+210_000_000),SpatialHand())
assertTrue(s.hand(0,t+210_000_000).valid)
assertEquals(listOf(1,1,0,0),epochs(s))
}
@Test fun headExpiryThenDelayedHeadSampleDoesNotDoubleCountLoss() {
val s=session();s.setHands(7,heldHand(),heldHand())
for(i in 0..1) assertFalse(s.hand(i,t+100_000_001).valid)
assertEquals(listOf(1,1,1,1),epochs(s))
s.accept(sample(time=t+150_000_000))
s.setHands(7,heldHand(t+160_000_000),heldHand(t+160_000_000))
assertEquals(listOf(1,1,1,1),epochs(s))
}
@Test fun disabledHandsReleaseOnceAndWrongGenerationDoesNotAffectCurrentHands() {
val s=session();s.setHands(7,heldHand(),heldHand())
s.setHands(6,SpatialHand(),SpatialHand())
assertTrue(s.hand(0,t).valid);assertEquals(listOf(0,0,0,0),epochs(s))
repeat(3) { s.setHands(7,SpatialHand(),SpatialHand()) }
assertEquals(listOf(1,1,1,1),epochs(s))
s.reset(8,1.6f)
assertEquals(0,s.inputEpochs())
}
@Test fun invalidTriggerValueOnlyInvalidatesItsOwnGestureEpoch() {
val s=session();s.setHands(7,heldHand(),heldHand())
s.setHands(7,heldHand(t+10).copy(trigger=Float.NaN),heldHand(t+10))
assertEquals(listOf(1,0,0,0),epochs(s))
assertFalse(s.hand(0,t+10).triggerValid);assertTrue(s.hand(0,t+10).gripValid)
}
@Test fun eachPackedEpochWrapsWithinItsOwnByte() {
val s=session()
repeat(255) {
s.setHands(7,heldHand(t+it*2),heldHand(t+it*2))
s.setHands(7,SpatialHand(),SpatialHand())
}
assertEquals(listOf(255,255,255,255),epochs(s));assertEquals(-1,s.inputEpochs())
s.setHands(7,heldHand(t+512),heldHand(t+512));s.setHands(7,SpatialHand(),SpatialHand())
assertEquals(0,s.inputEpochs())
}
@Test fun historyNeverCrossesReconnectOrLargeTimeGap() {
val s=session();assertNotNull(s.poseAt(t+4_000_000,7));assertNull(s.poseAt(t+20_000_000,7))
s.reset(8,1.6f);assertNull(s.poseAt(t,7))
}
@Test fun cameraClockWarmsUpAndRejectsResetAndJitter() {
val clock=CameraClock();val offset=10_000_000_000L
for(i in 1..15) assertNull(clock.map(i*40_000_000L,offset+i*40_000_000L))
assertEquals(offset+640_000_000L,clock.map(640_000_000L,offset+640_000_000L))
assertNull(clock.map(1,offset+1))
val bad=CameraClock()
for(i in 1..30) assertNull(bad.map(i*40_000_000L,offset+i*40_000_000L+(i%2)*30_000_000L))
}
@Test fun halfRateCameraClockRetainsSourceTimeThroughSkippedPairs() {
val clock=CameraClock();val offset=10_000_000_000L
for(i in 1..15) assertNull(clock.map(i*80_000_000L,offset+i*80_000_000L))
assertEquals(offset+1_280_000_000L,clock.map(1_280_000_000L,offset+1_280_000_000L))
assertEquals(offset+1_440_000_000L,clock.map(1_440_000_000L,offset+1_440_000_000L))
assertNull(clock.map(1,offset+1))
}
@Test fun cameraClockKeepsAlignedMappingAfterLongSkippedGaps() {
val offset=10_000_000_000L
for(gap in listOf(240_000_000L,1_000_000_000L)) {
val clock=CameraClock()
for(i in 1..16) clock.map(i*80_000_000L,offset+i*80_000_000L)
val source=1_280_000_000L+gap
assertEquals(offset+source,clock.map(source,offset+source))
assertEquals(offset+source+80_000_000L,clock.map(source+80_000_000L,offset+source+80_000_000L))
}
}
@Test fun cameraClockRewarmsAfterSourceOrReceiptStopsOrReverses() {
val offset=10_000_000_000L;val end=1_280_000_000L
val resets=listOf(1L to offset+end+80_000_000L,
end to offset+end+80_000_000L,
end+80_000_000L to 1L,
end+80_000_000L to offset+end)
for((source,receipt) in resets) {
val clock=CameraClock()
for(i in 1..16) clock.map(i*80_000_000L,offset+i*80_000_000L)
for(i in 0..14) assertNull(clock.map(source+i*80_000_000L,receipt+i*80_000_000L))
assertEquals(receipt+1_200_000_000L,clock.map(source+1_200_000_000L,receipt+1_200_000_000L))
}
}
@Test fun cameraClockRewarmsAfterOffsetJumpAcrossSkippedFrames() {
val offset=10_000_000_000L
for(gap in listOf(240_000_000L,1_000_000_000L)) for(jump in listOf(-20_000_001L,20_000_001L)) {
val clock=CameraClock()
for(i in 1..16) clock.map(i*80_000_000L,offset+i*80_000_000L)
val source=1_280_000_000L+gap;val receipt=source+offset+jump
for(i in 0..14) assertNull(clock.map(source+i*80_000_000L,receipt+i*80_000_000L))
assertEquals(receipt+1_200_000_000L,clock.map(source+1_200_000_000L,receipt+1_200_000_000L))
}
}
@Test fun cameraClockRetainsMappingWithBoundedJitterAcrossSkippedFrames() {
val clock=CameraClock();val offset=10_000_000_000L
for(i in 1..16) clock.map(i*80_000_000L,offset+i*80_000_000L+(i%2)*20_000_000L)
assertEquals(offset+1_520_000_000L,clock.map(1_520_000_000L,offset+1_540_000_000L))
assertEquals(offset+2_520_000_000L,clock.map(2_520_000_000L,offset+2_520_000_000L))
assertNull(clock.map(2_760_000_000L,offset+2_780_000_001L))
}
@Test fun cameraClockRetainsMappingThroughIsolatedShortGapReceiptJitter() {
val clock=CameraClock();val offset=10_000_000_000L
for(i in 1..16) clock.map(i*80_000_000L,offset+i*80_000_000L)
assertEquals(offset+1_360_000_000L,clock.map(1_360_000_000L,offset+1_390_000_000L))
assertEquals(offset+1_440_000_000L,clock.map(1_440_000_000L,offset+1_440_000_000L))
}
@Test fun cameraClockInvalidTimestampRequiresFreshWarmup() {
val offset=10_000_000_000L
for((source,receipt) in listOf(0L to offset,-1L to offset,1L to 0L,1L to -1L)) {
val clock=CameraClock()
for(i in 1..16) clock.map(i*80_000_000L,offset+i*80_000_000L)
assertNull(clock.map(source,receipt))
for(i in 17..31) assertNull(clock.map(i*80_000_000L,offset+i*80_000_000L))
assertEquals(offset+2_560_000_000L,clock.map(2_560_000_000L,offset+2_560_000_000L))
}
}
}
@@ -0,0 +1,68 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class SteamVrSpatialGateTest {
@Test fun launchWaitsForRunningThenTheEntireSettleInterval() {
val gate = SteamVrSpatialGate()
assertTrue(gate.spatialEnabled(true, 0))
gate.prepare()
assertFalse(gate.spatialEnabled(true, 60_000))
gate.setRunning(false, 60_000)
assertFalse(gate.spatialEnabled(true, 60_000))
gate.setRunning(true, 60_000)
assertFalse(gate.spatialEnabled(true, 61_999))
assertTrue(gate.spatialEnabled(true, 62_000))
assertFalse(gate.spatialEnabled(false, 62_000))
}
@Test fun repeatedRunningAndActivityPreparationDoNotMoveTheDeadline() {
val gate = SteamVrSpatialGate()
gate.prepare()
gate.setRunning(true, 100)
gate.prepare()
gate.setRunning(true, 1_900)
assertEquals(2_100L, gate.deadline)
assertTrue(gate.spatialEnabled(true, 2_100))
gate.prepare()
gate.setRunning(true, 10_000)
assertTrue(gate.spatialEnabled(true, 10_000))
}
@Test fun explicitCancelRestoresSavedTrackingBeforeRunningAndDuringSettling() {
val gate = SteamVrSpatialGate()
gate.prepare()
gate.cancel()
assertTrue(gate.spatialEnabled(true, 100))
assertFalse(gate.spatialEnabled(false, 100))
gate.prepare()
gate.setRunning(true, 200)
gate.cancel()
assertNull(gate.deadline)
assertTrue(gate.spatialEnabled(true, 201))
assertFalse(gate.spatialEnabled(false, 10_000))
}
@Test fun stopAndNewLaunchCannotReuseAnEarlierDeadline() {
val gate = SteamVrSpatialGate()
gate.prepare()
gate.setRunning(true, 100)
gate.setRunning(false, 200)
assertTrue(gate.spatialEnabled(true, 200))
gate.prepare()
assertFalse(gate.spatialEnabled(true, 2_100))
gate.setRunning(false, 2_100)
assertFalse(gate.spatialEnabled(true, 2_100))
gate.setRunning(true, 3_000)
assertFalse(gate.spatialEnabled(true, 4_999))
assertTrue(gate.spatialEnabled(true, 5_000))
}
@Test fun discoveringAnAlreadyRunningSessionStillAllowsSettling() {
val gate = SteamVrSpatialGate()
gate.setRunning(true, 100)
assertFalse(gate.spatialEnabled(true, 2_099))
assertTrue(gate.spatialEnabled(true, 2_100))
}
}
@@ -0,0 +1,89 @@
package com.droiddeck.launcher.glasses
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28])
class StereoCalibrationProfileTest {
private fun base() = RuntimeEnvironment.getApplication().assets.open(StereoCalibrationProfile.BASE_ASSET)
.bufferedReader().use { it.readText() }
private fun device(verified: Boolean = true) = JSONObject(base()).put("identity", "my-glasses")
.put("verified", verified).toString()
private fun select(saved: String? = null, identity: String? = "my-glasses", width: Int = 640, height: Int = 480) =
StereoCalibrationProfile.select(saved, ::base, identity, width, height)
@Test fun newGlassesUseSharedGeometryWithoutClaimingIndividualValidation() {
val profile = select()!!
assertEquals("my-glasses", profile.identity)
assertTrue(profile.bundledBase)
assertFalse(profile.verified)
assertEquals(1.1209395390660726, profile.error, 0.0)
assertEquals(226.0499636040297, profile.matrices[0].values[0], 0.0)
assertEquals(-0.05607918f, profile.cameraToHead.position.x, 0f)
}
@Test fun unknownSerialCanUseTheModelBase() {
val profile = select(identity = null)!!
assertEquals(StereoCalibrationProfile.BASE_IDENTITY, profile.identity)
assertTrue(profile.bundledBase)
assertFalse(profile.verified)
}
@Test fun savedDeviceProfileWinsWithoutReadingTheAsset() {
val profile = StereoCalibrationProfile.select(device(), { error("Base must not be read") }, "my-glasses", 640, 480)!!
assertFalse(profile.bundledBase)
assertTrue(profile.verified)
}
@Test fun unfinishedPersonalCalibrationStillRequiresDistanceValidation() {
val profile = select(device(verified = false))!!
assertFalse(profile.bundledBase)
assertFalse(profile.verified)
}
@Test fun foreignOrCorruptDeviceProfileFallsBackToBase() {
assertTrue(select(JSONObject(device()).put("identity", "other-glasses").toString())!!.bundledBase)
assertTrue(select("not json")!!.bundledBase)
}
@Test fun otherCameraGeometryDoesNotUseTheBase() {
assertNull(select(width = 1280, height = 960))
assertNull(select(width = 480, height = 640))
}
@Test fun baseRequiresTheCorrectModelAndCameraPair() {
for ((key, value) in listOf("deviceModel" to "VITURE Luma Pro", "cameraPair" to "left1-right1", "identity" to "device-serial")) {
val text = JSONObject(base()).put(key, value).toString()
assertNull(StereoCalibrationProfile.select(null, { text }, "my-glasses", 640, 480))
}
}
@Test fun badLensDimensionsAndInvalidFitsAreRejectedBeforeNativeAllocation() {
for (name in listOf("k1", "d1", "k2", "d2", "r", "t")) {
val data = JSONObject(base())
data.getJSONObject(name).put("rows", 2)
assertNull(StereoCalibrationProfile.parse(data.toString(), StereoCalibrationProfile.BASE_IDENTITY, 640, 480, true))
}
for (error in listOf(-1.0, 2.01)) {
val data = JSONObject(base()).put("error", error)
assertNull(StereoCalibrationProfile.parse(data.toString(), StereoCalibrationProfile.BASE_IDENTITY, 640, 480, true))
}
}
@Test fun missingAssetAndNoDeviceProfileRequiresCalibration() {
assertNull(StereoCalibrationProfile.select(null, { null }, "my-glasses", 640, 480))
}
@Test fun shippedBaseContainsNoSourceSerialOrFalseValidationClaim() {
val data = JSONObject(base())
assertEquals(StereoCalibrationProfile.BASE_IDENTITY, data.getString("identity"))
assertFalse(data.getBoolean("verified"))
assertTrue(data.getJSONObject("sourceValidation").getBoolean("sourceDeviceVerified"))
}
}
@@ -0,0 +1,87 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
import kotlin.math.cos
import kotlin.math.sin
class StereoFingerGesturesTest {
private fun open(): MutableList<SpatialVector> {
val p=MutableList(21) { SpatialVector(320f,240f) }
p[4]=SpatialVector(390f,190f)
for((base,x) in listOf(5 to 355f,9 to 330f,13 to 305f,17 to 280f)) {
for(j in 0..3) p[base+j]=SpatialVector(x,180f-25f*j)
}
return p
}
private fun curl(p: MutableList<SpatialVector>,reach: Float=1f) = p.apply {
for(base in listOf(9,13,17)) this[base+3]=this[0]+(this[base]-this[0])*reach
}
private fun transform(p: List<SpatialVector>,angle: Float,scale: Float,mirror: Boolean) = p.map {
val x=(if(mirror) -it.x else it.x)*scale
val y=it.y*scale
SpatialVector(x*cos(angle)-y*sin(angle)+700f,x*sin(angle)+y*cos(angle)-80f)
}
@Test fun openingAndClosingAreIndependentOfImageScaleRotationAndHandedness() {
for(mirror in listOf(false,true)) for(angle in listOf(0f,.7f,2.8f)) {
val closed=curl(open()).apply { this[4]=this[8] }
val result=StereoFingerGestures.estimate(transform(closed,angle,1.5f,mirror),
transform(closed,angle+.12f,.7f,mirror),false)
assertTrue(result.gripValid);assertTrue(result.grip)
assertTrue(result.triggerValid);assertEquals(1f,result.trigger,0f)
val released=StereoFingerGestures.estimate(transform(open(),angle,1.5f,mirror),
transform(open(),angle+.12f,.7f,mirror),true)
assertTrue(released.gripValid);assertFalse(released.grip)
assertTrue(released.triggerValid);assertEquals(0f,released.trigger,0f)
}
}
@Test fun curlThresholdsProvideObservedHysteresisAndRelease() {
val partial=curl(open(),1.4f)
val idle=StereoFingerGestures.estimate(partial,partial,false)
val held=StereoFingerGestures.estimate(partial,partial,true)
assertTrue(idle.gripValid);assertFalse(idle.grip)
assertTrue(held.gripValid);assertTrue(held.grip)
val opening=curl(open(),1.6f)
assertFalse(StereoFingerGestures.estimate(opening,opening,true).grip)
val two=curl(open()).apply { for(i in 17..20) this[i]=open()[i] }
assertFalse(StereoFingerGestures.estimate(two,two,false).grip)
assertTrue(StereoFingerGestures.estimate(two,two,true).grip)
}
@Test fun shortStraightProjectedFingersDoNotCountAsCurled() {
val straight=open().apply {
for(base in listOf(9,13,17)) for(j in 1..3) this[base+j]=this[base]+SpatialVector(y=-4.5f*j)
}
val result=StereoFingerGestures.estimate(straight,straight,false)
assertTrue(result.gripValid);assertFalse(result.grip)
}
@Test fun pinchRequiresBothEyesAndUsesTheirMoreOpenEstimate() {
fun pinch(ratio: Float)=open().apply { this[4]=this[8]+SpatialVector(x=75f*ratio) }
val result=StereoFingerGestures.estimate(pinch(.25f),pinch(.30f),false)
assertTrue(result.triggerValid);assertEquals((.55f-.30f)/.35f,result.trigger,.0001f)
val conflict=StereoFingerGestures.estimate(pinch(.25f),pinch(.50f),false)
assertFalse(conflict.triggerValid);assertEquals(0f,conflict.trigger,0f)
assertTrue(conflict.gripValid);assertFalse(conflict.grip)
}
@Test fun conflictingCurlDoesNotReportAnObservedRelease() {
val result=StereoFingerGestures.estimate(curl(open()),open(),true)
assertFalse(result.gripValid);assertFalse(result.grip)
assertTrue(result.triggerValid)
}
@Test fun invalidJointsOnlyInvalidateTheirOwnGesture() {
val badPinch=open().apply { this[4]=SpatialVector(Float.NaN,190f) }
val pinch=StereoFingerGestures.estimate(badPinch,open(),false)
assertFalse(pinch.triggerValid);assertTrue(pinch.gripValid)
val badGrip=open().apply { this[15]=SpatialVector(300f,Float.POSITIVE_INFINITY) }
val grip=StereoFingerGestures.estimate(open(),badGrip,true)
assertTrue(grip.triggerValid);assertFalse(grip.gripValid);assertFalse(grip.grip)
}
@Test fun missingViewsAndDegeneratePalmsCannotArmInputs() {
val flat=open().apply { for(i in listOf(0,5,9,13,17)) this[i]=SpatialVector(i.toFloat(),0f) }
val collapsed=open().apply { this[17]=this[5] }
for(invalid in listOf(null,emptyList(),flat,collapsed)) {
val result=StereoFingerGestures.estimate(open(),invalid,true)
assertFalse(result.triggerValid);assertFalse(result.gripValid)
assertEquals(0f,result.trigger,0f);assertFalse(result.grip)
}
}
}
@@ -0,0 +1,72 @@
package com.droiddeck.launcher.glasses
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import org.junit.Assert.*
import org.junit.Test
class StereoInferenceRunnerTest {
@Test fun camerasRunConcurrentlyAndRightDetectorKeepsItsOwnerThread() {
StereoInferenceRunner().use { runner ->
val owner=runner.onRight { Thread.currentThread() }
val rightStarted=CountDownLatch(1)
val leftStarted=CountDownLatch(1)
val pair=runner.pair({
leftStarted.countDown()
assertTrue(rightStarted.await(2,TimeUnit.SECONDS))
1
}, {
rightStarted.countDown()
assertTrue(leftStarted.await(2,TimeUnit.SECONDS))
assertSame(owner,Thread.currentThread())
2
})
assertEquals(1 to 2,pair)
assertSame(owner,runner.onRight { Thread.currentThread() })
}
}
@Test fun leftFailureDoesNotReleasePixelsWhileRightStillUsesThem() {
StereoInferenceRunner().use { runner ->
val finished=CountDownLatch(1)
val failure=IllegalStateException("left")
try {
runner.pair({ throw failure }, { Thread.sleep(25);finished.countDown();1 })
fail("Expected inference failure")
} catch(e: IllegalStateException) { assertSame(failure,e) }
assertEquals(0L,finished.count)
}
}
@Test fun rightFailureReachesOwnerAndWorkerCanStillCloseDetector() {
StereoInferenceRunner().use { runner ->
val failure=IllegalStateException("right")
try {
runner.pair({ 1 }, { throw failure })
fail("Expected inference failure")
} catch(e: IllegalStateException) { assertSame(failure,e) }
assertTrue(runner.onRight { true })
}
}
@Test fun shutdownInterruptionWaitsForInferenceBeforeReturningBuffers() {
StereoInferenceRunner().use { runner ->
val owner=Executors.newSingleThreadExecutor()
val running=CountDownLatch(1);val release=CountDownLatch(1)
try {
val result=owner.submit<Boolean> {
Thread.currentThread().interrupt()
assertEquals(1 to 2,runner.pair({ 1 }, {
running.countDown();check(release.await(2,TimeUnit.SECONDS));2
}))
Thread.interrupted()
}
assertTrue(running.await(2,TimeUnit.SECONDS))
assertFalse(result.isDone)
release.countDown()
assertTrue(result.get(2,TimeUnit.SECONDS))
} finally { release.countDown();owner.shutdownNow() }
}
}
}
@@ -0,0 +1,30 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class StereoPointValidationTest {
private val p1=doubleArrayOf(400.0,0.0,320.0,0.0, 0.0,400.0,240.0,0.0, 0.0,0.0,1.0,0.0)
private val p2=p1.copyOf().apply { this[3]=-24.0 }
private val point=doubleArrayOf(0.0,0.0,.5,1.0)
private val left=doubleArrayOf(320.0,240.0)
private val right=doubleArrayOf(272.0,240.0)
@Test fun metricDepthAndReprojectionThresholdAreUnchanged() {
val result=StereoPointValidation.reconstruct(point,left,right,p1,p2)!!
assertEquals(.5f,result.z,0f)
assertNotNull(StereoPointValidation.reconstruct(point,left,doubleArrayOf(275.0,240.0),p1,p2))
assertNull(StereoPointValidation.reconstruct(point,left,doubleArrayOf(275.01,240.0),p1,p2))
}
@Test fun aRejectedLandmarkDoesNotRemoveItsValidNeighbors() {
val observations=listOf(left,doubleArrayOf(Double.NaN,240.0),left)
val points=observations.map { StereoPointValidation.reconstruct(point,it,right,p1,p2) }
assertNotNull(points[0]);assertNull(points[1]);assertNotNull(points[2])
}
@Test fun invalidDepthAndProjectionNeverProduceValidPoints() {
for(z in listOf(-.5,.149,1.501,Double.NaN,Double.POSITIVE_INFINITY))
assertNull(StereoPointValidation.reconstruct(doubleArrayOf(0.0,0.0,z,1.0),left,right,p1,p2))
assertNull(StereoPointValidation.reconstruct(point.copyOf().apply { this[3]=0.0 },left,right,p1,p2))
assertNull(StereoPointValidation.reconstruct(point,left,right,p1,p2.copyOf().apply { this[10]=-1.0 }))
assertNull(StereoPointValidation.reconstruct(point,left,right,p1,p2.copyOf().apply { this[0]=Double.NaN }))
}
}
@@ -0,0 +1,46 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.*
import org.junit.Test
class TrackingRecoveryTest {
@Test fun periodicStatusRefreshDetectsExpiryWithoutAnotherPoseCallback() {
val time=1_000_000_000L
val session=SpatialTracking.session
try {
session.reset(91,1.6f)
session.accept(SpatialSample(SpatialPose(),time,91,true))
SpatialTracking.refreshStatus(time)
assertTrue(SpatialTracking.status.value.headFresh)
assertNull(TrackingRecovery.forSession(true,true,SpatialTracking.status.value))
SpatialTracking.refreshStatus(time+100_000_001)
assertFalse(SpatialTracking.status.value.headFresh)
assertEquals(TrackingRecovery.LOST,TrackingRecovery.forSession(true,true,SpatialTracking.status.value))
session.accept(SpatialSample(SpatialPose(),time+110_000_000,91,true))
SpatialTracking.refreshStatus(time+110_000_000)
assertNull(TrackingRecovery.forSession(true,true,SpatialTracking.status.value))
} finally {
session.reset(92,1.6f)
SpatialTracking.refreshStatus(time+200_000_000)
}
}
@Test fun healthyAndNonSpatialSessionsNeverShowRecovery() {
assertNull(TrackingRecovery.forSession(true,true,SpatialTrackingStatus(trackingValid=true,headFresh=true)))
assertNull(TrackingRecovery.forSession(false,false,SpatialTrackingStatus()))
assertEquals(TrackingRecovery.DISCONNECTED,TrackingRecovery.forSession(true,false,SpatialTrackingStatus(trackingValid=true)))
}
@Test fun pendingRecenterStillRequiresLookingAroundUntilHeadTrackingReturns() {
val pending=SpatialTrackingStatus(recenterPending=true,needsRecenter=true)
assertEquals(TrackingRecovery.LOST,TrackingRecovery.forSession(true,true,pending))
assertEquals(TrackingRecovery.STABILIZING,TrackingRecovery.forSession(true,true,pending.copy(headFresh=true)))
assertNull(TrackingRecovery.forSession(true,true,pending.copy(headFresh=true,trackingValid=true,recenterPending=false,needsRecenter=false)))
}
@Test fun newTrackingFrameExplainsTheRequiredRecenter() {
val changed=SpatialTrackingStatus(needsRecenter=true,headFresh=true)
assertEquals(TrackingRecovery.RECENTER,TrackingRecovery.forSession(true,true,changed))
assertEquals(TrackingRecovery.LOST,TrackingRecovery.forSession(true,true,changed.copy(headFresh=false)))
}
}
@@ -0,0 +1,49 @@
package com.droiddeck.launcher.glasses
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class UvcStreamHealthTest {
private fun sample(frames: Long = 901, lastMs: Long = 30990, endMs: Long = 31000,
maximumGapMs: Long = 34) = LongArray(14).apply {
this[6]=lastMs*1_000_000
this[7]=1_000_000_000
this[8]=maximumGapMs*1_000_000
this[9]=frames
this[12]=endMs*1_000_000
this[13]=(endMs-lastMs)*1_000_000
}
@Test fun sustainedStreamMeasuresEntireWindow() {
val health=UvcStreamHealth.assess(sample(),30,30000)
assertTrue(health.healthy)
assertEquals(30000.0,health.observationMs,0.001)
assertEquals(30.0,health.observedFps,0.001)
}
@Test fun earlyFramesCannotHideLongFinalStall() {
val health=UvcStreamHealth.assess(sample(frames=31,lastMs=2000),30,30000)
assertFalse(health.healthy)
assertEquals(1.0,health.observedFps,0.001)
assertEquals(29000.0,health.maximumGapMs,0.001)
assertFalse(UvcStreamHealth.assess(sample(frames=1,lastMs=1000),30,30000).healthy)
}
@Test fun frameRateCannotHideInterFrameOrFinalGaps() {
assertFalse(UvcStreamHealth.assess(sample(maximumGapMs=500),30,30000).healthy)
assertFalse(UvcStreamHealth.assess(sample(lastMs=30500),30,30000).healthy)
}
@Test fun shortObservationAndLowFrameRateAreUnhealthy() {
assertFalse(UvcStreamHealth.assess(sample(frames=31,lastMs=1990,endMs=2000),30,30000).healthy)
assertFalse(UvcStreamHealth.assess(sample(frames=601),30,30000).healthy)
assertFalse(UvcStreamHealth.assess(sample().apply { this[4]=-1 },30,30000).healthy)
}
@Test fun lowFrameRateAllowsThreeFramePeriods() {
assertTrue(UvcStreamHealth.assess(sample(frames=151,lastMs=30600,maximumGapMs=400),5,30000).healthy)
assertFalse(UvcStreamHealth.assess(sample(frames=151,lastMs=30300,maximumGapMs=700),5,30000).healthy)
}
}
@@ -6,6 +6,16 @@ import kotlin.math.sqrt
import com.droiddeck.launcher.gpu.HeadPose
class VitureSdkTest {
@Test fun spatialIdentityUsesTheFullSdkDigestAndRejectsMissingValues() {
assertNull(VitureSpatialIdentity.fromSerialHash(null))
assertNull(VitureSpatialIdentity.fromSerialHash(ByteArray(32)))
assertNull(VitureSpatialIdentity.fromSerialHash(ByteArray(31) { 1 }))
val digest=ByteArray(32) { it.toByte() }
val id=VitureSpatialIdentity.fromSerialHash(digest)
assertEquals("viture:sn-sha256:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",id)
assertEquals(id,VitureSpatialIdentity.fromSerialHash(digest.copyOf()))
assertNotEquals(id,VitureSpatialIdentity.fromSerialHash(digest.copyOf().also { it[31]=0xff.toByte() }))
}
@Test fun pro2UsesItsOwnSdkTransportAndDisplayLimits() {
val m = VitureProtocol.sdkModel("Pro 2")!!
assertTrue(m.needsSdk)
@@ -33,6 +33,8 @@ class CardboardTest {
val c = Cardboard(enabled = true, source = Cardboard.SOURCE_SBS, viewer = "none", ipdMm = 70,
screenWidthDm = 55, screenDistanceDm = 25, headTracking = false)
assertEquals(c, Cardboard.decode(c.encode()))
val flat = c.copy(source = Cardboard.SOURCE_FLAT)
assertEquals(flat, Cardboard.decode(flat.encode()))
val wild = Cardboard.decode(JSONObject().put("ipdMm", 500).put("screenWidthDm", -3).put("viewer", "nope").put("source", 7))
assertEquals(80, wild.ipdMm)
assertEquals(5, wild.screenWidthDm)
@@ -106,6 +108,18 @@ class CardboardTest {
assertEquals(-cos(PI / 6).toFloat(), forward[2], eps)
}
@Test fun virtualScreenRecenterUsesTheCurrentHeightAndDirection() {
for (pitch in listOf(-30.0, -10.0, 15.0, 35.0)) {
val yaw = matrix(quat(0f, 0f, 1f, 50.0))
val tilt = HeadPose.multiply(matrix(quat(1f, 0f, 0f, pitch)), matrix(quat(0f, 0f, 1f, 12.0)))
val screen = HeadPose.multiply(HeadPose.multiply(yaw, HeadPose.fromGlasses(0f, 0f, 0f, 1f)), tilt)
val ref = HeadPose.recenter(screen, centerView = true)
assertVec(HeadPose.IDENTITY, HeadPose.relative(ref, screen))
val lookUp = matrix(quat(1f, 0f, 0f, 20.0))
assertVec(lookUp, HeadPose.relative(ref, HeadPose.multiply(screen, lookUp)))
}
}
@Test fun turningTheHeadTurnsTheView() {
val upright = floatArrayOf(1f, 0f, 0f, 0f, 0f, -1f, 0f, 1f, 0f)
val ref = HeadPose.recenter(upright)
@@ -66,12 +66,13 @@ class JoyConShmTest {
}
@Test fun reopeningKeepsTheSequenceMovingForward() {
JoyConShm.open(file) {}!!.write(JoyCon.LEFT, JoyConShm.Hand(connected = true))
val first = JoyConShm.open(file) {}!!
first.write(JoyCon.LEFT, JoyConShm.Hand(connected = true))
assertEquals(4, read().getInt(8))
file.outputStream().use { }
assertEquals(0L, file.length())
first.close()
val previous = read().getInt(8)
assertNotNull(JoyConShm.open(file) {})
assertEquals(2, read().getInt(8))
assertTrue(read().getInt(8) > previous)
java.io.RandomAccessFile(file, "rw").use { it.seek(8); it.write(byteArrayOf(7, 0, 0, 0)) }
JoyConShm.open(file) {}!!
val after = read().getInt(8)
@@ -79,6 +80,14 @@ class JoyConShmTest {
assertTrue("$after after a torn write", after > 7)
}
@Test fun anEmptyFileStartsANewSequence() {
file.parentFile!!.mkdirs()
file.outputStream().use { }
assertEquals(0L, file.length())
assertNotNull(JoyConShm.open(file) {})
assertEquals(2, read().getInt(8))
}
@Test fun anUnwritableLocationIsReportedAsMissing() {
val blocker = File(dir, "blocker").apply { writeText("") }
assertNull(JoyConShm.open(File(blocker, JoyConShm.FILE)) {})
@@ -0,0 +1,130 @@
package com.droiddeck.launcher.runtime
import android.content.ContextWrapper
import com.droiddeck.launcher.session.SessionPhase
import com.droiddeck.launcher.session.SessionState
import java.io.File
import java.nio.file.Files
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
@RunWith(RobolectricTestRunner::class)
class LinuxRuntimeLiveFilesTest {
private val root = Files.createTempDirectory("guest-live-files").toFile()
private val context = object : ContextWrapper(RuntimeEnvironment.getApplication()) {
override fun getFilesDir() = File(root, "files").apply { mkdirs() }
override fun getCacheDir() = File(root, "cache").apply { mkdirs() }
override fun getNoBackupFilesDir() = File(root, "no_backup").apply { mkdirs() }
}
@Before fun setUp() {
SessionState.running = false
SessionState.phase = SessionPhase.IDLE
}
@After fun tearDown() {
Thread.interrupted()
SessionState.running = false
SessionState.phase = SessionPhase.IDLE
root.deleteRecursively()
}
@Test fun cacheEvictionPreservesGuestSharedMemoryAndRuntimeBindings() {
val binds = LinuxRuntime.binds(context, null, null, null, null)
val shm = File(binds.single { it.endsWith(":/dev/shm") }.removeSuffix(":/dev/shm"))
val live = LinuxRuntime.liveDir(context)
val files = listOf(File(shm, "steamvr-ipc"), File(LinuxRuntime.prootTempDir(context), "loader"),
File(live, "drm/device"), File(live, "bwrap/1/state"))
files.forEach { it.parentFile!!.mkdirs(); it.writeText("active") }
val disposable = File(context.cacheDir, "download").apply { writeText("cached") }
context.cacheDir.deleteRecursively()
assertFalse(disposable.exists())
files.forEach { assertEquals("active", it.readText()) }
assertTrue(live.path in binds)
assertEquals(binds, LinuxRuntime.binds(context, null, null, null, null))
assertTrue(live.toPath().startsWith(context.noBackupFilesDir.toPath()))
}
@Test fun successfulRemovalClearsGuestScratchAndPreservesOtherNoBackupData() {
val runtime = runtime()
val scratch = scratch()
val unrelated = File(context.noBackupFilesDir, "calibration.json").apply { writeText("preserved") }
assertTrue(LinuxRuntimeInstaller.beginUninstall(context, runtime)!!.run(null))
assertFalse(runtime.exists())
assertFalse(scratch.exists())
assertFalse(File(context.noBackupFilesDir, "guest-runtime").exists())
assertEquals("preserved", unrelated.readText())
}
@Test fun failedRuntimeRemovalPreservesGuestScratch() {
val runtime = runtime()
val scratch = scratch()
val cannotMove = object : File(runtime.path) {
override fun renameTo(dest: File) = false
}
assertFalse(LinuxRuntimeInstaller.beginUninstall(context, cannotMove)!!.run(null))
assertTrue(runtime.exists())
assertEquals("active", scratch.readText())
}
@Test fun interruptedRemovalPreservesGuestScratch() {
val runtime = runtime()
val scratch = scratch()
val removal = LinuxRuntimeInstaller.beginUninstall(context, runtime)!!
Thread.currentThread().interrupt()
try {
assertFalse(removal.run(null))
} finally {
Thread.interrupted()
}
assertTrue(runtime.exists())
assertEquals("active", scratch.readText())
}
@Test fun aSessionStartingBeforeTheWorkerRunsPreservesRuntimeAndScratch() {
val runtime = runtime()
val scratch = scratch()
val removal = LinuxRuntimeInstaller.beginUninstall(context, runtime)!!
SessionState.phase = SessionPhase.STARTING_COMPOSITOR
assertFalse(removal.run(null))
assertTrue(runtime.exists())
assertEquals("active", scratch.readText())
}
@Test fun aSessionActiveBeforeScratchCleanupPreservesScratch() {
val runtime = runtime()
val scratch = scratch()
val removal = LinuxRuntimeInstaller.beginUninstall(context, runtime)!!
assertFalse(removal.run { _, _ -> SessionState.running = true })
assertEquals("active", scratch.readText())
}
private fun runtime() = LinuxRuntime.rootDir(context).apply {
mkdirs()
File(this, "runtime-file").writeText("installed")
}
private fun scratch() = File(LinuxRuntime.liveDir(context), "shm/steamvr-ipc").apply {
parentFile!!.mkdirs()
writeText("active")
}
}
@@ -0,0 +1,46 @@
package com.droiddeck.launcher.session
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class FexCatalogTest {
private val sha = "0fc1ca2ba13817ad70d1ea157119a75c9c02446b57d166e6d676cce0807c19c4"
private val url = "https://github.com/Droid-Deck/DroidDeck-Components/releases/download/fex-2610-r5/fex.tzst"
private fun json(release: String = "fex-2610-r5", url: String = this.url, sha256: String = sha) =
"""{"schemaVersion": 1, "category": "fex", "release": "$release", "url": "$url", "sha256": "$sha256", "size": 4083539}"""
@Test fun readsTheBuildComponentsNames() {
assertEquals(FexCatalog.Build("fex-2610-r5", url, sha), FexCatalog.parse(json()))
}
@Test fun readsTheSingleReleaseByItsVersion() {
val single = "https://github.com/Droid-Deck/DroidDeck-Components/releases/download/fex/fex.tzst"
assertEquals(FexCatalog.Build("fex-2610-r6", single, sha), FexCatalog.parse(json(release = "fex", url = single).replace("}", ", \"version\": \"2610-r6\"}")))
assertNull(FexCatalog.parse(json(release = "fex", url = single).replace("}", ", \"version\": \"latest\"}")))
assertNull(FexCatalog.parse(json(release = "fex", url = single)))
}
@Test fun refusesAnArchiveOutsideItsOwnRelease() {
assertNull(FexCatalog.parse(json(url = "https://example.com/fex.tzst")))
assertNull(FexCatalog.parse(json(url = url.replace("fex-2610-r5/", "fex-2610-r4/"))))
assertNull(FexCatalog.parse(json(release = "latest", url = url.replace("fex-2610-r5", "latest"))))
}
@Test fun refusesAMissingOrMalformedDigest() {
assertNull(FexCatalog.parse(json(sha256 = "abc")))
assertNull(FexCatalog.parse("""{"release": "fex-2610-r5", "url": "$url"}"""))
assertNull(FexCatalog.parse("not json"))
}
@Test fun theSessionGetsTheBuildAndEverySource() {
val env = FexCatalog.environment(FexCatalog.Build("fex-2610-r5", url, sha), listOf(url, "https://ghfast.top/$url"))
assertEquals(listOf("BL_FEX_TAG=fex-2610-r5", "BL_FEX_SHA256=$sha", "BL_FEX_URLS=$url https://ghfast.top/$url"), env)
}
}
@@ -14,6 +14,11 @@ class FexVersionTest {
assertNull(fexVersionOf("# FEX_TAG=fex-2610-r3\n"))
}
@Test fun readsAnInstalledOrCatalogTag() {
assertEquals("2610 r5", fexVersionOfTag("fex-2610-r5\n"))
assertNull(fexVersionOfTag("latest"))
}
@Test fun theShippedLauncherHasOne() {
val script = java.io.File("../tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-x64").takeIf { it.isFile }
?: java.io.File("tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-x64")
@@ -0,0 +1,171 @@
package com.droiddeck.launcher.ui
import android.view.Display
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.input.InputMode
import androidx.compose.ui.input.InputModeManager
import androidx.compose.ui.platform.LocalInputModeManager
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.*
import androidx.compose.ui.unit.dp
import com.droiddeck.launcher.glasses.GlassesResolution
import com.droiddeck.launcher.glasses.GlassesSettings
import com.droiddeck.launcher.glasses.GlassesState
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import java.io.File
@OptIn(ExperimentalTestApi::class, androidx.compose.ui.ExperimentalComposeUiApi::class)
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], qualifiers = "w360dp-h800dp")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class GlassesMenuNavigationTest {
@get:Rule val compose = createRobolectricComposeRule()
private lateinit var view: android.view.View
private lateinit var inputMode: InputModeManager
private val firstTab = FocusRequester()
private val glasses = GlassesState(present = true, connected = true, controllable = true,
displayId = Display.DEFAULT_DISPLAY + 1, model = "VITURE Luma Ultra", width = 1920, height = 1200,
hz = 60f, sbs90 = true, supportsSpatial = true, supportsScreen3dof = true, brightness = 4, volume = 3,
resolutions = listOf(GlassesResolution(1920, 1080), GlassesResolution(1920, 1200)))
@Test fun narrowMenuSeparatesDisplayVrTrackingAndDevice() {
show(inSession = false)
compose.onNodeWithTag("setting-glasses-resolution-two_d").assertExists()
compose.onNodeWithTag("setting-glasses-resolution-three_d").assertExists()
savePreview("display")
compose.onNodeWithTag("glasses-distance").performScrollTo()
savePreview("virtual-screen")
compose.onNodeWithTag("glasses-tab-vr").assertIsDisplayed().performClick()
compose.onNodeWithTag("setting-glasses-resolution-vr").assertIsDisplayed()
compose.onNodeWithTag("setting-glasses-resolution-two_d").assertDoesNotExist()
compose.onNodeWithTag("glasses-vr-fov").assertExists()
savePreview("vr")
compose.onNodeWithTag("glasses-tab-tracking").performClick()
compose.onNodeWithText("LUMA ULTRA CAMERAS — EXPERIMENTAL").assertExists()
compose.onNodeWithTag("setting-glasses-position").assertExists()
savePreview("tracking")
compose.onNodeWithTag("setting-glasses-hands").performScrollTo().assertIsDisplayed()
compose.onNodeWithTag("setting-glasses-joycons").assertExists()
savePreview("hands")
compose.onNodeWithTag("glasses-tab-hardware").assertIsDisplayed().performClick()
compose.onNodeWithTag("glasses-brightness").assertIsDisplayed()
compose.onNodeWithTag("glasses-volume").assertIsDisplayed()
compose.onNodeWithTag("setting-glasses-sunblock").assertIsDisplayed()
compose.onNodeWithTag("setting-glasses-position").assertDoesNotExist()
savePreview("device")
}
@Test fun controllerCanReachVrTabAndItsLaunchAction() {
var launches = 0
show(inSession = true, launch = { launches++ })
compose.runOnIdle { inputMode.requestInputMode(InputMode.Keyboard); firstTab.requestFocus() }
compose.onNodeWithTag("glasses-tab-display").assertIsFocused().performKeyInput {
pressKey(Key.DirectionRight)
pressKey(Key.Enter)
}
compose.onNodeWithTag("glasses-tab-vr").assertIsSelected()
compose.onNodeWithTag("glasses-tab-vr").performKeyInput { pressKey(Key.DirectionDown) }
compose.onNodeWithText("Start SteamVR").assertIsFocused().performKeyInput { pressKey(Key.Enter) }
assertEquals(1, launches)
savePreview("session-vr")
}
@Test fun unavailableHardwareDoesNotLeaveEmptyTabs() {
var tabs = emptyList<Pair<String, String>>()
compose.setContent { DroidDeckTheme { tabs = glassesTabs(glasses.copy(connected = false)) } }
compose.runOnIdle { assertEquals(listOf("display", "vr"), tabs.map { it.first }) }
}
@Test fun threeDofRecenterComesFirstOnEveryTab() {
assertRecenterComesFirst(GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN), false)
}
@Test fun sixDofRecenterComesFirstOnEveryTab() {
assertRecenterComesFirst(GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN, positionalTracking = true), false)
}
@Test fun vrRecenterComesFirstOnEveryTab() {
assertRecenterComesFirst(GlassesSettings(), true)
}
private fun assertRecenterComesFirst(settings: GlassesSettings, vr: Boolean) {
var recenters = 0
show(inSession = true, settings = settings, steamVrRunning = vr, recenter = { recenters++ })
val firstControls = listOf(
GlassesTab.DISPLAY to "setting-glasses-game",
GlassesTab.VR to "setting-glasses-auto3d",
GlassesTab.TRACKING to "setting-glasses-tracking",
GlassesTab.HARDWARE to "glasses-brightness",
)
for ((tab, control) in firstControls) {
compose.onNodeWithTag("glasses-tab-$tab").performClick()
val button = compose.onNodeWithText("Recenter").assertIsDisplayed()
assertTrue("Recenter must precede $control", button.fetchSemanticsNode().boundsInRoot.bottom <=
compose.onNodeWithTag(control).fetchSemanticsNode().boundsInRoot.top)
button.performClick()
}
assertEquals(4, recenters)
savePreview(if (vr) "recenter-vr" else if (settings.positionalTracking) "recenter-6dof" else "recenter-3dof")
}
private fun show(inSession: Boolean, launch: () -> Unit = {},
settings: GlassesSettings = GlassesSettings(positionalTracking = true),
steamVrRunning: Boolean = false, recenter: (() -> Unit)? = null) {
compose.setContent {
DroidDeckTheme {
view = LocalView.current
inputMode = LocalInputModeManager.current
var selected by remember { mutableStateOf(GlassesTab.DISPLAY) }
val host = rememberMenuHost()
Column(Modifier.fillMaxSize().background(MaterialTheme.colorScheme.background).padding(16.dp)) {
SegmentedTabs(glassesTabs(glasses), selected, Modifier.fillMaxWidth(), tabModifier = { key ->
Modifier.testTag("glasses-tab-$key").then(if (key == GlassesTab.DISPLAY)
Modifier.focusRequester(firstTab) else Modifier)
}) { selected = it; host.open = null }
key(selected) {
Column(Modifier.weight(1f).verticalScroll(rememberScrollState())) {
CompositionLocalProvider(LocalChipMinWidth provides 120.dp) {
GlassesGroups(host, glasses, settings, onRecenter = recenter, steamVrRunning = steamVrRunning,
track = { if (it in listOf("glasses-width", "glasses-distance", "glasses-vr-fov",
"glasses-brightness", "glasses-volume")) Modifier.testTag(it) else Modifier }, inSession = inSession, tab = selected,
steamVr = { if (inSession) ActionRow("SteamVR", null, "Start SteamVR", launch) })
}
}
}
}
}
}
}
private fun savePreview(name: String) {
val file = File("build/reports/glasses-menus/$name.png")
file.parentFile?.mkdirs()
compose.runOnIdle {
val root = view.rootView
val bitmap = android.graphics.Bitmap.createBitmap(root.width, root.height, android.graphics.Bitmap.Config.ARGB_8888)
root.draw(android.graphics.Canvas(bitmap))
file.outputStream().use { bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, it) }
bitmap.recycle()
}
}
}
@@ -6,9 +6,15 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
import com.droiddeck.launcher.R
import com.droiddeck.launcher.glasses.GlassesSettings
import com.droiddeck.launcher.glasses.GlassesState
@@ -24,6 +30,60 @@ import org.robolectric.annotation.Config
class GlassesPanelTest {
@get:Rule val compose = createRobolectricComposeRule()
@Test fun flatResolutionMenuShowsPerEyeResolutionsBesideRefresh() {
show()
compose.onNodeWithTag("setting-glasses-hz").assertExists()
compose.onNodeWithTag("setting-glasses-resolution-two_d").performScrollTo().performClick()
compose.onNodeWithText("1920 × 1080").assertExists()
compose.onNodeWithText("1920 × 1200").assertExists()
}
@Test fun stereoResolutionHasItsOwnPreference() {
show(stereo = true)
compose.onNodeWithTag("setting-glasses-resolution-three_d").assertExists()
compose.onNodeWithTag("setting-glasses-resolution-two_d").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-3d-hz").assertExists()
}
@Test fun vrResolutionDoesNotEditTheOrdinaryStereoPreference() {
show(stereo = true, steamVrRunning = true, tab = GlassesTab.VR)
compose.onNodeWithTag("setting-glasses-resolution-vr").performScrollTo().performClick()
compose.onNodeWithText("1920 × 1080").assertExists()
compose.onNodeWithTag("setting-glasses-resolution-three_d").assertDoesNotExist()
}
@Test fun homeDisplayCanConfigureBothOrdinaryResolutionProfiles() {
show(inSession = false)
for (profile in listOf("two_d", "three_d"))
compose.onNodeWithTag("setting-glasses-resolution-$profile").assertExists()
}
@Test fun nativeThirtyHzAppearsInTheFlatRefreshMenu() {
show(supports30Hz = true)
compose.onNodeWithTag("setting-glasses-hz").performScrollTo().performClick()
compose.onNodeWithText("30 Hz").assertExists()
}
@Test fun nativeThirtyHzAppearsInStereoEvenWithoutNinetyHzSupport() {
show(stereo = true, supports30Hz = true, sbs90 = false)
compose.onNodeWithTag("setting-glasses-3d-hz").performScrollTo().performClick()
compose.onNodeWithText("30 Hz").assertExists()
compose.onNodeWithText("90 Hz").assertDoesNotExist()
}
@Test fun unsupportedThirtyHzIsAbsentFromTheRefreshMenu() {
show()
compose.onNodeWithTag("setting-glasses-hz").performScrollTo().performClick()
compose.onNodeWithText("30 Hz").assertDoesNotExist()
}
@Test fun mirroredNativeThirtyHzExplainsTheDedicatedOutputRequirement() {
show(settings = GlassesSettings(gameOnGlasses = false), supports30Hz = true)
compose.onNodeWithText(text(R.string.glasses_refresh_30_hint)).assertExists()
compose.onNodeWithTag("setting-glasses-hz").performScrollTo().performClick()
compose.onNodeWithText("30 Hz").assertDoesNotExist()
}
@Test fun automaticFlatGameOffersVirtualScreenInsteadOfIneffectiveSliders() {
show()
compose.onNodeWithTag("glasses-width").assertDoesNotExist()
@@ -44,13 +104,13 @@ class GlassesPanelTest {
}
@Test fun automaticSteamVrShowsFieldOfViewAndStereoRefresh() {
show(stereo = true, steamVrRunning = true)
show(stereo = true, steamVrRunning = true, tab = GlassesTab.VR)
compose.onNodeWithTag("glasses-width").assertDoesNotExist()
compose.onNodeWithTag("glasses-distance").assertDoesNotExist()
compose.onNodeWithTag("glasses-vr-fov").assertIsEnabled()
compose.onNodeWithTag("setting-glasses-hz").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-3d-hz").assertExists()
compose.onNodeWithText(text(R.string.glasses_screen_direct_hint)).assertExists()
compose.onNodeWithTag("setting-glasses-auto3d").assertExists()
}
@Test fun mirroredSteamVrIn2dKeepsFieldOfViewAndRecenter() {
@@ -63,13 +123,22 @@ class GlassesPanelTest {
assertMirroredVrControls()
}
@Test fun mirroredSteamVrVirtualScreenKeepsGeometryAndRecenter() {
@Test fun mirroredSteamVrUsesStereoControlsWithASavedVirtualScreen() {
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN),
steamVrRunning = true, outputActive = false, tab = null)
compose.onNodeWithTag("glasses-width").assertIsEnabled()
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertExists()
compose.onNodeWithText(text(R.string.glasses_screen_no_output)).assertDoesNotExist()
compose.onNodeWithTag("glasses-width").assertDoesNotExist()
compose.onNodeWithTag("glasses-distance").assertDoesNotExist()
compose.onNodeWithText(text(R.string.glasses_screen_direct_hint)).assertExists()
assertMirroredVrControls()
}
@Test fun automatic3dForVrUsesStereoControlsWithASavedVirtualScreen() {
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN, auto3d = true),
stereo = true, steamVrRunning = true, tab = null)
compose.onNodeWithTag("glasses-width").assertDoesNotExist()
compose.onNodeWithTag("glasses-distance").assertDoesNotExist()
compose.onNodeWithText(text(R.string.glasses_screen_direct_hint)).assertExists()
assertMirroredVrControls()
}
private fun assertMirroredVrControls() {
@@ -78,13 +147,44 @@ class GlassesPanelTest {
compose.onNodeWithText(text(R.string.glasses_screen_no_output)).assertDoesNotExist()
}
@Test fun automaticStereoOutputWithoutSteamVrUsesVirtualScreenDimensions() {
@Test fun automaticStereoOutputDoesNotEnable3dofOrVirtualScreenDimensions() {
show(stereo = true)
compose.onNodeWithTag("glasses-width").assertIsEnabled()
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
compose.onNodeWithTag("setting-glasses-3dof").assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_off)))
compose.onNodeWithTag("glasses-width").assertDoesNotExist()
compose.onNodeWithTag("glasses-distance").assertDoesNotExist()
compose.onNodeWithTag("glasses-vr-fov").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-hz").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-3d-hz").assertExists()
compose.onNodeWithText(text(R.string.glasses_screen_flat_hint)).assertExists()
compose.onNodeWithText(text(R.string.glasses_screen_sbs_hint)).assertDoesNotExist()
}
@Test fun automaticFlatSbsKeepsTrackingInactive() {
show(stereo = true, tab = GlassesTab.TRACKING)
compose.onNodeWithText(text(R.string.glasses_tracking_flat_hint)).assertExists()
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
}
@Test fun flatGame3dofToggleIsIndependentOfSbsAndShowsScreenControls() {
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN))
compose.onNodeWithTag("setting-glasses-3dof").assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_on)))
compose.onNodeWithTag("setting-glasses-3d").assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_off)))
compose.onNodeWithTag("glasses-width").assertIsEnabled()
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
}
@Test fun separate3dofToggleIsHiddenWithoutHeadTrackingSupport() {
show(supportsScreen3dof = false)
compose.onNodeWithTag("setting-glasses-3dof").assertDoesNotExist()
}
@Test fun virtualScreenRecenterExplainsHowToSetTheHeight() {
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN), tab = GlassesTab.TRACKING)
compose.onNodeWithText(text(R.string.glasses_recenter_screen_hint)).assertExists()
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertExists()
}
@Test fun gameWithoutGlassesOutputExplainsWhyGeometryIsUnavailable() {
@@ -99,8 +199,8 @@ class GlassesPanelTest {
show(inSession = false, outputActive = false)
compose.onNodeWithTag("glasses-width").assertIsEnabled()
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
compose.onNodeWithTag("glasses-vr-fov").assertIsEnabled()
compose.onNodeWithText(text(R.string.glasses_screen_next_session)).assertExists()
compose.onNodeWithTag("glasses-vr-fov").assertDoesNotExist()
compose.onNodeWithText(text(R.string.glasses_screen_app_wide)).assertExists()
}
@Test fun flatTrackingExplainsTheModeAndHidesInactiveRecenter() {
@@ -116,12 +216,13 @@ class GlassesPanelTest {
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
compose.onNodeWithTag("setting-glasses-hz").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-3d").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-3dof").assertDoesNotExist()
}
@Test fun runningSteamVrFieldOfViewRemainsAvailableWhenUsbControlDisconnects() {
show(stereo = true, steamVrRunning = true, controlAvailable = false)
show(stereo = true, steamVrRunning = true, controlAvailable = false, tab = GlassesTab.VR)
compose.onNodeWithTag("glasses-vr-fov").assertIsEnabled()
compose.onNodeWithTag("setting-glasses-picture").assertIsEnabled()
compose.onNodeWithTag("setting-glasses-picture").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-3d-hz").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-3d").assertDoesNotExist()
}
@@ -142,6 +243,152 @@ class GlassesPanelTest {
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
}
@Test fun homeVrCanConfigureResolutionAndFieldOfViewBeforeLaunching() {
show(inSession = false, tab = GlassesTab.VR)
compose.onNodeWithTag("setting-glasses-resolution-vr").assertExists()
compose.onNodeWithTag("glasses-vr-fov").assertIsEnabled()
compose.onNodeWithTag("setting-glasses-auto3d").assertExists()
compose.onNodeWithTag("setting-glasses-picture").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-resolution-two_d").assertDoesNotExist()
}
@Test fun displayKeepsVrAndHardwareControlsInTheirOwnTabs() {
show(inSession = false)
compose.onNodeWithTag("setting-glasses-auto3d").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-resolution-vr").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-sunblock").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-tracking").assertDoesNotExist()
}
@Test fun recenterIsHiddenWhenHeadTrackingIsOff() {
show(settings = GlassesSettings(headTracking = false, picture3d = GlassesSettings.PICTURE_SCREEN))
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
}
@Test fun trackedMenusCanRecenterWithoutStartingASession() {
var recenters = 0
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN), inSession = false,
onRecenter = { recenters++ })
compose.onNodeWithText(text(R.string.glasses_recenter_screen_hint)).assertExists()
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).performClick()
org.junit.Assert.assertEquals(1, recenters)
}
@Test fun positionalMenusUseTheSameRecenterAction() {
var recenters = 0
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN, positionalTracking = true),
supportsSpatial = true, inSession = false, onRecenter = { recenters++ })
compose.onNodeWithText(text(R.string.glasses_recenter_screen_hint)).assertExists()
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).performClick()
org.junit.Assert.assertEquals(1, recenters)
}
@Test fun menusWithoutDedicatedGlassesOutputDoNotOfferRecenter() {
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN, gameOnGlasses = false),
inSession = false)
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
}
@Test fun spatialSteamVrIncludesHeadTrackingAndRestoresTheSavedVirtualScreenSwitch() {
val vrRunning = androidx.compose.runtime.mutableStateOf(true)
val saved = GlassesSettings(headTracking = false, positionalTracking = true,
picture3d = GlassesSettings.PICTURE_SCREEN)
show(settings = saved, supportsSpatial = true, tab = GlassesTab.TRACKING, observedSteamVr = vrRunning)
compose.onNodeWithTag("setting-glasses-tracking").assertIsNotEnabled().assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_on)))
compose.onNodeWithText(text(R.string.glasses_head_tracking_spatial_vr_hint)).assertExists()
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
compose.runOnIdle { vrRunning.value = false }
compose.onNodeWithTag("setting-glasses-tracking").assertIsEnabled().assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_off)))
compose.onNodeWithText(text(R.string.glasses_head_tracking_spatial_hint)).assertExists()
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
}
@Test fun rotationOnlySteamVrKeepsTheHeadTrackingSwitchOptional() {
show(settings = GlassesSettings(headTracking = false), supportsSpatial = true,
steamVrRunning = true, tab = GlassesTab.TRACKING)
compose.onNodeWithTag("setting-glasses-tracking").assertIsEnabled().assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_off)))
compose.onNodeWithText(text(R.string.glasses_head_tracking_hint)).assertExists()
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
}
@Test fun spatialPreferencesDoNotClaimHeadTrackingForPhoneOutput() {
val noDisplay = androidx.compose.runtime.mutableStateOf(GlassesState(present = true,
connected = true, controllable = true, supportsSpatial = true))
show(settings = GlassesSettings(headTracking = false, positionalTracking = true),
steamVrRunning = true, outputActive = false, tab = GlassesTab.TRACKING, observedState = noDisplay)
compose.onNodeWithTag("setting-glasses-tracking").assertIsEnabled().assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_off)))
compose.onNodeWithText(text(R.string.glasses_screen_no_output)).assertExists()
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
}
@Test fun experimentalJoyConsAreOffAndRequirePositionalTracking() {
show(supportsSpatial = true, tab = GlassesTab.TRACKING)
compose.onNodeWithText(text(R.string.glasses_experimental).uppercase()).assertExists()
compose.onNodeWithTag("setting-glasses-joycons").assertIsNotEnabled().assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_off)))
}
@Test fun experimentalGroupFollowsOrdinaryTrackingAndEnablesJoyCons() {
show(supportsSpatial = true, settings = GlassesSettings(positionalTracking = true), tab = GlassesTab.TRACKING)
compose.onNodeWithTag("setting-glasses-joycons").performScrollTo().assertIsEnabled()
val ordinary = compose.onNodeWithTag("setting-glasses-tracking").fetchSemanticsNode().positionInRoot.y
val experimental = compose.onNodeWithText(text(R.string.glasses_experimental).uppercase()).fetchSemanticsNode().positionInRoot.y
org.junit.Assert.assertTrue(experimental > ordinary)
compose.onNodeWithTag("setting-glasses-hands").assertExists()
compose.onNodeWithTag("setting-glasses-steady-hand-aim").assertDoesNotExist()
}
@Test fun steadyHandAimIsOptionalAndOffByDefault() {
show(supportsSpatial = true, settings = GlassesSettings(positionalTracking = true, handControllers = true),
tab = GlassesTab.TRACKING)
compose.onNodeWithTag("setting-glasses-steady-hand-aim").performScrollTo().assertIsEnabled().assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_off)))
compose.onNodeWithText(text(R.string.glasses_steady_hand_aim_hint)).assertExists()
}
@Test fun savedSteadyHandAimIsHiddenInJoyConMode() {
show(supportsSpatial = true, settings = GlassesSettings(positionalTracking = true, joyConTracking = true, steadyHandAim = true),
tab = GlassesTab.TRACKING)
compose.onNodeWithTag("setting-glasses-steady-hand-aim").assertDoesNotExist()
}
@Test fun ultraControlsDisappearOnDisconnectAndForOtherGlassesWithSavedPreferences() {
val state = androidx.compose.runtime.mutableStateOf(GlassesState(present = true, connected = true,
controllable = true, supportsSpatial = true, model = "VITURE Luma Ultra"))
show(settings = GlassesSettings(positionalTracking = true, handControllers = true, joyConTracking = true),
tab = GlassesTab.TRACKING, observedState = state)
compose.onNodeWithTag("setting-glasses-position").assertExists()
compose.onNodeWithTag("setting-glasses-hands").assertExists()
compose.onNodeWithTag("setting-glasses-joycons").assertExists()
val otherStates = listOf(state.value.copy(connected = false)) +
listOf("VITURE Luma Ultra", "VITURE Luma Pro", "VITURE Pro", "XREAL", "RayNeo").map {
state.value.copy(model = it, supportsSpatial = false)
}
for (other in otherStates) {
compose.runOnIdle { state.value = other }
compose.onNodeWithTag("setting-glasses-position").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-hands").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-steady-hand-aim").assertDoesNotExist()
compose.onNodeWithTag("setting-glasses-joycons").assertDoesNotExist()
compose.onNodeWithText(text(R.string.glasses_spatial_setup)).assertDoesNotExist()
compose.onNodeWithText(text(R.string.glasses_eye_height)).assertDoesNotExist()
compose.onNodeWithText(text(R.string.glasses_floor)).assertDoesNotExist()
}
compose.runOnIdle { state.value = state.value.copy(supportsSpatial = true) }
compose.onNodeWithTag("setting-glasses-position").assertExists()
}
private fun show(
settings: GlassesSettings = GlassesSettings(),
stereo: Boolean = false,
@@ -149,23 +396,33 @@ class GlassesPanelTest {
inSession: Boolean = true,
outputActive: Boolean = inSession,
controlAvailable: Boolean = true,
supportsScreen3dof: Boolean = true,
supportsSpatial: Boolean = false,
supports30Hz: Boolean = false,
sbs90: Boolean = true,
tab: String? = GlassesTab.DISPLAY,
observedState: androidx.compose.runtime.State<GlassesState>? = null,
observedSteamVr: androidx.compose.runtime.State<Boolean>? = null,
onRecenter: (() -> Unit)? = {},
) {
val state = GlassesState(present = true, connected = controlAvailable, controllable = true,
displayId = Display.DEFAULT_DISPLAY + 1, width = if (stereo) 3840 else 1920, height = 1200,
model = "VITURE Luma Ultra", sbs = stereo, sbs90 = true)
model = "VITURE Luma Ultra", sbs = stereo, sbs90 = sbs90, supportsScreen3dof = supportsScreen3dof,
supports30Hz = supports30Hz, supportsSpatial = supportsSpatial, resolutions = listOf(
com.droiddeck.launcher.glasses.GlassesResolution(1920, 1080),
com.droiddeck.launcher.glasses.GlassesResolution(1920, 1200)))
compose.setContent {
DroidDeckTheme {
Column(Modifier.verticalScroll(rememberScrollState())) {
GlassesGroups(rememberMenuHost(), state, settings,
GlassesGroups(rememberMenuHost(), observedState?.value ?: state, settings,
track = { key ->
when (key) {
"glasses-width", "glasses-distance", "glasses-vr-fov" -> Modifier.testTag(key)
else -> Modifier
}
}, onRecenter = {},
}, onRecenter = onRecenter,
inSession = inSession, outputActive = outputActive,
steamVrRunning = steamVrRunning, tab = tab)
steamVrRunning = observedSteamVr?.value ?: steamVrRunning, tab = tab)
}
}
}
@@ -0,0 +1,160 @@
package com.droiddeck.launcher.ui
import android.app.Application
import android.content.Context
import android.os.Handler
import android.os.Looper
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
import com.droiddeck.launcher.R
import com.droiddeck.launcher.glasses.Glasses
import com.droiddeck.launcher.glasses.GlassesSettings
import com.droiddeck.launcher.glasses.GlassesState
import kotlinx.coroutines.flow.MutableStateFlow
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], qualifiers = "w1200dp-h752dp-land", application = Application::class)
@LooperMode(LooperMode.Mode.PAUSED)
class GlobalGlassesSettingsTest {
@get:Rule val compose = createRobolectricComposeRule()
private val handler = Handler(Looper.getMainLooper())
private val connected = GlassesState(present = true, connected = true, controllable = true,
displayId = 2, width = 1920, height = 1080, model = "VITURE Luma Ultra",
supportsScreen3dof = true, supportsSpatial = true)
private lateinit var stateBefore: GlassesState
private lateinit var settingsBefore: GlassesSettings
private var appBefore: Any? = null
private var handlerBefore: Any? = null
@Before fun prepare() {
stateBefore = Glasses.state.value
settingsBefore = Glasses.settings.value
appBefore = field("app").get(null)
handlerBefore = field("handler").get(null)
val app = RuntimeEnvironment.getApplication()
app.getSharedPreferences("glasses", Context.MODE_PRIVATE).edit().clear().commit()
field("app").set(null, app)
field("handler").set(null, handler)
state().value = GlassesState()
settings().value = GlassesSettings()
}
@After fun restore() {
handler.removeCallbacksAndMessages(null)
state().value = stateBefore
settings().value = settingsBefore
field("app").set(null, appBefore)
field("handler").set(null, handlerBefore)
}
@Test fun globalSetupOffersGlassesWithoutSteamOrAnInstalledRuntime() {
show()
openGlasses()
compose.onNodeWithText(text(R.string.setup_glasses_scope)).assertExists()
compose.onNodeWithText(text(R.string.setup_glasses_connect)).assertExists()
compose.onNodeWithTag("setting-glasses-game").assertDoesNotExist()
compose.onNodeWithTag("play-steamvr").assertDoesNotExist()
compose.onNodeWithText(text(R.string.setup_tab_diagnostics)).performClick()
compose.onNodeWithTag("setting-logs").assertExists()
openGlasses()
compose.onNodeWithText(text(R.string.setup_glasses_connect)).assertExists()
}
@Test fun hotplugKeepsTheGlobalPageAndSharedTrackingPreferences() {
val saved = GlassesSettings(positionalTracking = true, handControllers = true)
settings().value = saved
show()
openGlasses()
compose.runOnIdle { state().value = connected }
compose.onNodeWithTag("setting-glasses-3dof").assertExists()
compose.onNodeWithTag("setup-glasses-tab-tracking").performScrollTo().performClick()
compose.onNodeWithTag("setting-glasses-position").assertExists()
compose.runOnIdle { state().value = GlassesState() }
compose.onNodeWithText(text(R.string.setup_glasses_connect)).assertExists()
compose.onNodeWithTag("setting-glasses-position").assertDoesNotExist()
assertEquals(saved, Glasses.settings.value)
compose.runOnIdle { state().value = connected }
compose.onNodeWithTag("setting-glasses-position").assertExists()
compose.onNodeWithTag("setting-glasses-hands").assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_on)))
assertEquals(saved, Glasses.settings.value)
}
@Test fun globalScreenTrackingUsesTheExistingSavedSettings() {
state().value = connected
settings().value = GlassesSettings(gameOnGlasses = false, headTracking = false)
show()
openGlasses()
compose.onNodeWithTag("setting-glasses-3dof").performScrollTo().performClick()
shadowOf(Looper.getMainLooper()).idle()
assertTrue(Glasses.settings.value.screen3dof)
val prefs = RuntimeEnvironment.getApplication().getSharedPreferences("glasses", Context.MODE_PRIVATE)
assertTrue(prefs.getBoolean("gameOnGlasses", false))
assertTrue(prefs.getBoolean("headTracking", false))
assertEquals(GlassesSettings.PICTURE_SCREEN, prefs.getInt("picture3d", -1))
compose.onNodeWithText(text(R.string.setup_tab_launcher)).performClick()
openGlasses()
compose.onNodeWithTag("setting-glasses-3dof").assert(
SemanticsMatcher.expectValue(SemanticsProperties.StateDescription, text(R.string.widgets_on)))
}
private fun openGlasses() {
compose.onNodeWithText(text(R.string.setup_tab_glasses)).performClick()
}
private fun show() {
compose.setContent {
DroidDeckTheme {
CompositionLocalProvider(LocalFrontFocus provides FrontFocus(), LocalNarrowPane provides false) {
FocusGlideHost {
SetupPanel(FrontEndState(
installed = null, ready = false, available = null, busy = false, stage = "", percent = -1,
desktopInstalled = false, offlineAccount = null, offline = false, frameGenLabel = "",
romsDir = null, logsEnabled = false, steamGames = emptyList(), emulators = emptyList(), running = null,
), FrontEndActions(
onPlay = {}, onPlayDesktopUi = {}, onSteamGame = {}, onDesktop = {}, onEmulator = {},
onRom = {}, onResume = {}, onSteamSettings = {}, onDesktopSettings = {},
onInstallPackage = {}, onRemovePackage = {}, onRuntime = {}, onFrameGenPick = {},
onImportLossless = {}, onProtons = {}, onComponents = {}, onPerformance = {},
onRoms = {}, onFiles = {}, onLogs = {}, onOffline = {},
onWirelessAdbPair = { _, _, _, _ -> }, onWirelessAdbApply = { _, _, _, _ -> },
onSetPhantomProcessLimit = { _, _ -> },
), onOpenDeveloperOptions = {}, onRequestWirelessAdb = {})
}
}
}
}
}
private fun text(id: Int) = RuntimeEnvironment.getApplication().getString(id)
private fun field(name: String) = Glasses::class.java.getDeclaredField(name).apply { isAccessible = true }
@Suppress("UNCHECKED_CAST")
private fun state() = field("_state").get(null) as MutableStateFlow<GlassesState>
@Suppress("UNCHECKED_CAST")
private fun settings() = field("_settings").get(null) as MutableStateFlow<GlassesSettings>
}
@@ -0,0 +1,273 @@
package com.droiddeck.launcher.ui
import androidx.compose.ui.test.*
import androidx.compose.ui.platform.LocalView
import androidx.compose.runtime.mutableStateOf
import com.droiddeck.launcher.glasses.SpatialCalibrationProgress
import com.droiddeck.launcher.glasses.SpatialTrackingStatus
import com.droiddeck.launcher.glasses.CalibrationDistance
import com.droiddeck.launcher.glasses.CalibrationDistanceCheck
import com.droiddeck.launcher.R
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import java.io.File
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], qualifiers = "w411dp-h891dp")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class SpatialSetupTest {
@get:Rule val compose = createRobolectricComposeRule()
private lateinit var view: android.view.View
@Test
@Config(qualifiers = "fr-w411dp-h891dp")
fun frenchCalibrationControlsKeepTheSelectedDistanceAndActions() {
val context = RuntimeEnvironment.getApplication()
var selected = 0f
compose.setContent {
DroidDeckTheme {
view = LocalView.current
SpatialSetupScreen(SpatialTrackingStatus(trackingValid = true, synchronized = true),
SpatialCalibrationProgress(views = 24, fitted = true), null, "",
SpatialSetupActions(validate = { selected = it }))
}
}
compose.onNodeWithText("Configuration spatiale").assertIsDisplayed()
compose.onNodeWithText("Spatial setup").assertDoesNotExist()
compose.onNodeWithText(context.getString(R.string.spatial_validate)).performScrollTo().performClick()
assertEquals(.3048f, selected, .000001f)
savePreview("calibration-fr")
}
@Test
@Config(qualifiers = "ar-w411dp-h891dp")
fun arabicCalibrationDiagnosticsUseTranslatedLabelsAndFormatNumbers() {
val context = RuntimeEnvironment.getApplication()
compose.setContent {
DroidDeckTheme {
view = LocalView.current
SpatialSetupScreen(SpatialTrackingStatus(cameraHz = 25f, cameraCopyPermille = 500, frames = 12),
SpatialCalibrationProgress(), null, "", SpatialSetupActions())
}
}
compose.onNodeWithText(context.getString(R.string.spatial_title)).assertIsDisplayed()
compose.onNodeWithText("Spatial setup").assertDoesNotExist()
compose.onNodeWithTag("setting-spatial-diagnostics").performScrollTo().performClick()
compose.onNodeWithText(context.getString(R.string.spatial_input_values, 12.5f, 50f)).performScrollTo().assertIsDisplayed()
compose.onNodeWithText("Show diagnostics").assertDoesNotExist()
savePreview("calibration-ar")
}
@Test fun directLaunchWithoutConnectedUltraClosesBeforeShowingCalibration() {
val field = com.droiddeck.launcher.glasses.Glasses::class.java.getDeclaredField("_state")
.apply { isAccessible = true }
@Suppress("UNCHECKED_CAST")
val state = field.get(null) as kotlinx.coroutines.flow.MutableStateFlow<com.droiddeck.launcher.glasses.GlassesState>
val previous = state.value
try {
for (candidate in listOf(previous.copy(connected = false, supportsSpatial = true),
previous.copy(connected = true, controllable = true, supportsSpatial = false))) {
state.value = candidate
val activity = org.robolectric.Robolectric.buildActivity(SpatialTrackingActivity::class.java).create()
assertTrue(activity.get().isFinishing)
activity.destroy()
}
} finally { state.value = previous }
}
@Test fun sharedCalibrationIsUsableWithoutClaimingPerDeviceVerification() {
compose.setContent {
DroidDeckTheme {
SpatialSetupScreen(SpatialTrackingStatus(calibration = true,
calibrationSource = "luma-ultra-base", calibrationVerified = false),
SpatialCalibrationProgress(fitted = true), null, "", SpatialSetupActions())
}
}
compose.onNodeWithText("Shared Luma Ultra calibration").performScrollTo().assertIsDisplayed()
compose.onNodeWithText("Measured calibration verified").assertDoesNotExist()
compose.onNodeWithText("Validation required").assertDoesNotExist()
compose.onNodeWithText("Hand tracking — Experimental").assertExists()
}
@Test fun pendingRecenterExplainsTheWaitAndBlocksCaptures() {
compose.setContent {
DroidDeckTheme(Themes.ELECTRIC) {
view = LocalView.current
SpatialSetupScreen(SpatialTrackingStatus(message = "Hold still while tracking stabilizes", recenterPending = true),
SpatialCalibrationProgress(collecting = true), null, "", SpatialSetupActions())
}
}
compose.onNodeWithText("Hold still while tracking stabilizes").assertExists()
compose.onNodeWithText("Waiting for tracking…").assertIsNotEnabled()
savePreview("tracking-recovery")
compose.onNodeWithText("Capture board view").performScrollTo().assertIsNotEnabled()
}
@Test fun completedFitValidatesTheSelectedImperialOrMetricDistance() {
var distance = 0f
compose.setContent {
DroidDeckTheme {
view = LocalView.current
SpatialSetupScreen(SpatialTrackingStatus(trackingValid = true, synchronized = true),
SpatialCalibrationProgress(collecting = true, views = 24, fitted = true), null, "Ready to validate",
SpatialSetupActions(validate = { distance = it }))
}
}
compose.onNodeWithText("Validate distance").performScrollTo().performClick()
assertEquals(0.3048f, distance, 0.000001f)
savePreview("distance-check")
for (choice in CalibrationDistance.entries.drop(1)) {
compose.onNodeWithTag("setting-calibration-distance").performScrollTo().performClick()
compose.onNodeWithText(RuntimeEnvironment.getApplication().getString(choice.labelRes)).performClick()
compose.onNodeWithText("Validate distance").performScrollTo().performClick()
assertEquals(choice.meters, distance, 0.000001f)
}
}
@Test fun captureRejectionAppearsBesideTheCounter() {
val reason = "Capture rejected: 4 / 12 required corners visible in both cameras."
compose.setContent {
DroidDeckTheme {
SpatialSetupScreen(SpatialTrackingStatus(trackingValid = true, synchronized = true),
SpatialCalibrationProgress(collecting = true, views = 2, captureMessage = reason), null, "",
SpatialSetupActions())
}
}
compose.onNodeWithText("Capture board view").performScrollTo().assertIsEnabled()
compose.onNodeWithText("2 / 24 views captured").assertIsDisplayed()
compose.onNodeWithText(reason).assertIsDisplayed()
}
@Test fun pendingCaptureCannotQueueAnotherPress() {
compose.setContent {
DroidDeckTheme {
SpatialSetupScreen(SpatialTrackingStatus(trackingValid = true, synchronized = true),
SpatialCalibrationProgress(collecting = true, capturing = true), null, "", SpatialSetupActions())
}
}
compose.onNodeWithText("Capturing…").performScrollTo().assertIsNotEnabled()
}
@Test fun distanceResultAppearsBesideValidationButton() {
val reason=CalibrationDistanceCheck.evaluate(RuntimeEnvironment.getApplication(),.3048f,3,null).message
compose.setContent {
DroidDeckTheme {
view=LocalView.current
SpatialSetupScreen(SpatialTrackingStatus(trackingValid=true,synchronized=true),
SpatialCalibrationProgress(views=25,fitted=true,validationMessage=reason),null,
"Fit 1.12 px. Measure the distance, then validate.",SpatialSetupActions())
}
}
compose.onNodeWithText("Validate distance").performScrollTo().assertIsEnabled()
compose.onNodeWithText(reason).assertIsDisplayed()
savePreview("distance-result")
}
@Test fun pendingDistanceCheckShowsFeedbackAndPreventsDuplicateRequests() {
val message="Checking the next camera image. Keep the full board visible and hold still."
compose.setContent {
DroidDeckTheme {
SpatialSetupScreen(SpatialTrackingStatus(trackingValid=true,synchronized=true),
SpatialCalibrationProgress(views=25,fitted=true,validating=true,validationMessage=message),null,
"Fit complete",SpatialSetupActions())
}
}
compose.onNodeWithText("Checking distance…").performScrollTo().assertIsNotEnabled()
compose.onNodeWithText(message).assertIsDisplayed()
}
@Test fun restoredCapturesCanBeRecalculatedWithoutMixingNewTrackingFrames() {
var solves=0
compose.setContent {
DroidDeckTheme {
SpatialSetupScreen(SpatialTrackingStatus(trackingValid = true, synchronized = true),
SpatialCalibrationProgress(views = 35, captureMessage = "35 saved views restored."), null,
"Head-to-camera alignment varies by 25 cm (9.84 in).",
SpatialSetupActions(solve = { solves++ }))
}
}
compose.onNodeWithText("Capture board view").performScrollTo().assertIsNotEnabled()
compose.onNodeWithText("35 saved views restored.").assertIsDisplayed()
compose.onNodeWithText("Calculate calibration").performScrollTo().assertIsEnabled().performClick()
assertEquals(1,solves)
}
@Test
@Config(qualifiers = "w840dp-h360dp-land")
fun landscapeSetupLeavesMostOfTheHeightForScrolling() {
compose.setContent {
DroidDeckTheme {
view = LocalView.current
SpatialSetupScreen(SpatialTrackingStatus(trackingValid = true, synchronized = true),
SpatialCalibrationProgress(views = 35), null, "Ready to calculate", SpatialSetupActions())
}
}
compose.waitForIdle()
val page=compose.onNodeWithTag("spatial-setup-page").fetchSemanticsNode().boundsInRoot
val viewport=compose.onNode(hasScrollAction()).fetchSemanticsNode().boundsInRoot
assertTrue("Calibration controls need at least 75% of the page height", viewport.height >= page.height*.75f)
savePreview("landscape-compact")
}
@Test fun diagnosticsSeparateCameraRateFromDeliberatelySkippedFrames() {
compose.setContent {
DroidDeckTheme {
SpatialSetupScreen(SpatialTrackingStatus(cameraHz = 25f, cameraFrameDivisor = 2, cameraCopyPermille = 500,
frames = 12, dropped = 3, rateLimitedFrames = 15),
SpatialCalibrationProgress(), null, "", SpatialSetupActions())
}
}
compose.onNodeWithTag("setting-spatial-diagnostics").performScrollTo().performClick()
compose.onNodeWithText("Input limit 12.5 Hz • 50% of camera frames. Head tracking stays at full rate.").assertExists()
compose.onNodeWithText("12 processed • 3 dropped • 15 skipped by input limit").assertExists()
compose.onNodeWithText("25.0 Hz", substring = true).assertExists()
}
@Test fun diagnosticsShowTheAdaptiveFractionInsteadOfTheLegacyDivisor() {
compose.setContent {
DroidDeckTheme {
SpatialSetupScreen(SpatialTrackingStatus(cameraHz = 25f, cameraFrameDivisor = 1, cameraCopyPermille = 600),
SpatialCalibrationProgress(), null, "", SpatialSetupActions())
}
}
compose.onNodeWithTag("setting-spatial-diagnostics").performScrollTo().performClick()
compose.onNodeWithText("Input limit 15.0 Hz • 60% of camera frames. Head tracking stays at full rate.").assertExists()
}
@Test fun manualRateControlBecomesAvailableAfterAutomaticRateIsDisabled() {
val automatic=mutableStateOf(true)
val halfRate=mutableStateOf(true)
compose.setContent {
DroidDeckTheme {
SpatialSetupScreen(SpatialTrackingStatus(), SpatialCalibrationProgress(), null, "",
SpatialSetupActions(adaptive = { automatic.value=it }, halfRate = { halfRate.value=it }),
halfRate = halfRate.value, adaptive = automatic.value)
}
}
compose.onNodeWithTag("setting-spatial-diagnostics").performScrollTo().performClick()
compose.onNodeWithTag("setting-spatial-half-rate").assertDoesNotExist()
compose.onNodeWithTag("setting-spatial-adaptive-rate").performScrollTo().performClick()
compose.onNodeWithTag("setting-spatial-half-rate").performScrollTo().performClick()
assertEquals(false,automatic.value)
assertEquals(false,halfRate.value)
}
private fun savePreview(name: String) {
val file = File("build/reports/spatial-setup/$name.png")
file.parentFile?.mkdirs()
compose.runOnIdle {
val root = view.rootView
val bitmap = android.graphics.Bitmap.createBitmap(root.width, root.height, android.graphics.Bitmap.Config.ARGB_8888)
root.draw(android.graphics.Canvas(bitmap))
file.outputStream().use { bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, it) }
bitmap.recycle()
}
}
}
@@ -0,0 +1,118 @@
package com.droiddeck.launcher.ui
import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import androidx.compose.ui.graphics.toArgb
import com.droiddeck.launcher.glasses.TrackingRecovery
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import java.io.File
import kotlin.math.abs
@RunWith(RobolectricTestRunner::class)
@Config(sdk=[34])
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class TrackingRecoveryViewTest {
private fun view()=TrackingRecoveryView(RuntimeEnvironment.getApplication())
private fun render(view: View, width: Int=1920, height: Int=1200): Bitmap {
view.measure(View.MeasureSpec.makeMeasureSpec(width,View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(height,View.MeasureSpec.EXACTLY))
view.layout(0,0,width,height)
return Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888).also { view.draw(Canvas(it)) }
}
private fun save(bitmap: Bitmap, name: String) {
val file=File("build/reports/tracking-recovery/$name.png");file.parentFile!!.mkdirs()
file.outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG,100,it) }
}
@Test fun recoveredTrackingRemovesTheScreenAndItsTouchTarget() {
val view=view()
assertEquals(View.GONE,view.visibility)
view.update(TrackingRecovery.LOST,false)
assertEquals(View.VISIBLE,view.visibility)
assertTrue(view.contentDescription.contains("Look around slowly"))
assertTrue(view.isClickable)
view.update(null,false)
assertEquals(View.GONE,view.visibility)
assertNull(view.contentDescription)
}
@Test fun everyRecoveryStateHasItsOwnActionAndFitsThePhone() {
val view=view()
for((state,instruction) in listOf(TrackingRecovery.LOST to "Look around slowly",TrackingRecovery.RECENTER to "tap Recenter",
TrackingRecovery.STABILIZING to "Keep the glasses steady",TrackingRecovery.DISCONNECTED to "USB-access prompt")) {
view.update(state,false,Themes.byId(Themes.ELECTRIC))
assertTrue(view.contentDescription.contains(instruction))
render(view,1080,600).also { save(it,"phone-${state.name.lowercase()}");it.recycle() }
}
}
@Test fun stereoDuplicatesTheWholeMessageForEachEyeAndMatchesMono() {
val view=view();view.update(TrackingRecovery.LOST,false)
val mono=render(view)
try {
view.update(TrackingRecovery.LOST,true)
val stereo=render(view,3840,1200)
try {
save(mono,"glasses-mono");save(stereo,"glasses-stereo")
val expected=IntArray(mono.width)
val eye=IntArray(mono.width)
val pixels=mono.width*mono.height
for(x in listOf(0,mono.width)) {
var changed=0
var difference=0L
for(y in 0 until mono.height) {
mono.getPixels(expected,0,mono.width,0,y,mono.width,1)
stereo.getPixels(eye,0,mono.width,x,y,mono.width,1)
for(i in eye.indices) {
val actual=eye[i]
val reference=expected[i]
if(actual!=reference) changed++
difference+=abs((actual and 255)-(reference and 255))
difference+=abs(((actual ushr 8) and 255)-((reference ushr 8) and 255))
difference+=abs(((actual ushr 16) and 255)-((reference ushr 16) and 255))
}
}
// Native text rasterization can round a translated glyph edge differently.
assertTrue("Eye at x=$x changed $changed pixels",changed<pixels/1000)
assertTrue("Eye at x=$x has different content",difference.toDouble()/(pixels*3)<0.1)
}
} finally {
stereo.recycle()
}
} finally {
mono.recycle()
}
}
@Test fun activePaletteChangesTheBackgroundAndMessageColors() {
val view=view()
for(theme in listOf(Themes.GRAPHITE,Themes.ELECTRIC,Themes.PAPER)) {
val palette=Themes.byId(theme)
view.update(TrackingRecovery.LOST,false,palette)
val bitmap=render(view)
try {
assertEquals(palette.background.toArgb(),bitmap.getPixel(0,0))
val row=IntArray(bitmap.width)
val textColor=palette.onBackground.toArgb()
var foundTextColor=false
for(y in 0 until bitmap.height) {
bitmap.getPixels(row,0,bitmap.width,0,y,bitmap.width,1)
if(row.any { it==textColor }) {
foundTextColor=true
break
}
}
assertTrue("Missing message color for theme $theme",foundTextColor)
save(bitmap,"theme-$theme")
} finally {
bitmap.recycle()
}
}
}
}