Skip to content

Commit

Permalink
Improve test utility and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaslogen committed Aug 1, 2022
1 parent 44448f4 commit 870115c
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ interface ComposeTestUtils {
.value as List<AnnotatedString>).first().toString()
}

/**
* Get the existing [ComposeView] from the Activity and set the Compose content there, but only if the ComposeView already exists.
* Otherwise, just call [AndroidComposeTestRule.setContent] to create a new [ComposeView], add the view to the Activity and set its Compose content.
*/
fun AndroidComposeTestRule<ActivityScenarioRule<ComposeActivity>, ComposeActivity>.clearAndSetContent(content: @Composable () -> Unit) {
(this.activity.findViewById<ViewGroup>(android.R.id.content).getChildAt(0) as ComposeView).setContent(content)
(this.activity.findViewById<ViewGroup>(android.R.id.content)?.getChildAt(0) as? ComposeView)?.setContent(content)
?: this.setContent(content)
}

0 comments on commit 870115c

Please sign in to comment.