https://kotlinlang.org logo
#compose
Title
# compose
z

ziv kesten

03/28/2022, 8:37 AM
I am running a simple Test on `LaunchedEffect`:
Copy code
@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: ------------
Copy code
@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?
🧵 2
s

Stylianos Gakis

03/28/2022, 8:57 AM
Is your
Box
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?
z

ziv kesten

03/28/2022, 9:01 AM
I use
pointerInteropFilter
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.
So the problem with my example was that
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
call
👌 1
s

Stylianos Gakis

03/28/2022, 11:36 AM
Aha so at least had nothing to do with my guess. Good to know.
🙏 1
13 Views