Skip to content

A fully functional Android app built entirely with Kotlin and Jetpack Compose

Fix navigation test for ForYou to TopicScreen

Don Turnerfbf05584659a
diff --git a/app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt b/app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt
index c67a589bb..accb3b28c 100644
--- a/app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt
+++ b/app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt
@@ -22,7 +22,9 @@ import androidx.compose.ui.test.assertIsSelected
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createAndroidComposeRule
+import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onAllNodesWithText
+import androidx.compose.ui.test.onFirst
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
@@ -32,6 +34,7 @@ import androidx.test.espresso.Espresso
import androidx.test.espresso.NoActivityResumedException
import com.google.samples.apps.nowinandroid.MainActivity
import com.google.samples.apps.nowinandroid.R
+import com.google.samples.apps.nowinandroid.core.data.repository.NewsRepository
import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepository
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRule
@@ -75,6 +78,9 @@ class NavigationTest {
@Inject
lateinit var topicsRepository: TopicsRepository
+ @Inject
+ lateinit var newsRepository: NewsRepository
+
// The strings used for matching in these tests
private val navigateUp by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_navigate_up)
private val forYou by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_title)
@@ -271,21 +277,27 @@ class NavigationTest {
@Test
fun navigatingToTopicFromForYou_showsTopicDetails() {
composeTestRule.apply {
- // Follow a topic
- onNodeWithText(sampleTopic).performClick()
-
- // Get the topic ID
- val topic = runBlocking {
- topicsRepository.getTopics().first().filter { it.name == sampleTopic }.first()
+ // Get the first news resource
+ val newsResource = runBlocking {
+ newsRepository.getNewsResources().first().first()
}
- // Tap the first topic chip
- onNodeWithTag("topicChip:${topic.id}", useUnmergedTree = true)
- .assertExists()
- .performClick()
+ // Get its first topic and follow it
+ val topic = newsResource.topics.first()
+ onNodeWithText(topic.name).performClick()
- // TEST FAILING HERE
- composeTestRule.waitUntil(timeoutMillis = 3_600_000, condition = { false })
+ // Get the news feed and scroll to the news resource
+ // Note: Possible flakiness. If the content of the news resource is long then the topic
+ // tag might not be visible meaning it cannot be clicked
+ onNodeWithTag("forYou:feed")
+ .performScrollToNode(
+ hasTestTag("newsResourceCard:${newsResource.id}"),
+ )
+
+ // Click the first topic tag
+ onAllNodesWithTag("topicTag:${topic.id}", useUnmergedTree = true)
+ .onFirst()
+ .performClick()
// Verify that we're on the correct topic details screen
onNodeWithTag("topic:${topic.id}").assertExists()
diff --git a/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt
index eaaea73aa..2395eb156 100644
--- a/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt
+++ b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt
@@ -117,9 +117,11 @@ fun NewsResourceCardExpanded(
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
// Use custom label for accessibility services to communicate button's action to user.
// Pass null for action to only override the label and not the actual action.
- modifier = modifier.semantics {
- onClick(label = clickActionLabel, action = null)
- },
+ modifier = modifier
+ .semantics {
+ onClick(label = clickActionLabel, action = null)
+ }
+ .testTag("newsResourceCard:${userNewsResource.id}"),
) {
Column {
if (!userNewsResource.headerImageUrl.isNullOrEmpty()) {
@@ -337,12 +339,13 @@ fun NewsResourceTopics(
}
Text(
text = followableTopic.topic.name.uppercase(Locale.getDefault()),
- modifier = Modifier.semantics {
- this.contentDescription = contentDescription
- },
+ modifier = Modifier
+ .semantics {
+ this.contentDescription = contentDescription
+ }
+ .testTag("topicTag:${followableTopic.topic.id}"),
)
},
- modifier = Modifier.testTag("topicChip:${followableTopic.topic.id}"),
)
}
}