Ying Chen
06/15/2023, 5:35 PMcomposeTestRule.onNodeWithText(xxx).performClick()
doesn't work for an AndroidView wrapped Button
I also tried using a Modifier.testTag for the AndroidView wrapped Button, it can find the Button but won't be clickabledewildte
06/15/2023, 5:46 PMonView
?Ying Chen
06/15/2023, 5:49 PMEspresso.onView(ViewMatchers.withText(xxx)).perform(click())
Unfortunately Espresso doesn't work for both pure Compose case or AndroidView wrapped Button casedewildte
06/15/2023, 5:50 PMdewildte
06/15/2023, 5:50 PMModifier.clickable
.Ying Chen
06/15/2023, 5:55 PMdewildte
06/15/2023, 5:57 PMYing Chen
06/15/2023, 5:57 PMdewildte
06/15/2023, 5:57 PMEspresso.onView(ViewMatchers.withText(xxx)).perform(click())
Should work.dewildte
06/15/2023, 5:59 PMcomposeTestRule.onNodeWithTestTag(YOUR_TEST_TAG).printToString()
produce?Ying Chen
06/15/2023, 6:07 PM|-Node #9 at (l=136.0, t=57.0, r=184.0, b=116.0)px, Tag: 'xxx'if pure compose:
|-Node #9 at (l=136.0, t=57.0, r=200.0, b=114.0)px Role = 'Button' Focused = 'false' Actions = [OnClick, RequestFocus] MergeDescendants = 'true' MergeDescendants = 'true' |-Node #11 at (l=166.0, t=65.0, r=170.0, b=106.0)px Text = '[xxx]' Actions = [GetTextLayoutResult]
Ying Chen
06/15/2023, 6:11 PMAndroidWrappedButton() {
it.text = "xxx"
it.setOnClickListener { onClick.invoke() }
}
Ying Chen
06/15/2023, 6:15 PMdewildte
06/15/2023, 6:16 PMdewildte
06/15/2023, 6:16 PMYing Chen
06/15/2023, 6:20 PMModifier.semantics { text = ... }
and it requires a annotated string instead of a stringYing Chen
06/15/2023, 6:23 PMdewildte
06/15/2023, 6:24 PMNode #9Should be clickable, no?
Ying Chen
06/15/2023, 6:25 PM> Node #9
Should be clickable, no?AndroidView wrapped component, no so it can be "clicked" but the click event is not triggered 😕
Ying Chen
06/15/2023, 6:32 PM.semantics { text = AnnotatedString("xxx") })
dewildte
06/15/2023, 8:59 PMYing Chen
06/15/2023, 9:45 PMval onClick
, no matter use modifier.clickable or it.setOnClickListener
@Composable
fun AndroidWrappedButton(
modifier: Modifier = Modifier,
setup: (Button) -> Unit = {}
) {
AndroidView(modifier = modifier, factory = { context -> Button(context) }, update = setup)
}
@Test
fun testAndroidWrappedButton() {
val onClick: () -> Unit = {
println("onClick called")
}
composeTestRule.setContent {
AndroidWrappedButton(
Modifier
.semantics { text = AnnotatedString("AndroidButton") }
.clickable { onClick }
) {
// it.setOnClickListener {
// onClick
// }
}
}
val androidButtonViewText = composeTestRule.onNodeWithText("AndroidButton")
androidButtonViewText.assertExists()
androidButtonViewText.performClick()
}
Ying Chen
06/15/2023, 9:53 PM@Test
fun testAndroidWrappedButton() {
val onClick: () -> Unit = {
println("onClick called")
}
composeTestRule.setContent {
AndroidWrappedButton(
Modifier
.semantics { text = AnnotatedString("AndroidButton") }
.clickable {
println("print clickable")
onClick
}
) {
it.setOnClickListener {
println("print setOnClickListener")
onClick
}
}
}
val androidButtonViewText = composeTestRule.onNodeWithText("AndroidButton")
androidButtonViewText.assertExists()
androidButtonViewText.performClick()
}