From 34fd20e94f2ef313b2a71ac0dcef656330d18110 Mon Sep 17 00:00:00 2001 From: JanFidor Date: Tue, 15 Nov 2022 16:11:47 +0100 Subject: [PATCH] Revert "add robot for topic feature ui tests" This reverts commit 9dde120cc5d6fdfe2bae6d69891171bc045652e9. --- .../nowinandroid/feature/topic/TopicRobot.kt | 70 --------- .../feature/topic/TopicScreenTest.kt | 135 +++++++++++------- 2 files changed, 85 insertions(+), 120 deletions(-) delete mode 100644 feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicRobot.kt diff --git a/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicRobot.kt b/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicRobot.kt deleted file mode 100644 index 06fc4d43b..000000000 --- a/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicRobot.kt +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.samples.apps.nowinandroid.feature.topic - -import androidx.activity.ComponentActivity -import androidx.compose.ui.test.hasScrollToNodeAction -import androidx.compose.ui.test.hasText -import androidx.compose.ui.test.junit4.AndroidComposeTestRule -import androidx.compose.ui.test.onFirst -import androidx.compose.ui.test.onNodeWithContentDescription -import androidx.compose.ui.test.onNodeWithText -import androidx.compose.ui.test.performScrollToNode -import androidx.test.ext.junit.rules.ActivityScenarioRule -import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.model.data.NewsResource - -internal class TopicRobot( - private val composeTestRule: AndroidComposeTestRule, ComponentActivity> -) { - private val topicLoading = composeTestRule.activity.resources.getString(R.string.topic_loading) - - fun setContent(topicUiState: TopicUiState, newsUiState: NewsUiState) { - composeTestRule.setContent { - TopicScreen( - topicUiState = topicUiState, - newsUiState = newsUiState, - onBackClick = { }, - onFollowClick = { }, - onBookmarkChanged = { _, _ -> }, - ) - } - } - - fun loadingIndicatorExists() { - composeTestRule - .onNodeWithContentDescription(topicLoading) - .assertExists() - } - - fun topicExists(topic: FollowableTopic) { - composeTestRule - .onNodeWithText(topic.topic.name) - .assertExists() - - composeTestRule - .onNodeWithText(topic.topic.longDescription) - .assertExists() - } - - fun scrollToNewsResource(newsResource: NewsResource) { - composeTestRule - .onAllNodes(hasScrollToNodeAction()) - .onFirst() - .performScrollToNode(hasText(newsResource.title)) - } -} \ No newline at end of file diff --git a/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt b/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt index 1df807635..0f20a5e5e 100644 --- a/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt +++ b/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt @@ -17,15 +17,20 @@ package com.google.samples.apps.nowinandroid.feature.topic import androidx.activity.ComponentActivity -import androidx.compose.ui.test.junit4.AndroidComposeTestRule +import androidx.compose.ui.test.hasScrollToNodeAction +import androidx.compose.ui.test.hasText import androidx.compose.ui.test.junit4.createAndroidComposeRule -import androidx.test.ext.junit.rules.ActivityScenarioRule +import androidx.compose.ui.test.onFirst +import androidx.compose.ui.test.onNodeWithContentDescription +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performScrollToNode import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video import com.google.samples.apps.nowinandroid.core.model.data.Topic import kotlinx.datetime.Instant +import org.junit.Before import org.junit.Rule import org.junit.Test @@ -39,77 +44,107 @@ class TopicScreenTest { @get:Rule val composeTestRule = createAndroidComposeRule() + private lateinit var topicLoading: String + + @Before + fun setup() { + composeTestRule.activity.apply { + topicLoading = getString(R.string.topic_loading) + } + } + @Test fun niaLoadingWheel_whenScreenIsLoading_showLoading() { - launchTopicRobot( - composeTestRule, - TopicUiState.Loading, - NewsUiState.Loading - ) { - loadingIndicatorExists() + composeTestRule.setContent { + TopicScreen( + topicUiState = TopicUiState.Loading, + newsUiState = NewsUiState.Loading, + onBackClick = { }, + onFollowClick = { }, + onBookmarkChanged = { _, _ -> }, + ) } + + composeTestRule + .onNodeWithContentDescription(topicLoading) + .assertExists() } @Test fun topicTitle_whenTopicIsSuccess_isShown() { val testTopic = testTopics.first() - - launchTopicRobot( - composeTestRule, - TopicUiState.Success(testTopic), - NewsUiState.Loading - ) { - topicExists(testTopic) + composeTestRule.setContent { + TopicScreen( + topicUiState = TopicUiState.Success(testTopic), + newsUiState = NewsUiState.Loading, + onBackClick = { }, + onFollowClick = { }, + onBookmarkChanged = { _, _ -> }, + ) } + + // Name is shown + composeTestRule + .onNodeWithText(testTopic.topic.name) + .assertExists() + + // Description is shown + composeTestRule + .onNodeWithText(testTopic.topic.longDescription) + .assertExists() } @Test fun news_whenTopicIsLoading_isNotShown() { - launchTopicRobot( - composeTestRule, - TopicUiState.Loading, - NewsUiState.Success( - sampleNewsResources.mapIndexed { index, newsResource -> - SaveableNewsResource( - newsResource = newsResource, - isSaved = index % 2 == 0, - ) - } + composeTestRule.setContent { + TopicScreen( + topicUiState = TopicUiState.Loading, + newsUiState = NewsUiState.Success( + sampleNewsResources.mapIndexed { index, newsResource -> + SaveableNewsResource( + newsResource = newsResource, + isSaved = index % 2 == 0, + ) + } + ), + onBackClick = { }, + onFollowClick = { }, + onBookmarkChanged = { _, _ -> }, ) - ) { - loadingIndicatorExists() } + + // Loading indicator shown + composeTestRule + .onNodeWithContentDescription(topicLoading) + .assertExists() } @Test fun news_whenSuccessAndTopicIsSuccess_isShown() { val testTopic = testTopics.first() - launchTopicRobot( - composeTestRule, - TopicUiState.Success(testTopic), - NewsUiState.Success( - sampleNewsResources.mapIndexed { index, newsResource -> - SaveableNewsResource( - newsResource = newsResource, - isSaved = index % 2 == 0, - ) - } + composeTestRule.setContent { + TopicScreen( + topicUiState = TopicUiState.Success(testTopic), + newsUiState = NewsUiState.Success( + sampleNewsResources.mapIndexed { index, newsResource -> + SaveableNewsResource( + newsResource = newsResource, + isSaved = index % 2 == 0, + ) + } + ), + onBackClick = { }, + onFollowClick = { }, + onBookmarkChanged = { _, _ -> }, ) - ) { - // Scroll to first news title if available - scrollToNewsResource(sampleNewsResources.first()) } - } -} -private fun launchTopicRobot( - composeTestRule: AndroidComposeTestRule, ComponentActivity>, - topicUiState: TopicUiState, - newsUiState: NewsUiState, - func: TopicRobot.() -> Unit -) = TopicRobot(composeTestRule).apply { - setContent(topicUiState, newsUiState) - func() + // Scroll to first news title if available + composeTestRule + .onAllNodes(hasScrollToNodeAction()) + .onFirst() + .performScrollToNode(hasText(sampleNewsResources.first().title)) + } } private const val TOPIC_1_NAME = "Headlines"