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.ktindex 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.assertIsSelectedimport androidx.compose.ui.test.hasTestTagimport androidx.compose.ui.test.hasTextimport androidx.compose.ui.test.junit4.createAndroidComposeRule+import androidx.compose.ui.test.onAllNodesWithTagimport androidx.compose.ui.test.onAllNodesWithText+import androidx.compose.ui.test.onFirstimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithText@@ -32,6 +34,7 @@ import androidx.test.espresso.Espressoimport androidx.test.espresso.NoActivityResumedExceptionimport com.google.samples.apps.nowinandroid.MainActivityimport com.google.samples.apps.nowinandroid.R+import com.google.samples.apps.nowinandroid.core.data.repository.NewsRepositoryimport com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepositoryimport com.google.samples.apps.nowinandroid.core.model.data.Topicimport com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRule@@ -75,6 +78,9 @@ class NavigationTest {@Injectlateinit var topicsRepository: TopicsRepository+ @Inject+ lateinit var newsRepository: NewsRepository+// The strings used for matching in these testsprivate 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 {@Testfun 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 screenonNodeWithTag("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.ktindex 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}"),)}}