From 2d8957a401f202351d2f1b8a975e034645ad73dc Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:41:46 -0400 Subject: [PATCH] feat(launch): show compatibility install progress (#1629) --- .../droiddeck/launcher/FrontEndActivity.kt | 127 ++++++++- .../java/com/droiddeck/launcher/ProtonMenu.kt | 21 +- app/src/main/res/values/strings.xml | 4 + .../droiddeck/launcher/CompatLaunchTest.kt | 268 ++++++++++++++++++ .../droiddeck/launcher/ProtonMenuAutoTest.kt | 8 + 5 files changed, 416 insertions(+), 12 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/CompatLaunchTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index a80e3c5d61..c6daf0da36 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -91,12 +91,16 @@ class FrontEndActivity : ComponentActivity() { private val components = ComponentsMenu(this, ui) private val decky = DeckyMenu(this, ui) private val updates = UpdatesMenu(this, ui) - private val protons = ProtonMenu(this, ui) { !busy && flood == null && pkgStage == null && components.compBusy == null } + private val protons = ProtonMenu(this, ui) { !busy && flood == null && compatLaunch == null && pkgStage == null && components.compBusy == null } private var installed by mutableStateOf(null) private var flood by mutableStateOf(null) private var floodProgress by androidx.compose.runtime.mutableFloatStateOf(0f) private class PendingFlood(val from: androidx.compose.ui.geometry.Rect, val intent: Intent) + private class CompatLaunch(val intent: Intent, val loading: com.droiddeck.launcher.session.LoadingState, var menuInstall: Boolean) + private var compatLaunch by mutableStateOf(null) + private var compatLaunchPoll: Runnable? = null + private var compatLaunchVisible = false private var returning by mutableStateOf(null) private class ReturningFlood(val color: Int, val to: androidx.compose.ui.geometry.Rect?) private var ready by mutableStateOf(false) @@ -134,9 +138,14 @@ class FrontEndActivity : ComponentActivity() { private var catalog by mutableStateOf?>(null) private var catalogLoading by mutableStateOf(false) private var packageRows by mutableStateOf?>(null) - private var pkgId by mutableStateOf(null) - private var pkgStage by mutableStateOf(null) - private var pkgPercent by mutableIntStateOf(-1) + private object PackageOperation { + val id = mutableStateOf(null) + val stage = mutableStateOf(null) + val percent = mutableIntStateOf(-1) + } + private var pkgId by PackageOperation.id + private var pkgStage by PackageOperation.stage + private var pkgPercent by PackageOperation.percent private var desktopInstalled by mutableStateOf(false) private var desktopPresent by mutableStateOf(false) private var battleNetInstalled by mutableStateOf(false) @@ -440,13 +449,14 @@ class FrontEndActivity : ComponentActivity() { override fun startActivity(intent: Intent?) { if (intent?.component?.className == SessionActivity::class.java.name) { + if (compatLaunch != null) return when { busy || LinuxRuntimeInstaller.isBusy() -> { android.widget.Toast.makeText(this, R.string.main_wait_runtime, android.widget.Toast.LENGTH_SHORT).show() return } protons.protonBusyId != null || ProtonExtras.installInProgress -> { - android.widget.Toast.makeText(this, R.string.main_wait_proton_install, android.widget.Toast.LENGTH_SHORT).show() + waitForCompat(intent) return } pkgStage != null -> { @@ -474,6 +484,8 @@ class FrontEndActivity : ComponentActivity() { flood = null floodProgress = 0f com.droiddeck.launcher.ui.LaunchOrigin.flooding = null + if (!busy && !LinuxRuntimeInstaller.isBusy() && pkgStage == null && + (protons.protonBusyId != null || ProtonExtras.installInProgress)) waitForCompat(f.intent) return } val signal = com.droiddeck.launcher.ui.Themes.byId(theme).signal @@ -481,6 +493,71 @@ class FrontEndActivity : ComponentActivity() { overridePendingTransition(0, 0) } + private fun waitForCompat(next: Intent, menuInstall: Boolean = protons.protonBusyId != null) { + if (compatLaunch != null) return + com.droiddeck.launcher.ui.LaunchOrigin.take() + com.droiddeck.launcher.ui.LaunchOrigin.flooding = null + val waiting = CompatLaunch(Intent(next), com.droiddeck.launcher.session.LoadingState(this), menuInstall) + compatLaunch = waiting + val poll = object : Runnable { + override fun run() { + if (isDestroyed || isFinishing || compatLaunch !== waiting) return + if (protons.protonBusyId != null || ProtonExtras.installInProgress) { + waiting.menuInstall = waiting.menuInstall || protons.protonBusyId != null + val percent = if (protons.protonBusyId != null) protons.protonPercent else -1 + val stage = protons.protonStage ?: getString(R.string.load_compat_wait) + waiting.loading.say(if (percent >= 0) getString(R.string.load_compat_progress, stage, percent) else stage, + com.droiddeck.launcher.session.LoadingState.Topic.RUNTIME, readable = true) + waiting.loading.percent = percent + waiting.loading.tick() + ui.postDelayed(this, 250) + return + } + if (!compatLaunchVisible) { ui.postDelayed(this, 250); return } + val problem = protons.protonProblem.takeIf { waiting.menuInstall } + if (problem == null && (busy || LinuxRuntimeInstaller.isBusy() || pkgStage != null)) { + waiting.loading.say(pkgStage ?: stage.takeIf { it.isNotBlank() } ?: getString(R.string.main_wait_runtime), + com.droiddeck.launcher.session.LoadingState.Topic.RUNTIME, readable = true) + waiting.loading.percent = if (pkgStage != null) pkgPercent else percent + waiting.loading.tick() + ui.postDelayed(this, 250) + return + } + compatLaunchPoll = null + if (problem != null) waiting.loading.showEnded(problem) + else { + compatLaunch = null + startActivity(waiting.intent) + } + } + } + compatLaunchPoll = poll + poll.run() + } + + private fun cancelCompatLaunch() { + compatLaunchPoll?.let(ui::removeCallbacks) + compatLaunchPoll = null + compatLaunch = null + } + + private fun restoreCompatLaunch(state: Bundle) { + savedIntent(state, KEY_COMPAT_LAUNCH)?.let { next -> + val problem = state.getString(KEY_COMPAT_PROBLEM) + if (problem == null) waitForCompat(next, state.getBoolean(KEY_COMPAT_MENU_INSTALL)) + else compatLaunch = CompatLaunch(next, + com.droiddeck.launcher.session.LoadingState(this).apply { showEnded(problem) }, false) + } + } + + private fun saveCompatLaunch(state: Bundle) { + compatLaunch?.let { + state.putParcelable(KEY_COMPAT_LAUNCH, it.intent) + state.putBoolean(KEY_COMPAT_MENU_INSTALL, it.menuInstall) + if (it.loading.ended) state.putString(KEY_COMPAT_PROBLEM, it.loading.step) + } + } + private var shortcutPicker by mutableStateOf(false) private var shortcutLibraryScanning by mutableStateOf(false) @Volatile private var libraryScanGeneration = 0 @@ -542,7 +619,7 @@ class FrontEndActivity : ComponentActivity() { } private fun launchGame(game: Library.SteamGame, returnHome: Boolean = false): Boolean { - if (protons.protonBusyId != null || ProtonExtras.installInProgress || pkgStage != null) { + if (pkgStage != null) { android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show() return false } @@ -654,7 +731,7 @@ class FrontEndActivity : ComponentActivity() { android.widget.Toast.makeText(this, R.string.epic_client_uninstalling, android.widget.Toast.LENGTH_SHORT).show() return false } - if (protons.protonBusyId != null || ProtonExtras.installInProgress || pkgStage != null) { + if (pkgStage != null) { android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show() return false } @@ -1216,12 +1293,21 @@ class FrontEndActivity : ComponentActivity() { com.droiddeck.launcher.ui.LaunchOrigin.flooding = null } } + androidx.activity.compose.BackHandler(compatLaunch != null) { cancelCompatLaunch() } + compatLaunch?.loading?.let { loading -> + com.droiddeck.launcher.ui.LoadingOverlay( + loading.step, loading.percent, loading.elapsed, getString(R.string.load_compat_hint), loading.ended, + loading.topic, loading.readable, title = getString(R.string.load_compat_title), + onCancel = ::cancelCompatLaunch, onClose = ::cancelCompatLaunch, + ) + } } } } if (savedInstanceState == null) ui.post { startSteamAtStartupIfEnabled() } else { + restoreCompatLaunch(savedInstanceState) pendingSwitch = savedIntent(savedInstanceState, KEY_PENDING_SWITCH) savedIntent(savedInstanceState, KEY_SWITCHING_TO)?.let { awaitSessionEnd(it, savedInstanceState.getLong(KEY_SWITCH_DEADLINE)) } } @@ -1308,6 +1394,14 @@ class FrontEndActivity : ComponentActivity() { } override fun dispatchKeyEvent(event: KeyEvent): Boolean { + if (compatLaunch != null) { + if (event.keyCode == KeyEvent.KEYCODE_BACK || event.keyCode == KeyEvent.KEYCODE_BUTTON_B) { + if (event.action == KeyEvent.ACTION_UP) cancelCompatLaunch() + return true + } + if (!compatLaunch!!.loading.ended && event.keyCode !in listOf( + KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE)) return true + } if (event.keyCode == KeyEvent.KEYCODE_BUTTON_A) com.droiddeck.launcher.ui.HeldKeys.confirm = event.action == KeyEvent.ACTION_DOWN if (showComponents && (event.keyCode == KeyEvent.KEYCODE_BUTTON_L1 || event.keyCode == KeyEvent.KEYCODE_BUTTON_R1)) { if (event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) { @@ -1324,8 +1418,20 @@ class FrontEndActivity : ComponentActivity() { } } + override fun dispatchGenericMotionEvent(event: android.view.MotionEvent): Boolean { + if (compatLaunch != null && !compatLaunch!!.loading.ended && + com.droiddeck.launcher.input.PadBridge.isControllerMotionEvent(event)) return true + return super.dispatchGenericMotionEvent(event) + } + + override fun onPause() { + compatLaunchVisible = false + super.onPause() + } + override fun onResume() { super.onResume() + compatLaunchVisible = true orientation = SessionPrefs.orientation(this) launcherOrientation = com.droiddeck.launcher.core.AppUiPrefs.orientation(this) requestedOrientation = launcherOrientation @@ -1378,6 +1484,7 @@ class FrontEndActivity : ComponentActivity() { override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) + saveCompatLaunch(outState) pendingSwitch?.let { outState.putParcelable(KEY_PENDING_SWITCH, it) } switchingTo?.let { outState.putParcelable(KEY_SWITCHING_TO, it) @@ -1390,6 +1497,7 @@ class FrontEndActivity : ComponentActivity() { else @Suppress("DEPRECATION") state.getParcelable(key) override fun onDestroy() { + cancelCompatLaunch() updates.unregister() super.onDestroy() } @@ -1594,7 +1702,7 @@ class FrontEndActivity : ComponentActivity() { } private fun openBattleNet(args: List = emptyList()) { - if (protons.protonBusyId != null || ProtonExtras.installInProgress || pkgStage != null) { + if (pkgStage != null) { android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show() return } @@ -2316,6 +2424,9 @@ class FrontEndActivity : ComponentActivity() { private const val SESSION_SWITCH_WAIT_MS = 30_000L private const val SESSION_SWITCH_POLL_MS = 250L private const val KEY_PENDING_SWITCH = "pending_switch" + private const val KEY_COMPAT_LAUNCH = "compat_launch" + private const val KEY_COMPAT_MENU_INSTALL = "compat_menu_install" + private const val KEY_COMPAT_PROBLEM = "compat_problem" private const val KEY_SWITCHING_TO = "switching_to" private const val KEY_SWITCH_DEADLINE = "switch_deadline" private val SESSION_ENDED = setOf(SessionPhase.IDLE, SessionPhase.FAILED) diff --git a/app/src/main/java/com/droiddeck/launcher/ProtonMenu.kt b/app/src/main/java/com/droiddeck/launcher/ProtonMenu.kt index 1d870a6bcb..8e8efc80c2 100644 --- a/app/src/main/java/com/droiddeck/launcher/ProtonMenu.kt +++ b/app/src/main/java/com/droiddeck/launcher/ProtonMenu.kt @@ -20,10 +20,19 @@ internal class ProtonMenu( private val ui: Handler, private val idle: () -> Boolean = { true }, ) { - var protonRows by mutableStateOf>(emptyList()) - var protonBusyId by mutableStateOf(null) - var protonStage by mutableStateOf(null) - var protonPercent by mutableIntStateOf(-1) + // Install workers and their results outlive the activity that starts them. + private object Operation { + val rows = mutableStateOf>(emptyList()) + val busyId = mutableStateOf(null) + val stage = mutableStateOf(null) + val percent = mutableIntStateOf(-1) + val problem = mutableStateOf(null) + } + var protonRows by Operation.rows + var protonBusyId by Operation.busyId + var protonStage by Operation.stage + var protonPercent by Operation.percent + var protonProblem by Operation.problem private val checking = java.util.concurrent.atomic.AtomicBoolean(false) @VisibleForTesting internal var installer: (ProtonExtras.Tool, (String, Int) -> Unit) -> String? = { tool, progress -> ProtonExtras.install(activity, tool, progress) } @@ -33,6 +42,7 @@ internal class ProtonMenu( if (protonBusyId != null || ProtonExtras.installInProgress || SessionState.running) return ProtonExtras.unqueue(activity, tool) protonBusyId = id + protonProblem = null protonStage = activity.getString(R.string.store_starting) protonPercent = -1 Thread({ @@ -43,6 +53,7 @@ internal class ProtonMenu( }.getOrElse { it.message ?: it.javaClass.simpleName } if (problem == null && !automatic && tool.id == ProtonExtras.OWN) ProtonExtras.setDeclined(activity, false) ui.post { + protonProblem = problem protonBusyId = null protonStage = null protonPercent = -1 @@ -60,6 +71,7 @@ internal class ProtonMenu( val tool = ProtonExtras.tools.firstOrNull { it.id == id } ?: return if (protonBusyId != null || SessionState.running) return protonBusyId = id + protonProblem = null protonStage = activity.getString(R.string.main_removing_named, dir ?: tool.name) protonPercent = -1 Thread({ @@ -98,6 +110,7 @@ internal class ProtonMenu( !LinuxRuntime.isInstalled(activity) || !AutoComponents.online(activity) || AutoComponents.unfavourable(activity)) return if (ProtonExtras.installedAll(activity, ProtonExtras.tools.first { it.id == ProtonExtras.OWN }).size > 1) { protonBusyId = ProtonExtras.OWN + protonProblem = null Thread({ ProtonExtras.tidy(activity) val tidied = ProtonExtras.installedAll(activity, ProtonExtras.tools.first { it.id == ProtonExtras.OWN }).size <= 1 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1b75df827e..89991465b5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1287,6 +1287,10 @@ Wait for the runtime operation to finish Wait for the compatibility tool install to finish + Preparing compatibility tools + Waiting for compatibility setup to finish + Your session will start when setup finishes. + %1$s ยท %2$d%% Wait for the desktop app install to finish Wait for the install to finish before launching a game Close the Steam session first, so the game can\'t save over the import diff --git a/app/src/test/java/com/droiddeck/launcher/CompatLaunchTest.kt b/app/src/test/java/com/droiddeck/launcher/CompatLaunchTest.kt new file mode 100644 index 0000000000..e19b1d6347 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/CompatLaunchTest.kt @@ -0,0 +1,268 @@ +package com.droiddeck.launcher + +import android.content.Intent +import android.os.Bundle +import android.os.Looper +import com.droiddeck.launcher.session.LoadingState +import com.droiddeck.launcher.session.ProtonExtras +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.ui.LaunchOrigin +import java.time.Duration +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +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 + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE) +class CompatLaunchTest { + private lateinit var activity: FrontEndActivity + private lateinit var protons: ProtonMenu + + @Before fun setUp() { + installing(false) + LaunchOrigin.take() + activity = Robolectric.buildActivity(FrontEndActivity::class.java).get() + protons = ReflectionHelpers.getField(activity, "protons") + ReflectionHelpers.setField(activity, "compatLaunchVisible", true) + protons.protonBusyId = "droiddeck" + protons.protonProblem = null + protons.protonStage = "Downloading DroidDeck Proton" + protons.protonPercent = 37 + setString("setPkgStage", null) + } + + @After fun cleanUp() { + cancel() + protons.protonBusyId = null + protons.protonProblem = null + protons.protonStage = null + protons.protonPercent = -1 + setString("setPkgStage", null) + installing(false) + LaunchOrigin.take() + } + + @Test fun waitingShowsRealProgressThenStartsTheOriginalRequestOnce() { + activity.startActivity(request("steam://rungameid/42")) + assertNull(shadowOf(activity).nextStartedActivity) + assertEquals(37, loading().percent) + assertTrue(loading().step.contains("37%")) + protons.protonStage = "Verifying download" + protons.protonPercent = -1 + advance() + assertEquals("Verifying download", loading().step) + assertEquals(-1, loading().percent) + protons.protonBusyId = null + advance() + assertEquals("steam://rungameid/42", shadowOf(activity).nextStartedActivity.getStringExtra(SessionService.EXTRA_STEAM_URL)) + assertNull(waiting()) + advance() + assertNull(shadowOf(activity).nextStartedActivity) + } + + @Test fun cancellingTheLaunchLeavesTheInstallRunningAndPreventsALateStart() { + activity.startActivity(request("first")) + cancel() + assertEquals("droiddeck", protons.protonBusyId) + protons.protonBusyId = null + advance() + assertNull(waiting()) + assertNull(shadowOf(activity).nextStartedActivity) + } + + @Test fun installationFailureStaysOnTheLoadingErrorWithoutStarting() { + activity.startActivity(request("first")) + protons.protonProblem = "Checksum verification failed" + protons.protonBusyId = null + advance() + assertTrue(loading().ended) + assertEquals("Checksum verification failed", loading().step) + assertNull(shadowOf(activity).nextStartedActivity) + } + + @Test fun repeatedLaunchesDoNotReplaceThePendingGame() { + activity.startActivity(request("first")) + activity.startActivity(request("second")) + protons.protonBusyId = null + advance() + assertEquals("first", shadowOf(activity).nextStartedActivity.getStringExtra(SessionService.EXTRA_STEAM_URL)) + assertNull(shadowOf(activity).nextStartedActivity) + } + + @Test fun anInstallOutsideTheLauncherUsesIndeterminateProgress() { + protons.protonBusyId = null + installing(true) + activity.startActivity(request("first")) + assertEquals(-1, loading().percent) + assertNull(shadowOf(activity).nextStartedActivity) + installing(false) + advance() + assertNotNull(shadowOf(activity).nextStartedActivity) + } + + @Test fun finishingInTheBackgroundWaitsUntilTheLauncherIsVisible() { + activity.startActivity(request("first")) + ReflectionHelpers.setField(activity, "compatLaunchVisible", false) + protons.protonBusyId = null + advance() + assertNull(shadowOf(activity).nextStartedActivity) + ReflectionHelpers.setField(activity, "compatLaunchVisible", true) + advance() + assertNotNull(shadowOf(activity).nextStartedActivity) + } + + @Test fun aRestoredCompletedRequestWaitsForResume() { + activity.startActivity(request("first")) + val saved = save() + cancel() + protons.protonBusyId = null + recreate(saved) + advance() + assertNull(shadowOf(activity).nextStartedActivity) + ReflectionHelpers.setField(activity, "compatLaunchVisible", true) + advance() + assertEquals("first", shadowOf(activity).nextStartedActivity.getStringExtra(SessionService.EXTRA_STEAM_URL)) + } + + @Test fun overlappingPackageWorkKeepsTheOriginalLaunchUntilItFinishes() { + activity.startActivity(request("first")) + setString("setPkgStage", "Installing desktop") + ReflectionHelpers.callInstanceMethod(activity, "setPkgPercent", ReflectionHelpers.ClassParameter.from(Int::class.javaPrimitiveType, 64)) + protons.protonBusyId = null + advance() + assertNotNull(waiting()) + assertNull(shadowOf(activity).nextStartedActivity) + assertEquals("Installing desktop", loading().step) + assertEquals(64, loading().percent) + setString("setPkgStage", null) + advance() + assertEquals("first", shadowOf(activity).nextStartedActivity.getStringExtra(SessionService.EXTRA_STEAM_URL)) + advance() + assertNull(shadowOf(activity).nextStartedActivity) + } + + @Test fun overlappingRuntimeWorkDoesNotDropThePendingLaunch() { + activity.startActivity(request("first")) + setString("setStage", "Updating runtime") + ReflectionHelpers.callInstanceMethod(activity, "setBusy", ReflectionHelpers.ClassParameter.from(Boolean::class.javaPrimitiveType, true)) + protons.protonBusyId = null + advance() + assertNotNull(waiting()) + assertNull(shadowOf(activity).nextStartedActivity) + assertEquals("Updating runtime", loading().step) + ReflectionHelpers.callInstanceMethod(activity, "setBusy", ReflectionHelpers.ClassParameter.from(Boolean::class.javaPrimitiveType, false)) + advance() + assertEquals("first", shadowOf(activity).nextStartedActivity.getStringExtra(SessionService.EXTRA_STEAM_URL)) + } + + @Test fun aPackageDownloadStillBlocksTheLaunchAfterRecreationAndReportsOldWorkerProgress() { + activity.startActivity(request("first")) + setString("setPkgStage", "Downloading AppImage") + ReflectionHelpers.callInstanceMethod(activity, "setPkgPercent", ReflectionHelpers.ClassParameter.from(Int::class.javaPrimitiveType, 23)) + val workerActivity = activity + val saved = save() + cancel() + recreate(saved) + ReflectionHelpers.setField(activity, "compatLaunchVisible", true) + protons.protonBusyId = null + advance() + assertNotNull(waiting()) + assertNull(shadowOf(activity).nextStartedActivity) + assertEquals("Downloading AppImage", loading().step) + assertEquals(23, loading().percent) + android.os.Handler(Looper.getMainLooper()).post { + setString("setPkgStage", "Placing AppImage", workerActivity) + ReflectionHelpers.callInstanceMethod(workerActivity, "setPkgPercent", ReflectionHelpers.ClassParameter.from(Int::class.javaPrimitiveType, 90)) + } + advance() + assertEquals("Placing AppImage", loading().step) + assertEquals(90, loading().percent) + assertNull(shadowOf(activity).nextStartedActivity) + android.os.Handler(Looper.getMainLooper()).post { setString("setPkgStage", null, workerActivity) } + advance() + assertEquals("first", shadowOf(activity).nextStartedActivity.getStringExtra(SessionService.EXTRA_STEAM_URL)) + advance() + assertNull(shadowOf(activity).nextStartedActivity) + } + + @Test fun aFailedInstallAfterRecreationKeepsItsProgressAndError() = recreationFailure(false) + + @Test fun aFailedInstallBeforeRestorationStillShowsItsError() = recreationFailure(true) + + private fun recreationFailure(finishBeforeRestore: Boolean) { + val entered = CountDownLatch(1) + val finish = CountDownLatch(1) + protons.protonBusyId = null + protons.installer = { _, progress -> + progress("Verifying download", 64) + entered.countDown() + check(finish.await(5, TimeUnit.SECONDS)) + "Checksum verification failed" + } + try { + protons.installProton("droiddeck") + assertTrue(entered.await(5, TimeUnit.SECONDS)) + activity.startActivity(request("first")) + val saved = save() + cancel() + if (finishBeforeRestore) { + finish.countDown() + waitForInstall() + } + recreate(saved) + if (!finishBeforeRestore) { + advance() + assertEquals(64, loading().percent) + finish.countDown() + waitForInstall() + } + ReflectionHelpers.setField(activity, "compatLaunchVisible", true) + advance() + assertEquals("Checksum verification failed", loading().step) + assertTrue(loading().ended) + assertNull(shadowOf(activity).nextStartedActivity) + } finally { + finish.countDown() + waitForInstall() + } + } + + private fun waitForInstall() { + val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5) + while (protons.protonBusyId != null && System.nanoTime() < deadline) { + shadowOf(Looper.getMainLooper()).idle() + Thread.sleep(10) + } + assertNull(protons.protonBusyId) + } + + private fun save() = Bundle().also { + ReflectionHelpers.callInstanceMethod(activity, "saveCompatLaunch", ReflectionHelpers.ClassParameter.from(Bundle::class.java, it)) + } + + private fun recreate(saved: Bundle) { + activity = Robolectric.buildActivity(FrontEndActivity::class.java).get() + protons = ReflectionHelpers.getField(activity, "protons") + ReflectionHelpers.callInstanceMethod(activity, "restoreCompatLaunch", ReflectionHelpers.ClassParameter.from(Bundle::class.java, saved)) + } + + private fun setString(method: String, value: String?, target: FrontEndActivity = activity) { + ReflectionHelpers.callInstanceMethod(target, method, ReflectionHelpers.ClassParameter.from(String::class.java, value)) + } + + private fun request(url: String) = Intent(activity, SessionActivity::class.java).putExtra(SessionService.EXTRA_STEAM_URL, url) + private fun installing(on: Boolean) = ReflectionHelpers.setStaticField(ProtonExtras::class.java, "installInProgress", on) + private fun waiting(): Any? = ReflectionHelpers.callInstanceMethod(activity, "getCompatLaunch") + private fun loading(): LoadingState = ReflectionHelpers.getField(requireNotNull(waiting()), "loading") + private fun cancel() { ReflectionHelpers.callInstanceMethod(activity, "cancelCompatLaunch") } + private fun advance() = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(250)) +} diff --git a/app/src/test/java/com/droiddeck/launcher/ProtonMenuAutoTest.kt b/app/src/test/java/com/droiddeck/launcher/ProtonMenuAutoTest.kt index 5f3dddf96c..9f79a6efad 100644 --- a/app/src/test/java/com/droiddeck/launcher/ProtonMenuAutoTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ProtonMenuAutoTest.kt @@ -56,6 +56,10 @@ class ProtonMenuAutoTest { .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") @@ -64,6 +68,10 @@ class ProtonMenuAutoTest { } @After fun reset() { + menu.protonBusyId = null + menu.protonProblem = null + menu.protonStage = null + menu.protonPercent = -1 AutoComponents.online = online AutoComponents.unfavourable = constrained SessionState.running = false