Reputation: 11754
I have a Composable
that has a Text
and Button
. Text
will show P
if the current orientation is portrait, and L
otherwise. Clicking on the Button
will change the orientation to landscape, (So after that, it should change the text from P
to L
)
Here's the Composable
@Composable
fun MyApp() {
val currentOrientation = LocalConfiguration.current.orientation
val orientation = if (currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
"P"
} else {
"L"
}
val activity = LocalContext.current as Activity
Column {
Text(text = orientation)
Button(onClick = {
// change orientation to landscape
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}) {
Text(text = "DO IT")
}
}
}
and here's how am testing it
@get:Rule
val composeRule = createComposeRule()
@Test
fun test() {
composeRule.setContent { MyApp() }
// Starts with portrait
composeRule.onNodeWithText("P").assertIsDisplayed()
// Change the orientation to Landscape
composeRule.onNodeWithText("DO IT").performClick()
// Now the text should be `L`
composeRule.onNodeWithText("L").assertIsDisplayed()
}
But I am getting the below error when I run the test to see if the text is updated or not. (Manual test works though)
java.lang.IllegalStateException: No compose views found in the app. Is your Activity resumed?
at androidx.compose.ui.test.TestContext.getAllSemanticsNodes$ui_test_release(TestOwner.kt:96)
at androidx.compose.ui.test.SemanticsNodeInteraction.fetchSemanticsNodes$ui_test_release(SemanticsNodeInteraction.kt:82)
at androidx.compose.ui.test.SemanticsNodeInteraction.fetchOneOrDie(SemanticsNodeInteraction.kt:155)
at androidx.compose.ui.test.SemanticsNodeInteraction.assertExists(SemanticsNodeInteraction.kt:147)
at androidx.compose.ui.test.SemanticsNodeInteraction.assertExists$default(SemanticsNodeInteraction.kt:146)
Here's the complete test file if you want to try it yourself.
Questions
Upvotes: 12
Views: 5982
Reputation: 177
I faced the same exact problem when performing the click event. At the time I'm testing with my device, at happened when your test device/emulator's screen is not awake (turn off).
My fix is just turn on the screen.
Upvotes: 5
Reputation: 2376
Was the screen on during the test?
In my case, this is easily reproduceable by simply turning the screen off. Note that I am using the emulator.
Upvotes: 28