Repository: android/nowinandroid
File: app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt (Kotlin)

Code before:
    }

    /*
     * When pressing back from any top level destination except "For you", the app navigates back
     * to the "For you" destination, no matter which destinations you visited in between.
     */
    @Test
    fun navigationBar_backFromAnyDestination_returnsToForYou() {
        composeTestRule.apply {
            // GIVEN the user navigated to the Interests destination
            onNodeWithText(interests).performClick()
            // TODO: Add another destination here to increase test coverage, see b/226357686.
            // WHEN the user uses the system button/gesture to go back,
            Espresso.pressBack()
            // THEN the app shows the For You destination
            onNodeWithText(forYou).assertExists()
        }
    }

    @Test
    fun navigationBar_multipleBackStackInterests() = runTest {
        composeTestRule.apply {
            onNodeWithText(interests).performClick()

            // Select a random topic
            val topic = datasource.getTopics().random().name
            onNodeWithTag("interests:topics").performScrollToNode(hasText(topic))
            onNodeWithText(topic).performClick()

            // Switch tab
            onNodeWithText(forYou).performClick()

            // Come back to Interests
            onNodeWithText(interests).performClick()

            // Verify we're not in the list of interests
            onNodeWithTag("interests:topics").assertDoesNotExist()
        }
    }
}


Review comment (suggestion):
It's not good practice to use anything random in tests as it can make them flaky. Select the last element instead.

Code after:
            // THEN the app quits
        }
    }

    /*
     * When pressing back from any top level destination except "For you", the app navigates back
     * to the "For you" destination, no matter which destinations you visited in between.
     */
    @Test
    fun navigationBar_backFromAnyDestination_returnsToForYou() {
        composeTestRule.apply {
            // GIVEN the user navigated to the Interests destination
            onNodeWithText(interests).performClick()
            // TODO: Add another destination here to increase test coverage, see b/226357686.
            // WHEN the user uses the system button/gesture to go back,
            Espresso.pressBack()
            // THEN the app shows the For You destination
            onNodeWithText(forYou).assertExists()
        }
    }

    @Test
    fun navigationBar_multipleBackStackInterests() = runTest {
        composeTestRule.apply {
            onNodeWithText(interests).performClick()

            // Select the last topic
            val topic = topicsRepository.getTopics().first().sortedBy(Topic::name).last().name
            onNodeWithTag("interests:topics").performScrollToNode(hasText(topic))
            onNodeWithText(topic).performClick()

            // Switch tab
            onNodeWithText(forYou).performClick()

            // Come back to Interests
            onNodeWithText(interests).performClick()

            // Verify we're not in the list of interests
            onNodeWithTag("interests:topics").assertDoesNotExist()
        }
    }
}