mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
* fix(input): restore on-screen touch behaviour: slide between buttons, mouse pass-through, keyboard on release, overlay haptics, stick reach and hit areas * fix(dual-screen): restore second-screen bars, swapped Terminal and Deck panel tabs * fix(input): fit on-screen layouts to the real view size, cutout, Steam + QAM mode and unfolded strip * fix(input): unfolded strip keeps press haptics * fix(input): restore layout import/export, element editor rows and layout tools Bring back .icp layout import and export on the saved layouts page, the per-element image, opacity and label controls, a uniform size slider and reset positions in the layout editor, and non-destructive Steam, QAM and keyboard button toggles stored on the layout style. Remove strings left unused by the input profile unification. * fix(input): keep hit slack to gamepad-shaped buttons and fit touch tests to saved layout geometry * fix(input): button size regenerates the gamepad and scales spacing A generated gamepad now stores its button size and is regenerated at that size, so spacing follows the radius and the layout keeps regenerating on rotation. Edited layouts scale positions from their anchor edge or centre so gaps grow and shrink with the controls. * fix(input): button size rescales moved gamepads around the generator Gamepad layouts that no longer match the generated positions exactly were scaled element by element from screen thirds, which split the face buttons and D-pad across anchors. Gamepad controls now take the generator's positions at the new size plus each control's own offset from the generated position, so groups keep their shape and spacing.
This commit is contained in:
1 parent
a33954b8bd
commit
8fa30b99f9
70 files changed
+2383
-1789
No files matched your search
@@ -5,6 +5,7 @@ import android.app.Presentation
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.view.Display
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.ui.test.*
|
||||
import com.droiddeck.launcher.input.*
|
||||
import com.droiddeck.launcher.R
|
||||
@@ -379,4 +380,147 @@ class InputSettingsTest {
|
||||
if (display.isValid) ShadowDisplayManager.removeDisplay(displayId)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun elementPropertiesEditOpacityLabelsAndOfferImages() {
|
||||
val element = mutableStateOf(InputSurfaceElement(InputControl.A, .1f, .1f, .2f, .2f, "custom", name = "Custom button"))
|
||||
compose.setContent { DroidDeckTheme {
|
||||
androidx.compose.foundation.layout.Column(androidx.compose.ui.Modifier.verticalScroll(androidx.compose.foundation.rememberScrollState())) {
|
||||
InputElementProperties(element.value) { element.value = it }
|
||||
}
|
||||
} }
|
||||
compose.onNodeWithTag("setting-element-opacity").performScrollTo().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(60f) }
|
||||
compose.runOnIdle { assertEquals(.6f, element.value.opacity, .001f) }
|
||||
compose.onNodeWithTag("setting-element-label").performScrollTo().performClick()
|
||||
compose.runOnIdle { assertFalse(element.value.showLabel) }
|
||||
compose.onNodeWithText(context.getString(R.string.input_button_text)).performScrollTo().performClick()
|
||||
compose.onNodeWithText(context.getString(R.string.screen_key_image)).assertExists()
|
||||
compose.runOnIdle { element.value = element.value.copy(icon = InputButtonIcons.MENU) }
|
||||
compose.onNodeWithTag("setting-element-label").assertDoesNotExist()
|
||||
compose.runOnIdle { element.value = InputSurfaceElement(InputControl.LEFT_PAD, .1f, .1f, .4f, .4f, "pad", InputElementKind.TRACKPAD, "Pad") }
|
||||
compose.onNodeWithTag("setting-element-label").assertDoesNotExist()
|
||||
compose.onNodeWithTag("setting-element-opacity").assertExists()
|
||||
}
|
||||
|
||||
@Test fun editorScalesEveryControlAndRestoresStockPositions() {
|
||||
val library = InputLibraries.read(context)
|
||||
val layout = library.layout("game", InputRole.SECONDARY)!!
|
||||
assertEquals(InputLibraries.DECK, layout.id)
|
||||
val round = mutableStateOf(0)
|
||||
compose.setContent { DroidDeckTheme { androidx.compose.runtime.key(round.value) { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY,
|
||||
layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.SECONDARY)) } } }
|
||||
compose.onNodeWithTag("input-editor-tools").performClick()
|
||||
compose.onNodeWithText(context.getString(R.string.input_reset_positions)).performScrollTo().assertIsNotEnabled()
|
||||
compose.onNodeWithTag("input-editor-scale").performScrollTo().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(80f) }
|
||||
compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick()
|
||||
val scaled = InputLibraries.read(context).layouts.first { it.id == layout.id }
|
||||
assertEquals(layout.elements.scaled(.8f), scaled.elements)
|
||||
compose.runOnIdle { round.value = 1 }
|
||||
compose.onNodeWithTag("input-editor-tools").performClick()
|
||||
compose.onNodeWithText(context.getString(R.string.input_reset_positions)).performScrollTo().performClick()
|
||||
compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick()
|
||||
compose.runOnIdle { assertEquals(layout.elements, InputLibraries.read(context).layouts.first { it.id == layout.id }.elements) }
|
||||
}
|
||||
|
||||
@Test fun quickButtonTogglesHideBuiltInButtonsWithoutDeletingThem() {
|
||||
compose.setContent { DroidDeckTheme { InputSettingsPage({}) } }
|
||||
compose.onNodeWithTag("input-profile-game").performScrollTo().performClick()
|
||||
compose.onNodeWithTag("input-part-primary").performScrollTo().performClick()
|
||||
val before = InputLibraries.read(context).layout("game", InputRole.PRIMARY)!!
|
||||
compose.onNodeWithTag("setting-quick-keyboard").performScrollTo().performClick()
|
||||
compose.runOnIdle {
|
||||
val after = InputLibraries.read(context).layout("game", InputRole.PRIMARY)!!
|
||||
assertEquals(before.elements, after.elements)
|
||||
assertEquals(setOf(InputCommand.KEYBOARD), after.style.hidden)
|
||||
}
|
||||
compose.onNodeWithTag("setting-quick-keyboard").performScrollTo().performClick()
|
||||
compose.runOnIdle { assertTrue(InputLibraries.read(context).layout("game", InputRole.PRIMARY)!!.style.hidden.isEmpty()) }
|
||||
}
|
||||
|
||||
@Test fun layoutsCanBeImportedAndExported() {
|
||||
val source = java.io.File(context.cacheDir, "jump.icp").apply {
|
||||
writeText("""{"name":"Jump keys","elements":[{"type":"BUTTON","shape":"CIRCLE","bindings":["KEY_SPACE"],"scale":1.0,"x":0.8,"y":0.7,"text":"Jump"}]}""")
|
||||
}
|
||||
val target = java.io.File(context.cacheDir, "out.icp").apply { delete() }
|
||||
val registry = object : androidx.activity.result.ActivityResultRegistry() {
|
||||
override fun <I, O> onLaunch(requestCode: Int, contract: androidx.activity.result.contract.ActivityResultContract<I, O>, input: I,
|
||||
options: androidx.core.app.ActivityOptionsCompat?) {
|
||||
dispatchResult(requestCode, android.net.Uri.fromFile(if (input is String) target else source))
|
||||
}
|
||||
}
|
||||
val owner = object : androidx.activity.result.ActivityResultRegistryOwner { override val activityResultRegistry = registry }
|
||||
compose.setContent { androidx.compose.runtime.CompositionLocalProvider(androidx.activity.compose.LocalActivityResultRegistryOwner provides owner) {
|
||||
DroidDeckTheme { InputSettingsPage({}) }
|
||||
} }
|
||||
compose.onNodeWithTag("input-profile-game").performScrollTo().performClick()
|
||||
compose.onNodeWithTag("input-part-primary").performScrollTo().performClick()
|
||||
compose.onNodeWithText(context.getString(R.string.input_saved_layouts)).performScrollTo().performClick()
|
||||
compose.onNodeWithTag("layout-manage-${InputLibraries.OVERLAY}").performScrollTo().performClick()
|
||||
compose.onNodeWithText(context.getString(R.string.screen_key_layout_export)).assertDoesNotExist()
|
||||
compose.onNodeWithText(context.getString(R.string.common_close)).performClick()
|
||||
compose.onNodeWithTag("input-layout-import").performScrollTo().performClick()
|
||||
compose.waitUntil(5_000) { InputLibraries.read(context).layout("game", InputRole.PRIMARY)!!.name == "Jump keys" }
|
||||
val imported = InputLibraries.read(context).layout("game", InputRole.PRIMARY)!!
|
||||
assertEquals(InputLayoutScreen.MAIN, imported.screen)
|
||||
assertEquals(InputAction.Keys(listOf(57)), InputLibraries.read(context).compile("game").elementAction(InputRole.PRIMARY, imported.elements.single().id, InputControl.A))
|
||||
compose.onNodeWithText(context.getString(R.string.input_saved_layouts)).performScrollTo().performClick()
|
||||
compose.onNodeWithTag("layout-manage-${imported.id}").performScrollTo().performClick()
|
||||
compose.onNodeWithText(context.getString(R.string.screen_key_layout_export)).performClick()
|
||||
compose.waitUntil(5_000) { target.isFile && target.length() > 0 }
|
||||
assertEquals(source.readText().let { org.json.JSONObject(it).getString("name") }, org.json.JSONObject(target.readText()).getString("name"))
|
||||
assertEquals("KEY_SPACE", org.json.JSONObject(target.readText()).getJSONArray("elements").getJSONObject(0).getJSONArray("bindings").getString(0))
|
||||
}
|
||||
|
||||
@Test fun buttonSizeRegeneratesTheGamepadAndUndoesAsOneStep() {
|
||||
val library = InputLibraries.read(context)
|
||||
val layout = library.layout("game", InputRole.PRIMARY)!!
|
||||
val (_, metrics) = InputLibraries.primaryDisplay(context)
|
||||
assertNotNull(OnScreenLayout.generatedSize(context, layout, metrics.widthPixels, metrics.heightPixels))
|
||||
compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY,
|
||||
layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.PRIMARY)) } }
|
||||
compose.onNodeWithTag("input-editor-tools").performClick()
|
||||
val slider = compose.onNodeWithTag("input-editor-scale").performScrollTo()
|
||||
slider.assert(SemanticsMatcher.expectValue(androidx.compose.ui.semantics.SemanticsProperties.StateDescription, "100%"))
|
||||
for (value in listOf(110f, 125f)) slider.performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(value) }
|
||||
compose.onNodeWithText(context.getString(R.string.input_editor_undo)).performScrollTo().performClick()
|
||||
slider.assert(SemanticsMatcher.expectValue(androidx.compose.ui.semantics.SemanticsProperties.StateDescription, "100%"))
|
||||
slider.performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(125f) }
|
||||
compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick()
|
||||
compose.runOnIdle {
|
||||
val saved = InputLibraries.read(context).layouts.first { it.id == layout.id }
|
||||
assertEquals(1.25f, saved.scale)
|
||||
assertNotNull(OnScreenLayout.generatedSize(context, saved, metrics.widthPixels, metrics.heightPixels))
|
||||
assertNotEquals(layout.elements.first { it.id == "legacy:a" }.y, saved.elements.first { it.id == "legacy:a" }.y)
|
||||
}
|
||||
}
|
||||
|
||||
@Test @Config(qualifiers = "w833dp-h468dp-land-369dpi")
|
||||
fun buttonSizeKeepsMovedGamepadGroupsInShape() {
|
||||
val face = setOf("legacy:a", "legacy:b", "legacy:x", "legacy:y")
|
||||
val original = InputLibraries.read(context)
|
||||
val gamepad = original.layout("game", InputRole.PRIMARY)!!
|
||||
val moved = gamepad.copy(elements = gamepad.elements.map { if (it.id in face) it.copy(x = it.x + .012f, y = it.y + .017f) else it })
|
||||
InputLibraries.update(context) { it.withLayout(moved) }
|
||||
val (_, metrics) = InputLibraries.primaryDisplay(context)
|
||||
assertNull(OnScreenLayout.generatedSize(context, moved, metrics.widthPixels, metrics.heightPixels))
|
||||
val library = InputLibraries.read(context)
|
||||
compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY,
|
||||
layoutId = moved.id, bindingsId = library.profile("game").binding(InputRole.PRIMARY)) } }
|
||||
compose.onNodeWithTag("input-editor-tools").performClick()
|
||||
compose.onNodeWithTag("input-editor-scale").performScrollTo().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(125f) }
|
||||
compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick()
|
||||
compose.runOnIdle {
|
||||
val saved = InputLibraries.read(context).layouts.first { it.id == moved.id }
|
||||
assertEquals(1.25f, saved.scale)
|
||||
val w = metrics.widthPixels; val h = metrics.heightPixels
|
||||
fun centre(id: String) = saved.elements.first { it.id == id }.let { (it.x + it.width / 2) * w to (it.y + it.height / 2) * h }
|
||||
assertEquals(centre("legacy:y").first, centre("legacy:a").first, 1f)
|
||||
assertEquals(centre("legacy:x").second, centre("legacy:b").second, 1f)
|
||||
assertTrue(centre("legacy:y").second < centre("legacy:x").second && centre("legacy:x").second < centre("legacy:a").second)
|
||||
assertEquals(centre("legacy:a").second - centre("legacy:y").second, centre("legacy:b").first - centre("legacy:x").first, 1f)
|
||||
assertEquals(centre("legacy:up").first, centre("legacy:down").first, 1f)
|
||||
assertEquals(centre("legacy:left").second, centre("legacy:right").second, 1f)
|
||||
val before = moved.elements.first { it.id == "legacy:a" }
|
||||
assertTrue(saved.elements.first { it.id == "legacy:a" }.width > before.width * 1.2f)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,8 +93,7 @@ class InputSurfaceOwnershipTest {
|
||||
|
||||
@Test fun removingAButtonReleasesOnlyItsOwnInput() {
|
||||
val buttons = child("HoldButton")
|
||||
buttons.layout(0, 0, 300, 300)
|
||||
touch(buttons, MotionEvent.ACTION_DOWN, 150f, 240f)
|
||||
press(buttons)
|
||||
assertTrue(state().isDown(PadState.A))
|
||||
val position = JoyConPadTest.RecordingWriter.deckPads.copyOf()
|
||||
(buttons.parent as ViewGroup).removeView(buttons)
|
||||
@@ -123,8 +122,7 @@ class InputSurfaceOwnershipTest {
|
||||
|
||||
private fun holdFaceButton() {
|
||||
val buttons = child("HoldButton")
|
||||
buttons.layout(0, 0, 300, 300)
|
||||
touch(buttons, MotionEvent.ACTION_DOWN, 150f, 240f)
|
||||
press(buttons)
|
||||
}
|
||||
|
||||
private fun assertTrackpadStillHeld(position: ShortArray) {
|
||||
@@ -139,6 +137,11 @@ class InputSurfaceOwnershipTest {
|
||||
if (view is ViewGroup) for (i in 0 until view.childCount) yieldAll(descendants(view.getChildAt(i)))
|
||||
}
|
||||
|
||||
private fun press(button: View) {
|
||||
button.javaClass.getDeclaredMethod("press", Int::class.java, Boolean::class.java, Boolean::class.java)
|
||||
.apply { isAccessible = true }.invoke(button, 0, true, false)
|
||||
}
|
||||
|
||||
private fun touch(view: View, action: Int, x: Float, y: Float, fingers: Int = 1) {
|
||||
val properties = Array(fingers) { i -> MotionEvent.PointerProperties().apply { id = i; toolType = MotionEvent.TOOL_TYPE_FINGER } }
|
||||
val coords = Array(fingers) { MotionEvent.PointerCoords().apply { this.x = x; this.y = y; pressure = 1f; size = 1f } }
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.view.HapticFeedbackConstants
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.activity.ComponentActivity
|
||||
import com.droiddeck.launcher.input.*
|
||||
import java.io.File
|
||||
import org.junit.After
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
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
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class])
|
||||
class InputSurfaceTouchTest {
|
||||
private val context: Context get() = RuntimeEnvironment.getApplication()
|
||||
private val bridge = PadBridge(File(context.cacheDir, "surface-touch"))
|
||||
private val events = mutableListOf<String>()
|
||||
private val router = InputRouter(object : InputOutput {
|
||||
override fun key(code: Int, down: Boolean) { events += "key $code $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) { events += command.name }
|
||||
override fun command(command: InputCommand, role: InputRole) { events += "${command.name}:$role" }
|
||||
override fun requestMotion() {}
|
||||
})
|
||||
private lateinit var view: OnScreenControls
|
||||
private val a = InputSurfaceElement(InputControl.A, .7f, .3f, .2f, .2f)
|
||||
private val b = InputSurfaceElement(InputControl.B, .7f, .6f, .2f, .2f)
|
||||
private val stick = InputSurfaceElement(InputControl.LS, .1f, .3f, .2f, .4f, floating = false)
|
||||
private val keyboard = InputSurfaceElement(InputControl.A, .4f, .8f, .1f, .1f, "builtin:keyboard", name = "Keyboard")
|
||||
private var time = 1000L
|
||||
|
||||
@Before fun setup() {
|
||||
bridge.setInputRouter(router)
|
||||
bridge.setInputProfile(InputProfile("touch", elements = mapOf(InputRole.PRIMARY to mapOf(
|
||||
"builtin:keyboard" to InputAction.Command(InputCommand.KEYBOARD), "latch" to InputAction.Keys(listOf(42))))))
|
||||
view = OnScreenControls(context, bridge)
|
||||
show(a, b, stick, keyboard)
|
||||
}
|
||||
|
||||
@After fun close() { view.releaseAll(); bridge.stop() }
|
||||
|
||||
private fun show(vararg elements: InputSurfaceElement) {
|
||||
view.updateLayout(SavedInputLayout("touch", "Touch", elements.toList(), aspect = 1000f / 600f))
|
||||
measure(view)
|
||||
}
|
||||
|
||||
private fun measure(target: View) {
|
||||
target.measure(View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY))
|
||||
target.layout(0, 0, 1000, 600)
|
||||
}
|
||||
|
||||
private fun state() = JoyConPadTest.RecordingWriter.pads[0] ?: PadState()
|
||||
|
||||
@Test fun aFingerSlidingBetweenButtonsMovesThePress() {
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f))
|
||||
assertTrue(state().isDown(PadState.A))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (800f to 420f))
|
||||
assertFalse(state().isDown(PadState.A))
|
||||
assertTrue(state().isDown(PadState.B))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (600f to 100f))
|
||||
assertFalse(state().isDown(PadState.B))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (800f to 240f))
|
||||
assertTrue(state().isDown(PadState.A))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (800f to 240f))
|
||||
assertFalse(state().isDown(PadState.A))
|
||||
}
|
||||
|
||||
@Test fun aStickKeepsItsFingerWhenItCrossesAButton() {
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (800f to 240f))
|
||||
assertFalse(state().isDown(PadState.A))
|
||||
assertEquals(1f, state().leftX, .01f)
|
||||
touch(MotionEvent.ACTION_UP, 1 to (800f to 240f))
|
||||
assertEquals(0f, state().leftX, 0f)
|
||||
}
|
||||
|
||||
@Test fun mouseClicksPassThroughToTheGame() {
|
||||
val missed = mutableListOf<Int>()
|
||||
view.onTouchMiss = { missed += it.actionMasked }
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_MOUSE)
|
||||
touch(MotionEvent.ACTION_UP, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_MOUSE)
|
||||
assertFalse(state().isDown(PadState.A))
|
||||
assertEquals(listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP), missed)
|
||||
view.onTouchMiss = null
|
||||
assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f), tool = MotionEvent.TOOL_TYPE_MOUSE))
|
||||
assertFalse(state().isDown(PadState.A))
|
||||
}
|
||||
|
||||
@Test fun keyboardShortcutOpensOnRelease() {
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f))
|
||||
assertTrue(events.isEmpty())
|
||||
touch(MotionEvent.ACTION_UP, 1 to (450f to 510f))
|
||||
assertEquals(listOf("KEYBOARD:PRIMARY"), events)
|
||||
}
|
||||
|
||||
@Test fun cancelledDraggedOrDisabledKeyboardShortcutDoesNotOpen() {
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f))
|
||||
touch(MotionEvent.ACTION_CANCEL, 1 to (450f to 510f))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (450f to 510f))
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (450f to 510f), flags = MotionEvent.FLAG_CANCELED)
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (600f to 510f))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (600f to 510f))
|
||||
assertTrue(events.isEmpty())
|
||||
show(a, b, stick)
|
||||
view.setQuickHidden(true)
|
||||
measure(view)
|
||||
assertFalse(view.takesTouchAt(450f, 510f))
|
||||
assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f)))
|
||||
assertTrue(events.isEmpty())
|
||||
}
|
||||
|
||||
@Test fun keyboardShortcutWorksWithHiddenPadAndButtonsOnly() {
|
||||
view.setQuickHidden(true)
|
||||
measure(view)
|
||||
assertFalse(view.takesTouchAt(800f, 240f))
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (450f to 510f))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (450f to 510f))
|
||||
view.setButtonsOnly(true)
|
||||
measure(view)
|
||||
val (x, y) = centre(element(view, "builtin:keyboard"))
|
||||
assertFalse(view.takesTouchAt(800f, 240f))
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (x to y))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (x to y))
|
||||
assertEquals(listOf("KEYBOARD:PRIMARY", "KEYBOARD:PRIMARY"), events)
|
||||
}
|
||||
|
||||
@Test fun hidingTheViewReleasesLatchedKeys() {
|
||||
val controller = Robolectric.buildActivity(ComponentActivity::class.java).setup()
|
||||
try {
|
||||
controller.get().setContentView(view)
|
||||
show(InputSurfaceElement(InputControl.A, .7f, .3f, .2f, .2f, "latch", toggle = true))
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (800f to 240f))
|
||||
assertEquals(listOf("key 42 true"), events)
|
||||
view.visibility = View.GONE
|
||||
assertEquals(listOf("key 42 true", "key 42 false"), events)
|
||||
} finally { controller.pause().stop().destroy() }
|
||||
}
|
||||
|
||||
@Test fun onlyPanelsVibrateOnPress() {
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f))
|
||||
assertTrue(state().isDown(PadState.A))
|
||||
assertNotEquals(HapticFeedbackConstants.VIRTUAL_KEY, shadowOf(button(view)).lastHapticFeedbackPerformed())
|
||||
touch(MotionEvent.ACTION_UP, 1 to (800f to 240f))
|
||||
val panel = InputSurfaceView(context, bridge, listOf(a))
|
||||
measure(panel)
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (800f to 240f), target = panel)
|
||||
assertEquals(HapticFeedbackConstants.VIRTUAL_KEY, shadowOf(button(panel)).lastHapticFeedbackPerformed())
|
||||
touch(MotionEvent.ACTION_CANCEL, 1 to (800f to 240f), target = panel)
|
||||
}
|
||||
|
||||
@Test fun classicSticksTravelTheirFullRadius() {
|
||||
show(stick.copy(buttonShape = InputButtonShape.CIRCLE))
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (200f to 300f))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (250f to 300f))
|
||||
assertEquals(.5f, state().leftX, .01f)
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (300f to 300f))
|
||||
assertEquals(1f, state().leftX, .01f)
|
||||
touch(MotionEvent.ACTION_UP, 1 to (300f to 300f))
|
||||
}
|
||||
|
||||
@Test fun buttonsAcceptTouchesJustOutsideTheirEdge() {
|
||||
show(InputSurfaceElement(InputControl.A, .7f, .3f, .1f, 1f / 6f, buttonShape = InputButtonShape.CIRCLE),
|
||||
InputSurfaceElement(InputControl.B, .3f, .3f, .2f, .1f, buttonShape = InputButtonShape.PILL),
|
||||
InputSurfaceElement(InputControl.X, .3f, .6f, .2f, .1f))
|
||||
assertTrue(view.takesTouchAt(750f, 285f))
|
||||
assertTrue(view.takesTouchAt(805f, 230f))
|
||||
assertFalse(view.takesTouchAt(750f, 300f))
|
||||
assertFalse(view.takesTouchAt(805f, 285f))
|
||||
assertTrue(view.takesTouchAt(400f, 245f))
|
||||
assertFalse(view.takesTouchAt(400f, 255f))
|
||||
assertFalse(view.takesTouchAt(400f, 359f))
|
||||
assertTrue(view.takesTouchAt(400f, 361f))
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (750f to 285f))
|
||||
assertTrue(state().isDown(PadState.A))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (750f to 285f))
|
||||
}
|
||||
|
||||
@Test fun adaptiveSticksCaptureAroundThemAndTheLeftOneToTheEdge() {
|
||||
val left = InputSurfaceElement(InputControl.LS, .3f, .3f, .2f, .4f, hideWhenIdle = true)
|
||||
val right = InputSurfaceElement(InputControl.RS, .6f, .3f, .2f, .4f, hideWhenIdle = true)
|
||||
show(left, right)
|
||||
assertTrue(view.takesTouchAt(10f, 300f))
|
||||
assertTrue(view.takesTouchAt(10f, 390f))
|
||||
assertFalse(view.takesTouchAt(10f, 420f))
|
||||
assertTrue(view.takesTouchAt(795f, 300f))
|
||||
assertFalse(view.takesTouchAt(815f, 300f))
|
||||
val missed = mutableListOf<Int>()
|
||||
view.onTouchMiss = { missed += it.getPointerId(0) }
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (50f to 300f))
|
||||
touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl MotionEvent.ACTION_POINTER_INDEX_SHIFT), 1 to (50f to 300f), 2 to (60f to 310f))
|
||||
assertEquals(listOf(2), missed)
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (150f to 300f), 2 to (60f to 310f))
|
||||
assertTrue(state().leftX > .9f)
|
||||
show(left.copy(hideWhenIdle = false), right.copy(hideWhenIdle = false))
|
||||
assertFalse(view.takesTouchAt(10f, 300f))
|
||||
assertFalse(view.takesTouchAt(815f, 300f))
|
||||
}
|
||||
|
||||
private fun element(root: View, id: String): View = descendants(root).first { (it.tag as? InputSurfaceElement)?.id == id }
|
||||
private fun centre(target: View): Pair<Float, Float> {
|
||||
var x = target.width / 2f
|
||||
var y = target.height / 2f
|
||||
var at: View = target
|
||||
while (at !== view) { x += at.left; y += at.top; at = at.parent as View }
|
||||
return x to y
|
||||
}
|
||||
private fun button(root: View): View = descendants(root).first { it.javaClass.simpleName == "HoldButton" }
|
||||
private fun descendants(root: View): Sequence<View> = sequence {
|
||||
yield(root)
|
||||
if (root is ViewGroup) for (i in 0 until root.childCount) yieldAll(descendants(root.getChildAt(i)))
|
||||
}
|
||||
|
||||
private fun touch(action: Int, vararg pointers: Pair<Int, Pair<Float, Float>>, flags: Int = 0,
|
||||
tool: Int = MotionEvent.TOOL_TYPE_FINGER, target: View = view): Boolean {
|
||||
val properties = pointers.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = tool } }.toTypedArray()
|
||||
val coords = pointers.map { (_, position) -> MotionEvent.PointerCoords().apply { x = position.first; y = position.second; pressure = 1f; size = 1f } }.toTypedArray()
|
||||
val event = MotionEvent.obtain(1000L, time++, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, flags)
|
||||
return try { target.dispatchTouchEvent(event) } finally { event.recycle() }
|
||||
}
|
||||
}
|
||||
@@ -110,7 +110,6 @@ class InputUiLayoutTest {
|
||||
compose.onNodeWithText(context.getString(R.string.setup_tab_controller)).performClick()
|
||||
compose.onNodeWithTag("input-profile-game").assertIsDisplayed()
|
||||
compose.onNodeWithText(context.getString(R.string.ctrl_layout)).assertDoesNotExist()
|
||||
compose.onNodeWithText(context.getString(R.string.ctrl_configure)).assertDoesNotExist()
|
||||
screenshot("controller-tab")
|
||||
compose.onNodeWithTag("input-profile-game").performClick()
|
||||
compose.onNodeWithTag("input-part-physical").performScrollTo().performClick()
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.view.Display
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.currentComposer
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.onNodeWithContentDescription
|
||||
import androidx.compose.ui.test.onNodeWithTag
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.performClick
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.SessionActivity
|
||||
import com.droiddeck.launcher.input.DeckPanel
|
||||
import com.droiddeck.launcher.input.InputLibraries
|
||||
import com.droiddeck.launcher.input.InputSurfaceElement
|
||||
import com.droiddeck.launcher.input.SecondScreenMode
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
import com.droiddeck.launcher.session.SessionService
|
||||
import com.droiddeck.launcher.session.SessionState
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.GraphicsMode
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], qualifiers = "w800dp-h600dp")
|
||||
@GraphicsMode(GraphicsMode.Mode.NATIVE)
|
||||
class SecondScreenBarTest {
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
private val context get() = RuntimeEnvironment.getApplication()
|
||||
private var previousMode = ""
|
||||
private var previousDeckPad = false
|
||||
|
||||
@Before fun setUp() {
|
||||
previousMode = SessionState.mode
|
||||
previousDeckPad = SessionState.deckPad
|
||||
context.getSharedPreferences("input-profiles", android.content.Context.MODE_PRIVATE).edit().clear().commit()
|
||||
SessionPrefs.setDeckPanel(context, DeckPanel.BOTH)
|
||||
}
|
||||
|
||||
@After fun tearDown() {
|
||||
SessionState.mode = previousMode
|
||||
SessionState.deckPad = previousDeckPad
|
||||
}
|
||||
|
||||
private fun presentation(swap: (() -> Unit)? = {}): SecondScreenPresentation {
|
||||
val activity = Robolectric.buildActivity(SessionActivity::class.java).get()
|
||||
val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY)
|
||||
return SecondScreenPresentation(activity, display, {}, {}, {}, onMenu = {}, onSwap = swap)
|
||||
}
|
||||
|
||||
private fun descendants(view: View): Sequence<View> = sequence {
|
||||
yield(view)
|
||||
if (view is ViewGroup) for (index in 0 until view.childCount) yieldAll(descendants(view.getChildAt(index)))
|
||||
}
|
||||
|
||||
private fun rootChildren(panel: SecondScreenPresentation): List<View> {
|
||||
val root: ViewGroup = ReflectionHelpers.getField(panel, "root")
|
||||
return (0 until root.childCount).map { root.getChildAt(it) }
|
||||
}
|
||||
|
||||
@Test fun trackpadPanelHasTheBarWithTheKeyboardToggleState() {
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
val panel = presentation()
|
||||
try {
|
||||
panel.show()
|
||||
panel.showMode(SecondScreenMode.KEYBOARD_TRACKPAD)
|
||||
val children = rootChildren(panel)
|
||||
assertTrue(children[0] is ComposeView)
|
||||
assertTrue(children[1] is InputSurfaceView)
|
||||
val bar = panel.barModel(SecondScreenMode.KEYBOARD_TRACKPAD)!!
|
||||
assertEquals(context.getString(R.string.second_mode_keyboard_trackpad), bar.title)
|
||||
assertNotNull(bar.onClose); assertNotNull(bar.onMenu); assertNotNull(bar.onSteam); assertNotNull(bar.onQam)
|
||||
assertNull(bar.tabs)
|
||||
val keyboard = bar.extras.single()
|
||||
assertEquals(context.getString(R.string.second_show_keyboard), keyboard.description)
|
||||
assertFalse(keyboard.active)
|
||||
ReflectionHelpers.getField<MutableState<Boolean>>(panel, "keyboardShown").value = true
|
||||
val shown = panel.barModel(SecondScreenMode.KEYBOARD_TRACKPAD)!!.extras.single()
|
||||
assertEquals(context.getString(R.string.second_hide_keyboard), shown.description)
|
||||
assertTrue(shown.active)
|
||||
} finally { panel.closeControls() }
|
||||
}
|
||||
|
||||
@Test fun deckPanelHasTheBarWithSwapAndTabsAndProfileControlsStayFullScreen() {
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
SessionState.deckPad = true
|
||||
var swaps = 0
|
||||
val panel = presentation { swaps++ }
|
||||
try {
|
||||
panel.show()
|
||||
panel.showMode(SecondScreenMode.DECK_CONTROLS)
|
||||
assertTrue(rootChildren(panel)[0] is ComposeView)
|
||||
val bar = panel.barModel(SecondScreenMode.DECK_CONTROLS)!!
|
||||
assertEquals(context.getString(R.string.second_mode_deck_controls), bar.title)
|
||||
assertEquals(listOf(context.getString(R.string.swap_screens)), bar.extras.map { it.description })
|
||||
bar.extras.single().onClick()
|
||||
assertEquals(1, swaps)
|
||||
assertEquals(DeckPanel.entries.map { context.getString(it.label) }, bar.tabs!!.labels)
|
||||
assertEquals(DeckPanel.BOTH.ordinal, bar.tabs!!.selected)
|
||||
panel.showMode(SecondScreenMode.CUSTOM_CONTROLS)
|
||||
assertNull(panel.barModel(SecondScreenMode.CUSTOM_CONTROLS))
|
||||
assertTrue(rootChildren(panel)[0] is InputSurfaceView)
|
||||
} finally { panel.closeControls() }
|
||||
val noSwap = presentation(swap = null)
|
||||
try {
|
||||
noSwap.show()
|
||||
noSwap.showMode(SecondScreenMode.DECK_CONTROLS)
|
||||
assertTrue(noSwap.barModel(SecondScreenMode.DECK_CONTROLS)!!.extras.isEmpty())
|
||||
} finally { noSwap.closeControls() }
|
||||
}
|
||||
|
||||
@Test fun nonSteamSessionsHideSteamAndQuickAccess() {
|
||||
for (mode in listOf(SessionService.MODE_DESKTOP, SessionService.MODE_RUN)) {
|
||||
SessionState.mode = mode
|
||||
SessionState.deckPad = true
|
||||
val panel = presentation()
|
||||
try {
|
||||
panel.show()
|
||||
for (panelMode in listOf(SecondScreenMode.KEYBOARD_TRACKPAD, SecondScreenMode.DECK_CONTROLS)) {
|
||||
panel.showMode(panelMode)
|
||||
val bar = panel.barModel(panelMode)!!
|
||||
assertNull(bar.onSteam); assertNull(bar.onQam)
|
||||
assertNotNull(bar.onClose); assertNotNull(bar.onMenu)
|
||||
}
|
||||
} finally { panel.closeControls() }
|
||||
}
|
||||
val stock = InputLibraries.read(context).layouts.filter { it.id == InputLibraries.DECK || it.id == InputLibraries.TRACKPAD }
|
||||
assertTrue(stock.all { layout -> layout.elements.none { it.id.startsWith("command:") } })
|
||||
}
|
||||
|
||||
@Test fun deckTabsSwitchTheLayoutAndAreRemembered() {
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
SessionState.deckPad = true
|
||||
val library = InputLibraries.read(context)
|
||||
fun shown(panel: SecondScreenPresentation): List<InputSurfaceElement> {
|
||||
val controls = descendants(panel.window!!.decorView).filterIsInstance<InputSurfaceView>().single()
|
||||
return descendants(controls).mapNotNull { it.tag as? InputSurfaceElement }.toList()
|
||||
}
|
||||
val first = presentation()
|
||||
try {
|
||||
first.show()
|
||||
first.showMode(SecondScreenMode.DECK_CONTROLS)
|
||||
assertEquals(library.layouts.single { it.id == DeckPanel.BOTH.layout }.elements, shown(first))
|
||||
val controls = descendants(first.window!!.decorView).filterIsInstance<InputSurfaceView>().single()
|
||||
for (panel in listOf(DeckPanel.GRIPS, DeckPanel.LEFT, DeckPanel.RIGHT, DeckPanel.GAMEPAD)) {
|
||||
first.barModel(SecondScreenMode.DECK_CONTROLS)!!.tabs!!.onSelect(panel.ordinal)
|
||||
assertEquals(panel, SessionPrefs.deckPanel(context))
|
||||
assertEquals(panel.ordinal, first.barModel(SecondScreenMode.DECK_CONTROLS)!!.tabs!!.selected)
|
||||
assertSame(controls, descendants(first.window!!.decorView).filterIsInstance<InputSurfaceView>().single())
|
||||
assertEquals(library.layouts.single { it.id == panel.layout }.elements, shown(first))
|
||||
}
|
||||
first.barModel(SecondScreenMode.DECK_CONTROLS)!!.tabs!!.onSelect(DeckPanel.GRIPS.ordinal)
|
||||
} finally { first.closeControls() }
|
||||
val second = presentation()
|
||||
try {
|
||||
second.show()
|
||||
second.showMode(SecondScreenMode.DECK_CONTROLS)
|
||||
assertEquals(DeckPanel.GRIPS.ordinal, second.barModel(SecondScreenMode.DECK_CONTROLS)!!.tabs!!.selected)
|
||||
assertEquals(library.layouts.single { it.id == DeckPanel.GRIPS.layout }.elements, shown(second))
|
||||
second.showMode(SecondScreenMode.KEYBOARD_TRACKPAD)
|
||||
assertEquals(library.layouts.single { it.id == InputLibraries.TRACKPAD }.elements, shown(second))
|
||||
} finally { second.closeControls() }
|
||||
assertEquals(DeckPanel.GRIPS.bindings, SecondScreenMode.DECK_CONTROLS.panel(context))
|
||||
assertEquals(SecondScreenMode.KEYBOARD_TRACKPAD.bindings, SecondScreenMode.KEYBOARD_TRACKPAD.panel(context))
|
||||
}
|
||||
|
||||
@Test fun barTabRowSelectsTabs() {
|
||||
var picked = -1
|
||||
compose.setContent {
|
||||
DroidDeckTheme {
|
||||
BottomScreenBar(BottomBarModel("Deck", {}, {}, null, null, tabs = deckPanelTabs(context, DeckPanel.BOTH) { picked = it.ordinal }))
|
||||
}
|
||||
}
|
||||
for (panel in DeckPanel.entries) compose.onNodeWithText(context.getString(panel.label)).assertIsDisplayed()
|
||||
compose.onNodeWithTag("tab-${DeckPanel.LEFT.ordinal}").performClick()
|
||||
compose.runOnIdle { assertEquals(DeckPanel.LEFT.ordinal, picked) }
|
||||
}
|
||||
|
||||
@Test fun swappedTopScreenShowsTheTerminalWithItsBar() {
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
val activity = Robolectric.buildActivity(SessionActivity::class.java).get()
|
||||
ReflectionHelpers.getField<MutableState<SecondScreenMode>>(activity, "secondScreenMode\$delegate").value = SecondScreenMode.TERMINAL
|
||||
val method = SessionActivity::class.java.getDeclaredMethod("SwappedTopPanel", androidx.compose.runtime.Composer::class.java, Int::class.javaPrimitiveType)
|
||||
method.isAccessible = true
|
||||
compose.setContent { DroidDeckTheme { method.invoke(activity, currentComposer, 0) } }
|
||||
compose.onNodeWithText(context.getString(R.string.second_mode_terminal)).assertIsDisplayed()
|
||||
compose.onNodeWithContentDescription(context.getString(R.string.drawer_keyboard)).assertIsDisplayed()
|
||||
compose.onNodeWithContentDescription(context.getString(R.string.swap_screens)).assertIsDisplayed()
|
||||
compose.onNodeWithTag("bottom-bar-steam").assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test fun swappedTopScreenDeckPanelHasTabs() {
|
||||
SessionState.mode = SessionService.MODE_DESKTOP
|
||||
SessionState.deckPad = true
|
||||
val activity = Robolectric.buildActivity(SessionActivity::class.java).get()
|
||||
ReflectionHelpers.getField<MutableState<SecondScreenMode>>(activity, "secondScreenMode\$delegate").value = SecondScreenMode.DECK_CONTROLS
|
||||
val method = SessionActivity::class.java.getDeclaredMethod("SwappedTopPanel", androidx.compose.runtime.Composer::class.java, Int::class.javaPrimitiveType)
|
||||
method.isAccessible = true
|
||||
compose.setContent { DroidDeckTheme { method.invoke(activity, currentComposer, 0) } }
|
||||
compose.onNodeWithText(context.getString(R.string.second_mode_deck_controls)).assertIsDisplayed()
|
||||
compose.onNodeWithTag("bottom-bar-tabs").assertIsDisplayed()
|
||||
compose.onNodeWithTag("bottom-bar-steam").assertDoesNotExist()
|
||||
compose.onNodeWithTag("bottom-bar-close").assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test fun unfoldedControlsGetTheSessionBar() {
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
val activity = Robolectric.buildActivity(SessionActivity::class.java).get()
|
||||
assertTrue(ReflectionHelpers.callInstanceMethod<View>(activity, "unfoldedBar") is ComposeView)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user