Simplify String resource loading in NavigationTest.kt

pull/679/head
Simon Marquis 2 years ago committed by GitHub
parent 2c18740d62
commit 091abad348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,12 +16,14 @@
package com.google.samples.apps.nowinandroid.ui package com.google.samples.apps.nowinandroid.ui
import androidx.annotation.StringRes
import androidx.compose.ui.test.assertCountEquals import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertIsOn import androidx.compose.ui.test.assertIsOn
import androidx.compose.ui.test.assertIsSelected import androidx.compose.ui.test.assertIsSelected
import androidx.compose.ui.test.hasAnyAncestor import androidx.compose.ui.test.hasAnyAncestor
import androidx.compose.ui.test.hasTestTag import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText import androidx.compose.ui.test.hasText
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.onAllNodesWithText import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithContentDescription
@ -34,10 +36,10 @@ import com.google.samples.apps.nowinandroid.R
import dagger.hilt.android.testing.BindValue import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.rules.TemporaryFolder import org.junit.rules.TemporaryFolder
import kotlin.properties.ReadOnlyProperty
import com.google.samples.apps.nowinandroid.feature.bookmarks.R as BookmarksR import com.google.samples.apps.nowinandroid.feature.bookmarks.R as BookmarksR
import com.google.samples.apps.nowinandroid.feature.foryou.R as FeatureForyouR import com.google.samples.apps.nowinandroid.feature.foryou.R as FeatureForyouR
import com.google.samples.apps.nowinandroid.feature.interests.R as FeatureInterestsR import com.google.samples.apps.nowinandroid.feature.interests.R as FeatureInterestsR
@ -69,35 +71,21 @@ class NavigationTest {
@get:Rule(order = 2) @get:Rule(order = 2)
val composeTestRule = createAndroidComposeRule<MainActivity>() val composeTestRule = createAndroidComposeRule<MainActivity>()
// The strings used for matching in these tests private fun AndroidComposeTestRule<*, *>.stringResource(@StringRes resId: Int) =
private lateinit var done: String ReadOnlyProperty<Any?, String> { _, _ -> activity.getString(resId) }
private lateinit var navigateUp: String
private lateinit var forYouLoading: String
private lateinit var forYou: String
private lateinit var interests: String
private lateinit var sampleTopic: String
private lateinit var appName: String
private lateinit var saved: String
private lateinit var settings: String
private lateinit var brand: String
private lateinit var ok: String
@Before // The strings used for matching in these tests
fun setup() { private val done by composeTestRule.stringResource(FeatureForyouR.string.done)
composeTestRule.activity.apply { private val navigateUp by composeTestRule.stringResource(FeatureForyouR.string.navigate_up)
done = getString(FeatureForyouR.string.done) private val forYouLoading by composeTestRule.stringResource(FeatureForyouR.string.for_you_loading)
navigateUp = getString(FeatureForyouR.string.navigate_up) private val forYou by composeTestRule.stringResource(FeatureForyouR.string.for_you)
forYouLoading = getString(FeatureForyouR.string.for_you_loading) private val interests by composeTestRule.stringResource(FeatureInterestsR.string.interests)
forYou = getString(FeatureForyouR.string.for_you) private val sampleTopic = "Headlines"
interests = getString(FeatureInterestsR.string.interests) private val appName by composeTestRule.stringResource(R.string.app_name)
sampleTopic = "Headlines" private val saved by composeTestRule.stringResource(BookmarksR.string.saved)
appName = getString(R.string.app_name) private val settings by composeTestRule.stringResource(SettingsR.string.top_app_bar_action_icon_description)
saved = getString(BookmarksR.string.saved) private val brand by composeTestRule.stringResource(SettingsR.string.brand_android)
settings = getString(SettingsR.string.top_app_bar_action_icon_description) private val ok by composeTestRule.stringResource(SettingsR.string.dismiss_dialog_button_text)
brand = getString(SettingsR.string.brand_android)
ok = getString(SettingsR.string.dismiss_dialog_button_text)
}
}
@Test @Test
fun firstScreen_isForYou() { fun firstScreen_isForYou() {

Loading…
Cancel
Save