Skip to content

Commit fbf0558

Browse files
committed
Fix navigation test for ForYou to TopicScreen
1 parent 2eee83e commit fbf0558

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import androidx.compose.ui.test.assertIsSelected
2222
import androidx.compose.ui.test.hasTestTag
2323
import androidx.compose.ui.test.hasText
2424
import androidx.compose.ui.test.junit4.createAndroidComposeRule
25+
import androidx.compose.ui.test.onAllNodesWithTag
2526
import androidx.compose.ui.test.onAllNodesWithText
27+
import androidx.compose.ui.test.onFirst
2628
import androidx.compose.ui.test.onNodeWithContentDescription
2729
import androidx.compose.ui.test.onNodeWithTag
2830
import androidx.compose.ui.test.onNodeWithText
@@ -32,6 +34,7 @@ import androidx.test.espresso.Espresso
3234
import androidx.test.espresso.NoActivityResumedException
3335
import com.google.samples.apps.nowinandroid.MainActivity
3436
import com.google.samples.apps.nowinandroid.R
37+
import com.google.samples.apps.nowinandroid.core.data.repository.NewsRepository
3538
import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepository
3639
import com.google.samples.apps.nowinandroid.core.model.data.Topic
3740
import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRule
@@ -75,6 +78,9 @@ class NavigationTest {
7578
@Inject
7679
lateinit var topicsRepository: TopicsRepository
7780

81+
@Inject
82+
lateinit var newsRepository: NewsRepository
83+
7884
// The strings used for matching in these tests
7985
private val navigateUp by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_navigate_up)
8086
private val forYou by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_title)
@@ -271,21 +277,27 @@ class NavigationTest {
271277
@Test
272278
fun navigatingToTopicFromForYou_showsTopicDetails() {
273279
composeTestRule.apply {
274-
// Follow a topic
275-
onNodeWithText(sampleTopic).performClick()
276-
277-
// Get the topic ID
278-
val topic = runBlocking {
279-
topicsRepository.getTopics().first().filter { it.name == sampleTopic }.first()
280+
// Get the first news resource
281+
val newsResource = runBlocking {
282+
newsRepository.getNewsResources().first().first()
280283
}
281284

282-
// Tap the first topic chip
283-
onNodeWithTag("topicChip:${topic.id}", useUnmergedTree = true)
284-
.assertExists()
285-
.performClick()
285+
// Get its first topic and follow it
286+
val topic = newsResource.topics.first()
287+
onNodeWithText(topic.name).performClick()
286288

287-
// TEST FAILING HERE
288-
composeTestRule.waitUntil(timeoutMillis = 3_600_000, condition = { false })
289+
// Get the news feed and scroll to the news resource
290+
// Note: Possible flakiness. If the content of the news resource is long then the topic
291+
// tag might not be visible meaning it cannot be clicked
292+
onNodeWithTag("forYou:feed")
293+
.performScrollToNode(
294+
hasTestTag("newsResourceCard:${newsResource.id}"),
295+
)
296+
297+
// Click the first topic tag
298+
onAllNodesWithTag("topicTag:${topic.id}", useUnmergedTree = true)
299+
.onFirst()
300+
.performClick()
289301

290302
// Verify that we're on the correct topic details screen
291303
onNodeWithTag("topic:${topic.id}").assertExists()

core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ fun NewsResourceCardExpanded(
117117
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
118118
// Use custom label for accessibility services to communicate button's action to user.
119119
// Pass null for action to only override the label and not the actual action.
120-
modifier = modifier.semantics {
121-
onClick(label = clickActionLabel, action = null)
122-
},
120+
modifier = modifier
121+
.semantics {
122+
onClick(label = clickActionLabel, action = null)
123+
}
124+
.testTag("newsResourceCard:${userNewsResource.id}"),
123125
) {
124126
Column {
125127
if (!userNewsResource.headerImageUrl.isNullOrEmpty()) {
@@ -337,12 +339,13 @@ fun NewsResourceTopics(
337339
}
338340
Text(
339341
text = followableTopic.topic.name.uppercase(Locale.getDefault()),
340-
modifier = Modifier.semantics {
341-
this.contentDescription = contentDescription
342-
},
342+
modifier = Modifier
343+
.semantics {
344+
this.contentDescription = contentDescription
345+
}
346+
.testTag("topicTag:${followableTopic.topic.id}"),
343347
)
344348
},
345-
modifier = Modifier.testTag("topicChip:${followableTopic.topic.id}"),
346349
)
347350
}
348351
}

0 commit comments

Comments
 (0)