ziv kesten
03/28/2022, 8:37 AM@Test
fun testSomeState() {
composeTestRule.setContent {
SomeComposable {
println("click $it")
}
}
composeTestRule.onNodeWithTag("Zivi")
.performClick()
.assertExists()
}
@Composable
fun SomeComposable(onClick: (Int) -> Unit) {
var someState by remember { mutableStateOf(0) }
Button(modifier = Modifier.testTag("Zivi").pointerInteropFilter {
someState = when (it.action) {
MotionEvent.ACTION_DOWN -> { 1 }
MotionEvent.ACTION_UP -> { 2 }
else -> { someState }
}
true
}, onClick = { }) {}
LaunchedEffect(someState) {
println("LaunchedEffect someState $someState") // prints "LaunchedEffect someState 0"
// and then "LaunchedEffect someState 2"
if (someState == 2) { onClick(someState) }
}
}
------------
This works well, however, if i change Button
to a Box
, like this:
------------
@Composable
fun SomeComposable(onClick: (Int) -> Unit) {
var someState by remember { mutableStateOf(0) }
Box(modifier = Modifier.testTag("Zivi").pointerInteropFilter {
someState = when (it.action) {
MotionEvent.ACTION_DOWN -> { 1 }
MotionEvent.ACTION_UP -> { 2 }
else -> { someState }
}
true
}) {}
LaunchedEffect(someState) {
println("LaunchedEffect someState $someState") // prints "LaunchedEffect someState 0"
if (someState == 2) { onClick(someState) }
}
}
Then the LaunchedEffect
is not called on performClick
, can anyone help me understand why?Stylianos Gakis
03/28/2022, 8:57 AMBox
clickable? Not super familiar with the pointerInteropFilter
modifier, but Button under the hood calls a surface where a Modifier.clickable
is attached to it. While the pointerInteropFilter
modifier doesn’t seem to do that. That may be the issue at first glance.
Maybe the implementation of the test performClick
relies on something that is set there?ziv kesten
03/28/2022, 9:01 AMpointerInteropFilter
since i am creating a custom button with "Hold" and "release" actions.
The functionality works, however when trying to UI test this i found this edge case where LaunchedEffect isnt called - only on the UI test.
I assume from you insight that performClick
from the testRule is related to clickable
somehow.
This might be a good lead.Box
had no size, when adding some size like `fillMaxSize`() to it, the LaunchedEffect
is called.
Unfortunately in my original problem (which i simplified to this experiment for understanding) i still dont get the LaunchedEffect
callStylianos Gakis
03/28/2022, 11:36 AM