Fix navigation test for ForYou to TopicScreen

pull/1681/head
Don Turner 2 months ago
parent 2eee83eeda
commit fbf0558465

@ -22,7 +22,9 @@ import androidx.compose.ui.test.assertIsSelected
import androidx.compose.ui.test.hasTestTag import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onFirst
import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.onNodeWithText
@ -32,6 +34,7 @@ import androidx.test.espresso.Espresso
import androidx.test.espresso.NoActivityResumedException import androidx.test.espresso.NoActivityResumedException
import com.google.samples.apps.nowinandroid.MainActivity import com.google.samples.apps.nowinandroid.MainActivity
import com.google.samples.apps.nowinandroid.R 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.data.repository.TopicsRepository
import com.google.samples.apps.nowinandroid.core.model.data.Topic import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRule import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRule
@ -75,6 +78,9 @@ class NavigationTest {
@Inject @Inject
lateinit var topicsRepository: TopicsRepository lateinit var topicsRepository: TopicsRepository
@Inject
lateinit var newsRepository: NewsRepository
// The strings used for matching in these tests // The strings used for matching in these tests
private val navigateUp by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_navigate_up) private val navigateUp by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_navigate_up)
private val forYou by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_title) private val forYou by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_title)
@ -271,21 +277,27 @@ class NavigationTest {
@Test @Test
fun navigatingToTopicFromForYou_showsTopicDetails() { fun navigatingToTopicFromForYou_showsTopicDetails() {
composeTestRule.apply { composeTestRule.apply {
// Follow a topic // Get the first news resource
onNodeWithText(sampleTopic).performClick() val newsResource = runBlocking {
newsRepository.getNewsResources().first().first()
// Get the topic ID
val topic = runBlocking {
topicsRepository.getTopics().first().filter { it.name == sampleTopic }.first()
} }
// Tap the first topic chip // Get its first topic and follow it
onNodeWithTag("topicChip:${topic.id}", useUnmergedTree = true) val topic = newsResource.topics.first()
.assertExists() onNodeWithText(topic.name).performClick()
.performClick()
// TEST FAILING HERE // Get the news feed and scroll to the news resource
composeTestRule.waitUntil(timeoutMillis = 3_600_000, condition = { false }) // 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 // Verify that we're on the correct topic details screen
onNodeWithTag("topic:${topic.id}").assertExists() onNodeWithTag("topic:${topic.id}").assertExists()

@ -117,9 +117,11 @@ fun NewsResourceCardExpanded(
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface), colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
// Use custom label for accessibility services to communicate button's action to user. // 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. // Pass null for action to only override the label and not the actual action.
modifier = modifier.semantics { modifier = modifier
onClick(label = clickActionLabel, action = null) .semantics {
}, onClick(label = clickActionLabel, action = null)
}
.testTag("newsResourceCard:${userNewsResource.id}"),
) { ) {
Column { Column {
if (!userNewsResource.headerImageUrl.isNullOrEmpty()) { if (!userNewsResource.headerImageUrl.isNullOrEmpty()) {
@ -337,12 +339,13 @@ fun NewsResourceTopics(
} }
Text( Text(
text = followableTopic.topic.name.uppercase(Locale.getDefault()), text = followableTopic.topic.name.uppercase(Locale.getDefault()),
modifier = Modifier.semantics { modifier = Modifier
this.contentDescription = contentDescription .semantics {
}, this.contentDescription = contentDescription
}
.testTag("topicTag:${followableTopic.topic.id}"),
) )
}, },
modifier = Modifier.testTag("topicChip:${followableTopic.topic.id}"),
) )
} }
} }

Loading…
Cancel
Save