add robot for author feature ui tests

pull/402/head
JanFidor 3 years ago
parent a71aebb277
commit a0096e755e

@ -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<ActivityScenarioRule<ComponentActivity>, 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()
}
}

@ -17,16 +17,15 @@
package com.google.samples.apps.nowinandroid.feature.author package com.google.samples.apps.nowinandroid.feature.author
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.compose.ui.test.onNodeWithText
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableAuthor 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.domain.model.SaveableNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.Author 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.NewsResource
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video
import kotlinx.datetime.Instant import kotlinx.datetime.Instant
import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
@ -40,106 +39,75 @@ class AuthorScreenTest {
@get:Rule @get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>() val composeTestRule = createAndroidComposeRule<ComponentActivity>()
private lateinit var authorLoading: String
@Before
fun setup() {
composeTestRule.activity.apply {
authorLoading = getString(R.string.author_loading)
}
}
@Test @Test
fun niaLoadingWheel_whenScreenIsLoading_showLoading() { fun niaLoadingWheel_whenScreenIsLoading_showLoading() {
composeTestRule.setContent { launchAuthorRobot(
AuthorScreen( composeTestRule, AuthorUiState.Loading, NewsUiState.Loading,
authorUiState = AuthorUiState.Loading, ) {
newsUiState = NewsUiState.Loading, loadingIndicatorExists()
onBackClick = { },
onFollowClick = { },
onBookmarkChanged = { _, _ -> },
)
} }
composeTestRule
.onNodeWithContentDescription(authorLoading)
.assertExists()
} }
@Test @Test
fun authorTitle_whenAuthorIsSuccess_isShown() { fun authorTitle_whenAuthorIsSuccess_isShown() {
val testAuthor = testAuthors.first() val testAuthor = testAuthors.first()
composeTestRule.setContent { launchAuthorRobot(
AuthorScreen( composeTestRule,
authorUiState = AuthorUiState.Success(testAuthor), AuthorUiState.Success(testAuthor),
newsUiState = NewsUiState.Loading, NewsUiState.Loading
onBackClick = { }, ) {
onFollowClick = { }, authorExists(testAuthor)
onBookmarkChanged = { _, _ -> },
)
} }
// Name is shown
composeTestRule
.onNodeWithText(testAuthor.author.name)
.assertExists()
// Bio is shown
composeTestRule
.onNodeWithText(testAuthor.author.bio)
.assertExists()
} }
@Test @Test
fun news_whenAuthorIsLoading_isNotShown() { fun news_whenAuthorIsLoading_isNotShown() {
composeTestRule.setContent { val newsUiState = NewsUiState.Success(
AuthorScreen(
authorUiState = AuthorUiState.Loading,
newsUiState = NewsUiState.Success(
sampleNewsResources.mapIndexed { index, newsResource -> sampleNewsResources.mapIndexed { index, newsResource ->
SaveableNewsResource( SaveableNewsResource(
newsResource = newsResource, newsResource = newsResource,
isSaved = index % 2 == 0, isSaved = index % 2 == 0,
) )
} }
),
onBackClick = { },
onFollowClick = { },
onBookmarkChanged = { _, _ -> },
) )
launchAuthorRobot(
composeTestRule,
AuthorUiState.Loading,
newsUiState
) {
loadingIndicatorExists()
} }
// Loading indicator shown
composeTestRule
.onNodeWithContentDescription(authorLoading)
.assertExists()
} }
@Test @Test
fun news_whenSuccessAndAuthorIsSuccess_isShown() { fun news_whenSuccessAndAuthorIsSuccess_isShown() {
val testAuthor = testAuthors.first() val testAuthor = testAuthors.first()
composeTestRule.setContent { val newsUiState = NewsUiState.Success(
AuthorScreen(
authorUiState = AuthorUiState.Success(testAuthor),
newsUiState = NewsUiState.Success(
sampleNewsResources.mapIndexed { index, newsResource -> sampleNewsResources.mapIndexed { index, newsResource ->
SaveableNewsResource( SaveableNewsResource(
newsResource = newsResource, newsResource = newsResource,
isSaved = index % 2 == 0, isSaved = index % 2 == 0,
) )
} }
),
onBackClick = { },
onFollowClick = { },
onBookmarkChanged = { _, _ -> },
) )
launchAuthorRobot(
composeTestRule,
AuthorUiState.Success(testAuthor),
newsUiState
) {
newsResourceExists(sampleNewsResources.first())
}
} }
// First news title shown
composeTestRule
.onNodeWithText(sampleNewsResources.first().title)
.assertExists()
} }
private fun launchAuthorRobot(
composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComponentActivity>, 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_1_NAME = "Author 1"

Loading…
Cancel
Save