diff --git a/feature/author/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/author/AuthorRobot.kt b/feature/author/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/author/AuthorRobot.kt new file mode 100644 index 000000000..ae35b0118 --- /dev/null +++ b/feature/author/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/author/AuthorRobot.kt @@ -0,0 +1,65 @@ +/* + * 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.author + +import androidx.activity.ComponentActivity +import androidx.compose.ui.test.junit4.AndroidComposeTestRule +import androidx.compose.ui.test.onNodeWithContentDescription +import androidx.compose.ui.test.onNodeWithText +import androidx.test.ext.junit.rules.ActivityScenarioRule +import com.google.samples.apps.nowinandroid.core.domain.model.FollowableAuthor +import com.google.samples.apps.nowinandroid.core.model.data.NewsResource + +internal class AuthorRobot( + private val composeTestRule: AndroidComposeTestRule, ComponentActivity> +) { + fun setContent(authorUiState: AuthorUiState, newsUiState: NewsUiState) { + composeTestRule.setContent { + AuthorScreen( + authorUiState = authorUiState, + newsUiState = newsUiState, + onBackClick = { }, + onFollowClick = { }, + onBookmarkChanged = { _, _ -> }, + ) + } + } + + fun loadingIndicatorExists() { + val authorLoading = composeTestRule.activity.getString(R.string.author_loading) + + composeTestRule + .onNodeWithContentDescription(authorLoading) + .assertExists() + } + + fun authorExists(author: FollowableAuthor) { + composeTestRule + .onNodeWithText(author.author.name) + .assertExists() + + composeTestRule + .onNodeWithText(author.author.bio) + .assertExists() + } + + fun newsResourceExists(newsResource: NewsResource) { + composeTestRule + .onNodeWithText(newsResource.title) + .assertExists() + } +} \ No newline at end of file diff --git a/feature/author/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/author/AuthorScreenTest.kt b/feature/author/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/author/AuthorScreenTest.kt index b955912f2..23654f2b2 100644 --- a/feature/author/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/author/AuthorScreenTest.kt +++ b/feature/author/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/author/AuthorScreenTest.kt @@ -17,16 +17,15 @@ package com.google.samples.apps.nowinandroid.feature.author import androidx.activity.ComponentActivity +import androidx.compose.ui.test.junit4.AndroidComposeTestRule import androidx.compose.ui.test.junit4.createAndroidComposeRule -import androidx.compose.ui.test.onNodeWithContentDescription -import androidx.compose.ui.test.onNodeWithText +import androidx.test.ext.junit.rules.ActivityScenarioRule import com.google.samples.apps.nowinandroid.core.domain.model.FollowableAuthor import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource import com.google.samples.apps.nowinandroid.core.model.data.Author import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video import kotlinx.datetime.Instant -import org.junit.Before import org.junit.Rule import org.junit.Test @@ -40,108 +39,77 @@ class AuthorScreenTest { @get:Rule val composeTestRule = createAndroidComposeRule() - private lateinit var authorLoading: String - - @Before - fun setup() { - composeTestRule.activity.apply { - authorLoading = getString(R.string.author_loading) - } - } - @Test fun niaLoadingWheel_whenScreenIsLoading_showLoading() { - composeTestRule.setContent { - AuthorScreen( - authorUiState = AuthorUiState.Loading, - newsUiState = NewsUiState.Loading, - onBackClick = { }, - onFollowClick = { }, - onBookmarkChanged = { _, _ -> }, - ) + launchAuthorRobot( + composeTestRule, AuthorUiState.Loading, NewsUiState.Loading, + ) { + loadingIndicatorExists() } - - composeTestRule - .onNodeWithContentDescription(authorLoading) - .assertExists() } @Test fun authorTitle_whenAuthorIsSuccess_isShown() { val testAuthor = testAuthors.first() - composeTestRule.setContent { - AuthorScreen( - authorUiState = AuthorUiState.Success(testAuthor), - newsUiState = NewsUiState.Loading, - onBackClick = { }, - onFollowClick = { }, - onBookmarkChanged = { _, _ -> }, - ) + launchAuthorRobot( + composeTestRule, + AuthorUiState.Success(testAuthor), + NewsUiState.Loading + ) { + authorExists(testAuthor) } - - // Name is shown - composeTestRule - .onNodeWithText(testAuthor.author.name) - .assertExists() - - // Bio is shown - composeTestRule - .onNodeWithText(testAuthor.author.bio) - .assertExists() } @Test fun news_whenAuthorIsLoading_isNotShown() { - composeTestRule.setContent { - AuthorScreen( - authorUiState = AuthorUiState.Loading, - newsUiState = NewsUiState.Success( - sampleNewsResources.mapIndexed { index, newsResource -> - SaveableNewsResource( - newsResource = newsResource, - isSaved = index % 2 == 0, - ) - } - ), - onBackClick = { }, - onFollowClick = { }, - onBookmarkChanged = { _, _ -> }, - ) + val newsUiState = NewsUiState.Success( + sampleNewsResources.mapIndexed { index, newsResource -> + SaveableNewsResource( + newsResource = newsResource, + isSaved = index % 2 == 0, + ) + } + ) + launchAuthorRobot( + composeTestRule, + AuthorUiState.Loading, + newsUiState + ) { + loadingIndicatorExists() } - - // Loading indicator shown - composeTestRule - .onNodeWithContentDescription(authorLoading) - .assertExists() } @Test fun news_whenSuccessAndAuthorIsSuccess_isShown() { val testAuthor = testAuthors.first() - composeTestRule.setContent { - AuthorScreen( - authorUiState = AuthorUiState.Success(testAuthor), - newsUiState = NewsUiState.Success( - sampleNewsResources.mapIndexed { index, newsResource -> - SaveableNewsResource( - newsResource = newsResource, - isSaved = index % 2 == 0, - ) - } - ), - onBackClick = { }, - onFollowClick = { }, - onBookmarkChanged = { _, _ -> }, - ) + val newsUiState = NewsUiState.Success( + sampleNewsResources.mapIndexed { index, newsResource -> + SaveableNewsResource( + newsResource = newsResource, + isSaved = index % 2 == 0, + ) + } + ) + launchAuthorRobot( + composeTestRule, + AuthorUiState.Success(testAuthor), + newsUiState + ) { + newsResourceExists(sampleNewsResources.first()) } - - // First news title shown - composeTestRule - .onNodeWithText(sampleNewsResources.first().title) - .assertExists() } } +private fun launchAuthorRobot( + composeTestRule: AndroidComposeTestRule, ComponentActivity>, + authorUiState: AuthorUiState, + newsUiState: NewsUiState, + func: AuthorRobot.() -> Unit +) = AuthorRobot(composeTestRule).apply { + setContent(authorUiState, newsUiState) + func() +} + private const val AUTHOR_1_NAME = "Author 1" private const val AUTHOR_2_NAME = "Author 2" private const val AUTHOR_3_NAME = "Author 3"