mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 23:36:04 +00:00
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:
1 parent
7c8f5c2707
commit
9a7cb70969
4 files changed
+402
-36
No files matched your search
@@ -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))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user