mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
Harden input migration and session lifecycle
Keep migration binding IDs stable, preserve unreadable libraries before recovery, and retain forward-version protection. Release keyboard owners on focus loss, handle unavailable settings displays, and allow controller confirmation or cancellation after physical key capture. Stop BLE routing and publication synchronously before asynchronous disconnect. Add regression coverage and record local and Thor validation.
This commit is contained in:
1 parent
7b7722bd25
commit
915f373d82
16 files changed
+414
-49
No files matched your search
@@ -127,6 +127,26 @@ class InputLibraryTest {
|
||||
assertEquals(future, prefs.getString("library", null))
|
||||
}
|
||||
|
||||
@Test fun unreadableLibrariesKeepStableEditorReferencesUntilAnEditPreservesRecoveryData() {
|
||||
val valid = InputLibraries.encode(InputLibraries.read(context))
|
||||
val invalidValues = listOf("{broken", org.json.JSONObject(valid).put("selected", "missing").toString())
|
||||
for (raw in invalidValues) {
|
||||
prefs.edit().putString("library", raw).commit()
|
||||
val first = InputLibraries.read(context)
|
||||
val second = InputLibraries.read(context)
|
||||
assertEquals(first, second)
|
||||
val id = first.profile("game").binding(InputRole.PRIMARY)
|
||||
assertEquals(first.preset(id), second.preset(id))
|
||||
assertEquals(raw, prefs.getString("library", null))
|
||||
val updated = InputLibraries.update(context) { it.copy(selected = "desktop") }
|
||||
assertEquals("desktop", InputLibraries.read(context).selected)
|
||||
assertEquals(updated, InputLibraries.read(context))
|
||||
assertTrue(prefs.all.filterKeys { it.startsWith("library-recovery.") }.containsValue(raw))
|
||||
}
|
||||
assertTrue(InputLibraries.hasLibraryRecovery(context))
|
||||
assertEquals(invalidValues.toSet(), prefs.all.filterKeys { it.startsWith("library-recovery.") }.values.toSet())
|
||||
}
|
||||
|
||||
@Test fun resettingAProfileDoesNotResetSharedBindingsOrAccumulatePrivateCopies() {
|
||||
var library = InputLibraries.read(context).duplicateProfile("desktop", "Editor", "editor", sharePhysical = true)
|
||||
val common = library.preset(library.profile("desktop").binding(InputRole.PHYSICAL))
|
||||
|
||||
@@ -65,4 +65,31 @@ class LegacyScreenKeyMigrationTest {
|
||||
assertEquals("28:NaN,0.5,44", ControllerPrefs.prefs(context).getString("screenKeys", null))
|
||||
assertFalse(prefs.contains(LegacyScreenKeyMigration.BACKUP))
|
||||
}
|
||||
|
||||
@Test fun failedScreenKeyMigrationKeepsUpgradedBindingIdsStableForEditorLookup() {
|
||||
val original = InputLibraries.read(context).chooseLayout("game", InputRole.SECONDARY, InputLibraries.DECK)
|
||||
val fixed = original.copy(layouts = original.layouts.map {
|
||||
when (it.id) {
|
||||
InputLibraries.OVERLAY -> it.copy(elements = emptyList(), template = InputLayoutTemplate.OVERLAY)
|
||||
InputLibraries.DECK -> it.copy(elements = emptyList(), template = InputLayoutTemplate.DECK)
|
||||
else -> it
|
||||
}
|
||||
}, bindings = original.bindings.map {
|
||||
if (it.layoutId in setOf(InputLibraries.OVERLAY, InputLibraries.DECK)) it.copy(elements = emptyMap(), clicks = emptyMap()) else it
|
||||
})
|
||||
val raw = JSONObject(InputLibraries.encode(fixed)).put("version", 2).toString()
|
||||
prefs.edit().putString("library", raw).commit()
|
||||
ControllerPrefs.prefs(context).edit().putString("screenKeys", "28:NaN,0.5,44").commit()
|
||||
val first = InputLibraries.read(context)
|
||||
val second = InputLibraries.read(context)
|
||||
assertEquals(first, second)
|
||||
for (role in listOf(InputRole.PRIMARY, InputRole.SECONDARY)) {
|
||||
val id = first.profile("game").binding(role)
|
||||
assertEquals(first.preset(id), second.preset(id))
|
||||
assertTrue(second.layout("game", role)!!.editable)
|
||||
}
|
||||
assertEquals(raw, prefs.getString("library", null))
|
||||
assertTrue(InputLibraries.hasPendingScreenKeyMigration(context))
|
||||
assertEquals("28:NaN,0.5,44", ControllerPrefs.prefs(context).getString("screenKeys", null))
|
||||
}
|
||||
}
|
||||
@@ -171,6 +171,16 @@ class SessionInputPointerTest {
|
||||
assertTrue(CompositorShadow.events.isEmpty())
|
||||
}
|
||||
|
||||
@Test fun focusCleanupReleasesHardwareAndKeyboardKeysWithoutReleasingAnotherOwner() {
|
||||
val held = ReflectionHelpers.getField<HeldInput>(activity, "heldKeys")
|
||||
output.key(29, true)
|
||||
for ((owner, code) in listOf(8 to 30, 16 to 31, 32 to 32, 16 to 29)) held.set(owner, code, true)
|
||||
ReflectionHelpers.callInstanceMethod<Unit>(activity, "releaseHeldGuestInput")
|
||||
assertEquals(setOf(30 to 0, 31 to 0, 32 to 0), CompositorShadow.keys.filter { it.second == 0 }.toSet())
|
||||
output.key(29, false)
|
||||
assertEquals(29 to 0, CompositorShadow.keys.last())
|
||||
}
|
||||
|
||||
@Test fun horizontalWheelUsesTheSameDirectionInBothMouseBackends() {
|
||||
output.scrollHorizontal(2)
|
||||
assertEquals(listOf(Triple(11, 0, 240)), CompositorShadow.events)
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.os.Handler
|
||||
import org.junit.After
|
||||
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.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], manifest = Config.NONE, shadows = [SteamControllerBleTest.Writer::class])
|
||||
class SteamControllerBleTest {
|
||||
private val keys = ArrayList<Pair<Int, Boolean>>()
|
||||
private val router = InputRouter(object : InputOutput {
|
||||
override fun key(code: Int, down: Boolean) { keys += code to down }
|
||||
override fun button(code: Int, down: Boolean) {}
|
||||
override fun pointer(dx: Float, dy: Float) {}
|
||||
override fun scroll(steps: Int) {}
|
||||
override fun command(command: InputCommand) {}
|
||||
override fun requestMotion() {}
|
||||
})
|
||||
private lateinit var controller: SteamControllerBle
|
||||
private lateinit var handler: Handler
|
||||
|
||||
@Before fun setUp() {
|
||||
Writer.reports.clear()
|
||||
Writer.connections.clear()
|
||||
Writer.features = 0
|
||||
router.setProfile(InputProfile.defaults(InputProfile.GAME)
|
||||
.withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(57))))
|
||||
controller = SteamControllerBle(RuntimeEnvironment.getApplication(), router)
|
||||
handler = ReflectionHelpers.getField(controller, "handler")
|
||||
shadowOf(handler.looper).pause()
|
||||
}
|
||||
|
||||
@After fun close() {
|
||||
controller.stop()
|
||||
handler.looper.quit()
|
||||
}
|
||||
|
||||
@Test fun stopReleasesAndDetachesInputBeforeQueuedCallbacksCanPublish() {
|
||||
state(ByteArray(45).apply { this[1] = 1 })
|
||||
assertEquals(listOf(57 to true), keys)
|
||||
handler.post { state(ByteArray(45).apply { this[1] = 1 }) }
|
||||
controller.stop()
|
||||
assertEquals(listOf(57 to true, 57 to false), keys)
|
||||
assertNull(ReflectionHelpers.getField<Any?>(controller, "routedInput"))
|
||||
assertTrue(Writer.reports.last().all { it == 0.toByte() })
|
||||
val reports = Writer.reports.size
|
||||
val connections = Writer.connections.toList()
|
||||
shadowOf(handler.looper).idle()
|
||||
state(ByteArray(45))
|
||||
state(ByteArray(45).apply { this[1] = 1 })
|
||||
ReflectionHelpers.callInstanceMethod<Unit>(controller, "release")
|
||||
ReflectionHelpers.setField(controller, "featureRead", 7L)
|
||||
ReflectionHelpers.callInstanceMethod<Unit>(controller, "onFeatureRead", ReflectionHelpers.ClassParameter.from(ByteArray::class.java, ByteArray(2)))
|
||||
controller.stop()
|
||||
assertEquals(reports, Writer.reports.size)
|
||||
assertEquals(connections, Writer.connections)
|
||||
assertEquals(0, Writer.features)
|
||||
assertEquals(listOf(57 to true, 57 to false), keys)
|
||||
}
|
||||
|
||||
@Test fun stoppingBeforeQueuedStartPreservesAnotherSourcesHeldKey() {
|
||||
val other = router.channel(InputRole.PRIMARY, InputControl.B, InputAction.Keys(listOf(57)))
|
||||
other.update(1f)
|
||||
state(ByteArray(45).apply { this[1] = 1 })
|
||||
controller.start()
|
||||
controller.stop()
|
||||
shadowOf(handler.looper).idle()
|
||||
assertFalse(ReflectionHelpers.getField<Boolean>(controller, "running"))
|
||||
assertEquals(listOf(57 to true), keys)
|
||||
other.close()
|
||||
assertEquals(listOf(57 to true, 57 to false), keys)
|
||||
}
|
||||
|
||||
private fun state(bytes: ByteArray) = ReflectionHelpers.callInstanceMethod<Unit>(controller, "onState",
|
||||
ReflectionHelpers.ClassParameter.from(ByteArray::class.java, bytes))
|
||||
|
||||
@Implements(FakeInputWriter::class)
|
||||
class Writer {
|
||||
companion object {
|
||||
val reports = ArrayList<ByteArray>()
|
||||
val connections = ArrayList<Boolean>()
|
||||
var features = 0
|
||||
@JvmStatic @Implementation fun writeTritonState(slot: Int, reportId: Int, report: ByteArray) { reports += report.copyOf() }
|
||||
@JvmStatic @Implementation fun setTritonConnected(slot: Int, connected: Boolean) { connections += connected }
|
||||
@JvmStatic @Implementation fun writeTritonFeatureReply(slot: Int, request: Long, report: ByteArray?) { features++ }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.hardware.input.InputManager
|
||||
import android.view.InputDevice
|
||||
import android.view.KeyCharacterMap
|
||||
import android.view.KeyEvent
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.ui.test.*
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.input.PadBridgeRoutingTest
|
||||
import com.droiddeck.launcher.input.PhysicalInputKey
|
||||
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
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], qualifiers = "w960dp-h640dp-land", shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class])
|
||||
class InputCaptureDialogTest {
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
private val context get() = RuntimeEnvironment.getApplication()
|
||||
private val open = mutableStateOf(true)
|
||||
private var selected: PhysicalInputKey? = null
|
||||
private lateinit var device: InputDevice
|
||||
|
||||
@Before fun setUp() {
|
||||
device = InputDeviceBuilder.newBuilder().setId(10).setName("Thor buttons")
|
||||
.setDescriptor("thor-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)
|
||||
compose.setContent { DroidDeckTheme {
|
||||
if (open.value) InputCaptureDialog({ open.value = false }) { selected = it; open.value = false }
|
||||
} }
|
||||
}
|
||||
|
||||
@Test fun physicalBackCanBeCapturedAndConfirmedWithControllerA() {
|
||||
send(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_DOWN)
|
||||
compose.onNodeWithText(context.getString(R.string.ctrl_edit)).assertIsNotEnabled()
|
||||
assertTrue(open.value)
|
||||
send(KeyEvent.KEYCODE_BACK, KeyEvent.ACTION_UP)
|
||||
compose.onNodeWithText(context.getString(R.string.ctrl_edit)).assertIsEnabled()
|
||||
assertNull(selected)
|
||||
send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_DOWN)
|
||||
assertEquals(KeyEvent.KEYCODE_BACK, selected?.keyCode)
|
||||
assertFalse(open.value)
|
||||
}
|
||||
|
||||
@Test fun controllerBCanBeCapturedAndAnotherPressCancelsWithoutSaving() {
|
||||
send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_DOWN)
|
||||
send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_UP)
|
||||
assertTrue(open.value)
|
||||
send(KeyEvent.KEYCODE_BUTTON_B, KeyEvent.ACTION_DOWN)
|
||||
assertFalse(open.value)
|
||||
assertNull(selected)
|
||||
}
|
||||
|
||||
@Test fun retryAllowsAnotherButtonAndConsumesItsReleaseBeforeConfirmation() {
|
||||
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_DOWN)
|
||||
send(KeyEvent.KEYCODE_F1, KeyEvent.ACTION_UP)
|
||||
compose.onNodeWithText(context.getString(R.string.input_capture_retry)).performClick()
|
||||
send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_DOWN)
|
||||
send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_UP)
|
||||
assertNull(selected)
|
||||
assertTrue(open.value)
|
||||
send(KeyEvent.KEYCODE_BUTTON_A, KeyEvent.ACTION_DOWN)
|
||||
assertEquals(KeyEvent.KEYCODE_BUTTON_A, selected?.keyCode)
|
||||
}
|
||||
|
||||
private fun send(code: Int, action: Int) {
|
||||
compose.runOnIdle {
|
||||
assertTrue(ShadowDialog.getLatestDialog().window!!.callback.dispatchKeyEvent(
|
||||
KeyEvent(100L, 100L, action, code, 0, 0, device.id, 700, 0, InputDevice.SOURCE_KEYBOARD)))
|
||||
}
|
||||
compose.waitForIdle()
|
||||
}
|
||||
}
|
||||
@@ -288,6 +288,22 @@ class InputSettingsTest {
|
||||
} finally { ShadowDisplayManager.removeDisplay(displayId) }
|
||||
}
|
||||
|
||||
@Test @Config(sdk = [34]) fun unavailableDisplayDismissesSettingsWithoutCrashing() {
|
||||
val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField<Int>(Display::class.java, "TYPE_EXTERNAL"))
|
||||
val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId)
|
||||
shadowOf(display).setFlags(Display.FLAG_PRESENTATION)
|
||||
ShadowDisplayManager.removeDisplay(displayId)
|
||||
val open = mutableStateOf(true)
|
||||
compose.setContent { DroidDeckTheme {
|
||||
val host = androidx.compose.ui.platform.LocalContext.current.findActivity() as androidx.activity.ComponentActivity
|
||||
if (open.value) InputDisplayContent(host, display, { open.value = false }) { InputSettingsPage({}) }
|
||||
} }
|
||||
compose.runOnIdle {
|
||||
assertFalse(open.value)
|
||||
assertTrue(ShadowDialog.getShownDialogs().none { it.isShowing })
|
||||
}
|
||||
}
|
||||
|
||||
@Test @Config(sdk = [34]) fun mainEditorOpenedFromSecondarySettingsUsesAnActivityWindow() {
|
||||
val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", ReflectionHelpers.getStaticField<Int>(Display::class.java, "TYPE_EXTERNAL"))
|
||||
val display = context.getSystemService(DisplayManager::class.java).getDisplay(displayId)
|
||||
|
||||
Reference in new issue
Block a user