Compose Preview Screenshot Testing is a new screenshot testing framework from Google. As of writing, it is still in the Alpha stage (v0.0.1-alpha16).I was trying it out in a simple TO-DO app project and I came across a use case that this testing where this framework falls short. Later I figured out a workaround. I did not find any solution on the internet or by AI.So here it is,I have a TaskItem Composable with Title and Description texts, Description is hidden by default and only visible after user clicks on that item.@Composablefun TaskItem( id: Long, title: String, isCompleted: Boolean, onToggle: () -> Unit) { var isExpanded by remember { mutableStateOf(false) } val hasDescription = !description.isNullOrBlank() Row( modifier.clickable{ isExpanded = !isExpanded } ) { Text( text = title, .... ) .... AnimatedVisibility(visible = isExpanded && hasDescription) { Text( text = description ?: "", .. ) } }}I can take a screenshot of this TaskItem in the default state directly.@PreviewTest@Preview@Composablefun TaskItemNotCompletedPreview() { WeekendtasksTheme(dynamicColor = false) { TaskItem( id = 2, title = "Active task", isCompleted = false, onToggle = {}, description = null ) }}And I get something like thisActive TODO Item screenshotBut how do I test description expanded test? One way is to pass the default expanded value from the outside of a composable. But then we are exposing internal state of a composable to the outside. We should keep the state as close to the composable as possible. This is especially important while creating Compose UI libraries.//Incorrect way ❌❌❌@Composablefun TaskItem( id: Long, title: String, isCompleted: Boolean, onToggle: () -> Unit, expanded: Boolean = false) { var isExpanded by remember { mutableStateOf(expanded) } val hasDescription = !description.isNullOrBlank() Row( modifier.clickable{ isExpanded = !isExpanded } ) { Text( text = title, .... ) .... AnimatedVisibility(visible = isExpanded && hasDescription) { Text( text = description ?: "", .. ) } }}@PreviewTest@Preview@Composablefun TaskItemNotCompletedExpandedPreview() { WeekendtasksTheme(dynamicColor = false) { TaskItem( id = 2, title = "Active task", isCompleted = false, onToggle = {}, description = null expanded = true ) }}I don’t prefer this approach. So what to do now? When testing with Roborazzi we can access the node and perform OnClick and then take a screenshot but in Compose Preview screenshot testing we use Preview Composables, so there is no direct way to perform OnClick.So I got the idea to use CompositionLocal for the initial default value. Here’s how:internal val LocalExpansion = staticCompositionLocalOf { false }@Composablefun TaskItem( id: Long, title: String, isCompleted: Boolean, onToggle: () -> Unit) { // Default value from CompositionLocal val localExpansion = LocalExpansion.current var isExpanded by remember { mutableStateOf(localExpansion) } val hasDescription = !description.isNullOrBlank() Row( modifier.clickable{ isExpanded = !isExpanded } ) { Text( text = title, .... ) .... AnimatedVisibility(visible = isExpanded && hasDescription) { Text( text = description ?: "", .. ) } }}@PreviewTest@Preview@Composablefun TaskItemWithDescriptionExpandedPreview() { WeekendtasksTheme(dynamicColor = false) { CompositionLocalProvider(LocalExpansion provides true) { TaskItem( id = 6, title = "Task with expanded description", isCompleted = false, onToggle = {}, description = "This is a longer description that provides more context about the task. It shows what the expanded view looks like.") } }}And we have a screenshot of expanded stateTask item with expanded state with descriptionCompose Preview Screenshot testing is still at the alpha stage, so there is still a chance that Google Android team will provide an official and correct way to achieve this.If you know a better way to do this, please do comment or reach out to me. I will either update this blog or remove it completely if this turns out not to be the right approach.I’m currently looking for a new role as a Senior Android Engineer. If you know of a team that’s hiring, or would like to connect, please reach out to me at aniketbhoite1203@gmail.com or via LinkedIn. Thank you!AI disclosure: AI was used only for grammar correction.Test Internal state in Compose Screenshot validation using CompositionLocal was originally published in ProAndroidDev on Medium, where people are continuing the conversation by highlighting and responding to this story.
Test Internal state in Compose Screenshot validation using CompositionLocal
Test Internal state in Compose Screenshot validation using CompositionLocal
Editor
Dec 2, 2026 · 4 min read

From other categories

Databases
How Atlan Moved Its Data Platform from Spark and StarRocks to DuckDB on S3
just now·7 min

Hardware
Weather Dashboard Shows Off Satellite Data
3h ago·2 min

Security
ISC Stormcast For Wednesday, December 2nd, 2026 https://isc.sans.edu/podcastdetail/10200, (Wed, Dec 2nd)
4h ago·1 min

AI & Machine Learning
Large CSV imports in Datasette (Datasette Advent Day 1)
6h ago·1 min

Operating Systems
Linux App Release Roundup (November 2026)
7h ago·1 min

Programming Languages
Build a .NET decision agent with Microsoft Agent Framework and Foundry
12h ago·17 min