feat: make desktop mode easy to uninstall safely (#1311)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-22 08:17:14 -04:00
1 parent d012a1f5b6
commit 22f7d5b909
6 files changed
+266 -7

No files matched your search

@@ -0,0 +1,122 @@
package com.droiddeck.launcher.runtime
import android.content.ContextWrapper
import com.droiddeck.launcher.BuildConfig
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.zip.GZIPInputStream
import org.junit.After
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
@RunWith(RobolectricTestRunner::class)
class DesktopRemovalTest {
private val dir = Files.createTempDirectory("desktop-removal").toFile()
private val context = object : ContextWrapper(RuntimeEnvironment.getApplication()) {
override fun getFilesDir() = File(dir, "files").apply { mkdirs() }
override fun getCacheDir() = File(dir, "cache").apply { mkdirs() }
}
private val root = LinuxRuntime.rootDir(context).apply { mkdirs() }
private val marker = File(root, ".droiddeck-pkg-${DesktopCatalog.DESKTOP_ID}")
private val journal = File(root, marker.name + ".installing")
private val pending = File(root, DesktopRemoval.PENDING)
private val listener = LinuxRuntimeInstaller.ProgressListener { _, _ -> }
@After fun cleanup() { dir.deleteRecursively() }
@Test fun removesTheDesktopWhileKeepingRuntimeAppsGamesAndSettings() {
val desktop = desktopFiles()
marker.writeText(BuildConfig.DESKTOP_KDE_TAG)
val keep = listOf("root/.local/share/Steam/steamapps/common/game/data", "root/.config/kdeglobals",
"opt/appimages/custom.AppImage", "usr/lib/shared.so", "usr/share/plasma/custom")
.map { file(it, "keep") }
assertTrue(DesktopCatalog.desktopPresent(context))
assertNull(DesktopCatalog.removeDesktop(context, listener))
assertFalse(Files.exists(desktop.toPath(), java.nio.file.LinkOption.NOFOLLOW_LINKS))
keep.forEach { assertEquals("keep", it.readText()) }
assertFalse(DesktopCatalog.desktopPresent(context))
assertFalse(DesktopCatalog.desktopInstalled(context))
}
@Test fun interruptedInstallCanBeRemovedWithoutBeingResumedAtTheNextLaunch() {
desktopFiles()
val entry = DesktopCatalog.desktopEntry(context)
journal.writeText(DesktopCatalog.entryJson(entry).toString())
val download = File(context.cacheDir, "pkg-${entry.id}-${entry.sha256.take(12)}.download").apply { writeText("download") }
val resume = File(download.path + ".resume.properties").apply { writeText("resume") }
val unrelated = file(".droiddeck-pkg-emulators.installing", DesktopCatalog.entryJson(DesktopCatalog.Entry(
"emulators", "Emulators", 1, "1", "tar", "https://example.invalid/emulators", "a".repeat(64), 1L, "", "", "",
)).toString())
assertTrue(DesktopCatalog.desktopPresent(context))
assertNotNull(DesktopCatalog.removeDesktop(context) { _, _ -> error("interrupted") })
assertTrue(pending.exists())
assertTrue(journal.exists())
assertTrue(DesktopCatalog.desktopPresent(context))
assertNull(DesktopCatalog.removeDesktop(context, listener))
assertFalse(journal.exists())
assertFalse(pending.exists())
assertFalse(download.exists())
assertFalse(resume.exists())
assertFalse(DesktopCatalog.desktopPresent(context))
assertEquals(listOf("emulators"), DesktopCatalog.interrupted(context).map { it.id })
assertTrue(unrelated.exists())
}
@Test fun refusesUnknownDesktopVersionsAndMissingRuntimeOwnershipBeforeDeletingAnything() {
val desktop = desktopFiles()
marker.writeText("future-desktop")
assertNotNull(DesktopCatalog.removeDesktop(context, listener))
assertEquals("future-desktop", marker.readText())
marker.writeText(BuildConfig.DESKTOP_KDE_TAG)
journal.writeText(DesktopCatalog.entryJson(DesktopCatalog.desktopEntry(context)).put("version", "future-desktop").toString())
assertNotNull(DesktopCatalog.removeDesktop(context, listener))
assertTrue(journal.exists())
journal.delete()
File(root, "var/lib/pacman/local/base-1/files").delete()
assertNotNull(DesktopCatalog.removeDesktop(context, listener))
assertEquals(BuildConfig.DESKTOP_KDE_TAG, marker.readText())
assertFalse(pending.exists())
assertTrue(Files.isSymbolicLink(desktop.toPath()))
}
@Test fun failedDownloadCleanupKeepsTheInstallRecordSoRemovalCanRetry() {
val entry = DesktopCatalog.desktopEntry(context)
val download = File(context.cacheDir, "pkg-${entry.id}-${entry.sha256.take(12)}.download")
for (blocked in listOf(download, File(download.path + ".resume.properties"))) {
desktopFiles()
journal.writeText(DesktopCatalog.entryJson(entry).toString())
blocked.mkdirs()
val blocker = File(blocked, "blocker").apply { writeText("fixture") }
assertNotNull(DesktopCatalog.removeDesktop(context, listener))
assertTrue(journal.exists())
assertTrue(pending.exists())
assertTrue(blocker.delete())
assertNull(DesktopCatalog.removeDesktop(context, listener))
assertFalse(blocked.exists())
assertFalse(journal.exists())
assertFalse(pending.exists())
}
}
private fun file(path: String, text: String) = File(root, path).apply { parentFile!!.mkdirs(); writeText(text) }
private fun desktopFiles(): File {
file("var/lib/pacman/local/base-1/files", "%FILES%\nusr/lib/shared.so\n")
val entry = GZIPInputStream(context.assets.open("desktop-removal/kde-remove.tsv.gzip")).bufferedReader().use { reader ->
reader.lineSequence().map(DesktopFileRemoval.Entry::parse).first { it.kind == "L" && it.path.startsWith("usr/") }
}
return File(root, entry.path).apply {
parentFile!!.mkdirs()
Files.createSymbolicLink(toPath(), Paths.get(entry.identity))
}
}
}
@@ -0,0 +1,101 @@
package com.droiddeck.launcher.ui
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onLast
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], qualifiers = "w1200dp-h752dp-land", application = android.app.Application::class)
class DesktopUninstallTest {
@get:Rule val compose = createRobolectricComposeRule()
private var removals = 0
private var runtimeBlocked by mutableStateOf(false)
@After fun closeConfirmation() { Steps.drop() }
@Test fun uninstallIsAvailableOnTheDesktopPageAndRequiresConfirmation() {
show()
compose.onNodeWithText("Open desktop").assertIsDisplayed()
compose.onNodeWithText("Uninstall").assertIsDisplayed().performClick()
settle()
assertEquals(0, removals)
compose.onNodeWithText("Uninstall the desktop?").assertIsDisplayed()
compose.onAllNodesWithText("Uninstall").onLast().performClick()
assertEquals(1, removals)
}
@Test fun uninstallIsDisabledWhileRuntimeChangesAreBlocked() {
show(blocked = true)
compose.onNodeWithText("Uninstall").assertIsNotEnabled()
}
@Test fun aPartialDesktopStillOffersUninstall() {
show(installed = false)
compose.onNodeWithText("Uninstall").assertIsDisplayed()
}
@Test fun anUninstalledDesktopNoLongerOffersUninstall() {
show(installed = false, present = false)
compose.onNodeWithText("Uninstall").assertDoesNotExist()
}
@Test fun startingRuntimeWorkWhileConfirmingDisablesUninstall() {
show()
compose.onNodeWithText("Uninstall").performClick()
settle()
compose.runOnIdle { runtimeBlocked = true }
settle()
compose.onAllNodesWithText("Uninstall").onLast().assertIsNotEnabled()
assertEquals(0, removals)
}
private fun settle() = compose.mainClock.advanceTimeBy(2_000)
private fun show(installed: Boolean = true, blocked: Boolean = false, present: Boolean = true) {
runtimeBlocked = blocked
compose.mainClock.autoAdvance = false
compose.setContent {
DroidDeckTheme {
CompositionLocalProvider(LocalFrontFocus provides FrontFocus()) {
FocusGlideHost {
Box(Modifier.fillMaxSize()) {
DesktopPage(FrontEndState(
installed = "r9", ready = true, available = null, busy = false, stage = "", percent = -1,
desktopInstalled = installed, desktopPresent = present, runtimeActionsBlocked = runtimeBlocked,
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 = { _, _ -> }, onRemoveDesktop = { removals++ },
), Modifier.fillMaxSize(), onSelect = {})
StepsLayer(Offset.Zero)
}
}
}
}
}
settle()
}
}