mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
fix: a touchpad tap holds its click long enough for games to see it (#644)
This commit is contained in:
1 parent
ba6f56d5a0
commit
1b4d08e448
2 files changed
+161
-6
No files matched your search
@@ -0,0 +1,140 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.graphics.RectF
|
||||
import android.os.Looper
|
||||
import android.view.MotionEvent
|
||||
import java.time.Duration
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
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 TouchpadGesturesTest {
|
||||
private val events = mutableListOf<String>()
|
||||
private lateinit var touchpad: TouchpadGestures
|
||||
private var time = 1000L
|
||||
private var downTime = 0L
|
||||
|
||||
private val listener = object : PointerGestures.Listener {
|
||||
override fun onMove(x: Float, y: Float) { events += "move" }
|
||||
override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) {
|
||||
val name = if (button == PointerGestures.BTN_RIGHT) "right" else "left"
|
||||
events += "$name ${if (pressed) "down" else "up"}"
|
||||
}
|
||||
override fun onWheel(steps: Int) { events += "wheel $steps" }
|
||||
override fun onLongPress() { events += "long press" }
|
||||
}
|
||||
|
||||
@Before fun setUp() {
|
||||
touchpad = TouchpadGestures(SLOP, listener)
|
||||
touchpad.bounds = RectF(0f, 0f, 1920f, 1080f)
|
||||
touchpad.place(960f, 540f)
|
||||
}
|
||||
|
||||
@Test fun tapHoldsTheLeftButtonBeforeReleasingIt() {
|
||||
tap()
|
||||
assertEquals(listOf("left down"), events)
|
||||
advance(40)
|
||||
assertEquals(listOf("left down"), events)
|
||||
advance(100)
|
||||
assertEquals(listOf("left down", "left up"), events)
|
||||
}
|
||||
|
||||
@Test fun twoFingerTapHoldsTheRightButtonBeforeReleasingIt() {
|
||||
touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f))
|
||||
advance(10)
|
||||
touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 310f))
|
||||
advance(60)
|
||||
touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 310f))
|
||||
advance(10)
|
||||
touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f))
|
||||
assertEquals(listOf("right down"), events)
|
||||
advance(30)
|
||||
assertEquals(listOf("right down"), events)
|
||||
advance(100)
|
||||
assertEquals(listOf("right down", "right up"), events)
|
||||
}
|
||||
|
||||
@Test fun slowTwoFingerTapHoldsTheRightButtonBeforeReleasingIt() {
|
||||
touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f))
|
||||
advance(10)
|
||||
touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 310f))
|
||||
advance(350)
|
||||
touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 310f))
|
||||
advance(10)
|
||||
touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f))
|
||||
assertEquals(listOf("right down"), events)
|
||||
advance(40)
|
||||
assertEquals(listOf("right down"), events)
|
||||
advance(100)
|
||||
assertEquals(listOf("right down", "right up"), events)
|
||||
}
|
||||
|
||||
@Test fun secondTapPressesOnlyAfterTheFirstClickIsReleased() {
|
||||
tap()
|
||||
advance(20)
|
||||
touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f))
|
||||
assertEquals(listOf("left down", "left up", "left down"), events)
|
||||
advance(80)
|
||||
touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f))
|
||||
advance(200)
|
||||
assertEquals(listOf("left down", "left up", "left down", "left up"), events)
|
||||
}
|
||||
|
||||
@Test fun secondFingerTappingAgainReleasesTheFirstRightClickFirst() {
|
||||
touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f))
|
||||
advance(10)
|
||||
touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 310f))
|
||||
advance(60)
|
||||
touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 310f))
|
||||
advance(10)
|
||||
touch(MotionEvent.ACTION_POINTER_DOWN, 1, 1 to (400f to 300f), 2 to (600f to 310f))
|
||||
advance(20)
|
||||
touch(MotionEvent.ACTION_POINTER_UP, 1, 1 to (400f to 300f), 2 to (600f to 310f))
|
||||
assertEquals(listOf("right down", "right up", "right down"), events)
|
||||
advance(200)
|
||||
assertEquals(listOf("right down", "right up", "right down", "right up"), events)
|
||||
}
|
||||
|
||||
@Test fun cancelReleasesAClickThatIsStillHeld() {
|
||||
tap()
|
||||
touchpad.cancel()
|
||||
assertEquals(listOf("left down", "left up"), events)
|
||||
advance(200)
|
||||
assertEquals(listOf("left down", "left up"), events)
|
||||
}
|
||||
|
||||
private fun tap() {
|
||||
touch(MotionEvent.ACTION_DOWN, 0, 1 to (400f to 300f))
|
||||
advance(80)
|
||||
touch(MotionEvent.ACTION_UP, 0, 1 to (400f to 300f))
|
||||
}
|
||||
|
||||
private fun advance(ms: Long) {
|
||||
time += ms
|
||||
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(ms))
|
||||
}
|
||||
|
||||
private fun touch(action: Int, index: Int, vararg pointers: Pair<Int, Pair<Float, Float>>) {
|
||||
if (action == MotionEvent.ACTION_DOWN) downTime = time
|
||||
val properties = pointers.map { (id, _) ->
|
||||
MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER }
|
||||
}.toTypedArray()
|
||||
val coords = pointers.map { (_, at) ->
|
||||
MotionEvent.PointerCoords().apply { x = at.first; y = at.second; pressure = 1f; size = 1f }
|
||||
}.toTypedArray()
|
||||
val masked = action or (index shl MotionEvent.ACTION_POINTER_INDEX_SHIFT)
|
||||
val event = MotionEvent.obtain(downTime, time, masked, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0)
|
||||
touchpad.onTouch(event)
|
||||
event.recycle()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val SLOP = 8f
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user