fix(steam): preserve Back taps and recover QAM controls

This commit is contained in:
Kurt Himebauch committed 2026-10-26 13:13:08 -04:00
1 parent dde1a55d00
commit fa5506df35
13 files changed
+395 -56

No files matched your search

@@ -1,28 +1,76 @@
package com.droiddeck.launcher.input
import android.os.Looper
import android.view.KeyEvent
import java.time.Duration
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], manifest = Config.NONE)
class BackQamInputTest {
private val changes = mutableListOf<Boolean>()
private val input = BackQamInput(changes::add)
private val taps = mutableListOf<Boolean>()
private val input = BackQamInput(changes::add, taps::add)
@Test fun aHoldAndItsRepeatsProduceOnePressAndOneRelease() {
@Test fun singleTapWaitsForTheDoubleTapWindowWithoutPressingQam() {
send(KeyEvent.ACTION_DOWN)
advance(100)
send(KeyEvent.ACTION_UP)
advance(499)
assertTrue(taps.isEmpty())
advance(1)
assertEquals(listOf(false), taps)
assertTrue(changes.isEmpty())
}
@Test fun doubleTapProducesOnlyTheDoubleAction() {
repeat(2) {
send(KeyEvent.ACTION_DOWN)
advance(100)
send(KeyEvent.ACTION_UP)
advance(100)
}
advance(1000)
assertEquals(listOf(true), taps)
assertTrue(changes.isEmpty())
}
@Test fun aHoldStartsAt500msAndNeverProducesATap() {
assertTrue(send(KeyEvent.ACTION_DOWN))
advance(499)
assertTrue(changes.isEmpty())
advance(1)
for (repeat in 1..10) assertTrue(send(KeyEvent.ACTION_DOWN, repeat = repeat))
advance(4000)
assertEquals(listOf(true), changes)
assertTrue(send(KeyEvent.ACTION_UP))
advance(1000)
assertEquals(listOf(true, false), changes)
assertTrue(taps.isEmpty())
}
@Test fun holdingTheSecondPressCancelsTheFirstTap() {
send(KeyEvent.ACTION_DOWN)
send(KeyEvent.ACTION_UP)
advance(400)
send(KeyEvent.ACTION_DOWN)
advance(499)
assertTrue(taps.isEmpty())
advance(1)
assertEquals(listOf(true), changes)
send(KeyEvent.ACTION_UP)
advance(1000)
assertTrue(taps.isEmpty())
}
@Test fun otherButtonsAndSelectPassThroughWhileBackIsHeld() {
send(KeyEvent.ACTION_DOWN)
advance(500)
for (code in listOf(KeyEvent.KEYCODE_BUTTON_X, KeyEvent.KEYCODE_BUTTON_SELECT)) {
for (action in listOf(KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP)) {
assertFalse(input.onKeyEvent(KeyEvent(action, code), true))
@@ -37,20 +85,42 @@ class BackQamInputTest {
assertFalse(send(KeyEvent.ACTION_DOWN, enabled = false))
assertFalse(send(KeyEvent.ACTION_UP, enabled = false))
assertTrue(changes.isEmpty())
assertTrue(taps.isEmpty())
}
@Test fun losingFocusReleasesQamAndConsumesTheLateRelease() {
send(KeyEvent.ACTION_DOWN)
advance(500)
input.cancel()
input.cancel()
assertTrue(send(KeyEvent.ACTION_DOWN, repeat = 1, enabled = false))
assertTrue(send(KeyEvent.ACTION_UP, enabled = false))
advance(1000)
assertEquals(listOf(true, false), changes)
assertFalse(send(KeyEvent.ACTION_UP, enabled = false))
assertTrue(taps.isEmpty())
}
@Test fun cancellationBeforeTheThresholdPreventsBothHoldAndTap() {
send(KeyEvent.ACTION_DOWN)
advance(100)
input.cancel()
send(KeyEvent.ACTION_UP)
advance(1000)
assertTrue(changes.isEmpty())
assertTrue(taps.isEmpty())
}
@Test fun cancellationAlsoDiscardsAPendingSingleTap() {
send(KeyEvent.ACTION_DOWN)
send(KeyEvent.ACTION_UP)
input.cancel()
advance(1000)
assertTrue(taps.isEmpty())
}
@Test fun openingAnOverlayReleasesQamBeforeRoutingItsNextKey() {
send(KeyEvent.ACTION_DOWN)
advance(500)
assertFalse(input.onKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_B), false))
assertTrue(send(KeyEvent.ACTION_UP, enabled = false))
assertEquals(listOf(true, false), changes)
@@ -58,33 +128,39 @@ class BackQamInputTest {
@Test fun repeatsAfterCancellationCannotReassertQam() {
send(KeyEvent.ACTION_DOWN)
advance(500)
input.cancel()
assertTrue(send(KeyEvent.ACTION_DOWN, repeat = 1))
advance(1000)
assertEquals(listOf(true, false), changes)
assertTrue(send(KeyEvent.ACTION_DOWN))
advance(500)
assertTrue(send(KeyEvent.ACTION_UP))
assertEquals(listOf(true, false, true, false), changes)
}
@Test fun cancelledReleaseStillReleasesQam() {
@Test fun cancelledReleaseNeverProducesATap() {
send(KeyEvent.ACTION_DOWN)
assertTrue(input.onKeyEvent(KeyEvent(0, 100, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK,
0, 0, 4, 0, KeyEvent.FLAG_CANCELED), true))
assertEquals(listOf(true, false), changes)
advance(1000)
assertTrue(changes.isEmpty())
assertTrue(taps.isEmpty())
}
@Test fun removingOneDeviceDoesNotReleaseAnotherDevicesHold() {
send(KeyEvent.ACTION_DOWN, device = 4)
send(KeyEvent.ACTION_DOWN, device = 5)
advance(500)
input.onDeviceRemoved(4)
assertTrue(changes.last())
assertEquals(listOf(true), changes)
send(KeyEvent.ACTION_UP, device = 5)
assertFalse(changes.last())
send(KeyEvent.ACTION_DOWN, device = 4)
input.onDeviceRemoved(4)
assertFalse(changes.last())
assertEquals(listOf(true, false), changes)
assertTrue(taps.isEmpty())
}
private fun advance(millis: Long) = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(millis))
private fun send(action: Int, repeat: Int = 0, device: Int = 4, enabled: Boolean = true) =
input.onKeyEvent(KeyEvent(0, 100, action, KeyEvent.KEYCODE_BACK, repeat, 0, device, 0), enabled)
}
@@ -1,6 +1,7 @@
package com.droiddeck.launcher.runtime
import android.app.Application
import android.app.NotificationManager
import android.content.Context
import android.media.AudioManager
import android.os.Looper
@@ -24,20 +25,21 @@ import org.robolectric.annotation.Implementation
import org.robolectric.shadows.ShadowSettings
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE, application = Application::class, shadows = [WriteSettingsShadow::class])
@Config(sdk = [28, 33], manifest = Config.NONE, application = Application::class, shadows = [WriteSettingsShadow::class])
class DeviceControlsComponentTest {
@get:Rule val tmp = TemporaryFolder()
private val context get() = RuntimeEnvironment.getApplication()
private val audio get() = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
private lateinit var component: DeviceControlsComponent
private lateinit var directory: File
private var visible = true
@Before fun start() {
WriteSettingsShadow.writable = true
Settings.System.putInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS, 128)
val root = tmp.newFolder()
directory = DeviceControlsComponent.directory(root)
component = DeviceControlsComponent(context, root)
component = DeviceControlsComponent(context, root) { visible }
component.publish()
component.start()
shadowOf(Looper.getMainLooper()).idle()
@@ -85,6 +87,35 @@ class DeviceControlsComponentTest {
assertEquals(1.0, JSONObject(File(directory, "state.json").readText()).getDouble("brightness"), 0.0001)
}
@Test fun hiddenPermissionRequestOffersANotificationAndRetainsTheLatestValue() {
visible = false
WriteSettingsShadow.writable = false
request("brightness", "0.25")
request("brightness", "0.75")
assertNull(shadowOf(context).nextStartedActivity)
val notifications = context.getSystemService(NotificationManager::class.java)
val notification = notifications.activeNotifications.single().notification
notification.contentIntent.send()
assertEquals(Settings.ACTION_MANAGE_WRITE_SETTINGS, shadowOf(context).nextStartedActivity.action)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(121))
WriteSettingsShadow.writable = true
tick()
assertEquals(192, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
assertTrue(notifications.activeNotifications.isEmpty())
}
@Test fun stoppingClearsThePermissionNotificationAndPendingRequest() {
visible = false
WriteSettingsShadow.writable = false
request("brightness", "0.75")
component.stop()
shadowOf(Looper.getMainLooper()).idle()
assertTrue(context.getSystemService(NotificationManager::class.java).activeNotifications.isEmpty())
WriteSettingsShadow.writable = true
tick()
assertEquals(128, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
}
@Test fun stoppedSessionsDoNotConsumeNewCommands() {
component.stop()
shadowOf(Looper.getMainLooper()).idle()