Status Update
Comments
pa...@google.com <pa...@google.com> #2
No, this is not the way it was supposed to be used :)
We did not consider delayed launches of activities. And I will need to think about it. Most likely we will have to provide a version of a composeTestRule that does not accept activity but also does not support "setContent" on it. I will take a look in January.
cl...@google.com <cl...@google.com>
je...@google.com <je...@google.com> #3
Our API is not very friendly to accommodate this scenario, but it is possible to achieve with public API:
@LargeTest
@RunWith(AndroidJUnit4::class)
class LateActivityLaunchTest {
private lateinit var activityScenario: ActivityScenario<CustomActivity>
@get:Rule
val rule = AndroidComposeTestRule(EmptyTestRule()) {
var activity: CustomActivity? = null
activityScenario.onActivity { activity = it }
checkNotNull(activity) { "Activity didn't launch" }
}
class EmptyTestRule : TestRule {
override fun apply(base: Statement, description: Description) = base
}
@Test
fun test() {
setupSomethingFirst()
ActivityScenario.launch<CustomActivity>(
Intent(ApplicationProvider.getApplicationContext(), CustomActivity::class.java)
).use {
activityScenario = it
rule.onNode(hasText("Hello")).assertExists()
}
}
private fun setupSomethingFirst() {
}
}
class CustomActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Text("Hello")
}
}
}
We should provide this in an easier accessible way to users though
Description
Followinghttps://developer.android.com/jetpack/compose/testing
AndroidComposeTestRule's implementation requires a TestRule to be input at creation time. However if a test method needs to construct its own ActivityScenario (eg. custom runtime Intent arguments), the AndroidComposeTestRule ends up owning an unused ActivityScenarioRule. It seems to work, but it's unclear if this breaks things in AndroidComposeTestRule.
Eg, an emulator test:
In this case there are two ActivityScenarios created, one in the @Test method, and a completely unwanted one in ComposeRule.
It would be good to have a utility and documentation for running assertions on a Compose tree embedded in a completely standalone Activity within a test method, without needing to provide the Activity creation in any @Rule.