feat: Add physical controller button shortcuts (#1202)

* feat(controller): add physical button shortcuts

* fix(controller): improve shortcut capture and assignment feedback

* refactor(controller): simplify menu button assignment UI

* feat(controller): save menu assignments on button release

* refactor(controller): remove obsolete shortcut UI resources

* fix(controller): place drawer remaps below Back

* refactor(controller): simplify shortcut capture wording

* fix(controller): refresh active shortcut bindings on changes

* fix(controller): preserve drawer confirmation for mapped A

* fix: preserve controller shortcut navigation and cancel

* test: align shortcut dialog assertions

---------

Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-10-30 07:08:59 -04:00
1 parent f69e2d0171
commit 4770d11e39
42 files changed
+1316

No files matched your search

@@ -0,0 +1,254 @@
package com.droiddeck.launcher.input
import android.content.Context
import android.hardware.input.InputManager
import android.view.InputDevice
import android.view.KeyCharacterMap
import android.view.KeyEvent
import com.droiddeck.launcher.input.PhysicalShortcuts.Action
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Before
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
import org.robolectric.shadows.InputDeviceBuilder
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], manifest = Config.NONE, shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class])
class PhysicalShortcutsTest {
private val context: Context get() = RuntimeEnvironment.getApplication()
private val router = PhysicalShortcutRouter()
private val actions = mutableListOf<Action>()
private lateinit var pad: InputDevice
@Before fun setUp() {
ControllerPrefs.prefs(context).edit().clear().commit()
pad = device(10, "retroid")
}
@Test fun defaultsDoNotInterceptAnyButton() {
assertTrue(PhysicalShortcuts.read(context).isEmpty())
for (code in listOf(KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_BUTTON_MODE,
KeyEvent.KEYCODE_BUTTON_SELECT, KeyEvent.KEYCODE_BUTTON_1)) {
assertFalse(route(key(code)))
assertFalse(route(key(code, KeyEvent.ACTION_UP)))
}
assertTrue(actions.isEmpty())
}
@Test fun assignmentPersistsAndConsumesTheWholePressWithoutRepeating() {
assign(key(), Action.STEAM)
assertEquals(listOf(PhysicalShortcuts.Binding(button(key()), Action.STEAM)), PhysicalShortcuts.read(context))
assertTrue(route(key()))
assertTrue(route(key(repeat = 1)))
assertTrue(route(key(repeat = 2)))
assertTrue(route(key(action = KeyEvent.ACTION_UP)))
assertEquals(listOf(Action.STEAM), actions)
assertFalse(route(key(action = KeyEvent.ACTION_UP)))
}
@Test fun releaseIsConsumedEvenIfAnActionChangedTheRoutingContextOrSettings() {
assign(key(), Action.DROIDDECK)
assertTrue(route(key()))
router.bindings = emptyList()
assertTrue(route(key(action = KeyEvent.ACTION_UP), enabled = false, steam = false))
assertEquals(listOf(Action.DROIDDECK), actions)
}
@Test fun aRepeatedDownWithoutAnInitialPressDoesNotTriggerAnAction() {
assign(key(), Action.QAM)
assertTrue(route(key(repeat = 1)))
assertTrue(route(key(action = KeyEvent.ACTION_UP)))
assertTrue(actions.isEmpty())
}
@Test fun disabledMappedConfirmLeavesTheWholePressForDrawerNavigation() {
assign(key(KeyEvent.KEYCODE_BUTTON_A), Action.DROIDDECK)
assertFalse(route(key(KeyEvent.KEYCODE_BUTTON_A), enabled = false))
assertFalse(route(key(KeyEvent.KEYCODE_BUTTON_A, repeat = 1), enabled = false))
assertFalse(route(key(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_UP), enabled = false))
assertTrue(actions.isEmpty())
}
@Test fun mappedConfirmOpeningTheDrawerConsumesItsReleaseWithoutStayingHeld() {
assign(key(KeyEvent.KEYCODE_BUTTON_A), Action.DROIDDECK)
assertTrue(route(key(KeyEvent.KEYCODE_BUTTON_A)))
assertTrue(route(key(KeyEvent.KEYCODE_BUTTON_A, repeat = 1), enabled = false))
assertTrue(route(key(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_UP), enabled = false))
assertFalse(route(key(KeyEvent.KEYCODE_BUTTON_A), enabled = false))
assertFalse(route(key(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_UP), enabled = false))
assertTrue(route(key(KeyEvent.KEYCODE_BUTTON_A)))
assertTrue(route(key(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_UP)))
assertEquals(listOf(Action.DROIDDECK, Action.DROIDDECK), actions)
}
@Test fun aResumePressKeepsItsRepeatsAndReleaseInTheResumePath() {
assign(key(), Action.STEAM)
assertFalse(route(key(), enabled = false))
assertFalse(route(key(repeat = 1), enabled = false))
assertFalse(route(key(action = KeyEvent.ACTION_UP), enabled = false))
assertTrue(actions.isEmpty())
assertTrue(route(key()))
assertEquals(listOf(Action.STEAM), actions)
}
@Test fun steamActionsAreInactiveOutsideSteamAndAllActionsRespectDisabledContexts() {
for (action in Action.entries) {
assign(key(), action)
assertFalse(route(key(), enabled = false))
assertFalse(route(key(action = KeyEvent.ACTION_UP), enabled = false))
assertEquals(action == Action.DROIDDECK, route(key(), steam = false))
route(key(action = KeyEvent.ACTION_UP), steam = false)
}
assertEquals(listOf(Action.DROIDDECK), actions)
}
@Test fun reassigningAButtonReplacesItsPreviousAction() {
assign(key(), Action.STEAM)
assign(key(), Action.QAM)
assertEquals(listOf(Action.QAM), PhysicalShortcuts.read(context).map { it.action })
assertTrue(route(key()))
assertEquals(listOf(Action.QAM), actions)
}
@Test fun reassigningAnActionOnlyReplacesThatControllersBinding() {
val second = device(11, "bluetooth")
assign(key(), Action.STEAM)
assign(key(device = second), Action.STEAM)
assign(key(KeyEvent.KEYCODE_BUTTON_2), Action.STEAM)
val saved = PhysicalShortcuts.read(context)
assertEquals(2, saved.size)
assertTrue(saved.any { it.button.sameInput(button(key(device = second))) })
assertTrue(saved.any { it.button.sameInput(button(key(KeyEvent.KEYCODE_BUTTON_2))) })
assertFalse(route(key()))
}
@Test fun deviceIdentitySurvivesReconnectionButDoesNotMatchAnUnrelatedController() {
assign(key(), Action.QAM)
assertFalse(route(key(device = device(11, "other"))))
val reconnected = device(12, "retroid")
assertTrue(route(key(device = reconnected)))
assertEquals(listOf(Action.QAM), actions)
}
@Test fun scanCodesDistinguishButtonsSharingAnAndroidKeyCode() {
assign(key(scan = 700), Action.STEAM)
assign(key(scan = 701), Action.QAM)
assertEquals(2, PhysicalShortcuts.read(context).size)
assertTrue(route(key(scan = 700)))
assertTrue(route(key(scan = 701)))
assertFalse(route(key(scan = 702)))
assertEquals(listOf(Action.STEAM, Action.QAM), actions)
}
@Test fun keyboardLikeExtraButtonsCanBeExplicitlyAssignedWithoutChangingKeyboardDefaults() {
val keyboard = device(11, "m1-keyboard", InputDevice.SOURCE_KEYBOARD, InputDevice.KEYBOARD_TYPE_ALPHABETIC)
val m1 = key(KeyEvent.KEYCODE_F1, device = keyboard)
assertFalse(PadBridge.isControllerKeyEvent(m1))
assign(m1, Action.STEAM)
assertTrue(route(m1))
assertFalse(route(key(KeyEvent.KEYCODE_F2, device = keyboard)))
assertEquals(listOf(Action.STEAM), actions)
}
@Test fun virtualAndSystemKeysCannotBeAssigned() {
val virtual = device(-1, "virtual")
assertNull(PhysicalShortcuts.button(key(device = virtual)))
for (code in listOf(KeyEvent.KEYCODE_HOME, KeyEvent.KEYCODE_POWER, KeyEvent.KEYCODE_APP_SWITCH,
KeyEvent.KEYCODE_SLEEP, KeyEvent.KEYCODE_WAKEUP, KeyEvent.KEYCODE_VOLUME_UP,
KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE)) {
assertNull(PhysicalShortcuts.button(key(code)))
}
assertNull(PhysicalShortcuts.button(key(KeyEvent.KEYCODE_UNKNOWN, scan = 0)))
assertNotNull(PhysicalShortcuts.button(key(KeyEvent.KEYCODE_UNKNOWN, scan = 700)))
}
@Test fun drawerDirectionalKeysRemainAvailableForNavigation() {
for (code in listOf(KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN,
KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT)) {
assertTrue(PhysicalShortcuts.isDrawerNavigationKey(key(code)))
}
assertFalse(PhysicalShortcuts.isDrawerNavigationKey(key(KeyEvent.KEYCODE_BUTTON_A)))
}
@Test fun resetAndRemovalPreserveOtherControllerSettings() {
ControllerPrefs.setSelectSteam(context, true)
ControllerPrefs.setTarget(context, "a", "b")
assign(key(), Action.STEAM)
assign(key(KeyEvent.KEYCODE_BUTTON_2), Action.QAM)
PhysicalShortcuts.remove(context, PhysicalShortcuts.read(context).first())
assertEquals(listOf(Action.QAM), PhysicalShortcuts.read(context).map { it.action })
PhysicalShortcuts.remove(context, PhysicalShortcuts.read(context).single())
assertTrue(PhysicalShortcuts.read(context).isEmpty())
assertTrue(ControllerPrefs.read(context).selectSteam)
assertEquals("b", ControllerPrefs.target(context, "a"))
assign(key(), Action.STEAM)
ControllerPrefs.resetAll(context)
assertTrue(PhysicalShortcuts.read(context).isEmpty())
}
@Test fun disconnectAndPauseClearHeldButtons() {
assign(key(), Action.QAM)
assertTrue(route(key()))
router.onDeviceRemoved(pad.id)
assertFalse(route(key(action = KeyEvent.ACTION_UP)))
assertTrue(route(key()))
router.clear()
assertFalse(route(key(action = KeyEvent.ACTION_UP)))
assertTrue(route(key()))
assertEquals(listOf(Action.QAM, Action.QAM, Action.QAM), actions)
}
@Test fun malformedPreferencesDoNotCrashInputDispatch() {
ControllerPrefs.prefs(context).edit().putString("physicalShortcuts", "invalid").commit()
assertTrue(PhysicalShortcuts.read(context).isEmpty())
}
@Test fun malformedEntriesDoNotDiscardValidAssignmentsWhenSaving() {
assign(key(), Action.STEAM)
assign(key(KeyEvent.KEYCODE_BUTTON_2), Action.QAM)
val entries = JSONArray(ControllerPrefs.prefs(context).getString("physicalShortcuts", null))
val damaged = JSONArray().put(entries.getJSONObject(0)).put(JSONObject().put("action", "STEAM"))
.put("not an object").put(entries.getJSONObject(1))
ControllerPrefs.prefs(context).edit().putString("physicalShortcuts", damaged.toString()).commit()
assertEquals(listOf(Action.STEAM, Action.QAM), PhysicalShortcuts.read(context).map { it.action })
assign(key(KeyEvent.KEYCODE_BUTTON_3), Action.DROIDDECK)
assertEquals(listOf(Action.STEAM, Action.QAM, Action.DROIDDECK),
PhysicalShortcuts.read(context).map { it.action })
}
@Test fun buttonSummaryKeepsTechnicalDetailsSeparate() {
val select = button(key(KeyEvent.KEYCODE_BUTTON_SELECT, scan = 314))
val androidName = KeyEvent.keyCodeToString(KeyEvent.KEYCODE_BUTTON_SELECT)
assertEquals(androidName.removePrefix("KEYCODE_").removePrefix("BUTTON_"), select.label)
assertFalse(select.label.contains("(314)"))
}
private fun assign(event: KeyEvent, action: Action) {
PhysicalShortcuts.assign(context, button(event), action)
router.bindings = PhysicalShortcuts.read(context)
}
private fun button(event: KeyEvent) = requireNotNull(PhysicalShortcuts.button(event))
private fun route(event: KeyEvent, enabled: Boolean = true, steam: Boolean = true) =
router.onKey(event, enabled, steam) { actions.add(it) }
private fun key(code: Int = KeyEvent.KEYCODE_BUTTON_1, action: Int = KeyEvent.ACTION_DOWN,
repeat: Int = 0, scan: Int = 700, device: InputDevice = pad): KeyEvent =
KeyEvent(100L, 100L, action, code, repeat, 0, device.id, scan, 0, device.sources)
private fun device(id: Int, descriptor: String, sources: Int = InputDevice.SOURCE_GAMEPAD,
type: Int = InputDevice.KEYBOARD_TYPE_NONE): InputDevice {
val device = InputDeviceBuilder.newBuilder().setId(id).setName("Test $descriptor")
.setDescriptor(descriptor).setSources(sources).setKeyboardType(type)
.setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)).build()
shadowOf(context.getSystemService(InputManager::class.java)).addInputDevice(device)
return device
}
}
@@ -0,0 +1,253 @@
package com.droiddeck.launcher.ui
import android.app.Application
import android.content.res.Configuration
import android.hardware.input.InputManager
import android.view.InputDevice
import android.view.KeyCharacterMap
import android.view.KeyEvent
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.test.*
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.width
import androidx.compose.ui.Modifier
import com.droiddeck.launcher.R
import com.droiddeck.launcher.input.ControllerPrefs
import com.droiddeck.launcher.input.PadBridgeRoutingTest
import com.droiddeck.launcher.input.PhysicalShortcuts
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.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.shadows.InputDeviceBuilder
import org.robolectric.shadows.ShadowDialog
import java.util.Locale
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], application = Application::class, qualifiers = "w960dp-h640dp-land",
shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class])
class PhysicalShortcutsDialogTest {
@get:Rule val compose = createRobolectricComposeRule()
private val context get() = RuntimeEnvironment.getApplication()
private lateinit var device: InputDevice
@Before fun setUp() {
context.getSharedPreferences("controller", 0).edit().clear().commit()
device = InputDeviceBuilder.newBuilder().setId(10).setName("Retroid extra buttons")
.setDescriptor("retroid-extra").setSources(InputDevice.SOURCE_KEYBOARD)
.setKeyboardType(InputDevice.KEYBOARD_TYPE_ALPHABETIC)
.setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)).build()
shadowOf(context.getSystemService(InputManager::class.java)).addInputDevice(device)
}
@Test fun assignmentSavesAutomaticallyOnRelease() {
var changes = 0
compose.setContent { DroidDeckTheme { PhysicalShortcutRows(onChanged = { changes++ }) } }
assign(PhysicalShortcuts.Action.STEAM)
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN)
compose.onNodeWithText(context.getString(R.string.shortcuts_release)).assertExists()
compose.onNodeWithTag("shortcuts-save").assertDoesNotExist()
assertTrue(PhysicalShortcuts.read(context).isEmpty())
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_UP)
compose.onNodeWithTag("shortcuts-save").assertDoesNotExist()
compose.runOnIdle {
val binding = PhysicalShortcuts.read(context).single()
assertEquals(KeyEvent.KEYCODE_F1, binding.button.keyCode)
assertEquals(PhysicalShortcuts.Action.STEAM, binding.action)
assertEquals(1, changes)
}
}
@Test fun buttonMappingPageOpensPhysicalShortcutAssignments() {
compose.setContent {
DroidDeckTheme { ControllerMappingPage(emptyMap(), { _, _ -> }, {}, {}) }
}
compose.onNodeWithTag("shortcuts-assign-STEAM").assertExists()
compose.onNodeWithTag("shortcuts-assign-QAM").assertExists()
compose.onNodeWithTag("shortcuts-assign-DROIDDECK").assertExists()
}
@Test fun backCancelsCaptureWithoutSaving() {
compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } }
assign(PhysicalShortcuts.Action.QAM)
send(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_DOWN)
compose.onNodeWithTag("shortcuts-cancel").assertDoesNotExist()
compose.runOnIdle {
assertTrue(PhysicalShortcuts.read(context).isEmpty())
}
compose.onNodeWithTag("shortcuts-assign-QAM").assertExists()
}
@Test fun controllerBCancelsCaptureWithoutSaving() {
compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } }
assign(PhysicalShortcuts.Action.QAM)
send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_DOWN)
compose.onNodeWithTag("shortcuts-assign-QAM").assertExists()
assertTrue(PhysicalShortcuts.read(context).isEmpty())
}
@Test fun closingConflictKeepsBothPreviousAssignments() {
PhysicalShortcuts.assign(context, requireNotNull(PhysicalShortcuts.button(event(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN))),
PhysicalShortcuts.Action.STEAM)
PhysicalShortcuts.assign(context, requireNotNull(PhysicalShortcuts.button(event(KeyEvent.KEYCODE_F2, KeyEvent.ACTION_DOWN))),
PhysicalShortcuts.Action.QAM)
val previous = PhysicalShortcuts.read(context)
compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } }
assign(PhysicalShortcuts.Action.QAM)
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN)
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_UP)
compose.onNodeWithTag("shortcuts-save").assertExists()
compose.onNodeWithTag("shortcuts-cancel").performScrollTo().performClick()
assertEquals(previous, PhysicalShortcuts.read(context))
}
@Test fun conflictingAssignmentIsNotReplacedUntilSave() {
PhysicalShortcuts.assign(context, requireNotNull(PhysicalShortcuts.button(event(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN))),
PhysicalShortcuts.Action.STEAM)
compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } }
assign(PhysicalShortcuts.Action.QAM)
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN)
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_UP)
compose.onNodeWithText(context.getString(R.string.shortcuts_conflict,
context.getString(R.string.drawer_steam_menu))).assertExists()
val button = requireNotNull(PhysicalShortcuts.button(event(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN)))
compose.onAllNodesWithText("${button.label} · ${button.name}").assertCountEquals(2)
assertEquals(PhysicalShortcuts.Action.STEAM, PhysicalShortcuts.read(context).single().action)
compose.onNodeWithTag("shortcuts-save").performScrollTo().performClick()
compose.runOnIdle { assertEquals(PhysicalShortcuts.Action.QAM, PhysicalShortcuts.read(context).single().action) }
}
@Test fun removalRestoresDefaultAndPreservesOtherAssignmentsAndControllerSettings() {
ControllerPrefs.setSelectSteam(context, true)
PhysicalShortcuts.assign(context, requireNotNull(PhysicalShortcuts.button(event(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN))),
PhysicalShortcuts.Action.STEAM)
PhysicalShortcuts.assign(context, requireNotNull(PhysicalShortcuts.button(event(KeyEvent.KEYCODE_F2, KeyEvent.ACTION_DOWN))),
PhysicalShortcuts.Action.QAM)
compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } }
assign(PhysicalShortcuts.Action.STEAM)
send(KeyEvent.KEYCODE_F3, KeyEvent.ACTION_DOWN)
compose.onNodeWithTag("shortcuts-remove").performScrollTo().performClick()
compose.runOnIdle {
assertEquals(PhysicalShortcuts.Action.QAM, PhysicalShortcuts.read(context).single().action)
assertTrue(ControllerPrefs.read(context).selectSteam)
}
compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(context.getString(R.string.game_settings_default))
}
@Test fun retryRecoversFromAMissingReleaseWithoutSavingTheOldButton() {
compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } }
assign(PhysicalShortcuts.Action.STEAM)
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN)
compose.onNodeWithTag("shortcuts-retry").performScrollTo().performClick()
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_UP)
compose.onNodeWithTag("shortcuts-save").assertDoesNotExist()
send(KeyEvent.KEYCODE_F2, KeyEvent.ACTION_DOWN)
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_UP)
compose.onNodeWithTag("shortcuts-save").assertDoesNotExist()
send(KeyEvent.KEYCODE_F2, KeyEvent.ACTION_UP)
compose.onNodeWithTag("shortcuts-save").assertDoesNotExist()
val button = requireNotNull(PhysicalShortcuts.button(event(KeyEvent.KEYCODE_F2, KeyEvent.ACTION_DOWN)))
compose.onNodeWithText("${button.label} · ${button.name}").assertExists()
compose.runOnIdle { assertEquals(KeyEvent.KEYCODE_F2, PhysicalShortcuts.read(context).single().button.keyCode) }
}
@Test fun longControllerNameDoesNotSqueezeTheMenuLabelInANarrowDrawer() {
val button = requireNotNull(PhysicalShortcuts.button(event(KeyEvent.KEYCODE_BUTTON_SELECT, KeyEvent.ACTION_DOWN)))
.copy(name = "Xbox Wireless Controller with a long device name")
PhysicalShortcuts.assign(context, button, PhysicalShortcuts.Action.STEAM)
compose.setContent {
DroidDeckTheme {
Box(Modifier.width(360.dp)) { PhysicalShortcutRows() }
}
}
val control = compose.onNodeWithTag("shortcuts-assign-STEAM").getUnclippedBoundsInRoot()
val label = compose.onNodeWithText(context.getString(R.string.drawer_steam_menu)).getUnclippedBoundsInRoot()
assertTrue(control.right - control.left < 200.dp)
assertTrue("Menu label must not wrap vertically: $label", label.bottom - label.top <= 48.dp)
compose.onNodeWithTag("shortcuts-assign-STEAM").assertTextContains(context.getString(R.string.ctrl_custom))
compose.onNodeWithText("${button.label} · ${button.name}").assertExists()
}
@Test fun replacingSameActionsButtonSavesWithoutConfirmation() {
val oldButton = requireNotNull(PhysicalShortcuts.button(event(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN)))
PhysicalShortcuts.assign(context, oldButton, PhysicalShortcuts.Action.STEAM)
compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } }
assign(PhysicalShortcuts.Action.STEAM)
send(KeyEvent.KEYCODE_F2, KeyEvent.ACTION_DOWN)
assertEquals(oldButton, PhysicalShortcuts.read(context).single().button)
send(KeyEvent.KEYCODE_F2, KeyEvent.ACTION_UP)
compose.onNodeWithTag("shortcuts-save").assertDoesNotExist()
compose.runOnIdle { assertEquals(KeyEvent.KEYCODE_F2, PhysicalShortcuts.read(context).single().button.keyCode) }
}
@Test
@Config(qualifiers = "de-rDE-w400dp-h850dp-port")
fun narrowLargeTextKeepsTranslatedCaptureReachableAndSavesOnRelease() {
compose.setContent {
val density = LocalDensity.current
CompositionLocalProvider(LocalDensity provides Density(density.density, fontScale = 1.5f)) {
DroidDeckTheme { PhysicalShortcutRows() }
}
}
assign(PhysicalShortcuts.Action.DROIDDECK)
compose.onNodeWithText(context.getString(R.string.shortcuts_capture,
context.getString(R.string.shortcuts_dd_menu))).assertExists()
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN)
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_UP)
compose.onNodeWithTag("shortcuts-save").assertDoesNotExist()
compose.runOnIdle { assertEquals(PhysicalShortcuts.Action.DROIDDECK, PhysicalShortcuts.read(context).single().action) }
}
@Test
@Config(qualifiers = "ar-rEG-w400dp-h850dp-port")
fun rightToLeftDialogKeepsCancelReachable() {
compose.setContent { DroidDeckTheme { PhysicalShortcutRows() } }
assign(PhysicalShortcuts.Action.QAM)
compose.onNodeWithTag("shortcuts-cancel").performScrollTo().assertIsDisplayed().performClick()
compose.onNodeWithTag("shortcuts-assign-QAM").assertExists()
}
@Test fun everySupportedLanguageHasALocalizedShortcutTitleAndValidFormatArguments() {
val languages = mutableListOf<String>()
context.resources.getXml(R.xml.locales_config).use { xml ->
while (xml.next() != org.xmlpull.v1.XmlPullParser.END_DOCUMENT) {
if (xml.eventType == org.xmlpull.v1.XmlPullParser.START_TAG && xml.name == "locale") {
languages.add(xml.getAttributeValue("http://schemas.android.com/apk/res/android", "name"))
}
}
}
val english = context.getString(R.string.shortcuts_title)
for (language in languages) {
val config = Configuration(context.resources.configuration).apply { setLocale(Locale.forLanguageTag(language)) }
val localized = context.createConfigurationContext(config)
if (language != "en") assertNotEquals(language, english, localized.getString(R.string.shortcuts_title))
assertTrue(localized.getString(R.string.shortcuts_capture, "ACTION").contains("ACTION"))
assertTrue(localized.getString(R.string.shortcuts_conflict, "ACTION").contains("ACTION"))
val detected = localized.getString(R.string.shortcuts_detected, "DEVICE", "BUTTON")
assertTrue(detected.contains("DEVICE"))
assertTrue(detected.contains("BUTTON"))
}
}
private fun assign(action: PhysicalShortcuts.Action) {
compose.onNodeWithTag("shortcuts-assign-${action.name}").performClick()
}
private fun send(code: Int, action: Int) {
compose.runOnIdle {
assertTrue(ShadowDialog.getLatestDialog().window!!.callback.dispatchKeyEvent(event(code, action)))
}
compose.waitForIdle()
}
private fun event(code: Int, action: Int) =
KeyEvent(100L, 100L, action, code, 0, 0, device.id, 700, 0, InputDevice.SOURCE_KEYBOARD)
}