fix(session): serialize launch requests in a coordinator (#3044)

* fix(session): serialize launch requests in a coordinator

Replace the gameLaunchRequest counter and the single pendingGameLink field
with a LaunchCoordinator. A launch is now identified by a ticket, so a stale
callback can only act on its own request, and a game link is superseded by
any newer launch. Launching is also refused while a package install stage
is in progress.

* fix(frontend): a replacement intent supersedes uncommitted launch state

A game link that resolved while the launcher was not resumed stayed pending, so
opening Settings, following a notification or picking a shortcut left it in the
coordinator and the next library refresh launched the old game over the new
screen. The same gap let an ACTION_SETUP intent leave an accepted launch current,
so a late Xbox manifest check could start the game after the user had moved on.

A replacement activity intent now cancels every ticket this activity has not
dispatched, and the refresh path may only finish a link owned by the intent the
activity shows. The toast for a superseded store lookup is gated on its ticket,
and the coordinator test now proves cancelAll() invalidates a link ticket.

The deferred Proton-compat launch is untouched: its ordering still predates this
change and has its own follow-up.
This commit is contained in:
visail authored and GitHub committed 2026-11-24 17:14:35 -05:00
1 parent 7c8f5c2707
commit 9a7cb70969
4 files changed
+402 -36

No files matched your search

@@ -53,6 +53,8 @@ import com.droiddeck.launcher.session.SessionLogShare
import com.droiddeck.launcher.session.SessionPaths
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SteamRepair
import com.droiddeck.launcher.session.LaunchCoordinator
import com.droiddeck.launcher.session.LaunchTicket
import com.droiddeck.launcher.core.CpuCores
import com.droiddeck.launcher.core.DownloadSource
import com.droiddeck.launcher.core.PhantomProcessLimit
@@ -215,7 +217,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
private var soundtracksLoaded by mutableStateOf(false)
private var gameStoresSpeedTier by mutableStateOf("fast")
private var navRequest by mutableStateOf<String?>(null)
private val gameLaunchRequest = java.util.concurrent.atomic.AtomicLong()
private val launchCoordinator = LaunchCoordinator<Intent, com.droiddeck.launcher.frontend.GameRequest>()
private var preparingGames by mutableStateOf(emptySet<String>())
private var storesShowMature by mutableStateOf(false)
private var storesSort by mutableStateOf(SessionPrefs.STORES_SORTS.first())
@@ -897,7 +899,6 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
runCatching { com.droiddeck.launcher.frontend.AutoComponents.restoreMissing(this) }.onFailure { Log.w(TAG, "components: restore failed", it) }
}, "components-restore").start()
}
private var pendingGameLink: com.droiddeck.launcher.frontend.GameRequest? = null
private var gameLinkReading = false
private var gameLinkTried = false
private var gameSyncFolder by mutableStateOf<String?>(null)
@@ -951,7 +952,6 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
else "settings:" + (com.droiddeck.launcher.ui.SettingsSection.of(section) ?: com.droiddeck.launcher.ui.SettingsSection.OVERVIEW).key
request.data = null
gameLinkReading = false
pendingGameLink = null
syncDualPanel()
return
}
@@ -964,7 +964,6 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
steamGames = emptyList()
shortcutLibraryScanning = true
} else shortcutLibraryScanning = false
pendingGameLink = null
gameLinkReading = false
gameLinkTried = false
soundtrackLaunch = false
@@ -977,6 +976,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
return
}
if (GameLaunchIntent.accepts(request.action)) {
val ticket = launchCoordinator.beginLink(request)
gameLinkReading = true
com.droiddeck.launcher.ui.DualScreenPanel.hide(taskId)
val copy = Intent(request)
@@ -985,12 +985,17 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
ui.post {
if (intent === request && !isDestroyed) {
gameLinkReading = false
pendingGameLink = game
if (!launchCoordinator.resolve(ticket, game)) {
clearGameLinkIntent(request)
syncDualPanel()
return@post
}
if (game == null) {
launchCoordinator.consume(ticket)
syncDualPanel()
android.widget.Toast.makeText(this, R.string.game_link_invalid, android.widget.Toast.LENGTH_LONG).show()
} else {
handleGameLink(steamGames, scanned = false)
handleGameLink(steamGames, scanned = false, ticket = ticket)
refresh()
}
}
@@ -999,37 +1004,66 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
}
}
private fun launchGame(game: Library.SteamGame, returnHome: Boolean = false, onHandled: () -> Unit = {}, onSettled: () -> Unit = {}) {
val request = gameLaunchRequest.incrementAndGet()
private fun beginUiLaunch(): LaunchTicket {
val dropped = launchCoordinator.pendingLink()
val ticket = launchCoordinator.beginLaunch()
dropped?.let { if (intent === it.owner) clearGameLinkIntent(it.owner) }
return ticket
}
private fun clearGameLinkIntent(link: Intent) {
link.data = null
link.action = Intent.ACTION_MAIN
link.removeExtra(GameLaunchIntent.EXTRA_APP_ID)
}
private fun launchGame(
game: Library.SteamGame,
returnHome: Boolean = false,
onHandled: () -> Unit = {},
onSettled: () -> Unit = {},
ticket: LaunchTicket? = null,
) {
if (!SessionPrefs.soundtracksIngame(this)) com.droiddeck.launcher.playback.SoundtrackController.pause(this)
if (pkgStage != null) {
ticket?.let(launchCoordinator::releaseHandling)
onHandled()
android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show()
return
}
val request = ticket ?: beginUiLaunch()
if (!launchCoordinator.isCurrent(request)) {
onHandled()
return
}
val files = game.gameFiles
if (game.source != com.droiddeck.launcher.stores.Store.XBOX.id || files == null) {
if (startGame(game, returnHome, onHandled)) onSettled()
if (startGame(game, returnHome, onHandled, request)) onSettled()
else launchCoordinator.releaseHandling(request)
return
}
Thread({
val refused = com.droiddeck.launcher.stores.xbox.XboxManifest.needsAppModel(files)
ui.post {
when {
request != gameLaunchRequest.get() || isFinishing || isDestroyed -> onHandled()
!launchCoordinator.isCurrent(request) || isFinishing || isDestroyed -> {
launchCoordinator.releaseHandling(request)
onHandled()
}
refused -> {
onHandled()
android.widget.Toast.makeText(this, getString(R.string.game_needs_app_model, game.name), android.widget.Toast.LENGTH_LONG).show()
onSettled()
}
startGame(game, returnHome, onHandled) -> onSettled()
startGame(game, returnHome, onHandled, request) -> onSettled()
else -> launchCoordinator.releaseHandling(request)
}
}
}, "xbox-app-model").start()
}
private fun startGame(game: Library.SteamGame, returnHome: Boolean, onHandled: () -> Unit): Boolean {
if (com.droiddeck.launcher.session.NoSteamLaunch.enabled(this, game)) return launchWithoutSteam(game, returnHome, onHandled)
private fun startGame(game: Library.SteamGame, returnHome: Boolean, onHandled: () -> Unit, ticket: LaunchTicket): Boolean {
if (com.droiddeck.launcher.session.NoSteamLaunch.enabled(this, game)) return launchWithoutSteam(game, returnHome, onHandled, ticket)
onHandled()
if (SessionState.running) {
if (SessionState.mode != SessionService.MODE_STEAM || SessionState.stopRequested) {
@@ -1074,15 +1108,34 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
.apply { if (returnHome) putExtra(SessionActivity.EXTRA_LINK_TASK, task) }
}
private fun launchWithoutSteam(game: Library.SteamGame, returnHome: Boolean, onHandled: () -> Unit = {}): Boolean {
private fun launchWithoutSteam(
game: Library.SteamGame,
returnHome: Boolean,
onHandled: () -> Unit = {},
ticket: LaunchTicket? = null,
): Boolean {
if (pkgStage != null) {
onHandled()
android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show()
return false
}
val app = applicationContext
val request = ticket ?: beginUiLaunch()
if (!launchCoordinator.isCurrent(request)) {
onHandled()
return false
}
Thread({
val added = if (com.droiddeck.launcher.session.NoSteamLaunch.needsAddedGame(game)) runCatching { com.droiddeck.launcher.session.NoSteamLaunch.addedGame(app, game) }.getOrNull() else null
val command = com.droiddeck.launcher.session.NoSteamLaunch.command(game, added)
ui.post {
onHandled()
if (isDestroyed) return@post
if (!launchCoordinator.isCurrent(request) || isFinishing || isDestroyed) {
launchCoordinator.releaseHandling(request)
return@post
}
if (command == null) {
launchCoordinator.releaseHandling(request)
android.widget.Toast.makeText(this, R.string.game_link_failed, android.widget.Toast.LENGTH_LONG).show()
return@post
}
@@ -1234,22 +1287,27 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
ui.postDelayed(poll, SESSION_SWITCH_POLL_MS)
}
private fun handleGameLink(games: List<Library.SteamGame>, scanned: Boolean = true) {
private fun handleGameLink(games: List<Library.SteamGame>, scanned: Boolean = true, ticket: LaunchTicket? = null) {
if (!lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.RESUMED)) return
val request = pendingGameLink ?: return
val link = launchCoordinator.pendingLink() ?: return
if (ticket != null && link.ticket !== ticket) return
val request = link.request ?: return
if (launchCoordinator.isHandling(link.ticket)) return
if (ticket == null && !link.ownedBy(intent)) {
// A refresh may only finish the link that belongs to the intent this activity shows.
launchCoordinator.consume(link.ticket)
return
}
val game = request.find(games, byTitle = scanned)
if (!scanned && game == null) return
gameLinkTried = true
ui.postDelayed({
if (!isDestroyed && lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.RESUMED)) syncDualPanel()
}, LINK_PANEL_MS)
val link = intent
val linkIntent = link.owner
fun consume() {
if (intent !== link || pendingGameLink !== request) return
pendingGameLink = null
link.data = null
link.action = Intent.ACTION_MAIN
link.removeExtra(GameLaunchIntent.EXTRA_APP_ID)
if (!launchCoordinator.consume(link.ticket)) return
if (intent === linkIntent) clearGameLinkIntent(linkIntent)
}
if (game == null) {
consume()
@@ -1257,7 +1315,10 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
android.widget.Toast.makeText(this,
if (request.program != null) R.string.game_link_program_unknown else R.string.game_link_missing,
android.widget.Toast.LENGTH_LONG).show()
} else launchGame(game, returnHome = true, onSettled = ::consume)
} else {
if (!launchCoordinator.beginHandling(link.ticket)) return
launchGame(game, returnHome = true, onSettled = ::consume, ticket = link.ticket)
}
}
private fun chooseGameShortcut(game: Library.SteamGame) {
@@ -1448,16 +1509,20 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
},
onStoreGame = { store, id ->
val key = "${store.id}:$id"
if (key !in preparingGames) {
if (pkgStage != null) {
android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show()
} else if (key !in preparingGames) {
preparingGames = preparingGames + key
val ticket = beginUiLaunch()
val app = applicationContext
Thread({
val g = runCatching { com.droiddeck.launcher.stores.StoreLaunch.installedGame(app, store, id) }.getOrNull()
runOnUiThread {
if (g != null && !isFinishing && !isDestroyed) prepareAndLaunch(g, key)
else {
if (g != null && launchCoordinator.isCurrent(ticket) && !isFinishing && !isDestroyed) {
prepareAndLaunch(g, key, ticket)
} else {
preparingGames = preparingGames - key
if (g == null && !isFinishing && !isDestroyed) android.widget.Toast.makeText(this, R.string.stores_launch_not_registered, android.widget.Toast.LENGTH_LONG).show()
if (g == null && launchCoordinator.isCurrent(ticket) && !isFinishing && !isDestroyed) android.widget.Toast.makeText(this, R.string.stores_launch_not_registered, android.widget.Toast.LENGTH_LONG).show()
}
}
}, "store-play").start()
@@ -1960,10 +2025,9 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
gameLaunchRequest.incrementAndGet()
supersedeUncommittedLaunch()
if (intent.action == SteamIntegration.ACTION_SETUP) {
gameLinkReading = false
pendingGameLink = null
syncDualPanel()
readIntegrationIntent(intent)
return
@@ -1973,6 +2037,11 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
startSteamAtStartupIfEnabled()
}
/** A replacement activity intent supersedes anything this activity has not dispatched yet. */
private fun supersedeUncommittedLaunch() {
launchCoordinator.cancelAll()
}
private var soundtrackLaunch = false
// A restarted launcher starts before the old one stops, so each clears the hooks only if they are still its own.
@@ -2251,13 +2320,22 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
if (Build.VERSION.SDK_INT >= 33) state.getParcelable(key, Intent::class.java)
else @Suppress("DEPRECATION") state.getParcelable(key)
private fun prepareAndLaunch(g: Library.SteamGame, key: String) {
val request = gameLaunchRequest.incrementAndGet()
private fun prepareAndLaunch(g: Library.SteamGame, key: String, ticket: LaunchTicket? = null) {
if (pkgStage != null) {
preparingGames = preparingGames - key
android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show()
return
}
val request = ticket ?: beginUiLaunch()
if (!launchCoordinator.isCurrent(request)) {
preparingGames = preparingGames - key
return
}
val done = { preparingGames = preparingGames - key }
com.droiddeck.launcher.frontend.AutoComponents.beforeLaunch(this, g, onBlocked = done) {
if (request != gameLaunchRequest.get() || isFinishing || isDestroyed) { done(); return@beforeLaunch }
if (!launchCoordinator.isCurrent(request) || isFinishing || isDestroyed) { done(); return@beforeLaunch }
com.droiddeck.launcher.stores.StoreLaunch.prepare(this, g) {
if (request == gameLaunchRequest.get() && !isFinishing && !isDestroyed) launchGame(g, onHandled = done)
if (launchCoordinator.isCurrent(request) && !isFinishing && !isDestroyed) launchGame(g, onHandled = done, ticket = request)
else done()
}
}
@@ -2267,7 +2345,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
// The queued launch was saved before this; a restored activity keeps its copy, and this one must not start it too.
queuedLaunchRunner.destroy()
triggerKeys.cancel()
gameLaunchRequest.incrementAndGet()
launchCoordinator.cancelAll()
cancelCompatLaunch()
updates.unregister()
runCatching { unregisterReceiver(shortcutsInstallStatus) }
@@ -2346,7 +2424,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
else com.droiddeck.launcher.ui.DualScreenPanel.hide(taskId)
}
private fun launchPending(): Boolean = gameLinkReading || (pendingGameLink != null && !gameLinkTried)
private fun launchPending(): Boolean = gameLinkReading || (launchCoordinator.hasPending() && !gameLinkTried)
private fun launchAndroidApp(app: HomeApp.LaunchableApp, displayId: Int?) {
val onOwnScreen = displayId == null || displayId == (display?.displayId ?: Display.DEFAULT_DISPLAY)
@@ -0,0 +1,82 @@
package com.droiddeck.launcher.session
/**
* Orders launch attempts and at most one pending game link.
*
* This is main-thread state: call it from the UI thread. A ticket is an identity,
* not a counter comparison, so a stale callback can only act on its own request.
*/
internal class LaunchTicket internal constructor(val serial: Long)
internal class LaunchCoordinator<O : Any, R : Any> {
internal class Link<O : Any, R : Any> internal constructor(
val ticket: LaunchTicket,
val owner: O,
val request: R?,
) {
/** Only the activity intent that delivered this link may finish it by a refresh. */
fun ownedBy(owner: O?): Boolean = this.owner === owner
}
private var next = 0L
private var current: LaunchTicket? = null
private var pending: Link<O, R>? = null
private var handling: LaunchTicket? = null
/** A launch that is allowed to proceed; it supersedes any pending link. */
fun beginLaunch(): LaunchTicket {
val ticket = LaunchTicket(++next)
current = ticket
pending = null
handling = null
return ticket
}
/** A game-link intent; it supersedes any earlier launch or link. */
fun beginLink(owner: O): LaunchTicket {
val ticket = LaunchTicket(++next)
current = ticket
pending = Link(ticket, owner, null)
handling = null
return ticket
}
/** The asynchronous link read returned; false means a newer request won. */
fun resolve(ticket: LaunchTicket, request: R?): Boolean {
val link = pending ?: return false
if (link.ticket !== ticket) return false
pending = Link(ticket, link.owner, request)
return true
}
fun beginHandling(ticket: LaunchTicket): Boolean {
val link = pending ?: return false
if (link.ticket !== ticket || link.request == null) return false
handling = ticket
return true
}
fun releaseHandling(ticket: LaunchTicket) {
if (handling === ticket) handling = null
}
fun isHandling(ticket: LaunchTicket): Boolean = handling === ticket
fun isCurrent(ticket: LaunchTicket): Boolean = current === ticket
fun hasPending(): Boolean = pending != null
fun pendingLink(): Link<O, R>? = pending
fun consume(ticket: LaunchTicket): Boolean {
val link = pending ?: return false
if (link.ticket !== ticket) return false
pending = null
handling = null
return true
}
fun cancelAll() {
next++
current = null
pending = null
handling = null
}
}
@@ -0,0 +1,85 @@
package com.droiddeck.launcher
import android.content.Intent
import com.droiddeck.launcher.core.SteamIntegration
import com.droiddeck.launcher.frontend.GameRequest
import com.droiddeck.launcher.session.LaunchCoordinator
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
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
/**
* A replacement activity intent invalidates everything this activity has not dispatched yet: a
* resolved but unhandled game link, and an accepted launch that is still waiting for its session.
*/
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class FrontEndIntentSupersedeTest {
private lateinit var activity: FrontEndActivity
private lateinit var coordinator: LaunchCoordinator<Intent, GameRequest>
@Before
fun setUp() {
activity = Robolectric.buildActivity(FrontEndActivity::class.java).get()
coordinator = ReflectionHelpers.getField(activity, "launchCoordinator")
}
@Test
fun aReplacementIntentDropsAResolvedPendingLink() {
val owner = Intent(activity, FrontEndActivity::class.java)
val ticket = coordinator.beginLink(owner)
assertTrue(coordinator.resolve(ticket, GameRequest(id = "app")))
supersede()
assertFalse(coordinator.hasPending())
assertNull(coordinator.pendingLink())
}
@Test
fun aReplacementIntentInvalidatesAnAcceptedLaunch() {
val ticket = coordinator.beginLaunch()
assertTrue(coordinator.isCurrent(ticket))
supersede()
assertFalse(coordinator.isCurrent(ticket))
assertNull(shadowOf(activity).nextStartedActivity)
}
@Test
fun anActionSetupIntentSupersedesTheGameLinkItReplaced() {
val owner = Intent(activity, FrontEndActivity::class.java)
val ticket = coordinator.beginLink(owner)
assertTrue(coordinator.resolve(ticket, GameRequest(id = "app")))
deliver(Intent(activity, FrontEndActivity::class.java).setAction(SteamIntegration.ACTION_SETUP))
assertFalse(coordinator.hasPending())
assertFalse(coordinator.isCurrent(ticket))
assertNull(shadowOf(activity).nextStartedActivity)
}
private fun supersede() {
ReflectionHelpers.callInstanceMethod<Void>(activity, "supersedeUncommittedLaunch")
}
/** onNewIntent() is protected, which is exactly how the platform delivers a replacement intent. */
private fun deliver(intent: Intent) {
val method = FrontEndActivity::class.java.getDeclaredMethod("onNewIntent", Intent::class.java)
method.isAccessible = true
try {
method.invoke(activity, intent)
} finally {
method.isAccessible = false
}
}
}
@@ -0,0 +1,121 @@
package com.droiddeck.launcher.session
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test
class LaunchCoordinatorTest {
@Test
fun aNewLaunchSupersedesAnOlderLaunch() {
val coordinator = LaunchCoordinator<String, String>()
val first = coordinator.beginLaunch()
val second = coordinator.beginLaunch()
assertFalse(coordinator.isCurrent(first))
assertTrue(coordinator.isCurrent(second))
}
@Test
fun anAllowedLaunchDropsThePendingLink() {
val coordinator = LaunchCoordinator<String, String>()
coordinator.beginLink("link")
coordinator.beginLaunch()
assertFalse(coordinator.hasPending())
}
@Test
fun aLinkReadResolvesAndIsConsumed() {
val coordinator = LaunchCoordinator<String, String>()
val ticket = coordinator.beginLink("link")
assertTrue(coordinator.resolve(ticket, "game"))
assertEquals("game", coordinator.pendingLink()?.request)
assertTrue(coordinator.consume(ticket))
assertFalse(coordinator.hasPending())
}
@Test
fun aStaleLinkReadCannotReplaceTheNewLink() {
val coordinator = LaunchCoordinator<String, String>()
val first = coordinator.beginLink("first")
val second = coordinator.beginLink("second")
assertFalse(coordinator.resolve(first, "old"))
assertTrue(coordinator.resolve(second, "new"))
assertEquals("new", coordinator.pendingLink()?.request)
}
@Test
fun handlingNeedsAResolvedLink() {
val coordinator = LaunchCoordinator<String, String>()
val ticket = coordinator.beginLink("link")
assertFalse(coordinator.beginHandling(ticket))
coordinator.resolve(ticket, "game")
assertTrue(coordinator.beginHandling(ticket))
assertTrue(coordinator.isHandling(ticket))
}
@Test
fun aStaleCompletionCannotConsumeTheNewLink() {
val coordinator = LaunchCoordinator<String, String>()
val first = coordinator.beginLink("first")
val second = coordinator.beginLink("second")
coordinator.resolve(second, "new")
assertFalse(coordinator.consume(first))
assertTrue(coordinator.hasPending())
}
@Test
fun aUiLaunchDropsThePendingLinkBeforeItLaunches() {
val coordinator = LaunchCoordinator<String, String>()
coordinator.beginLink("link")
val launch = coordinator.beginLaunch()
assertNull(coordinator.pendingLink())
assertTrue(coordinator.isCurrent(launch))
}
@Test
fun aSecondLinkReplacesTheFirst() {
val coordinator = LaunchCoordinator<String, String>()
val first = coordinator.beginLink("first")
val second = coordinator.beginLink("second")
assertFalse(coordinator.isCurrent(first))
assertTrue(coordinator.isCurrent(second))
assertEquals("second", coordinator.pendingLink()?.owner)
}
@Test
fun aLinkKeepsItsOwnerForIntentCleanup() {
val coordinator = LaunchCoordinator<String, String>()
val ticket = coordinator.beginLink("intent-a")
coordinator.resolve(ticket, "game")
assertSame("intent-a", coordinator.pendingLink()?.owner)
}
@Test
fun aLinkIsOwnedOnlyByItsIntent() {
val coordinator = LaunchCoordinator<String, String>()
val ticket = coordinator.beginLink("intent-a")
coordinator.resolve(ticket, "game")
val link = requireNotNull(coordinator.pendingLink())
assertTrue(link.ownedBy("intent-a"))
assertFalse(link.ownedBy("intent-b"))
assertFalse(link.ownedBy(null))
assertTrue(coordinator.consume(ticket))
assertNull(coordinator.pendingLink())
}
@Test
fun cancelAllInvalidatesEveryOutstandingTicket() {
val coordinator = LaunchCoordinator<String, String>()
coordinator.beginLaunch()
val link = coordinator.beginLink("link")
assertTrue(coordinator.isCurrent(link))
coordinator.cancelAll()
assertFalse(coordinator.isCurrent(link))
assertFalse(coordinator.hasPending())
assertNull(coordinator.pendingLink())
assertFalse(coordinator.resolve(link, "late"))
assertFalse(coordinator.consume(link))
}
}