czuckie
11/17/2022, 8:28 AMLaunchedEffect
but I can't find a way to emphasise that with a test.czuckie
11/17/2022, 8:40 AM@Test
fun onlyInvokesCallbackOnce() {
var count = 0
rule.setContent {
var targetSize by remember { mutableStateOf(0.dp) }
val size by animateDpAsState(targetValue = targetSize)
Box(modifier = Modifier.size(size)) {
MyComposable {
count++
}
}
LaunchedEffect(Unit) { targetSize = 100.dp }
}
rule.waitForIdle()
assertEquals(1, count)
}
Hugo Bernardi
11/17/2022, 9:35 AMval composeTestRule: ComposeTestRule = createEmptyComposeRule()
protected lateinit var scenario: ActivityScenario<HiltComponentActivity> // replace if needed
@Before
override fun setup() {
super.setup()
scenario = ActivityScenario.launch(HiltComponentActivity::class.java) // replace if needed
}
@Test
fun onlyInvokesCallbackOnce() {
var count = 0
val inc: () -> Unit = { count++ }
scenario.onActivity { activity -> activity.setContent { Test(inc) } }
assertEquals(1, count)
scenario.recreate()
scenario.onActivity { activity -> activity.setContent { Test(inc) } }
assertEquals(2, count)
}
@Composable
fun Test(inc: () -> Unit) {
var targetSize by remember { mutableStateOf(0.dp) }
val size by animateDpAsState(targetValue = targetSize)
Box(modifier = Modifier.size(size)) {
MyComposable {
inc()
}
}
LaunchedEffect(Unit) { targetSize = 100.dp }
}
Hugo Bernardi
11/17/2022, 9:42 AMrememberSaveable
with custom Saver but I may have misunderstood your problem.Zach Klippenstein (he/him) [MOD]
11/17/2022, 10:22 PMrememberSaveable
you might want to use StateRestorationTester instead.Zach Klippenstein (he/him) [MOD]
11/17/2022, 10:26 PMHugo Bernardi
11/18/2022, 7:34 AM