feat: add configurable touchscreen gestures (#940)

This commit is contained in:
Grimarius authored and GitHub committed 2026-10-20 03:22:41 +00:00
1 parent fd74819c50
commit fe7b75318a
55 files changed
+7404 -14

No files matched your search

@@ -0,0 +1,36 @@
package com.droiddeck.launcher.input
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class GesturePrefsTest {
@Test fun removedOptionsAreIgnoredWhenLoadingExistingSettings() {
val old = """{"holdMouseButtonWhileTouchingEnabled":true,"holdMouseButtonWhileTouchingAction":"right_click",
"showClickHighlight":true,"showCursorInTouchscreenMode":true,"tapAction":"key_E","longPressDelay":650}"""
val config = TouchGestureConfig.fromJson(old)
assertEquals("key_E", config.tapAction)
assertEquals(650, config.longPressDelay)
assertFalse(config.toJson().contains("holdMouseButtonWhileTouching"))
assertFalse(config.toJson().contains("showClickHighlight"))
assertFalse(config.toJson().contains("showCursorInTouchscreenMode"))
}
@Test fun gameOverridesFallBackToDefaultsAndSurviveSerialization() {
val context = RuntimeEnvironment.getApplication()
val defaults = TouchGestureConfig(tapAction = "key_E", mouseDragMovementMode = "relative")
GesturePrefs.save(context, null, defaults)
assertEquals(defaults, GesturePrefs.read(context, "123"))
val game = defaults.copy(twoFingerTapAction = "show_keyboard", longPressDelay = 650)
GesturePrefs.save(context, "123", game)
assertEquals(game, GesturePrefs.read(context, "123"))
assertEquals(defaults, GesturePrefs.read(context, "456"))
GesturePrefs.clear(context, "123")
assertEquals(defaults, GesturePrefs.read(context, "123"))
}
}
@@ -0,0 +1,61 @@
package com.droiddeck.launcher.input
import android.os.Looper
import android.os.SystemClock
import android.view.Gravity
import android.view.InputDevice
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.FrameLayout
import java.time.Duration
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class GestureRoutingTest {
@Test fun controlFingerDoesNotCountTowardGestureWhenControlIsTouchedFirst() = checkOrder(controlFirst = true)
@Test fun controlFingerDoesNotCountTowardGestureWhenScreenIsTouchedFirst() = checkOrder(controlFirst = false)
private fun checkOrder(controlFirst: Boolean) {
val context = RuntimeEnvironment.getApplication()
val buttons = mutableListOf<Pair<Int, Boolean>>()
val gestures = TouchscreenGestures(ViewConfiguration.get(context).scaledTouchSlop.toFloat(), object : TouchscreenGestures.Output {
override fun moveTo(x: Float, y: Float) {}
override fun moveBy(dx: Float, dy: Float, relative: Boolean) {}
override fun button(code: Int, down: Boolean) { buttons += code to down }
override fun key(code: Int, down: Boolean) {}
override fun wheel(steps: Int) {}
override fun showKeyboard() {}
})
val root = FrameLayout(context).apply { isMotionEventSplittingEnabled = true }
root.addView(View(context).apply { setOnTouchListener { _, e -> gestures.onTouch(e) } }, FrameLayout.LayoutParams(1000, 600))
root.addView(View(context).apply { setOnTouchListener { _, _ -> true } }, FrameLayout.LayoutParams(200, 600, Gravity.RIGHT))
root.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY))
root.layout(0, 0, 1000, 600)
val firstX = if (controlFirst) 900f else 400f
val secondX = if (controlFirst) 400f else 900f
val downTime = SystemClock.uptimeMillis()
fun send(action: Int, index: Int, vararg points: Pair<Int, Float>) {
val properties = points.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER } }.toTypedArray()
val coords = points.map { (_, px) -> MotionEvent.PointerCoords().apply { x = px; y = 300f; pressure = 1f; size = 1f } }.toTypedArray()
MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), action or (index shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), points.size,
properties, coords, 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0).also {
root.dispatchTouchEvent(it); it.recycle()
}
}
send(MotionEvent.ACTION_DOWN, 0, 3 to firstX)
send(MotionEvent.ACTION_POINTER_DOWN, 1, 3 to firstX, 8 to secondX)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(30))
send(MotionEvent.ACTION_POINTER_UP, 1, 3 to firstX, 8 to secondX)
send(MotionEvent.ACTION_UP, 0, 3 to firstX)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(100))
assertEquals(listOf(272 to true, 272 to false), buttons)
}
}
@@ -0,0 +1,266 @@
package com.droiddeck.launcher.input
import android.os.Looper
import android.os.SystemClock
import android.content.res.Configuration
import android.view.InputDevice
import android.view.MotionEvent
import android.view.SurfaceView
import com.droiddeck.launcher.SessionActivity
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SessionService
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.wayland.WaylandCompositor
import java.time.Duration
import org.junit.After
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.Implementation
import org.robolectric.annotation.Implements
import org.robolectric.util.ReflectionHelpers
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE, shadows = [SessionGestureRoutingTest.CompositorShadow::class])
class SessionGestureRoutingTest {
private lateinit var activity: SessionActivity
private lateinit var gestures: TouchscreenGestures
private lateinit var steamTap: ThreeFingerTap
private lateinit var launcherScroll: ScrollTouchGestures
private lateinit var directHold: DirectTouchHold
private val keys = mutableListOf<Pair<Int, Boolean>>()
private val directButtons = mutableListOf<Pair<Int, Boolean>>()
private val scrollPositions = mutableListOf<Pair<Float, Float>>()
private val scrollDeltas = mutableListOf<Pair<Float, Float>>()
private var keyboards = 0
@After fun tearDown() {
if (::gestures.isInitialized) gestures.cancel()
if (::launcherScroll.isInitialized) launcherScroll.cancel()
if (::directHold.isInitialized) directHold.cancel()
SessionState.mode = SessionService.MODE_STEAM
SessionState.steamUi = null
SessionState.suspended = false
}
@Test fun configuredThreeFingerActionTakesPrecedenceOverSteamShortcut() {
prepare(SessionPrefs.TOUCH_GESTURES, TouchGestureConfig(threeFingerTapAction = "key_E"))
tapThreeFingers(expectSteamGesture = false)
assertEquals(listOf(18 to true, 18 to false), keys)
assertEquals(0, keyboards)
}
@Test fun disablingThreeFingerTapDoesNotFallBackToSteamShortcut() {
prepare(SessionPrefs.TOUCH_GESTURES, TouchGestureConfig(threeFingerTapEnabled = false))
tapThreeFingers(expectSteamGesture = false)
assertTrue(keys.isEmpty())
assertEquals(0, keyboards)
}
@Test fun defaultThreeFingerActionOpensPcKeyboardOnce() {
prepare(SessionPrefs.TOUCH_GESTURES)
tapThreeFingers(expectSteamGesture = false)
assertEquals(1, keyboards)
assertTrue(keys.isEmpty())
}
@Test fun gestureModeKeepsItsBindingsWhileLauncherMouseIsAvailable() {
prepare(SessionPrefs.TOUCH_GESTURES, TouchGestureConfig(threeFingerTapAction = "key_E"))
setField("padMouseOn", true)
tapThreeFingers(expectSteamGesture = false)
assertEquals(listOf(18 to true, 18 to false), keys)
assertTrue(scrollPositions.isEmpty())
assertTrue(scrollDeltas.isEmpty())
}
@Test fun directModeStillRoutesLauncherTouchesToScrolling() {
prepare(SessionPrefs.TOUCH_DIRECT)
setField("padMouseOn", true)
val down = SystemClock.uptimeMillis()
for ((index, action) in listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP).withIndex()) {
val event = MotionEvent.obtain(down, down + index * 32L, action, 400f, if (index == 0) 300f else 200f, 0)
try { assertTrue(activity.onTouchEvent(event)) } finally { event.recycle() }
}
assertEquals(listOf(400f to 300f), scrollPositions)
assertTrue(scrollDeltas.isNotEmpty())
assertTrue(keys.isEmpty())
assertFalse(steamTap.active)
}
@Test fun upstreamSteamShortcutStillReceivesTouchesInLegacyModes() {
for (mode in listOf(SessionPrefs.TOUCH_DIRECT, SessionPrefs.TOUCH_PAD)) {
prepare(mode)
tapThreeFingers(expectSteamGesture = true)
assertTrue(keys.isEmpty())
assertEquals(0, keyboards)
}
}
@Test fun configurationChangeReleasesHeldGestureAndResetsPointerAnchor() {
startLongPress()
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(350))
assertEquals(listOf(18 to true), keys)
activity.onConfigurationChanged(Configuration(activity.resources.configuration).apply {
orientation = Configuration.ORIENTATION_PORTRAIT
})
assertEquals(listOf(18 to true, 18 to false), keys)
assertFalse(SessionActivity::class.java.getDeclaredField("gesturePointerReady").apply { isAccessible = true }.getBoolean(activity))
}
@Test fun surfaceResizeCancelsPendingHoldAndResetsPointerAnchor() {
startLongPress()
setField("surfaceW", 1000)
setField("surfaceH", 600)
val window = android.view.Surface(android.graphics.SurfaceTexture(0))
var activeHolder: android.view.SurfaceHolder? = null
val surface = object : SurfaceView(RuntimeEnvironment.getApplication()) {
override fun getHolder() = activeHolder ?: super.getHolder()
}.apply { layout(0, 0, 1000, 600) }
activeHolder = object : android.view.SurfaceHolder by surface.holder {
override fun getSurface() = window
}
setField("surfaceView", surface)
try { activity.surfaceChanged(surface.holder, 0, 800, 600) } finally { window.release() }
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(350))
assertTrue(keys.isEmpty())
assertFalse(SessionActivity::class.java.getDeclaredField("gesturePointerReady").apply { isAccessible = true }.getBoolean(activity))
}
@Test fun unchangedSurfaceDimensionsDoNotInterruptPendingHold() {
startLongPress()
setField("surfaceW", 1000)
setField("surfaceH", 600)
val surface = SessionActivity::class.java.getDeclaredField("surfaceView").apply { isAccessible = true }.get(activity) as SurfaceView
activity.surfaceChanged(surface.holder, 0, 1000, 600)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(350))
assertEquals(listOf(18 to true), keys)
}
@Test fun inactiveSurfaceDestructionDoesNotInterruptCurrentGesture() {
startLongPress()
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(350))
val staleSurface = SurfaceView(RuntimeEnvironment.getApplication())
activity.surfaceDestroyed(staleSurface.holder)
assertEquals(listOf(18 to true), keys)
assertTrue(SessionActivity::class.java.getDeclaredField("gesturePointerReady").apply { isAccessible = true }.getBoolean(activity))
}
@Test fun pausingCancelsPendingHoldsInBothTouchModes() {
for (mode in listOf(SessionPrefs.TOUCH_GESTURES, SessionPrefs.TOUCH_DIRECT)) {
prepare(mode, TouchGestureConfig(longPressEnabled = true, longPressAction = "key_E"))
val now = SystemClock.uptimeMillis()
val event = MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN, 400f, 300f, 0)
try { assertTrue(activity.onTouchEvent(event)) } finally { event.recycle() }
ReflectionHelpers.callInstanceMethod<Unit>(activity, "onPause")
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(700))
assertTrue(keys.isEmpty())
assertTrue(directButtons.isEmpty())
}
}
@Test fun pausingReleasesActiveHoldsInBothTouchModes() {
for (mode in listOf(SessionPrefs.TOUCH_GESTURES, SessionPrefs.TOUCH_DIRECT)) {
keys.clear()
directButtons.clear()
prepare(mode, TouchGestureConfig(longPressEnabled = true, longPressAction = "key_E"))
val now = SystemClock.uptimeMillis()
val event = MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN, 400f, 300f, 0)
try { assertTrue(activity.onTouchEvent(event)) } finally { event.recycle() }
val gestureMode = mode == SessionPrefs.TOUCH_GESTURES
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(if (gestureMode) 350 else 500))
assertEquals(if (gestureMode) listOf(18 to true) else emptyList(), keys)
assertEquals(if (gestureMode) emptyList() else listOf(PointerGestures.BTN_RIGHT to true), directButtons)
ReflectionHelpers.callInstanceMethod<Unit>(activity, "onPause")
assertEquals(if (gestureMode) listOf(18 to true, 18 to false) else emptyList(), keys)
assertEquals(if (gestureMode) emptyList() else listOf(PointerGestures.BTN_RIGHT to true, PointerGestures.BTN_RIGHT to false), directButtons)
}
}
private fun startLongPress() {
prepare(SessionPrefs.TOUCH_GESTURES, TouchGestureConfig(longPressEnabled = true, longPressAction = "key_E"))
val now = SystemClock.uptimeMillis()
MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN, 400f, 300f, 0).also {
try { assertTrue(activity.onTouchEvent(it)) } finally { it.recycle() }
}
setField("gesturePointerReady", true)
}
private fun prepare(mode: String, config: TouchGestureConfig = TouchGestureConfig()) {
SessionState.mode = SessionService.MODE_STEAM
SessionState.steamUi = "bigpicture"
SessionState.suspended = false
activity = Robolectric.buildActivity(SessionActivity::class.java).get()
steamTap = ThreeFingerTap(8f)
gestures = TouchscreenGestures(8f, object : TouchscreenGestures.Output {
override fun moveTo(x: Float, y: Float) {}
override fun moveBy(dx: Float, dy: Float, relative: Boolean) {}
override fun button(code: Int, down: Boolean) { fail("Unexpected mouse button: $code $down") }
override fun key(code: Int, down: Boolean) { keys += code to down }
override fun wheel(steps: Int) { fail("Unexpected wheel input") }
override fun showKeyboard() { keyboards++ }
}).also { it.configure(config) }
setField("routedTouchMode", mode)
setField("padMouseCheckedAt", SystemClock.uptimeMillis())
setField("threeFingerTap", steamTap)
setField("touchscreenGestures", gestures)
setField("surfaceView", SurfaceView(RuntimeEnvironment.getApplication()).apply { layout(0, 0, 1000, 600) })
setField("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() {}
}))
directHold = DirectTouchHold(8f, object : PointerGestures.Listener {
override fun onMove(x: Float, y: Float) {}
override fun onButton(button: Int, pressed: Boolean, x: Float, y: Float) { directButtons += button to pressed }
override fun onWheel(steps: Int) {}
override fun onLongPress() {}
}) {}
setField("directTouchHold", directHold)
launcherScroll = ScrollTouchGestures(8f, 50f, object : ScrollTouchGestures.Listener {
override fun onMove(x: Float, y: Float) { scrollPositions += x to y }
override fun onButton(button: Int, pressed: Boolean) {}
override fun onScroll(dx: Float, dy: Float) { scrollDeltas += dx to dy }
override fun onLongPress() {}
})
setField("scrollTouch", launcherScroll)
}
private fun tapThreeFingers(expectSteamGesture: Boolean) {
val down = SystemClock.uptimeMillis()
fun send(action: Int, index: Int, count: Int) {
val properties = Array(count) { MotionEvent.PointerProperties().apply { id = it; toolType = MotionEvent.TOOL_TYPE_FINGER } }
val coordinates = Array(count) { MotionEvent.PointerCoords().apply { x = 300f + it * 100; y = 300f; pressure = 1f } }
val event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action or (index shl MotionEvent.ACTION_POINTER_INDEX_SHIFT),
count, properties, coordinates, 0, 0, 1f, 1f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0)
try { assertTrue(activity.onTouchEvent(event)) } finally { event.recycle() }
}
send(MotionEvent.ACTION_DOWN, 0, 1)
send(MotionEvent.ACTION_POINTER_DOWN, 1, 2)
send(MotionEvent.ACTION_POINTER_DOWN, 2, 3)
assertEquals(expectSteamGesture, steamTap.active)
send(MotionEvent.ACTION_POINTER_UP, 2, 3)
send(MotionEvent.ACTION_POINTER_UP, 1, 2)
send(MotionEvent.ACTION_UP, 0, 1)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(200))
assertFalse(steamTap.active)
}
private fun setField(name: String, value: Any) {
SessionActivity::class.java.getDeclaredField(name).apply { isAccessible = true }.set(activity, value)
}
@Implements(value = WaylandCompositor::class, isInAndroidSdk = false)
class CompositorShadow {
companion object {
@JvmStatic @Implementation fun __staticInitializer__() {}
@JvmStatic @Implementation fun nativeSendTouch(action: Int, id: Int, x: Int, y: Int) {}
@JvmStatic @Implementation fun nativeSendSceneInput(type: Int, code: Int, value: Int) {}
}
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,37 @@
package com.droiddeck.launcher.ui
import org.junit.Assert.*
import org.junit.Test
class GestureActionsTest {
@Test fun categoryActionsAreAlphabeticalIncludingFilteredResults() {
val actions = listOf("Z", "C", "A", "B").map { GestureAction("key_$it", it, GestureActionGroup.LETTERS) } +
listOf("Right click", "Middle click", "Left click").map { GestureAction(it, it, GestureActionGroup.MOUSE) }
assertEquals(listOf("A", "B", "C", "Z"), filterGestureActions(actions, "", GestureActionGroup.LETTERS).map { it.title })
assertEquals(listOf("Left click", "Middle click", "Right click"), filterGestureActions(actions, "click", null).map { it.title })
}
@Test fun numberedFunctionKeysStayInNumericOrder() {
val actions = listOf(12, 2, 10, 1).map { GestureAction("key_F$it", "F$it", GestureActionGroup.FUNCTION) }
assertEquals(listOf("F1", "F2", "F10", "F12"), filterGestureActions(actions, "", null).map { it.title })
}
@Test fun actionGroupsKeepKeysAndMouseBindingsInTheirOwnCategories() {
assertEquals(GestureActionGroup.MOUSE, gestureActionGroup("middle_mouse_pan"))
assertEquals(GestureActionGroup.LETTERS, gestureActionGroup("key_F"))
assertEquals(GestureActionGroup.FUNCTION, gestureActionGroup("key_F12"))
assertEquals(GestureActionGroup.NUMPAD, gestureActionGroup("key_KP_ENTER"))
assertEquals(GestureActionGroup.MODIFIERS, gestureActionGroup("key_CTRL_R"))
}
@Test fun searchCombinesWordsAndCategoryWithoutChangingTheSelection() {
val actions = listOf(
GestureAction("key_ENTER", "Enter", GestureActionGroup.NAVIGATION),
GestureAction("key_KP_ENTER", "Numpad Enter", GestureActionGroup.NUMPAD),
GestureAction("key_KP_1", "Numpad 1", GestureActionGroup.NUMPAD),
)
assertEquals(listOf(actions[1]), filterGestureActions(actions, " ENTER numpad ", null))
assertEquals(listOf(actions[1]), filterGestureActions(actions, "enter", GestureActionGroup.NUMPAD))
assertTrue(filterGestureActions(actions, "not a key", null).isEmpty())
}
}
@@ -0,0 +1,90 @@
package com.droiddeck.launcher.ui
import android.app.Application
import android.view.View
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.test.*
import androidx.compose.ui.unit.Density
import com.droiddeck.launcher.R
import com.droiddeck.launcher.input.GesturePrefs
import com.droiddeck.launcher.input.TouchGestureConfig
import com.droiddeck.launcher.input.TouchscreenGestures.Gesture
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class, qualifiers = "w400dp-h850dp-port")
class GestureLocalizationTest {
@get:Rule val compose = createRobolectricComposeRule()
private val context get() = RuntimeEnvironment.getApplication()
@Before fun clearPrefs() {
context.getSharedPreferences("touch_gestures", 0).edit().clear().commit()
}
@Test
@Config(qualifiers = "fr-rFR-w400dp-h850dp-port")
fun pageKeysAndModifierNamesUseLocalizedResources() {
compose.setContent {
DroidDeckTheme {
Column {
Text(gestureActionTitle("page_up_down"))
Text(gestureActionTitle("key_SHIFT_L"))
}
}
}
compose.onNodeWithText("Page précédente / Page suivante").assertIsDisplayed()
compose.onNodeWithText("Maj gauche").assertIsDisplayed()
}
@Test
@Config(qualifiers = "de-rDE-w400dp-h850dp-port")
fun longTranslationsKeepDefaultRestorationAndSaveUsable() {
GesturePrefs.save(context, "123", TouchGestureConfig(tapAction = "key_E"))
compose.setContent {
val density = LocalDensity.current
CompositionLocalProvider(LocalDensity provides Density(density.density, fontScale = 1.5f)) {
DroidDeckTheme { TouchGestureDialog("123", {}, {}) }
}
}
val restore = compose.onNodeWithTag("gesture-inherit")
restore.assertIsDisplayed()
val scope = compose.onNodeWithTag("gesture-scope-game").fetchSemanticsNode().boundsInRoot
assertTrue("The restore action must have room for its translated label", restore.fetchSemanticsNode().boundsInRoot.width > scope.width)
restore.performClick()
compose.onNodeWithTag("gesture-save").assertIsDisplayed().performClick()
compose.runOnIdle { assertFalse(GesturePrefs.hasOverride(context, "123")) }
}
@Test
@Config(qualifiers = "ar-rEG-w400dp-h850dp-port")
fun arabicPickerSearchPreservesBindingsAndMirrorsFooter() {
assertEquals(View.LAYOUT_DIRECTION_RTL, context.resources.configuration.layoutDirection)
compose.setContent { DroidDeckTheme { TouchGestureDialog(null, {}, {}) } }
compose.onNodeWithText("إعدادات إيماءات اللمس").assertIsDisplayed()
val save = compose.onNodeWithTag("gesture-save").fetchSemanticsNode().boundsInRoot
val cancel = compose.onNodeWithText(context.getString(R.string.gesture_cancel)).fetchSemanticsNode().boundsInRoot
assertTrue("Save $save and Cancel $cancel should follow the right-to-left layout", save.left < cancel.left)
compose.onNodeWithTag("gesture-choice-tap").performScrollTo().performClick()
compose.onNodeWithTag("gesture-action-search").performTextInput("هروب")
compose.onNodeWithTag("gesture-action-key_ESC").assertIsDisplayed().performClick()
compose.onNodeWithTag("gesture-save").performClick()
compose.runOnIdle { assertEquals("key_ESC", GesturePrefs.read(context).tapAction) }
}
@Test
@Config(qualifiers = "ja-rJP-w400dp-h850dp-port")
fun recognizedGestureFeedbackUsesTheActiveLanguage() {
compose.setContent { DroidDeckTheme { GestureFeedback(Gesture.TWO_FINGER_HOLD) } }
compose.onNodeWithText("2本指で長押し").assertIsDisplayed()
}
}
@@ -0,0 +1,112 @@
package com.droiddeck.launcher.ui
import android.app.Application
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.test.*
import com.droiddeck.launcher.input.GesturePrefs
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class, qualifiers = "w960dp-h640dp-land")
class TouchGestureDialogTest {
@get:Rule val compose = createRobolectricComposeRule()
private val context get() = RuntimeEnvironment.getApplication()
@Before fun clearPrefs() {
context.getSharedPreferences("touch_gestures", 0).edit().clear().commit()
}
@Test fun searchSelectAndSaveAKeyboardAction() {
compose.setContent { DroidDeckTheme { TouchGestureDialog(null, {}, {}) } }
compose.onNodeWithTag("gesture-choice-tap").performScrollTo().performClick()
compose.onNodeWithTag("gesture-action-left_click").assertIsSelected()
compose.onNodeWithTag("gesture-action-search").performTextInput("numpad enter")
compose.onNodeWithTag("gesture-action-key_KP_ENTER").assertIsDisplayed().performClick()
compose.onNodeWithTag("gesture-save").performClick()
compose.runOnIdle { assertEquals("key_KP_ENTER", GesturePrefs.read(context).tapAction) }
}
@Test fun switchingTabsKeepsEditsAndLetsTwoFingerPanBeChanged() {
compose.setContent { DroidDeckTheme { TouchGestureDialog(null, {}, {}) } }
compose.onNodeWithTag("gesture-tab-1").performClick()
compose.onNodeWithTag("gesture-choice-two-drag").performScrollTo().performClick()
compose.onNodeWithTag("gesture-action-search").performTextInput("middle mouse pan")
compose.onNodeWithTag("gesture-action-middle_mouse_pan").performClick()
compose.onNodeWithTag("gesture-tab-3").performClick()
compose.onNodeWithTag("gesture-tab-1").performClick()
compose.onNodeWithText("Middle mouse pan").performScrollTo().assertIsDisplayed()
compose.onNodeWithTag("gesture-save").performClick()
compose.runOnIdle { assertEquals("middle_mouse_pan", GesturePrefs.read(context).twoFingerDragAction) }
}
@Test fun closingWithoutSavingDiscardsTheDraft() {
val open = mutableStateOf(true)
compose.setContent { DroidDeckTheme { if (open.value) TouchGestureDialog(null, { open.value = false }, {}) } }
chooseTap("Escape", "key_ESC")
compose.onNodeWithContentDescription("Close").performClick()
compose.runOnIdle { assertEquals("left_click", GesturePrefs.read(context).tapAction) }
}
@Test fun defaultsAndGameDraftsSurviveScopeChangesAndSaveTogether() {
compose.setContent { DroidDeckTheme { TouchGestureDialog("123", {}, {}) } }
chooseTap("numpad enter", "key_KP_ENTER")
compose.onNodeWithTag("gesture-scope-default").performClick()
chooseTap("Escape", "key_ESC")
compose.onNodeWithTag("gesture-scope-game").performClick()
compose.onNodeWithTag("gesture-choice-tap").performScrollTo()
compose.onNodeWithText("Numpad Enter").assertIsDisplayed()
compose.onNodeWithTag("gesture-save").performClick()
compose.runOnIdle {
assertEquals("key_ESC", GesturePrefs.read(context).tapAction)
assertEquals("key_KP_ENTER", GesturePrefs.read(context, "123").tapAction)
}
}
@Test fun aFocusChangeCannotRetargetAnOpenGameEditor() {
val game = mutableStateOf("123")
compose.setContent { DroidDeckTheme { TouchGestureDialog(game.value, {}, {}) } }
chooseTap("Escape", "key_ESC")
compose.runOnIdle {
game.value = "456"
Snapshot.sendApplyNotifications()
}
compose.onNodeWithTag("gesture-save").performClick()
compose.runOnIdle {
assertEquals("key_ESC", GesturePrefs.read(context, "123").tapAction)
assertFalse(GesturePrefs.hasOverride(context, "456"))
}
}
@Test
@Config(qualifiers = "w400dp-h850dp-port")
fun aNarrowWindowWithLargeTextKeepsNavigationAndSaveReachable() {
compose.setContent {
val density = LocalDensity.current
CompositionLocalProvider(LocalDensity provides Density(density.density, fontScale = 1.5f)) {
DroidDeckTheme { TouchGestureDialog("123", {}, {}) }
}
}
compose.onNodeWithContentDescription("Close").assertIsDisplayed()
compose.onNodeWithTag("gesture-save").assertIsDisplayed()
compose.onNodeWithTag("gesture-tab-3").performScrollTo().performClick()
compose.onNodeWithText("Mouse movement").performScrollTo().assertIsDisplayed()
compose.onNodeWithTag("gesture-save").assertIsDisplayed()
}
private fun chooseTap(search: String, id: String) {
compose.onNodeWithTag("gesture-choice-tap").performScrollTo().performClick()
compose.onNodeWithTag("gesture-action-search").performTextInput(search)
compose.onNodeWithTag("gesture-action-$id").performClick()
}
}