Fix direct-touch right-click and Steam Touch mode restoration (#677)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-19 14:18:26 -04:00
1 parent a789d60f6a
commit 7c20d4b5c3
4 files changed
+200

No files matched your search

@@ -0,0 +1,66 @@
package com.droiddeck.launcher.input
import android.os.Looper
import android.view.MotionEvent
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 = [28], manifest = Config.NONE)
class DirectTouchHoldTest {
private val events = mutableListOf<String>()
private val hold = DirectTouchHold(8f, object : PointerGestures.Listener {
override fun onMove(x: Float, y: Float) {}
override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) {
assertEquals(PointerGestures.BTN_RIGHT, button)
events += "$pressed at $x,$y"
}
override fun onWheel(steps: Int) {}
override fun onLongPress() {}
}) { events += "cancel touch" }
@Test fun holdReleasesRawTouchBeforeClickAndConsumesTheLift() {
assertFalse(touch(MotionEvent.ACTION_DOWN))
advance(500)
assertEquals(listOf("cancel touch", "true at 40.0,30.0"), events)
assertTrue(touch(MotionEvent.ACTION_UP))
advance(50)
assertEquals("false at 40.0,30.0", events.last())
assertFalse(touch(MotionEvent.ACTION_DOWN))
hold.cancel()
}
@Test fun tapDragSecondFingerAndCancellationDoNotRightClick() {
for (action in listOf(MotionEvent.ACTION_UP, MotionEvent.ACTION_MOVE,
MotionEvent.ACTION_POINTER_DOWN, MotionEvent.ACTION_CANCEL)) {
touch(MotionEvent.ACTION_DOWN)
assertFalse(touch(action, 60f))
advance(600)
assertTrue(events.isEmpty())
hold.cancel()
}
}
@Test fun modeChangeCancelsPendingHoldAndReleasesAnActiveClick() {
touch(MotionEvent.ACTION_DOWN)
hold.cancel()
advance(600)
assertTrue(events.isEmpty())
touch(MotionEvent.ACTION_DOWN)
advance(500)
hold.cancel()
advance(100)
assertEquals(listOf("cancel touch", "true at 40.0,30.0", "false at 40.0,30.0"), events)
}
private fun advance(ms: Long) = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms))
private fun touch(action: Int, x: Float = 40f): Boolean {
val event = MotionEvent.obtain(0, 0, action, x, 30f, 0)
return hold.onTouch(event).also { event.recycle() }
}
}
@@ -0,0 +1,69 @@
package com.droiddeck.launcher.input
import android.view.View
import com.droiddeck.launcher.SessionActivity
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SessionService
import com.droiddeck.launcher.session.SessionState
import java.nio.file.Files
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.util.ReflectionHelpers
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class SessionTouchRoutingTest {
private lateinit var activity: SessionActivity
private lateinit var steam: SteamTouchControls
private lateinit var touchpad: TouchpadGestures
private val directory = Files.createTempDirectory("touch-routing").toFile()
@Before fun setUp() {
activity = Robolectric.buildActivity(SessionActivity::class.java).get()
activity.getSharedPreferences("session", 0).edit().clear().commit()
SessionState.mode = SessionService.MODE_STEAM
SteamTouchDevice.prepare(directory)
steam = SteamTouchControls(activity, SteamTouchDevice.current!!, {}, {})
steam.visibility = View.VISIBLE
touchpad = TouchpadGestures(8f, object : PointerGestures.Listener {
override fun onMove(x: Float, y: Float) {}
override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) {}
override fun onWheel(steps: Int) {}
override fun onLongPress() {}
})
field("onScreenControls", OnScreenControls(activity, null).apply { visibility = View.GONE })
field("steamTouchControls", steam)
field("steamTouchOptions", SteamTouchConfig.Options(inputMode = SteamTouchConfig.INPUT_MOUSE, mouseMode = SteamTouchConfig.MOUSE_ABSOLUTE, trackpadSensitivity = 2f))
field("touchpad", touchpad)
field("routedTouchMode", SessionPrefs.TOUCH_DIRECT)
}
@After fun tearDown() {
SteamTouchDevice.release()
directory.deleteRecursively()
SessionState.mode = SessionService.MODE_STEAM
}
@Test fun disablingSteamTouchRestoresEverySavedModeEvenWhenRegularControlsAreAlreadyHidden() {
for (mode in listOf(SessionPrefs.TOUCH_PAD, SessionPrefs.TOUCH_DIRECT, SessionPrefs.TOUCH_AUTO, SessionPrefs.TOUCH_OFF)) {
SessionPrefs.setTouchMode(activity, mode)
SessionPrefs.setOscMode(activity, SessionPrefs.OSC_NEVER)
steam.visibility = View.VISIBLE
invoke("readPrefs")
assertEquals(SessionPrefs.TOUCH_DIRECT, ReflectionHelpers.getField<String>(activity, "routedTouchMode"))
invoke("updateOnScreenControls")
assertEquals(View.GONE, steam.visibility)
assertEquals(mode, ReflectionHelpers.getField<String>(activity, "routedTouchMode"))
assertEquals(1.15f, touchpad.sensitivity, 0f)
}
}
private fun field(name: String, value: Any) = ReflectionHelpers.setField(activity, name, value)
private fun invoke(name: String) = ReflectionHelpers.callInstanceMethod<Any?>(activity, name)
}