Fix compilation errors in androidInstrumentedTest

pull/2064/head
lihenggui 2 years ago
parent f0c58bc83b
commit 392050cee0

@ -48,3 +48,8 @@ kotlin {
}
}
}
compose.resources {
publicResClass = true
generateResClass = always
}

@ -37,6 +37,12 @@ import androidx.lifecycle.testing.TestLifecycleOwner
import com.google.samples.apps.nowinandroid.core.testing.data.userNewsResourcesTestData
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
import kotlinx.coroutines.test.runTest
import nowinandroid.core.ui.generated.resources.core_ui_unbookmark
import nowinandroid.feature.bookmarks.generated.resources.Res
import nowinandroid.feature.bookmarks.generated.resources.feature_bookmarks_empty_description
import nowinandroid.feature.bookmarks.generated.resources.feature_bookmarks_empty_error
import nowinandroid.feature.bookmarks.generated.resources.feature_bookmarks_loading
import org.jetbrains.compose.resources.getString
import org.junit.Rule
import org.junit.Test
import kotlin.test.assertEquals
@ -51,7 +57,7 @@ class BookmarksScreenTest {
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
@Test
fun loading_showsLoadingSpinner() {
fun loading_showsLoadingSpinner() = runTest {
composeTestRule.setContent {
BookmarksScreen(
feedState = NewsFeedUiState.Loading,
@ -64,7 +70,7 @@ class BookmarksScreenTest {
composeTestRule
.onNodeWithContentDescription(
composeTestRule.activity.resources.getString(R.string.feature_bookmarks_loading),
getString(Res.string.feature_bookmarks_loading),
)
.assertExists()
}
@ -109,7 +115,7 @@ class BookmarksScreenTest {
}
@Test
fun feed_whenRemovingBookmark_removesBookmark() {
fun feed_whenRemovingBookmark_removesBookmark() = runTest {
var removeFromBookmarksCalled = false
composeTestRule.setContent {
@ -129,8 +135,8 @@ class BookmarksScreenTest {
composeTestRule
.onAllNodesWithContentDescription(
composeTestRule.activity.getString(
com.google.samples.apps.nowinandroid.core.ui.R.string.core_ui_unbookmark,
getString(
nowinandroid.core.ui.generated.resources.Res.string.core_ui_unbookmark,
),
).filter(
hasAnyAncestor(
@ -148,7 +154,7 @@ class BookmarksScreenTest {
}
@Test
fun feed_whenHasNoBookmarks_showsEmptyState() {
fun feed_whenHasNoBookmarks_showsEmptyState() = runTest {
composeTestRule.setContent {
BookmarksScreen(
feedState = NewsFeedUiState.Success(emptyList()),
@ -161,13 +167,13 @@ class BookmarksScreenTest {
composeTestRule
.onNodeWithText(
composeTestRule.activity.getString(R.string.feature_bookmarks_empty_error),
getString(Res.string.feature_bookmarks_empty_error),
)
.assertExists()
composeTestRule
.onNodeWithText(
composeTestRule.activity.getString(R.string.feature_bookmarks_empty_description),
getString(Res.string.feature_bookmarks_empty_description),
)
.assertExists()
}

@ -32,6 +32,12 @@ import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPer
import com.google.samples.apps.nowinandroid.core.testing.data.followableTopicTestData
import com.google.samples.apps.nowinandroid.core.testing.data.userNewsResourcesTestData
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import nowinandroid.feature.foryou.generated.resources.Res
import nowinandroid.feature.foryou.generated.resources.feature_foryou_done
import nowinandroid.feature.foryou.generated.resources.feature_foryou_loading
import org.jetbrains.compose.resources.getString
import org.junit.Rule
import org.junit.Test
@ -44,13 +50,14 @@ class ForYouScreenTest {
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
private val doneButtonMatcher by lazy {
hasText(
composeTestRule.activity.resources.getString(R.string.feature_foryou_done),
)
runBlocking {
hasText(getString(Res.string.feature_foryou_done))
}
}
@Test
fun circularProgressIndicator_whenScreenIsLoading_exists() {
fun circularProgressIndicator_whenScreenIsLoading_exists() = runTest {
composeTestRule.setContent {
BoxWithConstraints {
ForYouScreen(
@ -70,13 +77,13 @@ class ForYouScreenTest {
composeTestRule
.onNodeWithContentDescription(
composeTestRule.activity.resources.getString(R.string.feature_foryou_loading),
getString(Res.string.feature_foryou_loading),
)
.assertExists()
}
@Test
fun circularProgressIndicator_whenScreenIsSyncing_exists() {
fun circularProgressIndicator_whenScreenIsSyncing_exists() = runTest {
composeTestRule.setContent {
BoxWithConstraints {
ForYouScreen(
@ -96,7 +103,7 @@ class ForYouScreenTest {
composeTestRule
.onNodeWithContentDescription(
composeTestRule.activity.resources.getString(R.string.feature_foryou_loading),
getString(Res.string.feature_foryou_loading),
)
.assertExists()
}
@ -194,7 +201,7 @@ class ForYouScreenTest {
}
@Test
fun feed_whenInterestsSelectedAndLoading_showsLoadingIndicator() {
fun feed_whenInterestsSelectedAndLoading_showsLoadingIndicator() = runTest {
composeTestRule.setContent {
BoxWithConstraints {
ForYouScreen(
@ -215,13 +222,13 @@ class ForYouScreenTest {
composeTestRule
.onNodeWithContentDescription(
composeTestRule.activity.resources.getString(R.string.feature_foryou_loading),
getString(Res.string.feature_foryou_loading),
)
.assertExists()
}
@Test
fun feed_whenNoInterestsSelectionAndLoading_showsLoadingIndicator() {
fun feed_whenNoInterestsSelectionAndLoading_showsLoadingIndicator() = runTest{
composeTestRule.setContent {
BoxWithConstraints {
ForYouScreen(
@ -241,7 +248,7 @@ class ForYouScreenTest {
composeTestRule
.onNodeWithContentDescription(
composeTestRule.activity.resources.getString(R.string.feature_foryou_loading),
getString(Res.string.feature_foryou_loading),
)
.assertExists()
}

@ -28,6 +28,8 @@ import com.google.samples.apps.nowinandroid.core.testing.data.followableTopicTes
import com.google.samples.apps.nowinandroid.feature.interests.InterestsScreen
import com.google.samples.apps.nowinandroid.feature.interests.InterestsUiState
import kotlinx.coroutines.runBlocking
import nowinandroid.core.ui.generated.resources.core_ui_interests_card_follow_button_content_desc
import nowinandroid.core.ui.generated.resources.core_ui_interests_card_unfollow_button_content_desc
import nowinandroid.feature.interests.generated.resources.feature_interests_empty_header
import nowinandroid.feature.interests.generated.resources.feature_interests_loading
import org.jetbrains.compose.resources.getString
@ -58,7 +60,6 @@ class InterestsScreenTest {
runBlocking {
interestsLoading = getString(InterestsR.string.feature_interests_loading)
interestsEmptyHeader = getString(InterestsR.string.feature_interests_empty_header)
// TODO res object is internal, it couldn't import from the other module
interestsTopicCardFollowButton =
getString(CoreUiR.string.core_ui_interests_card_follow_button_content_desc)
interestsTopicCardUnfollowButton =

Loading…
Cancel
Save