7

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

  1. What am I missing here and how can I fix it?
theapache64
  • 10,926
  • 9
  • 65
  • 108
  • Hey!! how did you fix it? – Akshay Ashok Nov 23 '21 at 13:34
  • @AkshayAshok I didn't. testing orientation change is not supported yet. here's a ticket to track: https://issuetracker.google.com/issues/201900572 – theapache64 Nov 23 '21 at 15:36
  • actually, I was trying to do an end-to-end test which involves a button click, I was using `ComposeRule.onNodeWithContentDescription("name").performClick` to do it, and I faced the same "**No compose views found in the app. Is your Activity resumed?**" I thought you can help me to find the cause. – Akshay Ashok Nov 23 '21 at 15:53
  • Can you show me your code? – theapache64 Nov 24 '21 at 04:50
  • Thanks, @theapache64 [Test case code](https://github.com/AkshayAshokCode/NotePad/blob/1ef08366f06fbf69297b16fc8b1ad5506032ee4f/app/src/androidTest/java/com/akshayashokcode/notepad/feature_note/presentation/NotesEndToEndTest.kt#L80) and [Code used to initialize button on screen](https://github.com/AkshayAshokCode/NotePad/blob/1ef08366f06fbf69297b16fc8b1ad5506032ee4f/app/src/main/java/com/akshayashokcode/notepad/feature_note/presentation/notes/NotesScreen.kt#L48) – Akshay Ashok Nov 24 '21 at 05:49

2 Answers2

22

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.

Primož Ivančič
  • 1,984
  • 1
  • 17
  • 29
3

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.

Htue Ko
  • 119
  • 2
  • 7