android 如何在组合测试中模拟卡上的刷卡手势?

u91tlkcl  于 2022-12-21  发布在  Android
关注(0)|答案(1)|浏览(106)

我有一个应用程序,它有一个可组合的MyCard()
我正在myTest()中测试应用程序,希望在卡上模拟swipeRight手势。
当我使用performTouchInput { swipeRight() }时,什么也没发生。UI不更新,卡停留在同一个地方。
我怎样才能在卡片上模拟向右刷卡的手势?我错过了什么?

预期结果

编号

@OptIn(ExperimentalCoroutinesApi::class)
class MyTest {
    @get:Rule
    val composeRule = createComposeRule()

    @Before
    fun setUp() {
        composeRule.setContent {
             MyCard()
        }
    }

    @Test
    fun myTest() = runTest {
        composeRule.onNodeWithTag("DraggableCard")
            .performTouchInput { swipeRight() }
    }
}
@SuppressLint("UnusedTransitionTargetStateParameter")
@Composable
fun MyCard() {
    var swipeState by remember { mutableStateOf(false) }

    val transitionState = remember {
        MutableTransitionState(swipeState).apply { targetState = !swipeState }
    }
    val transition = updateTransition(transitionState, "cardTransition")

    val offsetTransition by transition.animateFloat(
        label = "cardOffsetTransition",
        transitionSpec = { tween(durationMillis = 300) },
        targetValueByState = { if (swipeState) 75f else 0f },)

    Card(
        modifier = Modifier
            .testTag("DraggableCard")
            .fillMaxWidth()
            .height(35.dp)
            .padding(horizontal = 4.dp, vertical = 1.dp)
            .offset { IntOffset(offsetTransition.roundToInt(), 0) }
            .pointerInput(Unit) {
                detectHorizontalDragGestures { _, dragAmount ->
                    when {
                        dragAmount >= 6 -> { swipeState = true }
                        dragAmount < -6 -> { swipeState = false }
                    }
                }
            },
        backgroundColor = Color.Gray,
        content = { Text(text = "Hello") }
    )
}
cgh8pdjw

cgh8pdjw1#

我必须小心我在哪个节点上滑动,以及我如何控制测试autoclock
如果它不起作用,尝试向右刷语义层次结构中的不同节点,而不是卡本身。
下面是最终为我工作的伪代码。

private fun swipeRight(symbol: String) {
    composeRule.waitForIdle()

    composeRule.mainClock.autoAdvance = false

    composeRule.onNode(hasText("DraggableCard"))
        .performTouchInput { swipeRight() }

    composeRule.mainClock.advanceTimeBy(ANIMATION_DURATION.toLong() + 5L) /*add 5s buffer*/

    composeRule.mainClock.autoAdvance = true

    composeRule.onNode(hasText("DraggableCard")).assertExists()
        .performClick()

}
@SuppressLint("UnusedTransitionTargetStateParameter")
@Composable
fun DraggableCard(
    isSwiped: Boolean,
    cardHeight: Dp,
    cardOffset: Float,
) {
    val transitionState = remember {
        MutableTransitionState(isSwiped).apply {
            targetState = !isSwiped
        }
    }
    val transition = updateTransition(transitionState, "cardTransition")

    val offsetTransition by transition.animateFloat(
        label = "cardOffsetTransition",
        transitionSpec = { tween(durationMillis = 300) },
        targetValueByState = { if (isSwiped) cardOffset else 0f },
        )

    Card(
        modifier = Modifier
            .semantics(mergeDescendants = false) {
                testTag = "DraggableCard"
            }
            .fillMaxWidth()
            .height(cardHeight)
            .offset { IntOffset(offsetTransition.roundToInt(), 0) }
            .pointerInput(Unit) {
                detectHorizontalDragGestures { _, dragAmount ->
                    when {
                        dragAmount >= 6 -> {}
                        dragAmount < -6 -> {}
                    }
                }
            },
        content =
        { Text(text = "Hello") }
    )
}

相关问题