Files
DroidDeck/app/src/test/java/com/droiddeck/launcher/ProtonMenuAutoTest.kt
T
MaxsTechReview 634cb390c9 fix(proton-run): register the wrapper for an installed Valve Proton depot (#2392)
* fix(proton-run): find Proton tools in Steam libraries

* fix(proton-run): register the wrapper for an installed Valve Proton depot

Valve's ARM64 Proton depots are not compatibility tools, and running
them directly bypasses the DroidDeck wrapper. Register the wrapper from
the depot when a store app is launched before Steam has started, instead
of scanning steamapps/common.

* test(proton): install complete tool fixtures in the auto-update tests

* fix(proton-run): skip incomplete Proton depots and keep listing tools when the wrapper cannot be written

* fix(proton-run): pick a runnable depot and rebuild a wrapper whose launcher is missing

* test(proton-run): remove redundant depot fixture write

* fix(proton-run): rebuild a wrapper whose manifest is missing

* test(proton-run): cover runnable depot selection
2026-11-10 07:09:56 -05:00

293 lines
13 KiB
Kotlin

package com.droiddeck.launcher
import android.app.Activity
import android.content.Intent
import android.os.Handler
import android.os.Looper
import androidx.compose.ui.geometry.Rect
import com.droiddeck.launcher.frontend.AutoComponents
import com.droiddeck.launcher.runtime.LinuxRuntime
import com.droiddeck.launcher.session.ProtonExtras
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.session.SessionPhase
import com.droiddeck.launcher.session.LoadingState
import com.droiddeck.launcher.ui.LaunchOrigin
import com.droiddeck.launcher.ui.Motion
import org.json.JSONObject
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.util.ReflectionHelpers
import java.io.File
import java.util.concurrent.TimeUnit
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class ProtonMenuAutoTest {
private val online = AutoComponents.online
private val constrained = AutoComponents.unfavourable
private lateinit var activity: Activity
private lateinit var menu: ProtonMenu
private lateinit var root: File
private val attempts = mutableListOf<String>()
private var idle = true
@Before fun setup() {
activity = Robolectric.buildActivity(Activity::class.java).setup().get()
root = LinuxRuntime.rootDir(activity)
for (path in listOf("usr/bin/gamescope", "usr/local/bin/droiddeck-session", "opt/android-host/proot", "opt/android-host/loader")) {
File(root, path).apply { parentFile!!.mkdirs(); writeText("") }
}
AutoComponents.online = { true }
AutoComponents.unfavourable = { false }
SessionState.running = false
SessionState.phase = SessionPhase.IDLE
SessionState.stopRequested = false
LaunchOrigin.take()
LaunchOrigin.flooding = null
val latest = JSONObject()
for (tool in ProtonExtras.tools) latest.put(tool.id, JSONObject()
.put("tag", "${tool.prefix}-11-12").put("at", System.currentTimeMillis()))
File(activity.filesDir, "proton-latest.json").writeText(latest.toString())
menu = ProtonMenu(activity, Handler(Looper.getMainLooper())) { idle }
menu.protonBusyId = null
menu.protonProblem = null
menu.protonStage = null
menu.protonPercent = -1
menu.installer = { tool, progress ->
progress("Installing ${tool.name}", 50)
install("${tool.prefix}-11-12")
null
}
}
@After fun reset() {
menu.protonBusyId = null
menu.protonProblem = null
menu.protonStage = null
menu.protonPercent = -1
AutoComponents.online = online
AutoComponents.unfavourable = constrained
SessionState.running = false
SessionState.phase = SessionPhase.IDLE
SessionState.stopRequested = false
LaunchOrigin.take()
LaunchOrigin.flooding = null
}
private fun install(name: String): File = File(root, "root/.local/share/Steam/compatibilitytools.d/$name").apply {
mkdirs()
File(this, "toolmanifest.vdf").writeText("\"manifest\" { \"commandline\" \"/proton %verb%\" }")
File(this, "compatibilitytool.vdf").writeText("\"from_oslist\" \"windows\"")
File(this, "proton").apply { writeText(""); setExecutable(true) }
}
private fun waitUntil(done: () -> Boolean) {
val deadline = System.nanoTime() + 5_000_000_000L
while (System.nanoTime() < deadline) {
shadowOf(Looper.getMainLooper()).idle()
if (done()) return
Thread.sleep(10)
}
fail("Proton work did not finish")
}
@Test fun aCachedCheckUpdatesOnlyInstalledFamiliesAndReplacesOlderDroidDeckProton() {
val older = install("DroidDeck-Proton-11-11.1")
val request = File(root, "root/.local/share/droiddeck-compat/request.json").apply {
parentFile!!.mkdirs()
writeText("{\"seq\":1,\"dir\":\"DroidDeck-Proton-11-11.1\",\"valve\":false}")
}
val before = request.readText()
assertFalse(ProtonExtras.checkLatest(activity))
menu.checkUpdates()
waitUntil { menu.protonRows.singleOrNull { it.id == "droiddeck" }?.installed == "DroidDeck-Proton-11-12" && menu.protonBusyId == null && !older.exists() }
assertEquals(listOf("DroidDeck-Proton-11-12"), ProtonExtras.installedAll(activity, ProtonExtras.tools.first { it.id == "droiddeck" }))
assertEquals(before, request.readText())
assertTrue(ProtonExtras.installedAll(activity, ProtonExtras.tools.first { it.id == "ge" }).isEmpty())
assertTrue(ProtonExtras.installedAll(activity, ProtonExtras.tools.first { it.id == "cachyos" }).isEmpty())
}
@Test fun downloadsWaitForTheSessionNetworkPowerAndOtherInstallWork() {
install("DroidDeck-Proton-11-11.1")
menu.installer = { tool, _ -> attempts.add(tool.id); "failed" }
SessionState.running = true
menu.ensureAutoUpdates()
SessionState.running = false
AutoComponents.online = { false }
menu.ensureAutoUpdates()
AutoComponents.online = { true }
AutoComponents.unfavourable = { true }
menu.ensureAutoUpdates()
AutoComponents.unfavourable = { false }
idle = false
menu.ensureAutoUpdates()
assertNull(menu.protonBusyId)
assertTrue(attempts.isEmpty())
idle = true
menu.ensureAutoUpdates()
waitUntil { menu.protonBusyId == null }
assertEquals(listOf("droiddeck"), attempts)
}
@Test fun aFinishedInstallStartsTheComponentCheckAndAFailedOneDoesNot() {
var checks = 0
val watched = ProtonMenu(activity, Handler(Looper.getMainLooper()), onInstalled = { checks++ }) { idle }
watched.installer = { tool, _ -> install("${tool.prefix}-11-12"); null }
watched.installProton("droiddeck")
waitUntil { watched.protonBusyId == null && checks == 1 }
assertEquals(1, checks)
watched.installer = { _, _ -> "failed" }
watched.installProton("ge")
waitUntil { watched.protonBusyId == null }
assertEquals(1, checks)
}
@Test fun aFailedUpdateDoesNotLoopOrBlockOtherInstalledFamilies() {
val ge = install("GE-Proton11-11")
install("DroidDeck-Proton-11-11.1")
menu.installer = { tool, _ ->
attempts.add(tool.id)
if (tool.id == "ge") throw IllegalStateException("download failed")
install("${tool.prefix}-11-12")
null
}
menu.ensureAutoUpdates()
waitUntil { menu.protonBusyId == null }
menu.ensureAutoUpdates()
waitUntil { menu.protonBusyId == null }
assertEquals(listOf("ge", "droiddeck"), attempts)
assertTrue(ge.isDirectory)
assertEquals(listOf("DroidDeck-Proton-11-12"), ProtonExtras.installedAll(activity, ProtonExtras.tools.first { it.id == "droiddeck" }))
assertNull(menu.protonStage)
assertEquals(-1, menu.protonPercent)
}
@Test fun anUninstalledRuntimeOrCurrentProtonDoesNotStartADownload() {
install("DroidDeck-Proton-11-12")
menu.installer = { tool, _ -> attempts.add(tool.id); null }
menu.ensureAutoUpdates()
assertTrue(attempts.isEmpty())
install("GE-Proton11-11")
File(root, "usr/bin/gamescope").delete()
menu.ensureAutoUpdates()
assertTrue(attempts.isEmpty())
assertNull(menu.protonBusyId)
}
@Test fun aFailedReleaseIsNotRetriedAfterRecreatingTheLauncher() {
install("DroidDeck-Proton-11-11.1")
menu.installer = { tool, _ -> attempts.add(tool.id); "failed" }
menu.ensureAutoUpdates()
waitUntil { menu.protonBusyId == null }
val recreated = Robolectric.buildActivity(Activity::class.java).setup().get()
val next = ProtonMenu(recreated, Handler(Looper.getMainLooper()))
next.installer = { tool, _ -> attempts.add(tool.id); "failed" }
next.ensureAutoUpdates()
assertNull(next.protonBusyId)
assertEquals(listOf("droiddeck"), attempts)
}
@Test fun automaticUpdatesWaitForEveryNonEndedSessionPhaseEvenWhenRunningIsFalse() {
install("DroidDeck-Proton-11-11.1")
for (phase in SessionPhase.entries.filter { it != SessionPhase.IDLE && it != SessionPhase.FAILED }) {
SessionState.phase = phase
menu.ensureAutoUpdates()
assertNull("Must wait during $phase", menu.protonBusyId)
}
SessionState.phase = SessionPhase.FAILED
menu.ensureAutoUpdates()
waitUntil { menu.protonBusyId == null }
assertEquals("DroidDeck-Proton-11-12", ProtonExtras.installed(activity, ProtonExtras.tools.first { it.id == "droiddeck" }))
}
@Test fun droidDeckProtonIsInstalledWhenMissingUnlessTheUserRemovedIt() {
menu.installer = { tool, _ -> attempts.add(tool.id); install("${tool.prefix}-11-12"); null }
ProtonExtras.setDeclined(activity, true)
menu.ensureAutoUpdates()
assertNull(menu.protonBusyId)
assertTrue(attempts.isEmpty())
ProtonExtras.setDeclined(activity, false)
menu.ensureAutoUpdates()
waitUntil { menu.protonBusyId == null }
assertEquals(listOf("droiddeck"), attempts)
assertEquals("DroidDeck-Proton-11-12", ProtonExtras.installed(activity, ProtonExtras.tools.first { it.id == "droiddeck" }))
assertTrue(ProtonExtras.installedAll(activity, ProtonExtras.tools.first { it.id == "ge" }).isEmpty())
menu.removeProton("droiddeck")
waitUntil { menu.protonBusyId == null }
assertTrue(ProtonExtras.declined(activity))
menu.installer = { _, _ -> "failed" }
menu.installProton("droiddeck")
waitUntil { menu.protonBusyId == null }
assertTrue(ProtonExtras.declined(activity))
menu.installer = { tool, _ -> install("${tool.prefix}-11-12"); null }
menu.installProton("droiddeck")
waitUntil { menu.protonBusyId == null }
assertFalse(ProtonExtras.declined(activity))
}
@Test fun aPendingAnimatedLaunchPreventsAnAutomaticInstall() {
install("DroidDeck-Proton-11-11.1")
val launcher = Robolectric.buildActivity(FrontEndActivity::class.java).get()
val protons = ReflectionHelpers.getField<ProtonMenu>(launcher, "protons")
protons.installer = { tool, _ -> attempts.add(tool.id); "failed" }
Motion.refresh(launcher)
LaunchOrigin.mark(Rect(0f, 0f, 100f, 100f), "game")
launcher.startActivity(Intent(launcher, SessionActivity::class.java))
assertNotNull(ReflectionHelpers.callInstanceMethod<Any?>(launcher, "getFlood"))
assertNull(shadowOf(launcher).nextStartedActivity)
protons.ensureAutoUpdates()
assertNull(protons.protonBusyId)
assertTrue(attempts.isEmpty())
}
@Test fun anAnimatedLaunchRechecksTheProtonInstallGuardBeforeStarting() {
val launcher = Robolectric.buildActivity(FrontEndActivity::class.java).get()
Motion.refresh(launcher)
LaunchOrigin.mark(Rect(0f, 0f, 100f, 100f), "game")
launcher.startActivity(Intent(launcher, SessionActivity::class.java))
val pending = requireNotNull(ReflectionHelpers.callInstanceMethod<Any?>(launcher, "getFlood"))
ReflectionHelpers.getField<ProtonMenu>(launcher, "protons").protonBusyId = "droiddeck"
FrontEndActivity::class.java.getDeclaredMethod("launchFlooded", pending.javaClass)
.apply { isAccessible = true }.invoke(launcher, pending)
assertNull(shadowOf(launcher).nextStartedActivity)
assertNull(ReflectionHelpers.callInstanceMethod<Any?>(launcher, "getFlood"))
assertNull(LaunchOrigin.flooding)
}
@Test fun aCancellationDuringTeardownCannotPublishIdleEarly() {
val session = watchCancellation(SessionPhase.STOPPING)
assertEquals(SessionPhase.STOPPING, SessionState.phase)
assertTrue(SessionState.stopRequested)
assertFalse(session.isFinishing)
}
@Test fun cancellationBeforeSessionStartupStillFinishesTheActivity() {
val session = watchCancellation(SessionPhase.PREPARING)
assertEquals(SessionPhase.IDLE, SessionState.phase)
assertFalse(SessionState.stopRequested)
assertTrue(session.isFinishing)
}
private fun watchCancellation(phase: SessionPhase): SessionActivity {
val session = Robolectric.buildActivity(SessionActivity::class.java).get()
ReflectionHelpers.setField(session, "loading", LoadingState(session).apply { visible = false })
SessionState.phase = phase
SessionState.stopRequested = true
try {
ReflectionHelpers.callInstanceMethod<Void>(session, "watchSession")
shadowOf(Looper.getMainLooper()).idle()
} finally {
ReflectionHelpers.setField(session, "watching", false)
shadowOf(Looper.getMainLooper()).idleFor(500, TimeUnit.MILLISECONDS)
}
return session
}
}