Guard shared input slots and mouse backend transitions

This commit is contained in:
Delete228 committed 2026-11-06 23:56:30 +03:00
1 parent 915f373d82
commit 31b8adbb55
7 files changed
+244 -44

No files matched your search

@@ -28,10 +28,25 @@ class InputProfilesTest {
assertEquals(InputAction.Disabled, game.action(InputRole.PRIMARY, InputControl.X))
assertEquals(InputAction.Native(), game.action(InputRole.PHYSICAL, InputControl.A))
assertEquals(InputAction.Native(InputControl.GUIDE), game.action(InputRole.PHYSICAL, InputControl.SELECT))
assertEquals(InputAction.Native(InputControl.GUIDE), InputProfiles.read(context, InputProfile.DESKTOP)
.action(InputRole.PHYSICAL, InputControl.SELECT))
InputProfiles.reset(context, InputProfile.GAME)
assertEquals(InputAction.Native(), InputProfiles.read(context, InputProfile.GAME).action(InputRole.PRIMARY, InputControl.A))
}
@Test fun migrationPreservesSavedDesktopSelectBinding() {
ControllerPrefs.setSelectSteam(context, true)
context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().putString("profile.desktop",
"""{"version":1,"bindings":[{"role":"PHYSICAL","control":"select","action":{"type":"disabled"}}]}""").commit()
assertEquals(InputAction.Disabled, InputProfiles.read(context, InputProfile.DESKTOP)
.action(InputRole.PHYSICAL, InputControl.SELECT))
}
@Test fun migrationKeepsDesktopKeyboardWhenSelectToSteamIsOff() {
assertEquals(InputAction.Command(InputCommand.KEYBOARD), InputProfiles.read(context, InputProfile.DESKTOP)
.action(InputRole.PHYSICAL, InputControl.SELECT))
}
@Test fun bindingsAndDeviceKeysRoundTripWithoutAffectingOtherProfile() {
val game = InputProfile.defaults(InputProfile.GAME)
.withBinding(InputRole.SECONDARY, InputControl.A, InputAction.Keys(listOf(29, 30)))
@@ -21,6 +21,10 @@ import org.robolectric.annotation.Config
import org.robolectric.annotation.Implementation
import org.robolectric.annotation.Implements
import org.robolectric.util.ReflectionHelpers
import java.util.Collections
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE, shadows = [SessionInputPointerTest.CompositorShadow::class, SessionInputPointerTest.WriterShadow::class])
@@ -43,6 +47,7 @@ class SessionInputPointerTest {
CompositorShadow.events.clear()
CompositorShadow.keys.clear()
WriterShadow.mouse.clear()
WriterShadow.beforeWheel = null
}
@After fun tearDown() {
@@ -205,6 +210,52 @@ class SessionInputPointerTest {
assertEquals(listOf(Triple(3, 272, 1), Triple(3, 272, 0)), CompositorShadow.events)
}
@Test fun horizontalWheelFinishesBeforeRawMouseDestruction() {
checkWheelTransition(horizontal = true, transition = "releaseEvdevMouse", marker = "destroy")
}
@Test fun verticalWheelFinishesBeforeRawMouseSoftRelease() {
checkWheelTransition(horizontal = false, transition = "closeEvdevMouse", marker = "release")
}
private fun checkWheelTransition(horizontal: Boolean, transition: String, marker: String) {
ReflectionHelpers.setField(activity, "mouseWriter", FakeInputWriter(activity.cacheDir.path, FakeInputWriter.MOUSE_SLOT))
ReflectionHelpers.setField(activity, "evdevMouseActive", true)
val entered = CountDownLatch(1)
val continueWrite = CountDownLatch(1)
val transitioning = CountDownLatch(1)
val finished = CountDownLatch(1)
WriterShadow.beforeWheel = {
entered.countDown()
check(continueWrite.await(5, TimeUnit.SECONDS))
}
val executor = Executors.newFixedThreadPool(2)
fun scroll() { if (horizontal) output.scrollHorizontal(2) else output.scroll(2) }
try {
val writing = executor.submit { scroll() }
assertTrue(entered.await(5, TimeUnit.SECONDS))
val closing = executor.submit {
transitioning.countDown()
invoke(transition)
finished.countDown()
}
assertTrue(transitioning.await(5, TimeUnit.SECONDS))
assertFalse(finished.await(100, TimeUnit.MILLISECONDS))
continueWrite.countDown()
writing.get(5, TimeUnit.SECONDS)
closing.get(5, TimeUnit.SECONDS)
assertEquals(listOf(if (horizontal) "wheel:0:2:120" else "wheel:-2:0:120", marker), WriterShadow.mouse)
scroll()
assertEquals(2, WriterShadow.mouse.size)
assertEquals(listOf(if (horizontal) Triple(11, 0, 240) else Triple(4, 2, 0)), CompositorShadow.events)
} finally {
continueWrite.countDown()
executor.shutdownNow()
executor.awaitTermination(5, TimeUnit.SECONDS)
WriterShadow.beforeWheel = null
}
}
private fun refresh() = invoke("refreshInputProfile")
private fun invoke(name: String) = ReflectionHelpers.callInstanceMethod<Unit>(activity, name)
private fun <T> state(name: String): MutableState<T> = ReflectionHelpers.getField(activity, "$name\$delegate")
@@ -226,10 +277,15 @@ class SessionInputPointerTest {
class WriterShadow {
@Implementation fun writeMouseButton(code: Short, down: Boolean) { mouse += "button:$code:$down" }
@Implementation fun writeMouseDelta(dx: Float, dy: Float) { mouse += "delta:$dx:$dy" }
@Implementation fun writeMouseWheel(vertical: Int, horizontal: Int, value120: Int) { mouse += "wheel:$vertical:$horizontal:$value120" }
@Implementation fun writeMouseWheel(vertical: Int, horizontal: Int, value120: Int) {
beforeWheel?.invoke()
mouse += "wheel:$vertical:$horizontal:$value120"
}
@Implementation fun softRelease() { mouse += "release" }
@Implementation fun destroy() { mouse += "destroy" }
companion object {
val mouse = mutableListOf<String>()
val mouse: MutableList<String> = Collections.synchronizedList(mutableListOf())
@Volatile var beforeWheel: (() -> Unit)? = null
@JvmStatic @Implementation fun __staticInitializer__() {}
@JvmStatic @Implementation fun writeDeckControls(index: Int, mask: Int, pads: ShortArray, pressure: ShortArray) {}
}
@@ -1,5 +1,10 @@
package com.droiddeck.launcher.input
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCallback
import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattDescriptor
import android.os.Handler
import org.junit.After
import org.junit.Assert.*
@@ -13,6 +18,8 @@ import org.robolectric.annotation.Config
import org.robolectric.annotation.Implementation
import org.robolectric.annotation.Implements
import org.robolectric.util.ReflectionHelpers
import org.robolectric.shadows.ShadowBluetoothGatt
import java.util.UUID
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], manifest = Config.NONE, shadows = [SteamControllerBleTest.Writer::class])
@@ -38,6 +45,7 @@ class SteamControllerBleTest {
controller = SteamControllerBle(RuntimeEnvironment.getApplication(), router)
handler = ReflectionHelpers.getField(controller, "handler")
shadowOf(handler.looper).pause()
controller.start()
}
@After fun close() {
@@ -81,7 +89,78 @@ class SteamControllerBleTest {
assertEquals(listOf(57 to true, 57 to false), keys)
}
private fun state(bytes: ByteArray) = ReflectionHelpers.callInstanceMethod<Unit>(controller, "onState",
@Test fun staleControllerCannotClearOrPublishIntoItsReplacementsSlot() {
state(ByteArray(45).apply { this[1] = 1 })
assertEquals(listOf(57 to true), keys)
val replacement = SteamControllerBle(RuntimeEnvironment.getApplication())
val replacementHandler = ReflectionHelpers.getField<Handler>(replacement, "handler")
shadowOf(replacementHandler.looper).pause()
try {
replacement.start()
subscribe(replacement)
val report = ByteArray(45).apply { this[1] = 2 }
state(report, replacement)
val reports = Writer.reports.size
val connections = Writer.connections.toList()
controller.start()
shadowOf(handler.looper).idle()
subscribe(controller)
state(ByteArray(45))
ReflectionHelpers.setField(controller, "featureRead", 7L)
ReflectionHelpers.callInstanceMethod<Unit>(controller, "onFeatureRead",
ReflectionHelpers.ClassParameter.from(ByteArray::class.java, ByteArray(2)))
ReflectionHelpers.callInstanceMethod<Unit>(controller, "disconnect",
ReflectionHelpers.ClassParameter.from(String::class.java, "old link lost"))
controller.stop()
shadowOf(handler.looper).idle()
assertTrue(SteamControllerBle.connected)
assertEquals(connections, Writer.connections)
assertEquals(reports, Writer.reports.size)
assertArrayEquals(report, Writer.reports.last())
assertEquals(0, Writer.features)
assertEquals(listOf(57 to true, 57 to false), keys)
assertNull(ReflectionHelpers.getField<Any?>(controller, "routedInput"))
replacement.stop()
assertFalse(SteamControllerBle.connected)
assertFalse(Writer.connections.last())
assertTrue(Writer.reports.last().all { it == 0.toByte() })
} finally {
replacement.stop()
replacementHandler.looper.quit()
}
}
@Test fun stoppingAnUnstartedControllerPreservesTheActiveSlot() {
subscribe(controller)
val unused = SteamControllerBle(RuntimeEnvironment.getApplication())
try {
val reports = Writer.reports.size
val connections = Writer.connections.toList()
unused.stop()
assertTrue(SteamControllerBle.connected)
assertEquals(connections, Writer.connections)
assertEquals(reports, Writer.reports.size)
} finally {
unused.stop()
ReflectionHelpers.getField<Handler>(unused, "handler").looper.quit()
}
}
private fun subscribe(target: SteamControllerBle) {
val device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:11:22:33:44:55")
val gatt = ShadowBluetoothGatt.newInstance(device)
val input = UUID.fromString("100F6C7A-1735-4313-B402-38567131E5F3")
val characteristic = BluetoothGattCharacteristic(input, BluetoothGattCharacteristic.PROPERTY_NOTIFY, 0)
val descriptor = BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"), 0)
characteristic.addDescriptor(descriptor)
ReflectionHelpers.setField(target, "gatt", gatt)
ReflectionHelpers.setField(target, "input", input)
ReflectionHelpers.setField(target, "reportId", 0x45)
ReflectionHelpers.getField<BluetoothGattCallback>(target, "callback")
.onDescriptorWrite(gatt, descriptor, BluetoothGatt.GATT_SUCCESS)
}
private fun state(bytes: ByteArray, target: SteamControllerBle = controller) = ReflectionHelpers.callInstanceMethod<Unit>(target, "onState",
ReflectionHelpers.ClassParameter.from(ByteArray::class.java, bytes))
@Implements(FakeInputWriter::class)