From ecfb5157dd8f888697d112ac76f9d1c88382a07a Mon Sep 17 00:00:00 2001 From: Don Turner Date: Fri, 1 Nov 2024 22:31:25 +0000 Subject: [PATCH] Improve test data and add UI test for topic chip --- .../apps/nowinandroid/ui/NavigationTest.kt | 23 +++++++++++++++++++ .../test/repository/FakeNewsRepository.kt | 14 +++++------ .../core/network/model/NetworkNewsResource.kt | 14 +++++++++++ .../core/network/model/NetworkTopic.kt | 10 ++++++++ .../nowinandroid/core/ui/NewsResourceCard.kt | 2 ++ 5 files changed, 55 insertions(+), 8 deletions(-) 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 f421adaeb..f7f54d831 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 @@ -267,4 +267,27 @@ class NavigationTest { onNodeWithTag("topic:${topic.id}").assertExists() } } + + @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() + } + + // Tap the first topic chip + onNodeWithTag("topicChip:${topic.id}", useUnmergedTree = true) + .performClick() + + // TEST FAILING HERE + composeTestRule.waitUntil(timeoutMillis = 3_600_000, condition = { false }) + + // Verify that we're on the correct topic details screen + onNodeWithTag("topic:${topic.id}").assertExists() + } + } } diff --git a/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeNewsRepository.kt b/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeNewsRepository.kt index 0cdec6090..24baa4622 100644 --- a/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeNewsRepository.kt +++ b/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeNewsRepository.kt @@ -17,16 +17,13 @@ package com.google.samples.apps.nowinandroid.core.data.test.repository import com.google.samples.apps.nowinandroid.core.data.Synchronizer -import com.google.samples.apps.nowinandroid.core.data.model.asEntity import com.google.samples.apps.nowinandroid.core.data.repository.NewsRepository import com.google.samples.apps.nowinandroid.core.data.repository.NewsResourceQuery -import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEntity -import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.network.Dispatcher import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO import com.google.samples.apps.nowinandroid.core.network.demo.DemoNiaNetworkDataSource -import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource +import com.google.samples.apps.nowinandroid.core.network.model.asExternalModel import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow @@ -48,9 +45,11 @@ class FakeNewsRepository @Inject constructor( query: NewsResourceQuery, ): Flow> = flow { + val newsResources = datasource.getNewsResources() + val topics = datasource.getTopics() + emit( - datasource - .getNewsResources() + newsResources .filter { networkNewsResource -> // Filter out any news resources which don't match the current query. // If no query parameters (filterTopicIds or filterNewsIds) are specified @@ -64,8 +63,7 @@ class FakeNewsRepository @Inject constructor( ) .all(true::equals) } - .map(NetworkNewsResource::asEntity) - .map(NewsResourceEntity::asExternalModel), + .map { it.asExternalModel(topics) }, ) }.flowOn(ioDispatcher) diff --git a/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/model/NetworkNewsResource.kt b/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/model/NetworkNewsResource.kt index 89af19c99..372d8c85b 100644 --- a/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/model/NetworkNewsResource.kt +++ b/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/model/NetworkNewsResource.kt @@ -35,6 +35,20 @@ data class NetworkNewsResource( val topics: List = listOf(), ) +fun NetworkNewsResource.asExternalModel(topics: List) = + NewsResource( + id = id, + title = title, + content = content, + url = url, + headerImageUrl = headerImageUrl, + publishDate = publishDate, + type = type, + topics = topics + .filter { networkTopic -> this.topics.contains(networkTopic.id) } + .map(NetworkTopic::asExternalModel), + ) + /** * Network representation of [NewsResource] when fetched from /newsresources/{id} */ diff --git a/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/model/NetworkTopic.kt b/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/model/NetworkTopic.kt index e1043938f..0d21c09e7 100644 --- a/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/model/NetworkTopic.kt +++ b/core/network/src/main/kotlin/com/google/samples/apps/nowinandroid/core/network/model/NetworkTopic.kt @@ -32,3 +32,13 @@ data class NetworkTopic( val imageUrl: String = "", val followed: Boolean = false, ) + +fun NetworkTopic.asExternalModel(): Topic = + Topic( + id = id, + name = name, + shortDescription = shortDescription, + longDescription = longDescription, + url = url, + imageUrl = imageUrl, + ) 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 7c41d74d0..eaaea73aa 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 @@ -56,6 +56,7 @@ import androidx.compose.ui.draganddrop.DragAndDropTransferData import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalInspectionMode +import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.semantics.contentDescription @@ -341,6 +342,7 @@ fun NewsResourceTopics( }, ) }, + modifier = Modifier.testTag("topicChip:${followableTopic.topic.id}"), ) } }